Skip to content

Commit ec7cb88

Browse files
author
YunaiV
committed
增加 spring cloud x skywalking 链路追踪
1 parent da5219b commit ec7cb88

File tree

30 files changed

+724
-5
lines changed

30 files changed

+724
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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-14-sc-skywalking-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-14-sc-skywalking-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+
</dependencies>
57+
58+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.iocoder.springcloud.labx14.kafkademo.consumerdemo;
2+
3+
import cn.iocoder.springcloud.labx14.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.labx14.kafkademo.consumerdemo.listener;
2+
3+
import cn.iocoder.springcloud.labx14.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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.iocoder.springcloud.labx14.kafkademo.consumerdemo.listener;
2+
3+
import org.springframework.cloud.stream.annotation.Input;
4+
import org.springframework.messaging.SubscribableChannel;
5+
6+
public interface MySink {
7+
8+
String DEMO01_INPUT = "demo01-input";
9+
10+
@Input(DEMO01_INPUT)
11+
SubscribableChannel demo01Input();
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cn.iocoder.springcloud.labx14.kafkademo.consumerdemo.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,22 @@
1+
spring:
2+
application:
3+
name: demo-consumer-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+
server:
22+
port: ${random.int[10000,19999]} # 随机端口,方便启动多个消费者
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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-14-sc-skywalking-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-14-sc-skywalking-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+
</dependencies>
57+
58+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.iocoder.springcloud.labx14.kafkademo.producerdemo;
2+
3+
import cn.iocoder.springcloud.labx14.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.labx14.kafkademo.producerdemo.controller;
2+
3+
import cn.iocoder.springcloud.labx14.kafkademo.producerdemo.message.Demo01Message;
4+
import cn.iocoder.springcloud.labx14.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.labx14.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.labx14.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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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-output:
12+
destination: DEMO-TOPIC-01 # 目的地。这里使用 Kafka Topic
13+
content-type: application/json # 内容格式。这里使用 JSON
14+
# Spring Cloud Stream Kafka 配置项
15+
kafka:
16+
# Kafka Binder 配置项,对应 KafkaBinderConfigurationProperties 类
17+
binder:
18+
brokers: 127.0.0.1:9092 # 指定 Kafka Broker 地址,可以设置多个,以逗号分隔
19+
# Kafka 自定义 Binding 配置项,对应 KafkaBindingProperties Map
20+
bindings:
21+
demo01-output:
22+
# Kafka Producer 配置项,对应 KafkaProducerProperties 类
23+
producer:
24+
sync: true # 是否同步发送消息,默认为 false 异步。
25+
26+
server:
27+
port: 18080
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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-14</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-14-sc-skywalking-mq-kafka</artifactId>
13+
<packaging>pom</packaging>
14+
15+
<modules>
16+
<module>labx-14-sc-skywalking-mq-kafka-producer</module>
17+
<module>labx-14-sc-skywalking-mq-kafka-consumer</module>
18+
</modules>
19+
20+
</project>

0 commit comments

Comments
 (0)