!163 将spring-cloud-stream改为普通的mq依赖用法
* update: sky日志 * update: RocketMQ的集成方式 * feat:1. rabbit: 普通消息、延迟队列2.X
parent
16ca219267
commit
0dac5a544f
@ -1,60 +0,0 @@
|
||||
package org.dromara.stream.controller;
|
||||
|
||||
import org.dromara.common.core.domain.R;
|
||||
import org.dromara.stream.mq.producer.DelayProducer;
|
||||
import org.dromara.stream.mq.producer.LogStreamProducer;
|
||||
import org.dromara.stream.mq.producer.TestStreamProducer;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 测试mq
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@AllArgsConstructor
|
||||
@RequestMapping("/test-mq")
|
||||
public class TestMqController {
|
||||
|
||||
private final DelayProducer delayProducer;
|
||||
private final TestStreamProducer testStreamProducer;
|
||||
private final LogStreamProducer logStreamProducer;
|
||||
|
||||
/**
|
||||
* 发送消息Rabbitmq
|
||||
*
|
||||
* @param msg 消息内容
|
||||
* @param delay 延时时间
|
||||
*/
|
||||
@GetMapping("/sendRabbitmq")
|
||||
public R<Void> sendRabbitmq(String msg, Long delay) {
|
||||
delayProducer.sendMsg(msg, delay);
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送消息Rocketmq
|
||||
*
|
||||
* @param msg 消息内容
|
||||
*/
|
||||
@GetMapping("/sendRocketmq")
|
||||
public R<Void> sendRocketmq(String msg) {
|
||||
testStreamProducer.streamTestMsg(msg);
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送消息Kafka
|
||||
*
|
||||
* @param msg 消息内容
|
||||
*/
|
||||
@GetMapping("/sendKafka")
|
||||
public R<Void> sendKafka(String msg) {
|
||||
logStreamProducer.streamLogMsg(msg);
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package org.dromara.stream.mq;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class TestMessaging {
|
||||
/**
|
||||
* 消息id
|
||||
*/
|
||||
private String msgId;
|
||||
/**
|
||||
* 消息内容
|
||||
*/
|
||||
private String msgText;
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
package org.dromara.stream.mq.consumer;
|
||||
|
||||
|
||||
import org.dromara.stream.mq.TestMessaging;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class DelayConsumer {
|
||||
|
||||
@Bean
|
||||
Consumer<TestMessaging> delay() {
|
||||
log.info("初始化订阅");
|
||||
return obj -> {
|
||||
log.info("消息接收成功:" + obj);
|
||||
};
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
package org.dromara.stream.mq.consumer;
|
||||
|
||||
import org.dromara.stream.mq.TestMessaging;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class LogStreamConsumer {
|
||||
|
||||
@Bean
|
||||
Consumer<TestMessaging> log() {
|
||||
log.info("初始化订阅");
|
||||
return msg -> {
|
||||
log.info("通过stream消费到消息 => {}", msg.toString());
|
||||
};
|
||||
}
|
||||
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
package org.dromara.stream.mq.consumer;
|
||||
|
||||
import org.dromara.stream.mq.TestMessaging;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class TestStreamConsumer {
|
||||
|
||||
@Bean
|
||||
Consumer<TestMessaging> demo() {
|
||||
log.info("初始化订阅");
|
||||
return msg -> {
|
||||
log.info("通过stream消费到消息 => {}", msg.toString());
|
||||
};
|
||||
}
|
||||
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package org.dromara.stream.mq.producer;
|
||||
|
||||
import org.dromara.stream.mq.TestMessaging;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.stream.function.StreamBridge;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Component
|
||||
public class DelayProducer {
|
||||
|
||||
@Autowired
|
||||
private StreamBridge streamBridge;
|
||||
|
||||
public void sendMsg(String msg, Long delay) {
|
||||
// 构建消息对象
|
||||
TestMessaging testMessaging = new TestMessaging()
|
||||
.setMsgId(UUID.randomUUID().toString())
|
||||
.setMsgText(msg);
|
||||
Message<TestMessaging> message = MessageBuilder.withPayload(testMessaging)
|
||||
.setHeader("x-delay", delay).build();
|
||||
streamBridge.send("delay-out-0", message);
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
package org.dromara.stream.mq.producer;
|
||||
|
||||
import org.dromara.stream.mq.TestMessaging;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.stream.function.StreamBridge;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Component
|
||||
public class LogStreamProducer {
|
||||
|
||||
@Autowired
|
||||
private StreamBridge streamBridge;
|
||||
|
||||
public void streamLogMsg(String msg) {
|
||||
// 构建消息对象
|
||||
TestMessaging testMessaging = new TestMessaging()
|
||||
.setMsgId(UUID.randomUUID().toString())
|
||||
.setMsgText(msg);
|
||||
streamBridge.send("log-out-0", MessageBuilder.withPayload(testMessaging).build());
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
package org.dromara.stream.mq.producer;
|
||||
|
||||
import org.dromara.stream.mq.TestMessaging;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.stream.function.StreamBridge;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Component
|
||||
public class TestStreamProducer {
|
||||
|
||||
@Autowired
|
||||
private StreamBridge streamBridge;
|
||||
|
||||
public void streamTestMsg(String msg) {
|
||||
// 构建消息对象
|
||||
TestMessaging testMessaging = new TestMessaging()
|
||||
.setMsgId(UUID.randomUUID().toString())
|
||||
.setMsgText(msg);
|
||||
streamBridge.send("demo-out-0", MessageBuilder.withPayload(testMessaging).build());
|
||||
}
|
||||
}
|
@ -1,109 +0,0 @@
|
||||
server:
|
||||
port: 9402
|
||||
|
||||
# Spring
|
||||
spring:
|
||||
application:
|
||||
# 应用名称
|
||||
name: ruoyi-stream-mq
|
||||
profiles:
|
||||
# 环境配置
|
||||
active: @profiles.active@
|
||||
cloud:
|
||||
stream:
|
||||
function:
|
||||
# 重点配置 与 binding 名与消费者对应
|
||||
definition: delay;demo;log
|
||||
|
||||
--- # rabbitmq 配置
|
||||
spring:
|
||||
rabbitmq:
|
||||
host: localhost
|
||||
port: 5672
|
||||
username: root
|
||||
password: root
|
||||
cloud:
|
||||
stream:
|
||||
rabbit:
|
||||
bindings:
|
||||
delay-in-0:
|
||||
consumer:
|
||||
delayedExchange: true
|
||||
delay-out-0:
|
||||
producer:
|
||||
delayedExchange: true
|
||||
bindings:
|
||||
delay-in-0:
|
||||
destination: delay.exchange.cloud
|
||||
content-type: application/json
|
||||
group: delay-group
|
||||
binder: rabbit
|
||||
delay-out-0:
|
||||
destination: delay.exchange.cloud
|
||||
content-type: application/json
|
||||
group: delay-group
|
||||
binder: rabbit
|
||||
|
||||
--- # rocketmq 配置
|
||||
spring:
|
||||
cloud:
|
||||
stream:
|
||||
rocketmq:
|
||||
binder:
|
||||
# rocketmq 地址
|
||||
name-server: localhost:9876
|
||||
bindings:
|
||||
demo-out-0:
|
||||
producer:
|
||||
# 必须得写
|
||||
group: default
|
||||
bindings:
|
||||
demo-out-0:
|
||||
content-type: application/json
|
||||
destination: stream-test-topic
|
||||
group: test-group
|
||||
binder: rocketmq
|
||||
demo-in-0:
|
||||
content-type: application/json
|
||||
destination: stream-test-topic
|
||||
group: test-group
|
||||
binder: rocketmq
|
||||
|
||||
--- # kafka 配置
|
||||
spring:
|
||||
cloud:
|
||||
stream:
|
||||
kafka:
|
||||
binder:
|
||||
brokers: localhost:9092
|
||||
bindings:
|
||||
log-out-0:
|
||||
destination: stream-log-topic
|
||||
contentType: application/json
|
||||
group: log_group
|
||||
binder: kafka
|
||||
log-in-0:
|
||||
destination: stream-log-topic
|
||||
contentType: application/json
|
||||
group: log_group
|
||||
binder: kafka
|
||||
|
||||
--- # nacos 配置
|
||||
spring:
|
||||
cloud:
|
||||
nacos:
|
||||
# nacos 服务地址
|
||||
server-addr: @nacos.server@
|
||||
username: @nacos.username@
|
||||
password: @nacos.password@
|
||||
discovery:
|
||||
# 注册组
|
||||
group: @nacos.discovery.group@
|
||||
namespace: ${spring.profiles.active}
|
||||
config:
|
||||
# 配置组
|
||||
group: @nacos.config.group@
|
||||
namespace: ${spring.profiles.active}
|
||||
config:
|
||||
import:
|
||||
- optional:nacos:application-common.yml
|
@ -1,10 +0,0 @@
|
||||
Spring Boot Version: ${spring-boot.version}
|
||||
Spring Application Name: ${spring.application.name}
|
||||
_ _
|
||||
(_) | |
|
||||
_ __ _ _ ___ _ _ _ ______ ___| |_ _ __ ___ __ _ _ __ ___ ______ _ __ ___ __ _
|
||||
| '__| | | |/ _ \| | | | |______/ __| __| '__/ _ \/ _` | '_ ` _ \______| '_ ` _ \ / _` |
|
||||
| | | |_| | (_) | |_| | | \__ \ |_| | | __/ (_| | | | | | | | | | | | | (_| |
|
||||
|_| \__,_|\___/ \__, |_| |___/\__|_| \___|\__,_|_| |_| |_| |_| |_| |_|\__, |
|
||||
__/ | | |
|
||||
|___/ |_|
|
@ -0,0 +1,20 @@
|
||||
package org.dromara.stream.mq.consumer.rocketmq;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
|
||||
import org.apache.rocketmq.spring.core.RocketMQListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author xbhog
|
||||
* @date 2024/06/01 16:53
|
||||
**/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RocketMQMessageListener(topic = "TestTopic", consumerGroup = "springboot-mq-consumer-1")
|
||||
public class NormalRocketConsumer implements RocketMQListener<String> {
|
||||
@Override
|
||||
public void onMessage(String message) {
|
||||
log.info("【消费者】接收消息:{}" ,message);
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package org.dromara.stream.mq.consumer.rocketmq;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
|
||||
import org.apache.rocketmq.spring.core.RocketMQListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author xbhog
|
||||
* @date 2024/06/01 16:54
|
||||
**/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RocketMQMessageListener(consumerGroup = "transaction-group", topic = "transaction_topic")
|
||||
public class TransactionRocketConsumer implements RocketMQListener<String> {
|
||||
|
||||
@Override
|
||||
public void onMessage(String message) {
|
||||
log.info("【消费者】===>接收事务消息:{}",message);
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package org.dromara.stream.mq.producer.kafkaMq;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.kafka.core.KafkaTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* @author xbhog
|
||||
* @date 2024/05/19 18:02
|
||||
**/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class KafkaNormalProducer {
|
||||
@Resource
|
||||
private KafkaTemplate kafkaTemplate;
|
||||
|
||||
public void sendKafkaMsg(){
|
||||
CompletableFuture send = kafkaTemplate.send("test-topic","hello", "kafkaTest");
|
||||
send.join();
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package org.dromara.stream.mq.producer.rabbitMq;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.stream.config.RabbitTtlQueueConfig;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
/**
|
||||
* @author xbhog
|
||||
* @date 2024/05/25 17:15
|
||||
**/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class DelayRabbitProducer {
|
||||
@Resource
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
|
||||
@GetMapping("/sendDelay")
|
||||
public void sendDelayMessage(String message) {
|
||||
rabbitTemplate.convertAndSend(RabbitTtlQueueConfig.DELAY_EXCHANGE_NAME, RabbitTtlQueueConfig.DELAY_ROUTING_KEY, message);
|
||||
log.info("【生产者】Delayed message send: " + message);
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package org.dromara.stream.mq.producer.rabbitMq;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.stream.config.RabbitConfig;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author xbhog
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class NormalRabbitProducer {
|
||||
|
||||
@Resource
|
||||
RabbitTemplate rabbitTemplate;
|
||||
|
||||
|
||||
public void sendMq(String message) {
|
||||
rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_NAME, RabbitConfig.ROUTING_KEY, message);
|
||||
log.info("【生产者】Message send: " + message);
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
server:
|
||||
port: 9402
|
||||
|
||||
# Spring
|
||||
spring:
|
||||
application:
|
||||
# 应用名称
|
||||
name: ruoyi-test-mq
|
||||
profiles:
|
||||
# 环境配置
|
||||
active: @profiles.active@
|
||||
#MQ配置
|
||||
rabbitmq:
|
||||
host: 192.168.1.13
|
||||
port: 5672
|
||||
username: mq
|
||||
password: mq
|
||||
publisher-returns: true
|
||||
publisher-confirm-type: correlated
|
||||
kafka:
|
||||
bootstrap-servers: 192.168.1.13:9092
|
||||
producer:
|
||||
value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
|
||||
rocketmq:
|
||||
name-server: 192.168.1.13:9876
|
||||
producer:
|
||||
group: dist-test # 生产者组
|
||||
--- # nacos 配置
|
||||
spring:
|
||||
cloud:
|
||||
nacos:
|
||||
# nacos 服务地址
|
||||
server-addr: @nacos.server@
|
||||
discovery:
|
||||
# 注册组
|
||||
group: @nacos.discovery.group@
|
||||
namespace: ${spring.profiles.active}
|
||||
config:
|
||||
# 配置组
|
||||
group: @nacos.config.group@
|
||||
namespace: ${spring.profiles.active}
|
||||
config:
|
||||
import:
|
||||
- optional:nacos:application-common.yml
|
Loading…
Reference in New Issue