Skip to content

Commit a564adc

Browse files
author
YunaiV
committed
增加 spring cloud sleuth 链路追踪
1 parent 7e92a03 commit a564adc

File tree

28 files changed

+789
-1
lines changed

28 files changed

+789
-1
lines changed

labx-13/labx-13-sc-sleuth-db-elasticsearch/src/main/java/cn/iocoder/springcloud/labx13/springmvcdemo/controller/UserController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class UserController {
1717

1818
@GetMapping("/get")
1919
public String get(@RequestParam("id") Integer id) {
20-
this.findById(1);
20+
this.findById(id);
2121
return "success";
2222
}
2323

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>labx-13-sc-sleuth-mq-kafka</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>labx-13-sc-sleuth-mq-kafka-producer</artifactId>
13+
14+
<properties>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
18+
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
19+
</properties>
20+
21+
<!--
22+
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
23+
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
24+
-->
25+
<dependencyManagement>
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-parent</artifactId>
30+
<version>${spring.boot.version}</version>
31+
<type>pom</type>
32+
<scope>import</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.cloud</groupId>
36+
<artifactId>spring-cloud-dependencies</artifactId>
37+
<version>${spring.cloud.version}</version>
38+
<type>pom</type>
39+
<scope>import</scope>
40+
</dependency>
41+
</dependencies>
42+
</dependencyManagement>
43+
44+
<dependencies>
45+
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-web</artifactId>
49+
</dependency>
50+
51+
<!-- 引入 Spring Cloud Stream kafka 相关依赖,将 kafka 作为消息队列,并实现对其的自动配置 -->
52+
<dependency>
53+
<groupId>org.springframework.cloud</groupId>
54+
<artifactId>spring-cloud-starter-stream-kafka</artifactId>
55+
</dependency>
56+
57+
<!-- 引入 Spring Cloud Sleuth + Zipkin 相关依赖,实现对它们的自动配置,从而实现链路追踪 -->
58+
<dependency>
59+
<groupId>org.springframework.cloud</groupId>
60+
<artifactId>spring-cloud-starter-zipkin</artifactId>
61+
</dependency>
62+
</dependencies>
63+
64+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.iocoder.springcloud.labx13.kafkademo.producerdemo;
2+
3+
import cn.iocoder.springcloud.labx13.kafkademo.producerdemo.message.MySource;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.cloud.stream.annotation.EnableBinding;
7+
8+
@SpringBootApplication
9+
@EnableBinding(MySource.class)
10+
public class ProducerApplication {
11+
12+
public static void main(String[] args) {
13+
SpringApplication.run(ProducerApplication.class, args);
14+
}
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cn.iocoder.springcloud.labx13.kafkademo.producerdemo.controller;
2+
3+
import cn.iocoder.springcloud.labx13.kafkademo.producerdemo.message.Demo01Message;
4+
import cn.iocoder.springcloud.labx13.kafkademo.producerdemo.message.MySource;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.messaging.Message;
9+
import org.springframework.messaging.support.MessageBuilder;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RestController;
13+
14+
import java.util.Random;
15+
16+
@RestController
17+
@RequestMapping("/demo01")
18+
public class Demo01Controller {
19+
20+
private Logger logger = LoggerFactory.getLogger(getClass());
21+
22+
@Autowired
23+
private MySource mySource;
24+
25+
@GetMapping("/send")
26+
public boolean send() {
27+
// 创建 Message
28+
Demo01Message message = new Demo01Message()
29+
.setId(new Random().nextInt());
30+
// 创建 Spring Message 对象
31+
Message<Demo01Message> springMessage = MessageBuilder.withPayload(message)
32+
.build();
33+
// 发送消息
34+
return mySource.demo01Output().send(springMessage);
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cn.iocoder.springcloud.labx13.kafkademo.producerdemo.message;
2+
3+
/**
4+
* 示例 01 的 Message 消息
5+
*/
6+
public class Demo01Message {
7+
8+
/**
9+
* 编号
10+
*/
11+
private Integer id;
12+
13+
public Demo01Message setId(Integer id) {
14+
this.id = id;
15+
return this;
16+
}
17+
18+
public Integer getId() {
19+
return id;
20+
}
21+
22+
@Override
23+
public String toString() {
24+
return "Demo01Message{" +
25+
"id=" + id +
26+
'}';
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package cn.iocoder.springcloud.labx13.kafkademo.producerdemo.message;
2+
3+
import org.springframework.cloud.stream.annotation.Output;
4+
import org.springframework.messaging.MessageChannel;
5+
6+
public interface MySource {
7+
8+
@Output("demo01-output")
9+
MessageChannel demo01Output();
10+
11+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
spring:
2+
application:
3+
name: demo-producer-application
4+
cloud:
5+
# Spring Cloud Stream 配置项,对应 BindingServiceProperties 类
6+
stream:
7+
# Binder 配置项,对应 BinderProperties Map
8+
# binders:
9+
# Binding 配置项,对应 BindingProperties Map
10+
bindings:
11+
demo01-input:
12+
destination: DEMO-TOPIC-01 # 目的地。这里使用 Kafka Topic
13+
content-type: application/json # 内容格式。这里使用 JSON
14+
group: demo01-consumer-group # 消费者分组
15+
# Spring Cloud Stream Kafka 配置项
16+
kafka:
17+
# Kafka Binder 配置项,对应 KafkaBinderConfigurationProperties 类
18+
binder:
19+
brokers: 127.0.0.1:9092 # 指定 Kafka Broker 地址,可以设置多个,以逗号分隔
20+
21+
# Zipkin 配置项,对应 ZipkinProperties 类
22+
zipkin:
23+
base-url: http://127.0.0.1:9411 # Zipkin 服务的地址
24+
25+
# Spring Cloud Sleuth 配置项
26+
sleuth:
27+
messaging:
28+
# Spring Cloud Sleuth 针对 Kafka 组件的配置项
29+
kafka:
30+
enabled: true # 是否开启
31+
remote-service-name: kafka # 远程服务名,默认为 kafka
32+
33+
server:
34+
port: 18080
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>labx-10</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>labx-13-sc-stream-mq-kafka-consumer</artifactId>
13+
14+
<properties>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
18+
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
19+
</properties>
20+
21+
<!--
22+
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
23+
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
24+
-->
25+
<dependencyManagement>
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-parent</artifactId>
30+
<version>${spring.boot.version}</version>
31+
<type>pom</type>
32+
<scope>import</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.cloud</groupId>
36+
<artifactId>spring-cloud-dependencies</artifactId>
37+
<version>${spring.cloud.version}</version>
38+
<type>pom</type>
39+
<scope>import</scope>
40+
</dependency>
41+
</dependencies>
42+
</dependencyManagement>
43+
44+
<dependencies>
45+
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-web</artifactId>
49+
</dependency>
50+
51+
<!-- 引入 Spring Cloud Stream Kafka 相关依赖,将 kafka 作为消息队列,并实现对其的自动配置 -->
52+
<dependency>
53+
<groupId>org.springframework.cloud</groupId>
54+
<artifactId>spring-cloud-starter-stream-kafka</artifactId>
55+
</dependency>
56+
57+
<!-- 引入 Spring Cloud Sleuth + Zipkin 相关依赖,实现对它们的自动配置,从而实现链路追踪 -->
58+
<dependency>
59+
<groupId>org.springframework.cloud</groupId>
60+
<artifactId>spring-cloud-starter-zipkin</artifactId>
61+
</dependency>
62+
</dependencies>
63+
64+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.iocoder.springcloud.labx13.kafkademo.consumerdemo;
2+
3+
import cn.iocoder.springcloud.labx13.kafkademo.consumerdemo.listener.MySink;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.cloud.stream.annotation.EnableBinding;
7+
8+
@SpringBootApplication
9+
@EnableBinding(MySink.class)
10+
public class ConsumerApplication {
11+
12+
public static void main(String[] args) {
13+
SpringApplication.run(ConsumerApplication.class, args);
14+
}
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cn.iocoder.springcloud.labx13.kafkademo.consumerdemo.listener;
2+
3+
import cn.iocoder.springcloud.labx13.kafkademo.consumerdemo.message.Demo01Message;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.cloud.stream.annotation.StreamListener;
7+
import org.springframework.messaging.handler.annotation.Payload;
8+
import org.springframework.stereotype.Component;
9+
10+
@Component
11+
public class Demo01Consumer {
12+
13+
private Logger logger = LoggerFactory.getLogger(getClass());
14+
15+
@StreamListener(MySink.DEMO01_INPUT)
16+
public void onMessage(@Payload Demo01Message message) {
17+
logger.info("[onMessage][线程编号:{} 消息内容:{}]", Thread.currentThread().getId(), message);
18+
}
19+
20+
}

0 commit comments

Comments
 (0)