mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 skywalking 示例
This commit is contained in:
@@ -11,7 +11,7 @@ import java.util.Date;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/demo")
|
||||
public class DemoController {
|
||||
public class DemoController {
|
||||
|
||||
@Autowired
|
||||
private ESUserRepository userRepository;
|
||||
|
||||
30
lab-39/lab-39-rocketmq/pom.xml
Normal file
30
lab-39/lab-39-rocketmq/pom.xml
Normal file
@@ -0,0 +1,30 @@
|
||||
<?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-rocketmq</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- 实现对 SpringMVC 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对 RocketMQ 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.rocketmq</groupId>
|
||||
<artifactId>rocketmq-spring-boot-starter</artifactId>
|
||||
<version>2.0.4</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springboot.lab39.skywalkingdemo.rocketmqdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class RocketMQApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(RocketMQApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package cn.iocoder.springboot.lab39.skywalkingdemo.rocketmqdemo.consumer;
|
||||
|
||||
import cn.iocoder.springboot.lab39.skywalkingdemo.rocketmqdemo.message.DemoMessage;
|
||||
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
|
||||
import org.apache.rocketmq.spring.core.RocketMQListener;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@RocketMQMessageListener(
|
||||
topic = DemoMessage.TOPIC,
|
||||
consumerGroup = "demo-consumer-group-" + DemoMessage.TOPIC
|
||||
)
|
||||
public class DemoConsumer implements RocketMQListener<DemoMessage> {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Override
|
||||
public void onMessage(DemoMessage message) {
|
||||
logger.info("[onMessage][线程编号:{} 消息内容:{}]", Thread.currentThread().getId(), message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package cn.iocoder.springboot.lab39.skywalkingdemo.rocketmqdemo.controller;
|
||||
|
||||
import cn.iocoder.springboot.lab39.skywalkingdemo.rocketmqdemo.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("/rocketmq")
|
||||
public String echo() {
|
||||
this.sendMessage(1);
|
||||
return "rocketmq";
|
||||
}
|
||||
|
||||
public void sendMessage(Integer id) {
|
||||
producer.syncSend(id);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package cn.iocoder.springboot.lab39.skywalkingdemo.rocketmqdemo.message;
|
||||
|
||||
/**
|
||||
* 示例的 Message 消息
|
||||
*/
|
||||
public class DemoMessage {
|
||||
|
||||
public static final String TOPIC = "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 +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package cn.iocoder.springboot.lab39.skywalkingdemo.rocketmqdemo.producer;
|
||||
|
||||
import cn.iocoder.springboot.lab39.skywalkingdemo.rocketmqdemo.message.DemoMessage;
|
||||
import org.apache.rocketmq.client.producer.SendResult;
|
||||
import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class DemoProducer {
|
||||
|
||||
@Autowired
|
||||
private RocketMQTemplate rocketMQTemplate;
|
||||
|
||||
public SendResult syncSend(Integer id) {
|
||||
// 创建 DemoMessage 消息
|
||||
DemoMessage message = new DemoMessage();
|
||||
message.setId(id);
|
||||
// 同步发送消息
|
||||
return rocketMQTemplate.syncSend(DemoMessage.TOPIC, message);
|
||||
}
|
||||
|
||||
}
|
||||
10
lab-39/lab-39-rocketmq/src/main/resources/application.yaml
Normal file
10
lab-39/lab-39-rocketmq/src/main/resources/application.yaml
Normal file
@@ -0,0 +1,10 @@
|
||||
server:
|
||||
port: 8079
|
||||
|
||||
# rocketmq 配置项,对应 RocketMQProperties 配置类
|
||||
rocketmq:
|
||||
name-server: 127.0.0.1:9876 # RocketMQ Namesrv
|
||||
# Producer 配置项
|
||||
producer:
|
||||
group: demo-producer-group # 生产者分组
|
||||
send-message-timeout: 3000 # 发送消息超时时间,单位:毫秒。默认为 3000 。
|
||||
10
lab-39/lab-39-rocketmq/target/classes/application.yaml
Normal file
10
lab-39/lab-39-rocketmq/target/classes/application.yaml
Normal file
@@ -0,0 +1,10 @@
|
||||
server:
|
||||
port: 8079
|
||||
|
||||
# rocketmq 配置项,对应 RocketMQProperties 配置类
|
||||
rocketmq:
|
||||
name-server: 127.0.0.1:9876 # RocketMQ Namesrv
|
||||
# Producer 配置项
|
||||
producer:
|
||||
group: demo-producer-group # 生产者分组
|
||||
send-message-timeout: 3000 # 发送消息超时时间,单位:毫秒。默认为 3000 。
|
||||
@@ -19,6 +19,7 @@
|
||||
<module>lab-39-mongodb</module>
|
||||
<module>lab-39-elasticsearch</module>
|
||||
<module>lab-39-elasticsearch-jest</module>
|
||||
<module>lab-39-rocketmq</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
||||
Reference in New Issue
Block a user