springmvc集成zipkin性能监控(二)

上一遍是配置类,经过公司配置实践,可能会给项目中原始的适配器产生冲突。

但是那样会有一个好处,就是解耦合,不受本地开发环境和测试环境的影响,因为我们可以在部署的时候去简单的去添加两个配置文件即可

今天来记录一下,以配置文件的方式(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美元

 

猜你喜欢

转载自www.cnblogs.com/mengyixin/p/9857343.html