您好,登錄后才能下訂單哦!
一、整合前的準備
1.1、安裝windows/linux系統版本的rabbitmq。
1.2、此次整合使用eclipse
二、springboot整合rabbitmq
2.1、pom依賴引入
org.springframework.boot
spring-boot-starter-amqp
2.2、yml配置和configer文件編寫
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: xiangjiao
password: bunana
virtual-host: /xiangjiao
#publisher-confirms: true #消息發送到轉發器確認機制,是都確認回調
#publisher-returns: true
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MQConfiguration {
//隊列名稱
public static final String QUEUQ_NAME = "xiangjiao.queue";
//交換器名稱
public static final String EXCHANGE = "xiangjiao.exchange";
//路由key
public static final String ROUTING_KEY = "xiangjiao.routingKey";
//創建隊列
@Bean
public Queue getQueue(){
return new Queue(QUEUQ_NAME);
}
//實例化交換機
@Bean
public DirectExchange getDirectExchange(){
//DirectExchange(String name, boolean durable, boolean autoDelete)
return new DirectExchange(EXCHANGE, true, true);
}
//綁定消息隊列和交換機
@Bean
public Binding bindExchangeAndQueue(DirectExchange exchange,Queue queue){
return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
}
}
2.3、消息消費者監聽創建
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
import cn.linkpower.config.MQConfiguration;
@Service鄭州人流醫院 http://m.zzzy120.com/
public class MessageConsumerService {
@RabbitListener(queues=MQConfiguration.QUEUQ_NAME)
public void receiveMessage(String msg){
System.err.print("紅色 接收消息----"+String.valueOf(msg));
}
}
2.4、消息生產者
2.4.1、創建消息生產者接口
/**
* 定義消息發送接口
* @author 765199214
*
*/
public interface IMessageProductService {
public void sendMessage(String msg);
}
2.4.2、創建消息消費者實現類
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.linkpower.config.MQConfiguration;
import cn.linkpower.service.IMessageProductService;
@Service
public class MessageProductServiceImpl implements IMessageProductService {
@Autowired
private RabbitTemplate rabbitTemplate;
@Override
public void sendMessage(String msg) {
rabbitTemplate.convertAndSend(MQConfiguration.EXCHANGE,
MQConfiguration.ROUTING_KEY,
msg);
}
}
2.5、創建控制器
@RequestMapping("/sendMsg2")
@ResponseBody
public String sendMsg2(HttpServletRequest request){
String msg = request.getParameter("msg");
log.info("sendMsg --- msg--"+String.valueOf(msg));
messageProductServiceImpl.sendMessage(msg);
return "send ok";
}
三、測試
控制臺輸出:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。