Spring Cloud Sleuth 集成 zipkin 組件java
Spring Cloud Sleuth 主要功能就是在分佈式系統中提供追蹤解決方案,而且兼容支持了 zipkin,你只須要在pom文件中引入相應的依賴便可.git
https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/web
https://gitee.com/ge.yang/spring-demo/tree/master/Jarspring
1, 啓動 zipkin-service: java -jar zipkin-server-2.11.9-exec.jarapache
2, 啓動成功後訪問 zipkin 主頁: http://localhost:9411/zipkin/app
1, 引入Jar包 pom.xml :maven
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.gy.cloud</groupId> <artifactId>cloud</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>cloud-b</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>cloud-b</name> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 服務追蹤分析組件 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> </dependencies> </project>
2, 配置參數 application.yml :分佈式
server: port: 8762 spring: # 當前服務名稱 application: name: service-b # zipkin服務地址 zipkin: base-url: http://localhost:9411 # 服務註冊地址 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
3, 編寫調用接口,啓動類 :spring-boot
package com.gy.cloud.cloudb; import brave.sampler.Sampler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController @EnableEurekaClient @SpringBootApplication public class CloudBApplication { public static void main(String[] args) { SpringApplication.run(CloudBApplication.class, args); System.out.println("=== 服務B啓動成功 === "); } @Value("${server.port}") private String port; @GetMapping("/hi") public String home(String name) { name = name == null ? "SERVICE-B" : name; return "Hi " + name + " , I am from port: " + port; } @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } @Autowired private RestTemplate restTemplate; @GetMapping("/info") public String info(){ System.out.println("=== SERVICE-B/info 調取 SERVICE-C/hi ==="); return restTemplate.getForObject("http://localhost:8763/hi", String.class); } // 服務追蹤分析取樣器 @Bean public Sampler defaultSampler() { return Sampler.ALWAYS_SAMPLE; } }
啓動 service-b, 訪問: http://localhost:8762/info , 系統報錯.學習
service-c 與 service-b 基本相同, 只需稍做修改:
1, 修改配置參數 application.yml :
server: port: 8763 spring: application: name: service-c
2, 修改啓動類:
@GetMapping("/info") public String info(){ System.out.println("=== SERVICE-B/info 調取 SERVICE-C/hi ==="); return restTemplate.getForObject("http://localhost:8762/hi", String.class); }
啓動 service-c, 訪問: http://localhost:8762/info :
學習文檔
方誌朋的博客 : https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f9-sleuth/
項目源碼: https://gitee.com/ge.yang/spring-demo/tree/master/cloud