forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YunaiV
committed
Jan 8, 2020
1 parent
2aac2db
commit b9ea05b
Showing
47 changed files
with
1,745 additions
and
4 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...ivemq/src/main/java/cn/iocoder/springboot/lab39/skywalkingdemo/consumer/DemoConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ctivemq/src/main/java/cn/iocoder/springboot/lab39/skywalkingdemo/message/DemoMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ivemq/src/main/java/cn/iocoder/springboot/lab39/skywalkingdemo/producer/DemoProducer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?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> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.2.1.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>lab-40-activemq</artifactId> | ||
|
||
<dependencies> | ||
<!-- 实现对 ActiveMQ 的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-activemq</artifactId> | ||
</dependency> | ||
|
||
<!-- 实现对 SpringMVC 的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<!-- Brave 核心库 --> | ||
<!-- The below are needed to report traces to http://localhost:9411/api/v2/spans --> | ||
<dependency> | ||
<groupId>io.zipkin.brave</groupId> | ||
<artifactId>brave</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.zipkin.reporter2</groupId> | ||
<artifactId>zipkin-sender-okhttp3</artifactId> | ||
</dependency> | ||
|
||
<!-- Adds the MVC class and method names to server spans --> | ||
<!-- Brave 对 Spring MVC 的支持 --> | ||
<dependency> | ||
<groupId>io.zipkin.brave</groupId> | ||
<artifactId>brave-instrumentation-spring-webmvc</artifactId> | ||
</dependency> | ||
<!-- Brave 对 JMS 的支持 --> | ||
<dependency> | ||
<groupId>io.zipkin.brave</groupId> | ||
<artifactId>brave-instrumentation-jms</artifactId> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<dependencyManagement> | ||
<!-- Brave Bom 文件 --> | ||
<dependencies> | ||
<dependency> | ||
<groupId>io.zipkin.brave</groupId> | ||
<artifactId>brave-bom</artifactId> | ||
<version>5.9.1</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
...40-activemq/src/main/java/cn/iocoder/springboot/lab40/zipkindemo/ActiveMQApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cn.iocoder.springboot.lab40.zipkindemo; | ||
|
||
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); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...q/src/main/java/cn/iocoder/springboot/lab40/zipkindemo/config/SpringMvcConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cn.iocoder.springboot.lab40.zipkindemo.config; | ||
|
||
import brave.spring.webmvc.SpanCustomizingAsyncHandlerInterceptor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
@Import(SpanCustomizingAsyncHandlerInterceptor.class) // 创建拦截器 SpanCustomizingAsyncHandlerInterceptor Bean | ||
public class SpringMvcConfiguration implements WebMvcConfigurer { | ||
|
||
@Autowired | ||
public SpanCustomizingAsyncHandlerInterceptor webMvcTracingCustomizer; | ||
|
||
/** | ||
* Decorates server spans with application-defined web tags | ||
*/ | ||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { // 记录 SpringMVC 相关信息到 Span 中 | ||
registry.addInterceptor(webMvcTracingCustomizer); | ||
} | ||
|
||
} |
112 changes: 112 additions & 0 deletions
112
...vemq/src/main/java/cn/iocoder/springboot/lab40/zipkindemo/config/ZipkinConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package cn.iocoder.springboot.lab40.zipkindemo.config; | ||
|
||
import brave.CurrentSpanCustomizer; | ||
import brave.SpanCustomizer; | ||
import brave.Tracing; | ||
import brave.http.HttpTracing; | ||
import brave.jms.JmsTracing; | ||
import brave.servlet.TracingFilter; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.beans.factory.config.BeanPostProcessor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import zipkin2.Span; | ||
import zipkin2.reporter.AsyncReporter; | ||
import zipkin2.reporter.Sender; | ||
import zipkin2.reporter.okhttp3.OkHttpSender; | ||
|
||
import javax.jms.ConnectionFactory; | ||
import javax.servlet.Filter; | ||
|
||
@Configuration | ||
public class ZipkinConfiguration { | ||
|
||
// ==================== 通用配置 ==================== | ||
|
||
/** | ||
* Configuration for how to send spans to Zipkin | ||
*/ | ||
@Bean | ||
public Sender sender() { // Sender 采用 HTTP 通信方式 | ||
return OkHttpSender.create("http://127.0.0.1:9411/api/v2/spans"); | ||
} | ||
|
||
/** | ||
* Configuration for how to buffer spans into messages for Zipkin | ||
*/ | ||
@Bean | ||
public AsyncReporter<Span> spanReporter() { // 异步 Reporter | ||
return AsyncReporter.create(sender()); | ||
} | ||
|
||
/** | ||
* Controls aspects of tracing such as the service name that shows up in the UI | ||
*/ | ||
@Bean | ||
public Tracing tracing(@Value("${spring.application.name}") String serviceName) { | ||
return Tracing.newBuilder() | ||
.localServiceName(serviceName) // 应用名 | ||
.spanReporter(this.spanReporter()).build(); | ||
} | ||
|
||
/** | ||
* Allows someone to add tags to a span if a trace is in progress | ||
*/ | ||
@Bean | ||
public SpanCustomizer spanCustomizer(Tracing tracing) { | ||
return CurrentSpanCustomizer.create(tracing); | ||
} | ||
|
||
// ==================== HTTP 相关 ==================== | ||
|
||
/** | ||
* Decides how to name and tag spans. By default they are named the same as the http method | ||
*/ | ||
@Bean | ||
public HttpTracing httpTracing(Tracing tracing) { | ||
return HttpTracing.create(tracing); | ||
} | ||
|
||
/** | ||
* Creates server spans for http requests | ||
*/ | ||
@Bean | ||
public Filter tracingFilter(HttpTracing httpTracing) { // 拦截请求,记录 HTTP 请求的链路信息 | ||
return TracingFilter.create(httpTracing); | ||
} | ||
|
||
// ==================== SpringMVC 相关 ==================== | ||
// @see SpringMvcConfiguration 类上的,@Import(SpanCustomizingAsyncHandlerInterceptor.class) 。因为 SpanCustomizingAsyncHandlerInterceptor 未提供 public 构造方法 | ||
|
||
// ==================== RabbitMQ 相关 ==================== | ||
|
||
@Bean | ||
public JmsTracing jmsTracing(Tracing tracing) { | ||
return JmsTracing.newBuilder(tracing) | ||
.remoteServiceName("demo-mq-activemq") // 远程 ActiveMQ 服务名,可自定义 | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public BeanPostProcessor activeMQBeanPostProcessor(JmsTracing jmsTracing) { | ||
return new BeanPostProcessor() { | ||
|
||
@Override | ||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { | ||
return bean; | ||
} | ||
|
||
@Override | ||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { | ||
// 如果是 ConnectionFactory ,针对 ActiveMQ Producer 和 Consumer | ||
if (bean instanceof ConnectionFactory) { | ||
return jmsTracing.connectionFactory((ConnectionFactory) bean); | ||
} | ||
return bean; | ||
} | ||
|
||
}; | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
...-activemq/src/main/java/cn/iocoder/springboot/lab40/zipkindemo/consumer/DemoConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package cn.iocoder.springboot.lab40.zipkindemo.consumer; | ||
|
||
import cn.iocoder.springboot.lab40.zipkindemo.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); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
...ivemq/src/main/java/cn/iocoder/springboot/lab40/zipkindemo/controller/DemoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cn.iocoder.springboot.lab40.zipkindemo.controller; | ||
|
||
import cn.iocoder.springboot.lab40.zipkindemo.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); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...40-activemq/src/main/java/cn/iocoder/springboot/lab40/zipkindemo/message/DemoMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package cn.iocoder.springboot.lab40.zipkindemo.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 + | ||
'}'; | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
...-activemq/src/main/java/cn/iocoder/springboot/lab40/zipkindemo/producer/DemoProducer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cn.iocoder.springboot.lab40.zipkindemo.producer; | ||
|
||
import cn.iocoder.springboot.lab40.zipkindemo.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); | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
lab-40/lab-40-activemq/src/main/resources/application.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
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 # 可信任的反序列化包 |
Oops, something went wrong.