1. 程式人生 > >springmvc集成zipkin性能監控(二)

springmvc集成zipkin性能監控(二)

pro 添加 stat field 就是 com ren dispatch test6

上一遍是配置類,經過公司配置實踐,可能會給項目中原始的適配器產生沖突。

但是那樣會有一個好處,就是解耦合,不受本地開發環境和測試環境的影響,因為我們可以在部署的時候去簡單的去添加兩個配置文件即可

今天來記錄一下,以配置文件的方式(xml)的方式進行配置

一:導入jar包

     <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<!--<brave.version>3.16.0</brave.version>-->
		<brave.version>4.9.1</brave.version>
		<zipkin-reporter2.version>2.1.3</zipkin-reporter2.version>
		<zipkin-reporter.version>0.6.9</zipkin-reporter.version>
	</properties>

          <dependency> <groupId>io.zipkin.brave</groupId> <artifactId>brave</artifactId> <version>${brave.version}</version> </dependency> <dependency> <groupId>io.zipkin.brave</groupId> <artifactId>brave-spring-beans</artifactId> <version>${brave.version}</version> </dependency> <dependency> <groupId>io.zipkin.reporter2</groupId> <artifactId>zipkin-sender-okhttp3</artifactId> <version>${zipkin-reporter2.version}</version> </dependency> <dependency> <groupId>io.zipkin.brave</groupId> <artifactId>brave-instrumentation-servlet</artifactId> <version>${brave.version}</version> </dependency> <dependency> <groupId>io.zipkin.brave</groupId> <artifactId>brave-instrumentation-spring-web</artifactId> <version>${brave.version}</version> </dependency> <dependency> <groupId>io.zipkin.brave</groupId> <artifactId>brave-instrumentation-spring-webmvc</artifactId> <version>${brave.version}</version> </dependency> <dependency> <groupId>io.zipkin.brave</groupId> <artifactId>brave-context-log4j2</artifactId> <version>${brave.version}</version> </dependency>
          <dependency>
            <groupId>org.apache.logging.log4j</groupId>
             <artifactId>log4j-core</artifactId>
            <version>2.5</version>
          </dependency>

  二:寫配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.2.xsd">

  <context:property-placeholder/>

  <bean id="sender" class="zipkin2.reporter.okhttp3.OkHttpSender" factory-method="create">
    <constructor-arg type="String" value="http://localhost:9411/api/v2/spans"/>
  </bean>

  <bean id="tracing" class="brave.spring.beans.TracingFactoryBean">
    <property name="localServiceName" value="${zipkin.service:test66666}"/>
    <property name="spanReporter">
      <bean class="brave.spring.beans.AsyncReporterFactoryBean">
        <property name="encoder" value="JSON_V2"/>
        <property name="sender" ref="sender"/>
        <!-- wait up to half a second for any in-flight spans on close -->
        <property name="closeTimeout" value="500"/>
      </bean>
    </property>
    <property name="propagationFactory">
      <bean id="propagationFactory" class="brave.propagation.ExtraFieldPropagation" factory-method="newFactory">
        <constructor-arg index="0">
          <util:constant static-field="brave.propagation.B3Propagation.FACTORY"/>
        </constructor-arg>
        <constructor-arg index="1">
          <list>
            <value>user-name</value>
          </list>
        </constructor-arg>
      </bean>
    </property>
    <property name="currentTraceContext">
      <bean class="brave.context.log4j2.ThreadContextCurrentTraceContext" factory-method="create"/>
    </property>
  </bean>

  <bean id="httpTracing" class="brave.spring.beans.HttpTracingFactoryBean">
    <property name="tracing" ref="tracing"/>
  </bean>

  <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    <property name="interceptors">
      <list>
        <bean class="brave.spring.web.TracingClientHttpRequestInterceptor" factory-method="create">
          <constructor-arg type="brave.http.HttpTracing" ref="httpTracing"/>
        </bean>
      </list>
    </property>
  </bean>

  <mvc:interceptors>
    <bean class="brave.spring.webmvc.TracingHandlerInterceptor" factory-method="create">
      <constructor-arg type="brave.http.HttpTracing" ref="httpTracing"/>
    </bean>
  </mvc:interceptors>

  <!-- Loads the controller -->
  <!--<context:component-scan base-package="org.mozhu.zipkin.springmvc"/>-->
  <context:component-scan base-package="com.mzx.controller"/>
  <mvc:annotation-driven/>
</beans>

  三.配置web.xml前端控制器

<servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
            	classpath:conf/spring-mvc.xml
           		classpath:conf/spring-webmvc-servlet.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

 搞定了。

可能看著簡單,但是有句話怎麽說的,畫一條線1美元,但知道在哪劃線這就是999美元

springmvc集成zipkin性能監控(二)