搭建链路监控步骤

服务提供者

以我们之前的模块cloud-provider-payment8001为例Eureka7001单机版

pom

添加zipkin依赖,包含了sleuth

1
2
3
4
5
<!--包含了sleuth+zipkin-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

image-20200710174610719

yml

spring.zikpin.base-urlzipkin地址

spring.sleuth.sampler.probability采样率 值介于 0 到 1 之间,1 则表示全部采集

image-20200710175931915

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
#服务端口号
server:
port: 8001

spring:
application:
name: cloud-payment-service
zipkin:
base-url: http://localhost:9411
sleuth:
sampler:
#采样率值介于 0 到 1 之间,1 则表示全部采集
probability: 1
datasource:
type: com.alibaba.druid.pool.DruidDataSource #druid数据源
driver-class-name: org.gjt.mm.mysql.Driver #mysql驱动包
url: jdbc:mysql://localhost:3306/springcloud?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: kylin

mybatis:
mapper-locations: classpath:mapper/*.xml #各mapper的xml文件路径
type-aliases-package: com.kylin.entities #实体类所在包起别名

eureka:
client:
# 表示是否将自己注册进EurekaServer 默认为true
register-with-eureka: true
# 是否从EurekaServer抓取已有的注册信息 ,默认为true。 单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
#设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://localhost:7001/eureka
# defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com/eureka #集群版
instance:
instance-id: payment8001
prefer-ip-address: true #访问路径可以显示IP地址
#Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
# lease-renewal-interval-in-seconds: 1
#Eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务
# lease-expiration-duration-in-seconds: 2

controller

image-20200710175442732

1
2
3
4
@GetMapping("/payment/zipkin")
public String paymentZipkin(){
return "hi,I am paymentZipkin server fallback,welcome to here,(*^_^*)";
}

服务消费者

修改我们之前的模块cloud-consumer-order80

pom

添加zipkin依赖,包含了sleuth

1
2
3
4
5
<!--包含了sleuth+zipkin-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

yml

image-20200710175949919

controller

image-20200710180135204

1
2
3
4
5
6
7
//zipkin+sleuth
@GetMapping("/consumer/payment/zipkin")
public String paymentZipkin()
{
String result = restTemplate.getForObject("http://localhost:8001"+"/payment/zipkin/", String.class);
return result;
}

测试

启动7001 80 8001

image-20200710180230491)image-20200710180259316

多次访问客户端http://localhost/consumer/payment/zipkin调用服务提供者提供的服务

image-20200710180347440

访问http://localhost:9411/zipkin/,选择cloud-order-service

image-20200710180426014

可能到我们发送的请求

image-20200710184011306

点进第一个,就能看到请求链路了。

image-20200710184046943

测试成功!