Skip to content

Commit b9ea05b

Browse files
author
YunaiV
committed
增加 zipkin 示例,各种把
1 parent 2aac2db commit b9ea05b

File tree

47 files changed

+1745
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1745
-4
lines changed

Diff for: lab-39/lab-39-activemq/src/main/java/cn/iocoder/springboot/lab39/skywalkingdemo/consumer/DemoConsumer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package cn.iocoder.springboot.lab39.skywalkingdemo.consumer;
22

3-
import cn.iocoder.springboot.lab32.activemqdemo.message.DemoMessage;
3+
import cn.iocoder.springboot.lab39.skywalkingdemo.message.DemoMessage;
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
66
import org.springframework.jms.annotation.JmsListener;

Diff for: lab-39/lab-39-activemq/src/main/java/cn/iocoder/springboot/lab39/skywalkingdemo/message/DemoMessage.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cn.iocoder.springboot.lab32.activemqdemo.message;
1+
package cn.iocoder.springboot.lab39.skywalkingdemo.message;
22

33
import java.io.Serializable;
44

Diff for: lab-39/lab-39-activemq/src/main/java/cn/iocoder/springboot/lab39/skywalkingdemo/producer/DemoProducer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package cn.iocoder.springboot.lab39.skywalkingdemo.producer;
22

3-
import cn.iocoder.springboot.lab32.activemqdemo.message.DemoMessage;
3+
import cn.iocoder.springboot.lab39.skywalkingdemo.message.DemoMessage;
44
import org.springframework.beans.factory.annotation.Autowired;
55
import org.springframework.jms.core.JmsMessagingTemplate;
66
import org.springframework.stereotype.Component;

Diff for: lab-40/lab-40-activemq/pom.xml

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.2.1.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>lab-40-activemq</artifactId>
14+
15+
<dependencies>
16+
<!-- 实现对 ActiveMQ 的自动化配置 -->
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-activemq</artifactId>
20+
</dependency>
21+
22+
<!-- 实现对 SpringMVC 的自动化配置 -->
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-web</artifactId>
26+
</dependency>
27+
28+
<!-- Brave 核心库 -->
29+
<!-- The below are needed to report traces to http://localhost:9411/api/v2/spans -->
30+
<dependency>
31+
<groupId>io.zipkin.brave</groupId>
32+
<artifactId>brave</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>io.zipkin.reporter2</groupId>
36+
<artifactId>zipkin-sender-okhttp3</artifactId>
37+
</dependency>
38+
39+
<!-- Adds the MVC class and method names to server spans -->
40+
<!-- Brave 对 Spring MVC 的支持 -->
41+
<dependency>
42+
<groupId>io.zipkin.brave</groupId>
43+
<artifactId>brave-instrumentation-spring-webmvc</artifactId>
44+
</dependency>
45+
<!-- Brave 对 JMS 的支持 -->
46+
<dependency>
47+
<groupId>io.zipkin.brave</groupId>
48+
<artifactId>brave-instrumentation-jms</artifactId>
49+
</dependency>
50+
51+
</dependencies>
52+
53+
<dependencyManagement>
54+
<!-- Brave Bom 文件 -->
55+
<dependencies>
56+
<dependency>
57+
<groupId>io.zipkin.brave</groupId>
58+
<artifactId>brave-bom</artifactId>
59+
<version>5.9.1</version>
60+
<type>pom</type>
61+
<scope>import</scope>
62+
</dependency>
63+
</dependencies>
64+
</dependencyManagement>
65+
66+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.iocoder.springboot.lab40.zipkindemo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class ActiveMQApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(ActiveMQApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cn.iocoder.springboot.lab40.zipkindemo.config;
2+
3+
import brave.spring.webmvc.SpanCustomizingAsyncHandlerInterceptor;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.annotation.Import;
7+
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
8+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
9+
10+
@Configuration
11+
@Import(SpanCustomizingAsyncHandlerInterceptor.class) // 创建拦截器 SpanCustomizingAsyncHandlerInterceptor Bean
12+
public class SpringMvcConfiguration implements WebMvcConfigurer {
13+
14+
@Autowired
15+
public SpanCustomizingAsyncHandlerInterceptor webMvcTracingCustomizer;
16+
17+
/**
18+
* Decorates server spans with application-defined web tags
19+
*/
20+
@Override
21+
public void addInterceptors(InterceptorRegistry registry) { // 记录 SpringMVC 相关信息到 Span 中
22+
registry.addInterceptor(webMvcTracingCustomizer);
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package cn.iocoder.springboot.lab40.zipkindemo.config;
2+
3+
import brave.CurrentSpanCustomizer;
4+
import brave.SpanCustomizer;
5+
import brave.Tracing;
6+
import brave.http.HttpTracing;
7+
import brave.jms.JmsTracing;
8+
import brave.servlet.TracingFilter;
9+
import org.springframework.beans.BeansException;
10+
import org.springframework.beans.factory.annotation.Value;
11+
import org.springframework.beans.factory.config.BeanPostProcessor;
12+
import org.springframework.context.annotation.Bean;
13+
import org.springframework.context.annotation.Configuration;
14+
import zipkin2.Span;
15+
import zipkin2.reporter.AsyncReporter;
16+
import zipkin2.reporter.Sender;
17+
import zipkin2.reporter.okhttp3.OkHttpSender;
18+
19+
import javax.jms.ConnectionFactory;
20+
import javax.servlet.Filter;
21+
22+
@Configuration
23+
public class ZipkinConfiguration {
24+
25+
// ==================== 通用配置 ====================
26+
27+
/**
28+
* Configuration for how to send spans to Zipkin
29+
*/
30+
@Bean
31+
public Sender sender() { // Sender 采用 HTTP 通信方式
32+
return OkHttpSender.create("http://127.0.0.1:9411/api/v2/spans");
33+
}
34+
35+
/**
36+
* Configuration for how to buffer spans into messages for Zipkin
37+
*/
38+
@Bean
39+
public AsyncReporter<Span> spanReporter() { // 异步 Reporter
40+
return AsyncReporter.create(sender());
41+
}
42+
43+
/**
44+
* Controls aspects of tracing such as the service name that shows up in the UI
45+
*/
46+
@Bean
47+
public Tracing tracing(@Value("${spring.application.name}") String serviceName) {
48+
return Tracing.newBuilder()
49+
.localServiceName(serviceName) // 应用名
50+
.spanReporter(this.spanReporter()).build();
51+
}
52+
53+
/**
54+
* Allows someone to add tags to a span if a trace is in progress
55+
*/
56+
@Bean
57+
public SpanCustomizer spanCustomizer(Tracing tracing) {
58+
return CurrentSpanCustomizer.create(tracing);
59+
}
60+
61+
// ==================== HTTP 相关 ====================
62+
63+
/**
64+
* Decides how to name and tag spans. By default they are named the same as the http method
65+
*/
66+
@Bean
67+
public HttpTracing httpTracing(Tracing tracing) {
68+
return HttpTracing.create(tracing);
69+
}
70+
71+
/**
72+
* Creates server spans for http requests
73+
*/
74+
@Bean
75+
public Filter tracingFilter(HttpTracing httpTracing) { // 拦截请求,记录 HTTP 请求的链路信息
76+
return TracingFilter.create(httpTracing);
77+
}
78+
79+
// ==================== SpringMVC 相关 ====================
80+
// @see SpringMvcConfiguration 类上的,@Import(SpanCustomizingAsyncHandlerInterceptor.class) 。因为 SpanCustomizingAsyncHandlerInterceptor 未提供 public 构造方法
81+
82+
// ==================== RabbitMQ 相关 ====================
83+
84+
@Bean
85+
public JmsTracing jmsTracing(Tracing tracing) {
86+
return JmsTracing.newBuilder(tracing)
87+
.remoteServiceName("demo-mq-activemq") // 远程 ActiveMQ 服务名,可自定义
88+
.build();
89+
}
90+
91+
@Bean
92+
public BeanPostProcessor activeMQBeanPostProcessor(JmsTracing jmsTracing) {
93+
return new BeanPostProcessor() {
94+
95+
@Override
96+
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
97+
return bean;
98+
}
99+
100+
@Override
101+
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
102+
// 如果是 ConnectionFactory ,针对 ActiveMQ Producer 和 Consumer
103+
if (bean instanceof ConnectionFactory) {
104+
return jmsTracing.connectionFactory((ConnectionFactory) bean);
105+
}
106+
return bean;
107+
}
108+
109+
};
110+
}
111+
112+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cn.iocoder.springboot.lab40.zipkindemo.consumer;
2+
3+
import cn.iocoder.springboot.lab40.zipkindemo.message.DemoMessage;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.jms.annotation.JmsListener;
7+
import org.springframework.stereotype.Component;
8+
9+
@Component
10+
public class DemoConsumer {
11+
12+
private Logger logger = LoggerFactory.getLogger(getClass());
13+
14+
@JmsListener(destination = DemoMessage.QUEUE)
15+
public void onMessage(DemoMessage message) {
16+
logger.info("[onMessage][线程编号:{} 消息内容:{}]", Thread.currentThread().getId(), message);
17+
}
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cn.iocoder.springboot.lab40.zipkindemo.controller;
2+
3+
import cn.iocoder.springboot.lab40.zipkindemo.producer.DemoProducer;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@RestController
10+
@RequestMapping("/demo")
11+
public class DemoController {
12+
13+
@Autowired
14+
private DemoProducer producer;
15+
16+
@GetMapping("/activemq")
17+
public String echo() {
18+
this.sendMessage(1);
19+
return "activemq";
20+
}
21+
22+
public void sendMessage(Integer id) {
23+
producer.syncSend(id);
24+
}
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cn.iocoder.springboot.lab40.zipkindemo.message;
2+
3+
import java.io.Serializable;
4+
5+
public class DemoMessage implements Serializable {
6+
7+
public static final String QUEUE = "QUEUE_DEMO_";
8+
9+
/**
10+
* 编号
11+
*/
12+
private Integer id;
13+
14+
public DemoMessage setId(Integer id) {
15+
this.id = id;
16+
return this;
17+
}
18+
19+
public Integer getId() {
20+
return id;
21+
}
22+
23+
@Override
24+
public String toString() {
25+
return "DemoMessage{" +
26+
"id=" + id +
27+
'}';
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cn.iocoder.springboot.lab40.zipkindemo.producer;
2+
3+
import cn.iocoder.springboot.lab40.zipkindemo.message.DemoMessage;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.jms.core.JmsMessagingTemplate;
6+
import org.springframework.stereotype.Component;
7+
8+
@Component
9+
public class DemoProducer {
10+
11+
@Autowired
12+
private JmsMessagingTemplate jmsTemplate;
13+
14+
public void syncSend(Integer id) {
15+
// 创建 DemoMessage 消息
16+
DemoMessage message = new DemoMessage();
17+
message.setId(id);
18+
// 同步发送消息
19+
jmsTemplate.convertAndSend(DemoMessage.QUEUE, message);
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
spring:
2+
application:
3+
name: demo-application-activemq
4+
5+
# ActiveMQ 配置项,对应 ActiveMQProperties 配置类
6+
activemq:
7+
broker-url: tcp://127.0.0.1:61616 # Activemq Broker 的地址
8+
user: admin # 账号
9+
password: admin # 密码
10+
packages:
11+
trust-all: true # 可信任的反序列化包

0 commit comments

Comments
 (0)