1

zipkin-server

  • pom

<dependency>
            <groupId>io.zipkin</groupId>
            <artifactId>zipkin-ui</artifactId>
            <version>1.39.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth-zipkin-stream</artifactId>
            <version>1.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
            <version>1.0.0.RELEASE</version>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>mysql</groupId>-->
            <!--<artifactId>mysql-connector-java</artifactId>-->
        <!--</dependency>-->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
  • application.yaml

spring:
  rabbitmq:
    addresses: rabbitmq
#  datasource:
#      #脚本位置:依赖spanstore-jdbc-0.9.3.jar内
##    schema: classpath:/mysql.sql
#    url: jdbc:mysql://zipkin_mysql:3306/zipkin
#    username: zipkin
#    password: zipkin
#      # Switch this on to create the schema on startup:
#    initialize: true
#    continueOnError: true


  sleuth:
   # not use sleuth stream to trace zipkin server itself
    enabled: false
    #refer to org.springframework.cloud.sleuth.instrument.hystrix.SleuthHystrixAutoConfiguration.java
    hystrix:
      strategy:
        enabled: false

zipkin:
  storage:
    type: mem #mysql #mem
  • application

@EnableZipkinStreamServer
@SpringBootApplication
public class ZipkinServerApplication {

    public static void main(String[] args){
        SpringApplication.run(ZipkinServerApplication.class,args);
    }
}

zipkin-client

  • pom

<!-- zipkin client -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth-stream</artifactId>
            <version>1.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
            <version>1.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
            <version>1.0.6.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>io.reactivex</groupId>
                    <artifactId>rxjava</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.reactivex</groupId>
            <artifactId>rxjava</artifactId>
            <version>1.1.5</version>
        </dependency>
  • application.yml

spring:
# mq stream format to send trace data
  rabbitmq:
    addresses: rabbitmq
  #zipkin config
  zipkin:
      #defult is true,to use zipkin,false:not use
    enabled: true
    baseUrl: http://zipkin:9411/

    sleuth:
      sampler:
        # 采样率,默认0.1即10%,如需测试时每次都看到trace则修改为1.0,但对性能有影响,注意上线时修改为合理值
        percentage: 1.0
      hystrix:
        strategy:
          enabled: true
      #https://github.com/ReactiveX/RxJava/issues/2297
      rxjava:
        schedulers:
          hook:
            enabled: false
      # stream format  to send trace msg: enable sleuth.stream to use stream
      # default is true,refer to SleuthStreamAutoConfiguration.java
      stream:
        enabled: true
      # skip tracing urls' pattern,refer to org.springframework.cloud.sleuth.instrument.web.TraceWebAutoConfiguration
      #web:
        #skipPattern: /eureka.*

# zipkin properties for ServiceApplication.java to debug when there is no zipkin server
sample:
  zipkin:
    # When enabled=false, traces log to the console. Comment to send to zipkin
    enabled: true
  • application

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrix
@EnableHystrixDashboard
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableAsync
public class TraceDemoApplication {

    @Bean
    Sampler sampler() {
        return new AlwaysSampler();
    }

    public static void main(String[] args){
        SpringApplication.run(TraceDemoApplication.class,args);
    }
}

docker-compose

rabbitmq:
  image: rabbitmq:management
  expose:
    - 5672
    - 15672
  ports:
    - 5672:5672
    - 15672:15672
zipkin:
  image: zipkin-server
  restart: always
  ports:
   # Listen port for the Scribe transport
      - "9410:9410"
        # Historical port used for the Zipkin HTTP Api
      - "9411:9411"
      - "9901:9901"
  links:
    - rabbitmq

trace_demo:
  image: trace-demo
  restart: always
  ports:
    - "9998:9998"
  links:
    - discovery
    - config
    - rabbitmq
    - zipkin    

运行

http://192.168.99.100:9411/

clipboard.png

查询

clipboard.png

参考


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...