增加 spring cloud sleuth 链路追踪

This commit is contained in:
YunaiV
2020-03-20 19:19:27 +08:00
parent a564adcef7
commit cacf03188f
10 changed files with 206 additions and 3 deletions

View File

@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>labx-13</artifactId>
<groupId>cn.iocoder.springboot.labs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>labx-13-sc-sleuth-mq-activemq</artifactId>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
</properties>
<!--
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
在 https://dwz.cn/mcLIfNKt 文章中Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入 Spring Cloud Sleuth + Zipkin 相关依赖,实现对它们的自动配置,从而实现链路追踪 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<!-- 实现对 ActiveMQ 的自动化配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,13 @@
package cn.iocoder.springboot.labx13.activemqdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ActiveMQApplication {
public static void main(String[] args) {
SpringApplication.run(ActiveMQApplication.class, args);
}
}

View File

@@ -0,0 +1,19 @@
package cn.iocoder.springboot.labx13.activemqdemo.consumer;
import cn.iocoder.springboot.labx13.activemqdemo.message.DemoMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class DemoConsumer {
private Logger logger = LoggerFactory.getLogger(getClass());
@JmsListener(destination = DemoMessage.QUEUE)
public void onMessage(DemoMessage message) {
logger.info("[onMessage][线程编号:{} 消息内容:{}]", Thread.currentThread().getId(), message);
}
}

View File

@@ -0,0 +1,26 @@
package cn.iocoder.springboot.labx13.activemqdemo.controller;
import cn.iocoder.springboot.labx13.activemqdemo.producer.DemoProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/demo")
public class DemoController {
@Autowired
private DemoProducer producer;
@GetMapping("/activemq")
public String echo() {
this.sendMessage(1);
return "activemq";
}
public void sendMessage(Integer id) {
producer.syncSend(id);
}
}

View File

@@ -0,0 +1,30 @@
package cn.iocoder.springboot.labx13.activemqdemo.message;
import java.io.Serializable;
public class DemoMessage implements Serializable {
public static final String QUEUE = "QUEUE_DEMO_";
/**
* 编号
*/
private Integer id;
public DemoMessage setId(Integer id) {
this.id = id;
return this;
}
public Integer getId() {
return id;
}
@Override
public String toString() {
return "DemoMessage{" +
"id=" + id +
'}';
}
}

View File

@@ -0,0 +1,22 @@
package cn.iocoder.springboot.labx13.activemqdemo.producer;
import cn.iocoder.springboot.labx13.activemqdemo.message.DemoMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;
@Component
public class DemoProducer {
@Autowired
private JmsMessagingTemplate jmsTemplate;
public void syncSend(Integer id) {
// 创建 DemoMessage 消息
DemoMessage message = new DemoMessage();
message.setId(id);
// 同步发送消息
jmsTemplate.convertAndSend(DemoMessage.QUEUE, message);
}
}

View File

@@ -0,0 +1,23 @@
spring:
application:
name: demo-application-activemq
# ActiveMQ 配置项,对应 ActiveMQProperties 配置类
activemq:
broker-url: tcp://127.0.0.1:61616 # Activemq Broker 的地址
user: admin # 账号
password: admin # 密码
packages:
trust-all: true # 可信任的反序列化包
# Zipkin 配置项,对应 ZipkinProperties 类
zipkin:
base-url: http://127.0.0.1:9411 # Zipkin 服务的地址
# Spring Cloud Sleuth 配置项
sleuth:
messaging:
# Spring Cloud Sleuth 针对 JMS 组件的配置项
jms:
enabled: true # 是否开启
remote-service-name: jms # 远程服务名,默认为 jms

View File

@@ -8,15 +8,20 @@ spring:
# binders:
# Binding 配置项,对应 BindingProperties Map
bindings:
demo01-input:
demo01-output:
destination: DEMO-TOPIC-01 # 目的地。这里使用 Kafka Topic
content-type: application/json # 内容格式。这里使用 JSON
group: demo01-consumer-group # 消费者分组
# Spring Cloud Stream Kafka 配置项
kafka:
# Kafka Binder 配置项,对应 KafkaBinderConfigurationProperties 类
binder:
brokers: 127.0.0.1:9092 # 指定 Kafka Broker 地址,可以设置多个,以逗号分隔
# Kafka 自定义 Binding 配置项,对应 KafkaBindingProperties Map
bindings:
demo01-output:
# Kafka Producer 配置项,对应 KafkaProducerProperties 类
producer:
sync: true # 是否同步发送消息,默认为 false 异步。
# Zipkin 配置项,对应 ZipkinProperties 类
zipkin:

View File

@@ -25,7 +25,7 @@ spring:
# Spring Cloud Sleuth 配置项
sleuth:
messaging:
# Spring Cloud Sleuth 针对 kafka 组件的配置项,例如说 SpringMVC
# Spring Cloud Sleuth 针对 kafka 组件的配置项kafka
kafka:
enabled: true # 是否开启
remote-service-name: kafka # 远程服务名,默认为 kafka

View File

@@ -26,6 +26,7 @@
<module>labx-13-sc-sleuth-mq-rabbitmq</module>
<module>labx-13-sc-sleuth-mq-kafka</module>
<module>labx-13-sc-sleuth-mq-activemq</module>
</modules>