This commit is contained in:
simon 2023-09-25 21:13:34 +08:00
parent 69205be463
commit 6f8ee2a807
5 changed files with 144 additions and 0 deletions

View File

@ -0,0 +1,58 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.qiuguo.iot</groupId>
<artifactId>iot-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>iot-rabbit</artifactId>
<name>iot-rabbit</name>
<description>mq类</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.maven.plugin.version}</version>
<configuration>
<!--跳过对项目中main方法的查找-->
<skip>true</skip>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,29 @@
package com.qiuguo.iot.rabbit.config;
import com.qiuguo.iot.rabbit.constants.YunxiRabbitConst;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class RunXiRabbitConfig {
/**
* 交换机
*
* @return
*/
@Bean
public Exchange RunxiEventExchange() {
return new TopicExchange(YunxiRabbitConst.EXCHANGE_YUNXI_EVENT,
true,
false,
null);
}
}

View File

@ -0,0 +1,23 @@
package com.qiuguo.iot.rabbit.constants;
/**
* @author simon
* @date 2023/9/25
* @description
**/
public class YunxiRabbitConst {
/**
* 云栖活动交换机
*/
public static final String EXCHANGE_YUNXI_EVENT = "iot-yunxi-exchange";
/**
* 云栖活动队列
*/
public static final String QUEUE_YUNXI = "iot.yunxi.queue";
/**
* 云栖活动发送的路由键
*/
public static final String ROUTE_KEY_YUNXI = "iot.yunxi";
}

View File

@ -0,0 +1,33 @@
package com.qiuguo.iot.rabbit.service;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
import javax.annotation.Resource;
/**
* @author simon
* @date 2023/9/25
* @description
**/
@Service
public class MqSendMsgService {
@Resource
private RabbitTemplate rabbitTemplate;
public Mono<Boolean> sendRabbitMsg(String exchange, String routingKey, Object message) {
return Mono.create(sink -> {
rabbitTemplate.setConfirmCallback((correlation, ack, cause) -> {
if (ack) {
// 消息发送成功
sink.success(Boolean.TRUE);
} else {
// 消息发送失败
sink.error(new RuntimeException("Message send failed: " + cause));
}
});
rabbitTemplate.convertAndSend(exchange, routingKey, message);
});
}
}

View File

@ -15,6 +15,7 @@
<module>iot-base</module>
<module>iot-data</module>
<module>iot-third</module>
<module>iot-rabbit</module>
</modules>
<dependencies>