zipkin作为简易的应用trace工具,适合小微型应用使用,2X和1X发生了重大变化,下面讲解下如何高效利用2X:
首先启动zipkin-server,可以通过jar包启动,2X已经不在推跟随SpringApplication启动的方式了,所以用法要特别注意下。
首先必须单独建立应用,不能和其他应用绑定一起。
pom中需要引入
<dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> <version>2.12.9</version> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> <scope>runtime</scope> <version>2.12.9</version> </dependency>
尽量不要引入其他了,以免发生包冲突。
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import zipkin2.server.internal.EnableZipkinServer; import zipkin2.server.internal.RegisterZipkinHealthIndicators; @SpringBootApplication @EnableZipkinServer public class ZkServerApp { public static void main(String[] args) { new SpringApplicationBuilder(ZkServerApp.class) .listeners(new RegisterZipkinHealthIndicators()) .properties("spring.config.name=zipkin-server") //.properties("spring.config.location=classpath:/springcloud/zipkin-server.yml").run(args); .run(args); } }
主代码如上,配置如下,配置名必须为zipkin-server
spring: application: name: zipkin-server profiles.include: shared # 必须,否则会报错 #server: # port: 9411 此处已经无用 QUERY_PORT: 9411 # 这里可以更改很多配置,配置名参见zipkin-server-shared.yml,该文件在zipkin2源码中 #zipkin: # storage: # type: mysql #spring: # datasource: # schema: classpath:/mysql.sql # url: jdbc:mysql://localhost:3306/ZIPKIN?autoReconnect=true&characterEncoding=utf-8 # username: root # password: 123456 # initialize: true # continueOnError: true eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8761/eureka/
然后需要trace的应用程序就简单了只要在配置里面多加
spring: zipkin: base-url: http://localhost:9411 sleuth: sampler: probability: 1.0
这两个就行。pom中加入
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> <version>2.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-sleuth-zipkin</artifactId> <version>2.1.1.RELEASE</version> </dependency>
搞定