springboot整合rabbitmq(Springboot2.X)

一、整合前的準(zhǔn)備

創(chuàng)新互聯(lián)是一家網(wǎng)站設(shè)計(jì)公司,集創(chuàng)意、互聯(lián)網(wǎng)應(yīng)用、軟件技術(shù)為一體的創(chuàng)意網(wǎng)站建設(shè)服務(wù)商,主營(yíng)產(chǎn)品:響應(yīng)式網(wǎng)站建設(shè)、高端網(wǎng)站設(shè)計(jì)、全網(wǎng)營(yíng)銷推廣。我們專注企業(yè)品牌在網(wǎng)站中的整體樹立,網(wǎng)絡(luò)互動(dòng)的體驗(yàn),以及在手機(jī)等移動(dòng)端的優(yōu)質(zhì)呈現(xiàn)。成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、移動(dòng)互聯(lián)產(chǎn)品、網(wǎng)絡(luò)運(yùn)營(yíng)、VI設(shè)計(jì)、云產(chǎn)品.運(yùn)維為核心業(yè)務(wù)。為用戶提供一站式解決方案,我們深知市場(chǎng)的競(jìng)爭(zhēng)激烈,認(rèn)真對(duì)待每位客戶,為客戶提供賞析悅目的作品,網(wǎng)站的價(jià)值服務(wù)。

1.1、安裝windows/linux系統(tǒng)版本的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 #消息發(fā)送到轉(zhuǎn)發(fā)器確認(rèn)機(jī)制,是都確認(rèn)回調(diào)

#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 {

//隊(duì)列名稱

public static final String QUEUQ_NAME = "xiangjiao.queue";

//交換器名稱

public static final String EXCHANGE = "xiangjiao.exchange";

//路由key

public static final String ROUTING_KEY = "xiangjiao.routingKey";

//創(chuàng)建隊(duì)列

@Bean

public Queue getQueue(){

return new Queue(QUEUQ_NAME);

}

//實(shí)例化交換機(jī)

@Bean

public DirectExchange getDirectExchange(){

//DirectExchange(String name, boolean durable, boolean autoDelete)

return new DirectExchange(EXCHANGE, true, true);

}

//綁定消息隊(duì)列和交換機(jī)

@Bean

public Binding bindExchangeAndQueue(DirectExchange exchange,Queue queue){

return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);

}

}

2.3、消息消費(fèi)者監(jiān)聽創(chuàng)建

import org.springframework.amqp.rabbit.annotation.RabbitListener;

import org.springframework.stereotype.Service;

import cn.linkpower.config.MQConfiguration;

@Service鄭州人流醫(yī)院 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、消息生產(chǎn)者

2.4.1、創(chuàng)建消息生產(chǎn)者接口

/**

* 定義消息發(fā)送接口

* @author 765199214

*

*/

public interface IMessageProductService {

public void sendMessage(String msg);

}

2.4.2、創(chuàng)建消息消費(fèi)者實(shí)現(xiàn)類

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、創(chuàng)建控制器

@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";

}

三、測(cè)試

控制臺(tái)輸出:

springboot整合rabbitmq(Springboot2.X)

網(wǎng)站名稱:springboot整合rabbitmq(Springboot2.X)
鏈接分享:http://muchs.cn/article2/gddcoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、企業(yè)網(wǎng)站制作、手機(jī)網(wǎng)站建設(shè)、域名注冊(cè)、網(wǎng)站內(nèi)鏈、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)