您好,登錄后才能下訂單哦!
如何在springboot中使用消息中間件?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。
1.引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
2.application.properties
#rabbitmq配置 spring.application.name=springboot-mq spring.rabbitmq.host=192.168.17.129 spring.rabbitmq.port=5672 spring.rabbitmq.username=mytest spring.rabbitmq.password=mytest
3.rabbitmap配置類
import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitMQConfig { @Bean public Queue mqQueue(){ return new Queue("mqboot"); } }
4.發(fā)送類< 大專欄 zyzx(53)-springboot使用消息中間件/h6>
@Component public class Sender { @Autowired private AmqpTemplate rabbitTemplate; public void send(){ String content = "send: hello"+new Date(); System.out.println("Sender:"+content) this.rabbitTemplate.convertAndSend("mqboot",content); } }
收類
@Component @RabbitListener(queues = "mqboot") public class Receiver { @RabbitHandler public void process(String data){ System.out.println("Receiver:"+data); } }
6.測試
啟動(dòng)springBoot
如下顯示表明:連接成功:
@RunWith(SpringRunner.class) @SpringBootTest public class ApplicationTests { @Autowired private TeacherRepository teacherRepository; /*@Autowired private JavaMailSender javaMailSender;*/ @Autowired private Sender sender; @Test public void contextLoads() { //mq測試 sender.send(); } }
關(guān)于如何在springboot中使用消息中間件問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。