闲话

好久没有写博客了,6月份毕业,因为工作原因,公司上网受限,一直没能把学到的知识点写下来,工作了半年,其实学到的东西也不少,但是现在回忆起来的东西少之又少,有时甚至能在同个问题中踩了几次,越来越觉得及时记录一下学到的东西很重要。
好了,闲话少说,写下这段时间学习的东西,先记录一下用spring Boot配置多个RabbitMQ的情况。。。
最近公司新启动一个新平台的项目,需要用微服务这个这几年很火的概念来做,所以就学习了Spring Boot方面的知识,给同事展示Spring Boot的一些小事例的时候,同事提出了可不可以配置多个RabbitMQ?下面就是在Spring Boot配置多个RabbitMQ的例子。是自己摸索搭建的,也不知道对不对,有其他好的实现方法的网友可以互相交流一下。
项目代码构造
关注点在红框的代码。。。
代码
下面就把项目的代码展示下来
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中的消费者,接收first RabbitMQ中的队列hello1的数据
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中的消费者,接收second RabbitMQ中的队列hello2的数据
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中的生产者,发送消息到first RabbitMQ中的队列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中的生产者,发送消息到second RabbitMQ中的队列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
测试类,调用Sender发送消息
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项目中最重要的一个配置文件
<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>
运行&测试
通过运行HelloApplication.Java,将程序中的Receiver启动一直监控着队列,然后通过运行TestDemo01.java中的测试案例,发送消息到队列中,这时可以发现运行HelloApplication的程序控制台将刚刚发送的消息打印出来
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# Spring
# Boot
# 配置RabbitMQ
# RabbitMQ
# 详解spring boot集成RabbitMQ
# springboot集成rabbitMQ之对象传输的方法
# spring boot集成rabbitmq的实例教程
# Spring boot集成RabbitMQ的示例代码
# Spring Boot整合RabbitMQ实例(Topic模式)
# spring boot整合RabbitMQ实例详解(Fanout模式)
# Spring Boot与RabbitMQ结合实现延迟队列的示例
# Spring Boot系列教程之7步集成RabbitMQ的方法
# 多个
# 发送消息
# 配置文件
# 也不
# 好了
# 最重要
# 几次
# 提出了
# 这段
# 能在
# 就把
# 很重要
# 少之又少
# 能把
# 来做
# 这几年
# 少说
# 需要用
# 大家多多
# 他好
相关文章:
音乐网站服务器如何优化API响应速度?
兔展官网 在线制作,怎样制作微信请帖?
微信小程序 五星评分(包括半颗星评分)实例代码
如何在建站主机中优化服务器配置?
零服务器AI建站解决方案:快速部署与云端平台低成本实践
视频网站app制作软件,有什么好的视频聊天网站或者软件?
江苏网站制作公司有哪些,江苏书法考级官方网站?
如何快速查询网址的建站时间与历史轨迹?
建站之星3.0如何解决常见操作问题?
高防服务器租用首荐平台,企业级优惠套餐快速部署
如何在沈阳梯子盘古建站优化SEO排名与功能模块?
Android自定义listview布局实现上拉加载下拉刷新功能
如何在Mac上搭建Golang开发环境_使用Homebrew安装和管理Go版本
建站主机选择指南:服务器配置与SEO优化实战技巧
如何快速搭建二级域名独立网站?
建站之星代理如何优化在线客服效率?
如何在万网开始建站?分步指南解析
如何用VPS主机快速搭建个人网站?
制作销售网站教学视频,销售网站有哪些?
西安制作网站公司有哪些,西安货运司机用的最多的app或者网站是什么?
实例解析angularjs的filter过滤器
简单实现Android验证码
建站之星24小时客服电话如何获取?
建站上传速度慢?如何优化加速网站加载效率?
如何在云主机上快速搭建网站?
建站之星北京办公室:智能建站系统与小程序生成方案解析
如何通过西部数码建站助手快速创建专业网站?
专业企业网站设计制作公司,如何理解商贸企业的统一配送和分销网络建设?
如何获取免费开源的自助建站系统源码?
MySQL查询结果复制到新表的方法(更新、插入)
深圳网站制作案例,网页的相关名词有哪些?
建站之星后台管理:高效配置与模板优化提升用户体验
如何在景安云服务器上绑定域名并配置虚拟主机?
已有域名和空间如何搭建网站?
建站之星下载版如何获取与安装?
建站之星安装失败:服务器环境不兼容?
c++ stringstream用法详解_c++字符串与数字转换利器
较简单的网站制作软件有哪些,手机版网页制作用什么软件?
香港服务器部署网站为何提示未备案?
巅云智能建站系统:可视化拖拽+多端适配+免费模板一键生成
SQL查询语句优化的实用方法总结
详解一款开源免费的.NET文档操作组件DocX(.NET组件介绍之一)
如何用虚拟主机快速搭建网站?详细步骤解析
宝塔面板如何快速创建新站点?
如何破解联通资金短缺导致的基站建设难题?
如何通过VPS搭建网站快速盈利?
如何制作新型网站程序文件,新型止水鱼鳞网要拆除吗?
建站主机是什么?如何选择适合的建站主机?
C#如何在一个XML文件中查找并替换文本内容
深圳网站制作平台,深圳市做网站好的公司有哪些?
*请认真填写需求信息,我们会在24小时内与您取得联系。