最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Spring Boot 配置多個RabbitMQ

 更新時間:2017年06月23日 09:12:14   作者:1CSH1  
本篇文章主要介紹了Spring Boot 配置多個RabbitMQ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

閑話

好久沒有寫博客了,6月份畢業(yè),因?yàn)楣ぷ髟?,公司上網(wǎng)受限,一直沒能把學(xué)到的知識點(diǎn)寫下來,工作了半年,其實(shí)學(xué)到的東西也不少,但是現(xiàn)在回憶起來的東西少之又少,有時甚至能在同個問題中踩了幾次,越來越覺得及時記錄一下學(xué)到的東西很重要。

好了,閑話少說,寫下這段時間學(xué)習(xí)的東西,先記錄一下用spring Boot配置多個RabbitMQ的情況。。。

最近公司新啟動一個新平臺的項(xiàng)目,需要用微服務(wù)這個這幾年很火的概念來做,所以就學(xué)習(xí)了Spring Boot方面的知識,給同事展示Spring Boot的一些小事例的時候,同事提出了可不可以配置多個RabbitMQ?下面就是在Spring Boot配置多個RabbitMQ的例子。是自己摸索搭建的,也不知道對不對,有其他好的實(shí)現(xiàn)方法的網(wǎng)友可以互相交流一下。

項(xiàng)目代碼構(gòu)造

關(guān)注點(diǎn)在紅框的代碼。。。

代碼

下面就把項(xiàng)目的代碼展示下來

application.properties

配置文件

spring.application.name=rabbitmq-hello

# RabbitMQ
spring.rabbitmq.first.host=node9
spring.rabbitmq.first.port=5670
spring.rabbitmq.first.username=guest
spring.rabbitmq.first.password=guest

spring.rabbitmq.second.host=localhost
spring.rabbitmq.second.port=5672
spring.rabbitmq.second.username=guest
spring.rabbitmq.second.password=guest


# MySQL
spring.datasource.url = jdbc:mysql://localhost:3306/cloudtest
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver

HelloApplication.java

程序入口

package com.paas.springboot.demo01;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloApplication {

 public static void main(String[] args) {
  SpringApplication.run(HelloApplication.class, args);
 }

}

RabbitConfig.java

RabbitMQ配置類

package com.paas.springboot.demo01;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class RabbitConfig {

 @Bean(name="firstConnectionFactory")
 @Primary
 public ConnectionFactory firstConnectionFactory(
           @Value("${spring.rabbitmq.first.host}") String host, 
           @Value("${spring.rabbitmq.first.port}") int port,
           @Value("${spring.rabbitmq.first.username}") String username,
           @Value("${spring.rabbitmq.first.password}") String password
           ){
  CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
  connectionFactory.setHost(host);
  connectionFactory.setPort(port);
  connectionFactory.setUsername(username);
  connectionFactory.setPassword(password);
  return connectionFactory;
 }

 @Bean(name="secondConnectionFactory")
 public ConnectionFactory secondConnectionFactory(
           @Value("${spring.rabbitmq.second.host}") String host, 
           @Value("${spring.rabbitmq.second.port}") int port,
           @Value("${spring.rabbitmq.second.username}") String username,
           @Value("${spring.rabbitmq.second.password}") String password
           ){
  CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
  connectionFactory.setHost(host);
  connectionFactory.setPort(port);
  connectionFactory.setUsername(username);
  connectionFactory.setPassword(password);
  return connectionFactory;
 }

 @Bean(name="firstRabbitTemplate")
 @Primary
 public RabbitTemplate firstRabbitTemplate(
           @Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory
           ){
  RabbitTemplate firstRabbitTemplate = new RabbitTemplate(connectionFactory);
  return firstRabbitTemplate;
 }

 @Bean(name="secondRabbitTemplate")
 public RabbitTemplate secondRabbitTemplate(
           @Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory
           ){
  RabbitTemplate secondRabbitTemplate = new RabbitTemplate(connectionFactory);
  return secondRabbitTemplate;
 }

 @Bean(name="firstFactory")
 public SimpleRabbitListenerContainerFactory firstFactory(
              SimpleRabbitListenerContainerFactoryConfigurer configurer,
              @Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory  
              ) {
  SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
  configurer.configure(factory, connectionFactory);
  return factory;
 }

 @Bean(name="secondFactory")
 public SimpleRabbitListenerContainerFactory secondFactory(
              SimpleRabbitListenerContainerFactoryConfigurer configurer,
              @Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory   
              ) {
  SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
  configurer.configure(factory, connectionFactory);
  return factory;
 }

 @Bean
 public Queue firstQueue() {
  System.out.println("configuration firstQueue ........................");
  return new Queue("hello1");
 }

 @Bean
 public Object secondQueue() {
  System.out.println("configuration secondQueue ........................");
  return new Queue("hello2");
 }
}

Receiver.java

RabbitMQ中的消費(fèi)者,接收first RabbitMQ中的隊(duì)列hello1的數(shù)據(jù)

package com.paas.springboot.demo01;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "hello1", containerFactory="firstFactory")
public class Receiver {

 @RabbitHandler
 public void process(String hello) {
  System.out.println("Receiver : " + hello);
 }

}

Receiver2.java

RabbitMQ中的消費(fèi)者,接收second RabbitMQ中的隊(duì)列hello2的數(shù)據(jù)

package com.paas.springboot.demo01;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "hello2", containerFactory="secondFactory" )
public class Receiver2 {

 @RabbitHandler
 public void process(String hello) {
  System.out.println("Receiver : " + hello);
 }

}

Sender.java

RabbitMQ中的生產(chǎn)者,發(fā)送消息到first RabbitMQ中的隊(duì)列hello1和hello2

package com.paas.springboot.demo01;

import java.util.Date;
import javax.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

@Component
public class Sender {

 @Resource(name="firstRabbitTemplate")
 private RabbitTemplate firstRabbitTemplate;

 public void send1() {
  String context = "hello1 " + new Date();
  System.out.println("Sender : " + context);
  this.firstRabbitTemplate.convertAndSend("hello1", context);
 }

 public void send2() {
  String context = "hello2 " + new Date();
  System.out.println("Sender : " + context);
  this.firstRabbitTemplate.convertAndSend("hello2", context);
 }

}

Sender2.java

RabbitMQ中的生產(chǎn)者,發(fā)送消息到second RabbitMQ中的隊(duì)列hello1和hello2

package com.paas.springboot.demo01;

import java.util.Date;
import javax.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

@Component
public class Sender {

 @Resource(name="firstRabbitTemplate")
 private RabbitTemplate firstRabbitTemplate;

 public void send1() {
  String context = "hello1 " + new Date();
  System.out.println("Sender : " + context);
  this.firstRabbitTemplate.convertAndSend("hello1", context);
 }

 public void send2() {
  String context = "hello2 " + new Date();
  System.out.println("Sender : " + context);
  this.firstRabbitTemplate.convertAndSend("hello2", context);
 }

}

TestDemo01.java

測試類,調(diào)用Sender發(fā)送消息

package com.paas.springboot.demo01;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = HelloApplication.class)
public class TestDemo01 {

 @Autowired
 private Sender sender;

 @Autowired
 private Sender2 sender2;

 @Test
 public void hello() throws Exception {
  sender.send1();
  sender.send2();
 }

 @Test
 public void hello2() throws Exception {
  sender2.send1();
  sender2.send2();
 }
}

pom.xml

Maven項(xiàng)目中最重要的一個配置文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.paas.springboot.demo</groupId>
 <artifactId>springboot</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>springboot Maven Webapp</name>
 <url>http://maven.apache.org</url>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.4.3.RELEASE</version>
  <relativePath /> <!-- lookup parent from repository -->
 </parent>

 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-amqp</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
   <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-jdbc</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>com.jayway.jsonpath</groupId>
   <artifactId>json-path</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
  </dependency>
 </dependencies>

 <build>
  <finalName>springboot</finalName>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>

 <repositories>
  <repository>
   <id>spring-releases</id>
   <url>https://repo.spring.io/libs-release</url>
  </repository>
 </repositories>
 <pluginRepositories>
  <pluginRepository>
   <id>spring-releases</id>
   <url>https://repo.spring.io/libs-release</url>
  </pluginRepository>
 </pluginRepositories>

</project>

運(yùn)行&測試

通過運(yùn)行HelloApplication.Java,將程序中的Receiver啟動一直監(jiān)控著隊(duì)列,然后通過運(yùn)行TestDemo01.java中的測試案例,發(fā)送消息到隊(duì)列中,這時可以發(fā)現(xiàn)運(yùn)行HelloApplication的程序控制臺將剛剛發(fā)送的消息打印出來

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • eclipse中自動生成構(gòu)造函數(shù)的兩種方法

    eclipse中自動生成構(gòu)造函數(shù)的兩種方法

    下面小編就為大家?guī)硪黄猠clipse中自動生成構(gòu)造函數(shù)的兩種方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • Spring 應(yīng)用上下文獲取 Bean 的常用姿勢實(shí)例總結(jié)

    Spring 應(yīng)用上下文獲取 Bean 的常用姿勢實(shí)例總結(jié)

    這篇文章主要介紹了Spring 應(yīng)用上下文獲取 Bean,結(jié)合實(shí)例形式總結(jié)分析了Spring 應(yīng)用上下文獲取 Bean的實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下
    2020-05-05
  • Java實(shí)戰(zhàn)之實(shí)現(xiàn)文件資料上傳并生成縮略圖

    Java實(shí)戰(zhàn)之實(shí)現(xiàn)文件資料上傳并生成縮略圖

    這篇文章主要介紹了通過Java實(shí)現(xiàn)文件資料的上傳并生成一個縮略圖,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Java有一定的幫助,感興趣的小伙伴可以了解一下
    2021-12-12
  • Java實(shí)現(xiàn)經(jīng)典游戲之大魚吃小魚

    Java實(shí)現(xiàn)經(jīng)典游戲之大魚吃小魚

    這篇文章主要為大家詳細(xì)介紹了如何利用Java語言實(shí)現(xiàn)經(jīng)典游戲之大魚吃小魚,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Java游戲開發(fā)有一定幫助,需要的可以參考一下
    2022-08-08
  • 詳解Nacos配置中心的實(shí)現(xiàn)

    詳解Nacos配置中心的實(shí)現(xiàn)

    Spring Cloud Alibaba 是阿里巴巴提供的一站式微服務(wù)開發(fā)解決方案。而 Nacos 作為 Spring Cloud Alibaba 的核心組件之一,提供了兩個非常重要的功能:注冊中心和配置中心,我們今天來了解和實(shí)現(xiàn)一下二者
    2022-08-08
  • Calcite使用SQL實(shí)現(xiàn)查詢excel內(nèi)容

    Calcite使用SQL實(shí)現(xiàn)查詢excel內(nèi)容

    因?yàn)閏alcite本身沒有excel的適配器,?所以本文將模仿calcite-file,?搞一個calcite-file-excel實(shí)現(xiàn)查詢excel內(nèi)容,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-01-01
  • SpringBoot如何優(yōu)雅的處理重復(fù)請求

    SpringBoot如何優(yōu)雅的處理重復(fù)請求

    對于一些用戶請求,在某些情況下是可能重復(fù)發(fā)送的,如果是查詢類操作并無大礙,但其中有些是涉及寫入操作的,一旦重復(fù)了,可能會導(dǎo)致很嚴(yán)重的后果,所以本文給大家介紹了SpringBoot優(yōu)雅的處理重復(fù)請求的方法,需要的朋友可以參考下
    2023-12-12
  • Java實(shí)現(xiàn)為圖片添加水印功能

    Java實(shí)現(xiàn)為圖片添加水印功能

    在圖像處理領(lǐng)域,水印是一種常見的保護(hù)版權(quán)和標(biāo)識圖片歸屬的方法,Java提供了強(qiáng)大的圖像處理能力,可以通過Graphics2D類在圖像上繪制水印,下面我們來看看具體操作步驟吧
    2025-02-02
  • Spring AOP注解實(shí)戰(zhàn)指南

    Spring AOP注解實(shí)戰(zhàn)指南

    在現(xiàn)代軟件開發(fā)中,面向切面編程(AOP)是一種強(qiáng)大的編程范式,本文將介紹如何在Spring框架中通過AspectJ注解以及對應(yīng)的XML配置來實(shí)現(xiàn)AOP,在不改變主業(yè)務(wù)邏輯的情況下增強(qiáng)應(yīng)用程序的功能,需要的朋友可以參考下
    2024-06-06
  • Java中如何將String轉(zhuǎn)JSONObject

    Java中如何將String轉(zhuǎn)JSONObject

    這篇文章主要介紹了Java中如何將String轉(zhuǎn)JSONObject,String類型轉(zhuǎn)JSONObject,下面有兩種方式可以進(jìn)行轉(zhuǎn)換,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05

最新評論

黑水县| 岫岩| 防城港市| 大安市| 余江县| 安龙县| 酒泉市| 苍梧县| 兴仁县| 桦川县| 巢湖市| 区。| 谢通门县| 清水河县| 桂平市| 永嘉县| 浦东新区| 普兰县| 贵港市| 舟曲县| 湖州市| 满城县| 梁河县| 庆安县| 北辰区| 太和县| 盐城市| 嘉黎县| 松江区| 宜兰市| 泸定县| 阿瓦提县| 女性| 迁安市| 阿合奇县| 斗六市| 云梦县| 屏东县| 拉萨市| 临夏县| 尼勒克县|