一, 简介
Spring Cloud Sleuth 集成 zipkin 组件
Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,并且兼容支持了 zipkin,你只需要在pom文件中引入相应的依赖即可.
二, 下载 zipkin-server :
https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/
https://gitee.com/ge.yang/spring-demo/tree/master/Jar
1, 启动 zipkin-service: java -jar zipkin-server-2.11.9-exec.jar
2, 启动成功后访问 zipkin 主页: http://localhost:9411/zipkin/
三, 新建服务 service-b
1, 引入Jar包 pom.xml :
<?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, 编写调用接口,启动类 :
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-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
来源:oschina
链接:https://my.oschina.net/u/3681868/blog/3000113