rabbitmq 的demo
This commit is contained in:
parent
3e422db402
commit
e3b3edaacf
@ -12,10 +12,10 @@ spring:
|
|||||||
username: root
|
username: root
|
||||||
password: 123456
|
password: 123456
|
||||||
# 从库数据源
|
# 从库数据源
|
||||||
SLAVE:
|
# SLAVE:
|
||||||
url: jdbc:mysql://127.0.0.1/ruoyi?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
|
# url: jdbc:mysql://127.0.0.1/ruoyi?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
|
||||||
username: root
|
# username: root
|
||||||
password: 123456
|
# password: 123456
|
||||||
druid:
|
druid:
|
||||||
# 初始连接数
|
# 初始连接数
|
||||||
initialSize: 5
|
initialSize: 5
|
||||||
|
@ -22,10 +22,20 @@ spring:
|
|||||||
max-active: 8
|
max-active: 8
|
||||||
# #连接池最大阻塞等待时间(使用负值表示没有限制)
|
# #连接池最大阻塞等待时间(使用负值表示没有限制)
|
||||||
max-wait: -1ms
|
max-wait: -1ms
|
||||||
|
#给项目来个名字
|
||||||
|
application:
|
||||||
|
name: ruoyi
|
||||||
|
#配置rabbitMq 服务器
|
||||||
|
rabbitmq:
|
||||||
|
enable: false
|
||||||
|
host: 127.0.0.1
|
||||||
|
port: 5672
|
||||||
|
username: guest
|
||||||
|
password: guest
|
||||||
|
|
||||||
# Minio配置
|
# Minio配置
|
||||||
minio:
|
minio:
|
||||||
enable: true
|
enable: false
|
||||||
downLoadLimit: 1024
|
downLoadLimit: 1024
|
||||||
primary: MASTER
|
primary: MASTER
|
||||||
client:
|
client:
|
||||||
|
@ -39,6 +39,12 @@
|
|||||||
<version>${ruoyi.version}</version>
|
<version>${ruoyi.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.ruoyi</groupId>
|
||||||
|
<artifactId>ruoyi-middleware-rabbitmq</artifactId>
|
||||||
|
<version>${ruoyi.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.ruoyi</groupId>
|
<groupId>com.ruoyi</groupId>
|
||||||
<artifactId>ruoyi-middleware-starter</artifactId>
|
<artifactId>ruoyi-middleware-starter</artifactId>
|
||||||
@ -52,8 +58,8 @@
|
|||||||
<modules>
|
<modules>
|
||||||
<module>ruoyi-middleware-minio</module>
|
<module>ruoyi-middleware-minio</module>
|
||||||
<module>ruoyi-middleware-redis</module>
|
<module>ruoyi-middleware-redis</module>
|
||||||
|
<module>ruoyi-middleware-rabbitmq</module>
|
||||||
<module>ruoyi-middleware-starter</module>
|
<module>ruoyi-middleware-starter</module>
|
||||||
|
|
||||||
</modules>
|
</modules>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
</project>
|
</project>
|
||||||
|
33
ruoyi-middleware/ruoyi-middleware-rabbitmq/pom.xml
Normal file
33
ruoyi-middleware/ruoyi-middleware-rabbitmq/pom.xml
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<?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>
|
||||||
|
<artifactId>ruoyi-middleware</artifactId>
|
||||||
|
<groupId>com.ruoyi</groupId>
|
||||||
|
<version>3.8.8.3.1</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>ruoyi-middleware-rabbitmq</artifactId>
|
||||||
|
|
||||||
|
<description>
|
||||||
|
中间件
|
||||||
|
</description>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<!-- 通用工具-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.ruoyi</groupId>
|
||||||
|
<artifactId>ruoyi-common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!--rabbitmq-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,57 @@
|
|||||||
|
package com.ruoyi.middleware.rabbitmq;
|
||||||
|
|
||||||
|
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.amqp.support.converter.DefaultClassMapper;
|
||||||
|
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
|
||||||
|
import org.springframework.amqp.support.converter.MessageConverter;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author : JCccc
|
||||||
|
* @CreateTime : 2019/9/3
|
||||||
|
* @Description :
|
||||||
|
**/
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = { "enable" }, havingValue = "true", matchIfMissing = false)
|
||||||
|
public class DirectRabbitConfig {
|
||||||
|
|
||||||
|
// 队列 起名:TestDirectQueue
|
||||||
|
@Bean
|
||||||
|
public Queue TestDirectQueue() {
|
||||||
|
// durable:是否持久化,默认是false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效
|
||||||
|
// exclusive:默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable
|
||||||
|
// autoDelete:是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除。
|
||||||
|
// return new Queue("TestDirectQueue",true,true,false);
|
||||||
|
|
||||||
|
// 一般设置一下队列的持久化就好,其余两个就是默认false
|
||||||
|
return new Queue("TestDirectQueue", true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Direct交换机 起名:TestDirectExchange
|
||||||
|
@Bean
|
||||||
|
DirectExchange TestDirectExchange() {
|
||||||
|
// return new DirectExchange("TestDirectExchange",true,true);
|
||||||
|
return new DirectExchange("TestDirectExchange", true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 绑定 将队列和交换机绑定, 并设置用于匹配键:TestDirectRouting
|
||||||
|
@Bean
|
||||||
|
Binding bindingDirect() {
|
||||||
|
return BindingBuilder.bind(TestDirectQueue()).to(TestDirectExchange()).with("TestDirectRouting");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public MessageConverter jsonToMapMessageConverter() {
|
||||||
|
DefaultClassMapper defaultClassMapper = new DefaultClassMapper();
|
||||||
|
// 指定反序列化期间要信任的一组包,星号 ( * ) 表示全部信任
|
||||||
|
defaultClassMapper.setTrustedPackages("*");
|
||||||
|
Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter();
|
||||||
|
jackson2JsonMessageConverter.setClassMapper(defaultClassMapper);
|
||||||
|
return jackson2JsonMessageConverter;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.ruoyi.middleware.rabbitmq;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
||||||
|
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = { "enable" }, havingValue = "true", matchIfMissing = false)
|
||||||
|
@RabbitListener(queues = "TestDirectQueue") // 监听的队列名称 TestDirectQueue
|
||||||
|
public class DirectReceiver {
|
||||||
|
|
||||||
|
@RabbitHandler
|
||||||
|
public void process(Map map) {
|
||||||
|
System.out.println("DirectReceiver消费者收到消息 : " + map.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
package com.ruoyi.middleware.rabbitmq;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
|
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;
|
||||||
|
|
||||||
|
import com.ruoyi.common.annotation.Anonymous;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author : JCccc
|
||||||
|
* @CreateTime : 2019/9/3
|
||||||
|
* @Description :
|
||||||
|
**/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/rabbitmq")
|
||||||
|
public class SendMessageController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
RabbitTemplate rabbitTemplate; //使用RabbitTemplate,这提供了接收/发送等等方法
|
||||||
|
|
||||||
|
@GetMapping("/sendDirectMessage")
|
||||||
|
@Anonymous
|
||||||
|
public String sendDirectMessage() {
|
||||||
|
String messageId = String.valueOf(UUID.randomUUID());
|
||||||
|
String messageData = "test message, hello!";
|
||||||
|
String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||||
|
Map<String,Object> map=new HashMap<>();
|
||||||
|
map.put("messageId",messageId);
|
||||||
|
map.put("messageData",messageData);
|
||||||
|
map.put("createTime",createTime);
|
||||||
|
//将消息携带绑定键值:TestDirectRouting 发送到交换机TestDirectExchange
|
||||||
|
rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
|
||||||
|
return "ok";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -32,6 +32,10 @@
|
|||||||
<artifactId>ruoyi-middleware-redis</artifactId>
|
<artifactId>ruoyi-middleware-redis</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.ruoyi</groupId>
|
||||||
|
<artifactId>ruoyi-middleware-rabbitmq</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user