参照
环境
Java 8 / Idea / Gradle4.10.2
Spring-boot (2.1.3.RELEASE) / SpringCloud(Greenwich.RELEASE) / Zipkin-Server (2.12.9) / Sleuth (2.1.0.RELEASE)
本文基于Eureka注册中心实现,spring-cloud-dependencies管理Spring-Cloud依赖版本
Eureka注册中心
gradle核心依赖:
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
Application入口类
/**
*/
@EnableEurekaServer
@SpringBootApplication
public class Registry01Application {
public static void main(String[] args) {
SpringApplication.run(Registry01Application.class, args);
}
}
yml配置文件:
server:
port: 8010
spring:
application:
name: registry01
#注册中心配置,不注册到注册中心
eureka:
server:
#关闭自我保护
enableSelfPreservation: false
client:
registerWithEureka: false
fetchRegistry: false
运行main方法正常启动即可。
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}"
Application:
/**
* zipkin server 链路追踪分析服务,twitter推出。
* 访问:http://localhost:port/zipkin/
*/
@EnableZipkinServer
@EnableDiscoveryClient
@SpringBootApplication
public class ZipkinServer01Application {
/**
* 该方式启动ZipkinServer,使用zipkin-server-shared.yml配置文件
* 位于zipkin-server.jar内
* {@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);
}
yml配置文件
#eureka-client注册中心配置,注册到注册中心
eureka:
client:
#默认true
# enabled: false
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8010/eureka/
spring:
application:
name: zipkin-server01
服务集成Sleuth
gradle依赖:
//作为eureka client依赖
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 链路
//implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
Application:
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class Consumer02SleuthApplication {
public static void main(String[] args) {
SpringApplication.run(Consumer02SleuthApplication.class, args);
}
}
Controller:
@RestController
@RequestMapping("hello")
public class HelloController {
@GetMapping
public String local() {
return "hello";
}
}
yml配置文件:
#eureka-client注册中心配置,注册到注册中心
eureka:
client:
#默认true
# enabled: false
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8010/eureka/
server:
port: 8032
spring:
application:
name: consumer02-sleuth
#ZipkinProperties
zipkin:
#ZipkinSenderProperties
sender:
type: web
# zipkin-server01是zipkin-server注册到注册中心的名称
baseUrl: http://zipkin-server01/
#使用服务发现
#discoveryClientEnabled: true
#当前服务名称,不写默认取spring.application.name
#service:
#name: consumer02-sleuth
#SamplerProperties 采样配置, 1=100%的请求进行采样
sleuth:
sampler:
probability: 1
logging:
level:
root: info
web: debug
访问接口控制台日志模样大概如此(开启web日志):
2019-05-09 19:46:32.311 DEBUG [consumer02-sleuth,ea0d64226a3ac521,89f578a9b1da22fc,true] 173132 - [nio-8021-exec-9] o.s.web.servlet.DispatcherServlet :GET "/hello", parameters={}
2019-05-09 19:46:32.323 DEBUG [consumer02-sleuth,ea0d64226a3ac521,89f578a9b1da22fc,true] 173132 - [nio-8021-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping :Mapped to public java.lang.String tom.spring.cloud.consumer.controller.HelloController.local()
2019-05-09 19:46:32.351 DEBUG [consumer02-sleuth,ea0d64226a3ac521,89f578a9b1da22fc,true] 173132 - [nio-8021-exec-9] m.m.a.RequestResponseBodyMethodProcessor :Using 'text/plain', given [*/*] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json]
2019-05-09 19:46:32.352 DEBUG [consumer02-sleuth,ea0d64226a3ac521,89f578a9b1da22fc,true] 173132 - [nio-8021-exec-9] m.m.a.RequestResponseBodyMethodProcessor :Writing ["hello"]
2019-05-09 19:46:32.364 DEBUG [consumer02-sleuth,ea0d64226a3ac521,89f578a9b1da22fc,true] 173132 - [nio-8021-exec-9] o.s.web.servlet.DispatcherServlet :Completed 200 OK
解释一下DEBUG [consumer02-sleuth,ea0d64226a3ac521,89f578a9b1da22fc,true]代表的东西,第一个是代表服务名称就是注册到注册中心的名字配置spring.application.name;第二个是TraceId当次记录的标识,多服务间调用时该值是不变的;第三个是SpanId当前业务请求处理快的标识;第四个字段是代表是否收集,因为probability=1所以一直是true。
最后,启动注册中心,zipkin-server和服务程序,访问服务程序hello接口,观察控制台日志。访问zipkin-server主页可以看到访问链路信息。