Spring Boot 2.0 + zipkin 分布式跟踪系统快速入门

原文链接: http://www.cnblogs.com/shihaiming/p/9724909.html

原文: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.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.0.3.RELEASEversion> <relativePath/>  parent> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding> <java.version>1.8java.version> <sleuth.version>2.0.0.RELEASEsleuth.version> properties> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-actuatorartifactId> dependency>  <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-sleuthartifactId> dependency>  <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-sleuth-zipkinartifactId> dependency> dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-sleuthartifactId> <version>${sleuth.version}version> <type>pomtype> <scope>importscope> 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”即可看到调用跟踪信息。

 
 

 
 

转载于:https://www.cnblogs.com/shihaiming/p/9724909.html

你可能感兴趣的:(Spring Boot 2.0 + zipkin 分布式跟踪系统快速入门)