Spring Boot 2.0 + zipkin 分布式跟蹤系統快速入門


原文:https://www.jianshu.com/p/9bfe103418e2

 

 

注意

Spring Boot 2.0之后,使用EnableZipkinServer創建自定義的zipkin服務器已經被廢棄,將無法啟動
具體可看issue
Deprecates EnableZipkinServer to explain custom servers are unsupported

開始

搭建zipkin服務器

獲取最新發布的zipkin服務器,一個可執行的jar

curl -sSL https://zipkin.io/quickstart.sh | bash -s 

然后啟動它

java -jar zipkin.jar 

也可以通過docker來啟動

docker run -d -p 9411:9411 openzipkin/zipkin

啟動后就可以在瀏覽器打開http://your_host:9411/zipkin/查看了。

創建兩個服務

pom.xml

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <sleuth.version>2.0.0.RELEASE</sleuth.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- Sleuth automatically adds trace interceptors when in the classpath --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> <!-- Sends trace data to zipkin over http (defaults to http://localhost:9411/api/v2/spans) --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-sleuth-zipkin</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-sleuth</artifactId> <version>${sleuth.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> 

Backend.java

@EnableAutoConfiguration @RestController public class Backend { @RequestMapping("/api") public String printDate() { return new Date().toString() + " by hongxi"; } public static void main(String[] args) { SpringApplication.run(Backend.class, "--spring.application.name=backend", "--server.port=9000" ); } } 

Frontend.java

@EnableAutoConfiguration @RestController @CrossOrigin // So that javascript can be hosted elsewhere public class Frontend { @Autowired RestTemplate restTemplate; @RequestMapping("/") public String callBackend() { return restTemplate.getForObject("http://localhost:9000/api", String.class); } @Bean RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(Frontend.class, "--spring.application.name=frontend", "--server.port=8081" ); } } 

application.properties

spring.sleuth.traceId128=true spring.sleuth.sampler.probability=1.0 

然后分別啟動Backen和Frontend,在瀏覽器訪問http://localhost:8080/,可以看到頁面顯示

Wed Jun 20 16:47:00 CST 2018 by hongxi 

說明調用成功,然后回到http://your_host:9411/zipkin/點擊大大的那個按鈕“Find Traces”即可看到調用跟蹤信息。

 
 

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM