mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 skywalking 示例 - rabbitmq
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.2.RELEASE</version>
|
||||
<version>2.1.11.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
@@ -18,7 +18,7 @@
|
||||
<dependency>
|
||||
<groupId>org.springframework.kafka</groupId>
|
||||
<artifactId>spring-kafka</artifactId>
|
||||
<version>2.3.3.RELEASE</version>
|
||||
<version>2.2.11.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对 SpringMVC 的自动化配置 -->
|
||||
|
||||
29
lab-39/lab-39-rabbitmq-demo/pom.xml
Normal file
29
lab-39/lab-39-rabbitmq-demo/pom.xml
Normal file
@@ -0,0 +1,29 @@
|
||||
<?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-39-rabbitmq-demo</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- 实现对 RabbitMQ 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对 SpringMVC 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springboot.lab39.skywalkingdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class RabbitMQApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(RabbitMQApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package cn.iocoder.springboot.lab39.skywalkingdemo.config;
|
||||
|
||||
import cn.iocoder.springboot.lab39.skywalkingdemo.message.DemoMessage;
|
||||
import org.springframework.amqp.core.Binding;
|
||||
import org.springframework.amqp.core.BindingBuilder;
|
||||
import org.springframework.amqp.core.DirectExchange;
|
||||
import org.springframework.amqp.core.Queue;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class RabbitConfig {
|
||||
|
||||
// 创建 Queue
|
||||
@Bean
|
||||
public Queue demoQueue() {
|
||||
return new Queue(DemoMessage.QUEUE, // Queue 名字
|
||||
true, // durable: 是否持久化
|
||||
false, // exclusive: 是否排它
|
||||
false); // autoDelete: 是否自动删除
|
||||
}
|
||||
|
||||
// 创建 Direct Exchange
|
||||
@Bean
|
||||
public DirectExchange demoExchange() {
|
||||
return new DirectExchange(DemoMessage.EXCHANGE,
|
||||
true, // durable: 是否持久化
|
||||
false); // exclusive: 是否排它
|
||||
}
|
||||
|
||||
// 创建 Binding
|
||||
// Exchange:DemoMessage.EXCHANGE
|
||||
// Routing key:DemoMessage.ROUTING_KEY
|
||||
// Queue:DemoMessage.QUEUE
|
||||
@Bean
|
||||
public Binding demoBinding() {
|
||||
return BindingBuilder.bind(demoQueue()).to(demoExchange()).with(DemoMessage.ROUTING_KEY);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package cn.iocoder.springboot.lab39.skywalkingdemo.consumer;
|
||||
|
||||
import cn.iocoder.springboot.lab39.skywalkingdemo.message.DemoMessage;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@RabbitListener(queues = DemoMessage.QUEUE)
|
||||
public class DemoConsumer {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@RabbitHandler
|
||||
public void onMessage(DemoMessage message) {
|
||||
logger.info("[onMessage][线程编号:{} 消息内容:{}]", Thread.currentThread().getId(), message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package cn.iocoder.springboot.lab39.skywalkingdemo.controller;
|
||||
|
||||
import cn.iocoder.springboot.lab39.skywalkingdemo.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("/rabbitmq")
|
||||
public String echo() {
|
||||
this.sendMessage(1);
|
||||
return "rabbitmq";
|
||||
}
|
||||
|
||||
public void sendMessage(Integer id) {
|
||||
producer.syncSend(id);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package cn.iocoder.springboot.lab39.skywalkingdemo.message;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class DemoMessage implements Serializable {
|
||||
|
||||
public static final String QUEUE = "QUEUE_DEMO_";
|
||||
|
||||
public static final String EXCHANGE = "EXCHANGE_DEMO_";
|
||||
|
||||
public static final String ROUTING_KEY = "ROUTING_KEY_";
|
||||
|
||||
/**
|
||||
* 编号
|
||||
*/
|
||||
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 +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package cn.iocoder.springboot.lab39.skywalkingdemo.producer;
|
||||
|
||||
import cn.iocoder.springboot.lab39.skywalkingdemo.message.DemoMessage;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class DemoProducer {
|
||||
|
||||
@Autowired
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
|
||||
public void syncSend(Integer id) {
|
||||
// 创建 DemoMessage 消息
|
||||
DemoMessage message = new DemoMessage();
|
||||
message.setId(id);
|
||||
// 同步发送消息
|
||||
rabbitTemplate.convertAndSend(DemoMessage.EXCHANGE, DemoMessage.ROUTING_KEY, message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
server:
|
||||
port: 8079
|
||||
|
||||
spring:
|
||||
# RabbitMQ 配置项,对应 RabbitProperties 配置类
|
||||
rabbitmq:
|
||||
host: 127.0.0.1 # RabbitMQ 服务的地址
|
||||
port: 5672 # RabbitMQ 服务的端口
|
||||
username: guest # RabbitMQ 服务的账号
|
||||
password: guest # RabbitMQ 服务的密码
|
||||
10
lab-39/lab-39-rabbitmq-demo/target/classes/application.yaml
Normal file
10
lab-39/lab-39-rabbitmq-demo/target/classes/application.yaml
Normal file
@@ -0,0 +1,10 @@
|
||||
server:
|
||||
port: 8079
|
||||
|
||||
spring:
|
||||
# RabbitMQ 配置项,对应 RabbitProperties 配置类
|
||||
rabbitmq:
|
||||
host: 127.0.0.1 # RabbitMQ 服务的地址
|
||||
port: 5672 # RabbitMQ 服务的端口
|
||||
username: guest # RabbitMQ 服务的账号
|
||||
password: guest # RabbitMQ 服务的密码
|
||||
@@ -21,6 +21,7 @@
|
||||
<module>lab-39-elasticsearch-jest</module>
|
||||
<module>lab-39-rocketmq</module>
|
||||
<module>lab-39-kafka</module>
|
||||
<module>lab-39-rabbitmq-demo</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
||||
Reference in New Issue
Block a user