您好,欢迎访问一九零五行业门户网

SpringBoot怎么集成RabbitMQ

springboot 集成 rabbitmq首先搭建springboot项目,在pom xml文件中添加如下依赖
<依赖>    <groupid>org.springframework.boot</groupid>    <artifactid>spring-boot-starter-amqp</artifactid></依赖><依赖>    <groupid>org.springframework.boot</groupid>    <artifactid>spring-boot-starter-web</artifactid></依赖>
修改配置文件,添加如下rabbitmq配置
服务器:  port: 8888 # 设置端口号spring:  rabbitmq:    host: 127.0.0.1 # 设置 rabbitmq 的主机    port: 5672 # 设置 rabbitmq 服务端口    username: guest # 设置 rabbitmq 用户名    password: guest # 设置 rabbitmq 密码
新的公共常量类
public interface rabbitconstant {     /**      * 简单模式     */     string simple_queue_name = simple_queue;     /**      * 工作模式     */     string work_queue_name = work_queue;     /**      * 发布/订阅模式     */     string publish_subscribe_exchange_name = publish_subscribe_exchange;     字符串 publish_subscribe_first_queue_name = publish_subscribe_first_queue;     字符串 publish_subscribe_second_queue_name = publish_subscribe_second_queue;     /**      * 路由模式     */     string routing_exchange_name = routing_exchange;    字符串 routing_first_queue_name = routing_first_queue;     字符串 routing_second_queue_name = routing_second_queue;     字符串 routing_third_queue_name = routing_third_queue;     字符串 routing_first_queue_routing_key_name = routing_first_queue_routing_key;     字符串 routing_second_queue_routing_key_name = routing_second_queue_routing_key;     字符串 routing_third_queue_routing_key_name = routing_third_queue_routing_key;     /**      * 主题模式     */     string topics_exchange_name = topics_exchange;     字符串 topics_first_queue_name = topics_first_queue;     字符串 topics_second_queue_name =     字符串 topics_third_queue_name = topics_third_queue;     string topics_first_queue_routing_key = topics.first.routing.key;     string topics_second_queue_routing_key = topics.second.routing.key;     string topics_third_queue_routing_key = topics.third.routing.key;     字符串 topics_routing_key_first_wildcard = #.first.#;     字符串 topics_routing_key_second_wildcard = *.second.#;     字符串 topics_routing_key_thrid_wildcard = *.third.*;     /**      * 标题模式     */     string header_exchange_name = header_exchange;     字符串 header_first_queue_name = header_first_queue;     字符串 header_second_queue_name = header_second_queue;     /**     * rpc 模式     */     string rpc_queue_name = rpc_queue; }
添加一个controller请求类(用于验证结果,最后可以添加)
导入 com.example.rabbitmq.constant.rabbitconstant;导入 org.springframework.amqp.core.message;导入 org.springframework.amqp.core.messageproperties;导入 org.springframework.amqp.rabbit.core.rabbittemplate;导入 org.springframework.beans.factory.annotation.autowired;导入 org.springframework.web.bind.annotation.getmapping;导入 org.springframework.web.bind.annotation.restcontroller;导入 java.nio.charset.standardcharsets;@restcontroller public class rabbitcontroller {     @autowired     private rabbittemplate rabbittemplate;     @getmapping(value = /simple)     public void simple() {        rabbittemplate.convertandsend(rabbitconstant.simple_queue_name, 你好世界!);     }     @getmapping(value = /work)     public void work() {         rabbittemplate.convertandsend(rabbitconstant.work_queue_name, work hello!);     }     @getmapping(value = /pubsub)     public void pubsub() {         rabbittemplate.convertandsend(rabbitconstant.publish_subscribe_exchange_name, null, 发布/订阅你好);     }     @getmapping(value = /routing)     public void routing() {         // 向第一个队列发送消息        rabbittemplate.convertandsend(rabbitconstant.routing_exchange_name, rabbitconstant.routing_first_queue_routing_key_name, 路由你好);     }     @getmapping(value = /topics)     public void topics() {         // 向第一个队列发送消息。这时候队列可以接收到消息,因为队列的通配符是#first.#,而routing_key是topics first。路由。键,匹配成功        rabbittemplate.convertandsend(rabbitconstant.topics_exchange_name, rabbitconstant.topics_first_queue_routing_key, topics hello);        // 向第二个队列发送消息。这时候队列也能收到消息了,因为队列的通配符是*秒#,而routing_key是topic秒。路由。键,匹配成功        rabbittemplate.convertandsend(rabbitconstant.topics_exchange_name, rabbitconstant.topics_second_queue_routing_key, topics hello);         // 向第三个队列发送消息。此时队列无法接受消息,因为队列通配符是*第三个*,而routing_key是topics第三个。路由。键,匹配失败        rabbittemplate.convertandsend(rabbitconstant.topics_exchange_name, rabbitconstant.topics_third_queue_routing_key, topics hello);     }     @getmapping(value = /header)    public void header() {         // 这个消息应该被两个队列接收。第一个队列全部匹配成功,第二个队列 hello 值任意匹配成功        messageproperties messageproperties = new messageproperties();         messageproperties.setheader(matchall, yes);         messageproperties.setheader(你好, world);         message message = new message(header first hello.getbytes(standardcharsets.utf_8), messageproperties);         rabbittemplate.convertandsend(rabbitconstant.header_exchange_name, null, message);         // 这个消息应该只被第二个队列接受。第一个队列全部匹配失败,        messageproperties messagepropertiessecond = new messageproperties();         messagepropertiessecond.setheader(matchall, no);         message messagesecond = new message(header second hello.getbytes(standardcharsets.utf_8), messagepropertiessecond);         rabbittemplate.convertandsend(rabbitconstant.header_exchange_name, null, messagesecond);     }     @getmapping(value = /rpc)     public void rpc() {         object responsemsg = rabbittemplate.convertsendandreceive(rabbitconstant.rpc_queue_name, rpc hello!);         system.out.println(rabbit rpc 响应消息: + responsemsg);     } }
以上就是springboot怎么集成rabbitmq的详细内容。
其它类似信息

推荐信息