springboot整合rabbitmq
1.引入依赖
<!-- rabbit mq -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.在application.properties文件加入配置项
#rabbitmq
spring.rabbitmq.host=ip
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
#虚拟主机,一台机器可能有多台虚拟主机,这里选择默认配置/即可
spring.rabbitmq.virtual-host=/
#消费者数量
spring.rabbitmq.listener.simple.concurrency=10
spring.rabbitmq.listener.simple.max-concurrency=10
#每次从队列获取几个
spring.rabbitmq.listener.simple.prefetch=1
spring.rabbitmq.listener.simple.auto-startup=true
spring.rabbitmq.listener.simple.default-requeue-rejected=true
#失败后重试
spring.rabbitmq.template.retry.enabled=true
#重试间隔
spring.rabbitmq.template.retry.initial-interval=1000
#重试次数
spring.rabbitmq.template.retry.max-attempts=3
#最大间隔
spring.rabbitmq.template.retry.max-interval=10000
spring.rabbitmq.template.retry.multiplier=1.0
3.配置guest用户远程访问
在/usr/local/rabbitmq_server-3.7.13/etc/rabbitmq目录下的rabbitmq.config加入以下内容:
[{rabbit, [{loopback_users, []}]}].
如果没有rabbitmq.config文件则新创建一个,然后重启rabbitmq服务即可。
4.测试,简单编写一个最简单的Direct模式例子
(1)编写config类:
package com.hanyl.rabbitmq;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MQconfig {
public static final String QUEUE = "queue";
@Bean
public Queue queue() {
return new Queue(QUEUE, true);
}
}
(2)编写发送类:
package com.hanyl.rabbitmq;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.framework.common.redis.RedisService;
import lombok.extern.slf4j.Slf4j;
@Service
@Slf4j
public class MQSender {
@Autowired
AmqpTemplate amqpTemplate;
public void send(Object msg) {
String str = RedisService.beanTOString(msg);
log.info("send messge");
amqpTemplate.convertAndSend(MQconfig.QUEUE, str);
}
}
(3)编写receive类:
package com.hanyl.rabbitmq;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
@Service
@Slf4j
public class MQReceiver {
@RabbitListener(queues = MQconfig.QUEUE)
public void receive(String message) {
log.info("receive meaage:"+message);
}
}
(4)编写测试调用类:
package com.hanyl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.framework.common.redis.RedisService;
import com.framework.common.redis.key.SysConfigKey;
import com.framework.common.util.R;
import com.hanyl.rabbitmq.MQSender;
@RestController
public class TestController {
@Autowired
RedisService redisService;
@Autowired
MQSender mqSender;
// @RequestMapping("/test")
// public String test() {
// redisService.set(SysConfigKey.getConfig,"key1", "2222");
// String str=redisService.get(SysConfigKey.getConfig,"key1", String.class);
// System.out.println(str);
// return "hello spring boot";
// }
@RequestMapping("/mq")
@ResponseBody
public R send() {
mqSender.send("hello word");
return R.ok();
}
}
正常打印出结果 整合完毕: