【Zipkin】01-Zipkin与SpringCloud
Zipkin
简介
链路追踪。在微服务项目中,用户的一次完整的请求,可能会由多台服务器中不同的服务来完成。如果一个服务部署到了多台服务器,那么该请求通过这个服务时,一定只会通过该服务中的一台服务器。在我们需要查询该请求通过服务器的完整路径时,就需要通过链路追踪来解决该问题。
应用
安装
在Linux服务器的docker中安装
1 | 1. 拉取项目 |
拉取项目
docker pull openzipkin/zipkin
启动项目
docker run -d --restart always -p 9411:9411 --name zipkin openzipkin/zipkin
访问客户端页面
http://ip:9411/zipkin/
编写
在下面链路追踪的使用中,我选择使用一个feign调用的服务,两个service提供接口的服务。
cloud-service服务
将该代码复制两次,记得修改端口号
项目结构
pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27<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.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.8</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
</dependencies>application.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15server:
port: 8011
spring:
application:
name: cloud-service
# 链路追踪配置
zipkin:
base-url: http://api.zipkin:9411
sleuth:
sampler:
probability: 1 # 采样率介于0~1之间,1表示全部采样
eureka:
client:
service-url:
defaultZone: http://eureka8001.com:8001/eurekaServiceMain8011.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27/**
* 2. 引入链路追踪zipkin-sleuth
* 2.0 docker安装zipkin
* 1. `docker pull openzipkin/zipkin`
* 2. `docker run -d --restart always -p 9411:9411 --name zipkin openzipkin/zipkin`
* 3. 访问:http://ip:9411/zipkin/
* 2.1 依赖
* spring-cloud-starter-zipkin
* 2.2 配置
* spring:
* zipkin:
* base-url: http://api.zipkin:9411
* sleuth:
* sampler:
* probability: 1
* @author zsq
* @date 2021年08月27日 18:09:51
*/
public class ServiceMain8011 {
public static void main(String[] args) {
SpringApplication.run(ServiceMain8011.class);
}
}TestController.java
1
2
3
4
5
6
7
8
9
10
11
12
13@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping("/ok/{id}")
public String ok(@PathVariable String id) {
String result = "service - ok - id = " + id;
log.info(result);
return result;
}
}
cloud-feign-hystrix服务
通过feign,调用cloud-service的接口
项目结构
pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35<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.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.8</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
</dependencies>application.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24server:
port: 8021
spring:
application:
name: cloud-feign
zipkin:
base-url: http://api.zipkin:9411
sleuth:
sampler:
probability: 1
eureka:
client:
service-url:
defaultZone: http://eureka8001.com:8001/eureka
# 开启feign
feign:
hystrix:
enabled: true
# 开启Hystrix Dashboard访问
management:
endpoints:
web:
exposure:
include: hystrix.streamFeignMain8021.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57/**
* 1. 添加依赖:spring-cloud-starter-openfeign,spring-cloud-starter-netflix-hystrix
* 2. 启动Feign调用的熔断Hystrix,还需添加配置
* # 开启feign
* feign:
* hystrix:
* enabled: true
* 3. 启动Feign,添加注解:@EnableFeignClients;启动Hystrix,添加注解:@EnableHystrix
* 4. 若使用Hystrix Dashboard启动监控,需要添加actuator配置
* 4.1 依赖 spring-boot-starter-actuator
* 4.2 配置文件
* # 开启Hystrix Dashboard访问
* management:
* endpoints:
* web:
* exposure:
* include: hystrix.stream
* 4.3 注册Bean容器
* .@Bean
* public ServletRegistrationBean getServlet() {
* HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
* ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
* registrationBean.setLoadOnStartup(1);
* registrationBean.addUrlMappings("/hystrix.stream");
* registrationBean.setName("HystrixMetricsStreamServlet");
* return registrationBean;
* }
* @author zsq
* @date 2021年08月28日 00:39:27
*/
public class FeignMain8021 {
public static void main(String[] args) {
SpringApplication.run(FeignMain8021.class);
}
/**
* 此配置是为了服务监控配置,与服务容错本身无关,Spring Cloud 升级后的坑
* ServletRegistrationBean因为springboot的默认路径不是hystrix.stream
* 只要在自己的项目里配置下面的servlet就可以了
* @return ServletRegistrationBean
*/
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}TestFeignController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class TestFeignController {
private TestFeign testFeign;
public String ok( String id){
String result = "feign - service - ok - id = " + id;
log.info(result);
return testFeign.ok(id);
}
}TestFeign.java
1
2
3
4
5
6
7
public interface TestFeign {
String ok( String id);
}TestFeignImpl.java
1
2
3
4
5
6
7
8
9
10
11
public class TestFeignImpl implements TestFeign {
public String ok(String id) {
String result = "fallback - service - ok - id = " + id;
log.info(result);
return result;
}
}
启动项目
使用
调用接口
http://localhost:8021/feign/ok/1
,打开链路追踪页面查看详情
-------------本文结束感谢您的阅读-------------
相关文章