如何在springboot中使用消息中间件
如何在springboot中使用消息中间件?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
1.引入依赖
org.springframework.boot spring-boot-starter-amqp
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配置类
importorg.springframework.amqp.core.Queue;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
@Configuration
publicclassRabbitMQConfig{
@Bean
publicQueuemqQueue(){
returnnewQueue("mqboot");
}
}4.发送类< 大专栏 zyzx(53)-springboot使用消息中间件/h6>
@Component
publicclassSender{
@Autowired
privateAmqpTemplaterabbitTemplate;
publicvoidsend(){
Stringcontent="send:hello"+newDate();
System.out.println("Sender:"+content)
this.rabbitTemplate.convertAndSend("mqboot",content);
}
}收类
@Component
@RabbitListener(queues="mqboot")
publicclassReceiver{
@RabbitHandler
publicvoidprocess(Stringdata){
System.out.println("Receiver:"+data);
}
}6.测试
启动springBoot
如下显示表明:连接成功:
@RunWith(SpringRunner.class)
@SpringBootTest
publicclassApplicationTests{
@Autowired
privateTeacherRepositoryteacherRepository;
/*@Autowired
privateJavaMailSenderjavaMailSender;*/
@Autowired
privateSendersender;
@Test
publicvoidcontextLoads(){
//mq测试
sender.send();
}
}关于如何在springboot中使用消息中间件问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注恰卡编程网行业资讯频道了解更多相关知识。
推荐阅读
-
vue动态添加删除输入框(springboot vue怎么让数据库显示出来)
springbootvue怎么让数据库显示出来?一般情况下是前端调阅后端接口,来获取到数据库的数据,后端哪里会把数据库的数据整理...
-
springboot实现基于aop的切面日志
本文实例为大家分享了springboot实现基于aop的切面日志的具体代码,供大家参考,具体内容如下通过aop的切面方式实现日志...
-
SpringBoot定时任务功能怎么实现
-
SpringBoot中的@Import注解怎么使用
-
SpringBoot整合Lombok及常见问题怎么解决
SpringBoot整合Lombok及常见问题怎么解决这篇文章主要...
-
springboot图片验证码功能模块怎么实现
springboot图片验证码功能模块怎么实现本篇内容主要讲解“s...
-
Springboot+SpringSecurity怎么实现图片验证码登录
-
SpringBoot注解的知识点有哪些
SpringBoot注解的知识点有哪些这篇“SpringBoot注...
-
SpringBoot2.x中management.security.enabled=false无效怎么解决
SpringBoot2.x中management.security.enabled=false无效怎么解决...
-
springboot怎么禁用某项健康检查
springboot怎么禁用某项健康检查今天小编给大家分享一下sp...
