SpringBoot整合rabbitMq,简单用例

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();

	}
}

正常打印出结果 整合完毕:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值