Loading... ## 简介: 在微服务框架中,一个由客户端发起的请求,在后端系统中会调用多个不同的的服务节点,来协同产生最后的响应结果,因此每一个前端请求都会形成一条复杂的分布式服务调用链路,链路中的任何一环出现高延时或错误时都会引起整个请求最后的失败。 ### **zipkin官网** https://zipkin.io/pages/quickstart.html ### 使用docker搭建 ```docker docker run -d -p 9411:9411 openzipkin/zipkin ``` ### 浏览器查看 (localhost换成docker运行的ip地址) http://localhost:9411/zipkin/ ## 搭建Sleuth链路监控 使用“订单服务(服务消费者)”调用“支付服务(服务提供者)”,模拟集群服务间的链路调用,需要在消费端和提供端配置sleuth的服务支持 ### 创建eureka-服务中心 cloud-euraka-server7001 #### 引入pom依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> ``` #### 配置yaml文件: ```yaml server: port: 7001 eureka: instance: hostname: localhost #eureka服务端的实例名称 client: #false表示不向注册中心注册自己。 register-with-eureka: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 fetch-registry: false server: enable-self-preservation: false ``` #### 启动类 ```java @SpringBootApplication @EnableEurekaServer public class ServiceApplication { public static void main(String[] args) { SpringApplication.run(ServiceApplication.class, args); } } ``` ### 创建module-服务提供者 cloud-provider-payment8001 引入pom依赖: ```xml <!--包含了sleuth+zipkin--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> ``` 配置yaml文件: ```yaml spring: application: name: cloud-payment-service zipkin: base-url: http://192.168.231.129:9411 sleuth: sampler: #采样率值介于 0 到 1 之间,1 则表示全部采集 probability: 1 eureka: client: #表示是否将自己注册进EurekaServer默认为true。 register-with-eureka: true #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡 fetchRegistry: true service-url: #defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版 defaultZone: http://localhost:7001/eureka # 单机版 ``` 编写业务方法: ```java @RestController @Slf4j public class PaymentController { @GetMapping("/payment/zipkin") public String paymentZipkin() { return "hi ,i'am paymentzipkin server fall back,welcome to here"; } } ``` ### 创建module-服务调用者 cloue-consumer-order80 #### 引入pom依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> ``` #### 配置yaml文件: ```yaml spring: application: name: cloud-payment-order zipkin: base-url: http://192.168.231.129:9411 sleuth: sampler: #采样率值介于 0 到 1 之间,1 则表示全部采集 probability: 1 eureka: client: #表示是否将自己注册进EurekaServer默认为true。 register-with-eureka: true #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡 fetchRegistry: true service-url: #defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版 defaultZone: http://localhost:7001/eureka # 单机版 ``` #### 编写业务方法: ```java // ====================> zipkin+sleuth @GetMapping("/consumer/payment/zipkin") public String paymentZipkin() { String result = restTemplate.getForObject("http://localhost:8001"+"/payment/zipkin/", String.class); return result; } ``` ### 测试 1. 依次启动eureka7001/8001/80 2. 80调用8001几次测试 3. 打开浏览器访问: http://localhost:9411  最后修改:2023 年 02 月 26 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果文章有用,请随意打赏。