Skip to content

Commit

Permalink
add sleuth
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyb committed Oct 20, 2022
1 parent f86decd commit d6dc5a4
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 20 deletions.
20 changes: 10 additions & 10 deletions rms-mall-gateway/src/main/resources/bootstrap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ spring:
# uri: 127.0.0.1:8080/user/{id} # 匹配后路由地址
# predicates: # 断言, 路径相匹配的进行路由
# - Path=/user/{id}
# kafka:
# bootstrap-servers: 127.0.0.1:9092
# producer:
# retries: 3
# consumer:
# auto-offset-reset: latest
# zipkin:
# sender:
# type: kafka # 默认是 web
# base-url: http://localhost:9411/
kafka:
bootstrap-servers: 127.0.0.1:9092
producer:
retries: 3
consumer:
auto-offset-reset: latest
zipkin:
sender:
type: kafka # 默认是 web
base-url: http://localhost:9411/
main:
allow-bean-definition-overriding: true # 因为将来会引入很多依赖, 难免有重名的 bean

Expand Down
2 changes: 1 addition & 1 deletion rms-mall-gateway/src/main/resources/http/nacos-client.http
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### 查询服务
GET http://127.0.0.1:9001/imooc/rms-mall-nacos-client/nacos-client/service-instance?serviceId=rms-mall-gateway
Accept: application/json
e-commerce-user: eyJhbGciOiJSUzI1NiJ9.eyJlLWNvbW1lcmNlLXVzZXIiOiJ7XCJpZFwiOjEwLFwidXNlcm5hbWVcIjpcIndhbmd5YkBxcS5jb21cIn0iLCJqdGkiOiJiYTU0MjBjNy0wMTM1LTRhZjYtOGVjYS0wZGUwZmQ5OTI5ZDgiLCJleHAiOjE2NjYxMDg4MDB9.eulJFnRt_I7ycLFhcZVi2wLk8HdWvnHgrXKqBm9j86StFnMl4D4fSFgWGOQoD1dwFgOMT00JhHb77hK1wsxQDMbymIj9iii5m21tNmdRfGY7FbwMh7Bd9M2s3_wkct7xHxcdyQ-z7T_0GgUYRJsT6zpTzBW5bloiltG_V1mqNCxRpvXo9HlztizZg69BaZuVSpoSee-w9g74_cvS9FsZyB9ztu19d6pz-OoByjBbWC0u4F_9TS8zCkWsGpGTReiKiP4MVPVHv44BqAUqJFLsLky7kPRx351tVQDuCNT5mXN0U-Pu4HEOtIV59C8GwvtXJooLAQtsJ5vccF_ax_WkMQ
e-commerce-user: eyJhbGciOiJSUzI1NiJ9.eyJlLWNvbW1lcmNlLXVzZXIiOiJ7XCJpZFwiOjEwLFwidXNlcm5hbWVcIjpcIndhbmd5YkBxcS5jb21cIn0iLCJqdGkiOiJjMWE5MWRmNC0zYjc3LTQ2MjktYjZjNC01M2MwZDYzNDczMzAiLCJleHAiOjE2NjYyODE2MDB9.K2K6WRKIUy3a96CKdxOxMKHDXheZlOaVcc5yglqlGq8Hf5idsJApgT0gE6T13mawrnvafwG0OnI2KHOWX05h8cSfkS25_IXCEFy_Qk-Wyf8pgLCFWQZU4MhIbMqVCzv7WB196fZYunAlr0JaYL2BJRxMr4k2Lu5x2o9y2jTBTkT4Yc1qpfgZEQLM9Ab5s6mszEA36qHFZDlKiT5iT_mVZUbO8aaB-IDhNT5wi5HDeFYkr82C5Q_v7Ic1uwZLWnaZa0wTnFQFr_ouj39itaziUlWtvxaSlmOFCW3n9QMdlVagdaTqpCqeUNGdu5ptEUysrnDgd6eBLSganiS_SKHirA
token: imooc
17 changes: 12 additions & 5 deletions rms-mall-nacos-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,29 @@
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

<!-- 数据绑定 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

<!-- zipkin = spring-cloud-starter-sleuth + spring-cloud-sleuth-zipkin-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.5.0.RELEASE</version>
</dependency>

</dependencies>
<!--
SpringBoot的Maven插件, 能够以Maven的方式为应用提供SpringBoot的支持,可以将
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.wyb.rms.nacos.client.controller;

import com.wyb.rms.nacos.client.service.SleuthTraceInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* <h1>打印跟踪信息</h1>
*/
@Slf4j
@RestController
@RequestMapping("/sleuth")
public class SleuthTraceInfoController {

private final SleuthTraceInfoService traceInfoService;

public SleuthTraceInfoController(SleuthTraceInfoService traceInfoService) {
this.traceInfoService = traceInfoService;
}

/**
* <h2>打印日志跟踪信息</h2>
*/
@GetMapping("/trace-info")
public void logCurrentTraceInfo() {
traceInfoService.logCurrentTraceInfo();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.wyb.rms.nacos.client.service;

import brave.Tracer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
* <h1>使用代码更直观的看到 Sleuth 生成的相关跟踪信息</h1>
* */
@Slf4j
@Service
public class SleuthTraceInfoService {

/** brave.Tracer 跟踪对象 */
private final Tracer tracer;

public SleuthTraceInfoService(Tracer tracer) {
this.tracer = tracer;
}

/**
* <h2>打印当前的跟踪信息到日志中</h2>
* */
public void logCurrentTraceInfo() {

log.info("Sleuth trace id: [{}]", tracer.currentSpan().context().traceId());
log.info("Sleuth span id: [{}]", tracer.currentSpan().context().spanId());
}
}
27 changes: 25 additions & 2 deletions rms-mall-nacos-client/src/main/resources/bootstrap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ spring:

cloud:
nacos:
# 服务注册发现
discovery:
enabled: true # 如果不想使用 Nacos 进行服务注册和发现, 设置为 false 即可
server-addr: 127.0.0.1:8848
# server-addr: 127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850 # Nacos 服务器地址
namespace:
metadata:
management:
context-path: ${server.servlet.context-path}/actuator
config:
server-addr: localhost:8848
file-extension: yml
Expand All @@ -28,9 +37,23 @@ spring:
- data-id: common4.yml
group: DEFAULT_GROUP
refresh: true
discovery:
server-addr: localhost:8848

kafka:
bootstrap-servers: 127.0.0.1:9092
producer:
retries: 3
consumer:
auto-offset-reset: latest
sleuth:
sampler:
# ProbabilityBasedSampler 抽样策略
probability: 1.0 # 采样比例, 1.0 表示 100%, 默认是 0.1
# RateLimitingSampler 抽样策略, 设置了限速采集, spring.sleuth.sampler.probability 属性值无效
rate: 100 # 每秒间隔接受的 trace 量
zipkin:
sender:
type: kafka # 默认是 web
base-url: http://localhost:9411/

# 暴露端点
management:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ GET http://127.0.0.1:8000/rms-mall-nacos-client/nacos-client/getConfig
Accept: application/json

### 查看 Sleuth 跟踪信息
GET http://127.0.0.1:9001/imooc/ecommerce-nacos-client/sleuth/trace-info
GET http://127.0.0.1:9001/imooc/rms-mall-nacos-client/sleuth/trace-info
Accept: application/json
e-commerce-user: eyJhbGciOiJSUzI1NiJ9.eyJlLWNvbW1lcmNlLXVzZXIiOiJ7XCJpZFwiOjE3LFwidXNlcm5hbWVcIjpcIkltb29jUWlueWlAaW1vb2MuY29tXCJ9IiwianRpIjoiMGIxNzQyYWItMWU3OC00OTZjLWIyNTAtMjNkZGQ1ZGEyZTU1IiwiZXhwIjoxNjI0MjA0ODAwfQ.QKGHzohSHdYDHzUVHpe9gNPUgzfkPwrSbB-WiMWYjLlt2tr9BufzZM8bSt-whb_bd0hKoC6rkYYO0WUVR67uSML-2yaTL1xMIn8GH9Flyig3rpO4vefL3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