您好,登錄后才能下訂單哦!
小編給大家分享一下springboot如何實(shí)現(xiàn)rabbitmq的隊(duì)列初始化和綁定,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
配置文件,在rabbit中自動(dòng)建立exchange,queue和綁定它們的關(guān)系
代碼里初始化exchange
代碼里初始化queue
代碼里綁定exchange,queue和routekey
配置文件,直接聲明vhost
代碼里初始化exchange
/** * rabbitMq里初始化exchange. * * @return */ @Bean public TopicExchange crmExchange() { return new TopicExchange(EXCHANGE); }
代碼里初始化queue
/** * rabbitMq里初始化隊(duì)列crm.hello. * * @return */ @Bean public Queue helloQueue() { return new Queue(HELLO); }
代碼里綁定exchange,queue和routekey
/** * 綁定exchange & queue & routekey. * * @param queueMessage 隊(duì)列 * @param exchange 交換機(jī) * @param routekey 路由 * @return */ public Binding bindingExchange(Queue queueMessage, TopicExchange exchange, String routekey) { return BindingBuilder.bind(queueMessage).to(exchange).with(routekey); }
配置文件
spring: rabbitmq: host: localhost port: 5672 username: guest password: guest virtual-host: lind
完整代碼
package com.lind.microservice.productCenter.mq; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.TopicExchange; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * amqp配置. */ @Configuration public class AmqpConfig { /** * 交換機(jī). */ public final static String EXCHANGE = "crm"; /** * hello隊(duì)列. */ public final static String HELLO = "crm.hello"; /** * 建立訂單隊(duì)列. */ public final static String LIND_GENERATE_ORDER = "crm.generate.order"; /** * 綁定exchange & queue & routekey. * * @param queueMessage 隊(duì)列 * @param exchange 交換機(jī) * @param routekey 路由 * @return */ public Binding bindingExchange(Queue queueMessage, TopicExchange exchange, String routekey) { return BindingBuilder.bind(queueMessage).to(exchange).with(routekey); } /** * rabbitMq里初始化exchange. * * @return */ @Bean public TopicExchange crmExchange() { return new TopicExchange(EXCHANGE); } /** * rabbitMq里初始化隊(duì)列crm.hello. * * @return */ @Bean public Queue helloQueue() { return new Queue(HELLO); } /** * rabbitMq里初始化隊(duì)列crm.generate.order. * * @return */ @Bean public Queue orderQueue() { return new Queue(LIND_GENERATE_ORDER); } }
隊(duì)列發(fā)布者
package com.lind.microservice.productCenter.mq; import java.util.Date; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; @Configuration public class HelloPublisher { @Autowired AmqpTemplate rabbitTemplate; @Autowired AmqpConfig amqpConfig; public void hello() { String context = "hello " + new Date(); System.out.println("HelloPublisher : " + context); amqpConfig.bindingExchange( amqpConfig.helloQueue(), amqpConfig.crmExchange(), "crm.hello.#" ); this.rabbitTemplate.convertAndSend(AmqpConfig.EXCHANGE, AmqpConfig.HELLO, context); } }
隊(duì)列訂閱者
package com.lind.microservice.productCenter.mq; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @RabbitListener(queues = AmqpConfig.HELLO) public class HelloSubscriber { @RabbitHandler public void process(String hello) { System.out.println("HelloSubscriber : " + hello); } }
springboot一種全新的編程規(guī)范,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開(kāi)發(fā)過(guò)程,SpringBoot也是一個(gè)服務(wù)于框架的框架,服務(wù)范圍是簡(jiǎn)化配置文件。
看完了這篇文章,相信你對(duì)“springboot如何實(shí)現(xiàn)rabbitmq的隊(duì)列初始化和綁定”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。