基于上一篇的基于Http的稍加改造。
注册中心不需要做修改,跳过。
Zipkin-Server改动如下:
Gradle依赖:
//作为eureka client依赖
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
//zipkin链路信息请求接收服务器,2.12.5开始使用armeria server
implementation ("io.zipkin.java:zipkin-server:${zipkinVersion}") {
//排除自带log与spring boot冲突造成stackOverflowError
exclude group: "org.springframework.boot", module: "spring-boot-starter-log4j2"
}
implementation "io.zipkin.java:zipkin-autoconfigure-ui:${zipkinVersion}"
//start 新增部分,zipkin收集器和rabbitmq
implementation "io.zipkin.java:zipkin-autoconfigure-collector-rabbitmq:${zipkinVersion}"
implementation 'org.springframework.amqp:spring-rabbit'
//end
Application:
@EnableZipkinServer
@EnableDiscoveryClient
@SpringBootApplication
public class ZipkinServer01Application {
//这些参数可以通过命令行传入,此处为了演示直接代码设置
static {
//zipkin-server-shared.yml内的配置环境变量key
//设置mq地址和zipkin-ui监听端口
System.setProperty("QUERY_PORT", "8080");
System.setProperty("RABBIT_ADDRESSES", "localhost:5672");
System.setProperty("RABBIT_USER", "guest");
System.setProperty("RABBIT_PASSWORD", "guest");
}
/**
* {@link ZipkinServer}
* @param args
*/
public static void main(String[] args) {
//zipkin-server-shared.yml
new SpringApplicationBuilder(ZipkinServer.class)
.listeners(new RegisterZipkinHealthIndicators())
//指定配置文件名称为zipkin-server,默认是application
//zipkin-server.yml中指定profile=shared
.properties("spring.config.name=zipkin-server").run(args);
}
}
集成Sleuth的服务改动如下:
Gradle依赖:
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
//web模块
implementation 'org.springframework.boot:spring-boot-starter-web'
//feign是对hystrix+ribbon的一层封装
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
//sleuth 链路追踪,使用mq,starter-zipkin中已包含该依赖,可以注释掉了,去掉的
//implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
//start 由zipkin-report(sender)将统计信息发送到mq. 新增部分
implementation 'org.springframework.cloud:spring-cloud-starter-zipkin'
implementation 'org.springframework.amqp:spring-rabbit'
//end
yml配置文件做如下修改:
spring:
application:
name: consumer02-sleuth
#消息中间件rabbit配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
#ZipkinProperties
zipkin:
#ZipkinSenderProperties mq采用rabbit, 可用的值为rabbit,kafka,web
sender:
type: rabbit
# 使用mq接收统计信息,不需要配置baseUrl
#baseUrl: http://zipkin-server01/
#使用服务发现
#discoveryClientEnabled: true
#当前服务名称,不写默认取spring.application.name
#service:
#name: consumer02-sleuth
#SamplerProperties 采样配置, 1=100%的请求进行采样
sleuth:
sampler:
probability: 1
结束,启动rabbit, 注册中心,zipkin-server和带追踪的微服务(此处consumer02-sleuth), 访问hello接口,查看控制台日志信息,登陆zipkin-server的web界面查看接口访问链路信息。
可以登陆rabbit-web界面查看队列信息,默认情况下,队列名称为zipkin. 使用默认exchange。