第一步:在pom.xml里面添加rebbitmq依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
第二步:在application.properties添加rabbitmq配置
#rabbitmq
spring.rabbitmq.host=10.110.3.62
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
第三步:同controller包并列创建一个rabbitmq包,包下写rabbitmqconfig、rabbitMq发送者和rabbitMq接受者(Direct模式)
package com.jack.seckill.rabbitmq;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Configuration相当于xml配置
*
*/
@Configuration
public class MQconfig {
public static final String QUEUE="queue";
@Bean
public Queue queue() {
return new Queue(QUEUE,true);
}
}
package com.jack.seckill.rabbitmq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jack.seckill.redis.RedisService;
@Service
public class MQSender {
private static Logger log=LoggerFactory.getLogger(MQSender.class);
@Autowired
AmqpTemplate amqpTemplete;
public void send(Object message) {
String msg=RedisService.beanToString(message);
log.info("send message:"+msg);
amqpTemplete.convertAndSend(MQconfig.QUEUE,msg);
}
}
package com.jack.seckill.rabbitmq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
@Service
public class MQReceiver {
private static Logger log=LoggerFactory.getLogger(MQReceiver.class);
@RabbitListener(queues=MQconfig.QUEUE)
public void receive(String message) {
log.info("receive message:"+message);
}
}
第四步:书写contoller类
package com.jack.seckill.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jack.seckill.domain.User;
import com.jack.seckill.rabbitmq.MQSender;
import com.jack.seckill.redis.RedisService;
import com.jack.seckill.redis.UserKey;
import com.jack.seckill.result.CodeMsg;
import com.jack.seckill.result.Result;
import com.jack.seckill.service.UserService;
@Controller
@RequestMapping("/demo")
public class SampleController {
@Autowired
MQSender sender;
@RequestMapping("/mq")
@ResponseBody
public Result<String> mq(){
sender.send("nice to meet you");
return Result.success("hello,two");
}
}
备注:
如被拒绝,连接不上
1、关闭云服务器的防火墙
2、远程连接需要在rabbitmq进行配置
在rabbitmq文件的etc下的rabbitmq
vi rabbitmq.config
[{rabbit,[{loopback_users,[]}]}].
https://www.rabbitmq.com/access-control.html