- 浏览: 112112 次
- 性别:
- 来自: 上海
最新评论
-
limingcai:
大家,还是安装VM8.0啊,那样的话,到后面可以安装XCODE ...
在Windows 中安装iPhone开发环境 -
wilsonchen:
想看看全部内容,学习一下
在Windows 中安装iPhone开发环境 -
rembass:
向楼主学习!
在Windows 中安装iPhone开发环境 -
wjc19871222:
转载时没转全,去原贴看吧
在Windows 中安装iPhone开发环境 -
wjc19871222:
想看看全部内容,学习一下!
在Windows 中安装iPhone开发环境
项目代码构造
这里写图片描述
关注点在红框的代码。。。
代码
下面就把项目的代码展示下来
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);
}
}
1
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();
}
}
1
pom.xml
http://lib.csdn.net/article/java/65386 原文出处
这里写图片描述
关注点在红框的代码。。。
代码
下面就把项目的代码展示下来
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);
}
}
1
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();
}
}
1
pom.xml
http://lib.csdn.net/article/java/65386 原文出处
发表评论
-
CentOS 7下安装配置RabbitMQ详细教程 虚拟机安装
2018-06-23 14:23 1383<!-- 安装ifconfig --> yum i ... -
SpringBoot注入多实例,多线程处理方式
2018-06-19 14:21 2503https://www.cnblogs.com/qindong ... -
原 局域网笔记本ping不通台式机,MYSQL错误 NO.1130 ERROR
2018-06-14 15:16 726同一局域网 笔记本ping不同台式机,mysq客户端连接数据库 ... -
几句良言 胜过十年代码
2013-12-02 08:53 747转载原文出处 http://qindongliang1922 ... -
时间字符串转换
2012-06-13 15:23 1100字符串转换成时间 DateFormat f = new Sim ... -
正则表达式,常用正则
2012-04-18 15:11 1168//邮箱正则表达式 ... -
oracle客户端连数据的配置文件
2012-04-13 10:17 1016安装oracle客户端的时候,配置数据 都在这个安装路径下 o ... -
多线程理解,线程
2012-03-31 18:12 1051最近在玩安卓,要提到多线程, 总感觉那东西有点不明白,总 ... -
struts1.2 和spring ,hibernate整合一起
2012-03-20 17:10 1121struts里面的配置 这个是从spring里拿到的bean ... -
struts1.2 和spring ,hibernate整合
2012-05-25 10:24 1290项目结构图: jsp部分为: ... -
从Word读出写入txt文本
2012-02-16 17:13 1146package com.purang.pdf; import ... -
从PDF读写到TXT文本
2012-02-16 17:13 1096package com.purang.pdf; import ... -
Grails1.3.7 初学者注意的问题
2012-02-03 17:02 1160开发环境:IntelliJ IDEA 11.0.1, Gra ... -
Random随机生成ID
2011-12-30 14:12 4936[size=medium]常用UUID public s ... -
JAVA简单抽象实现方法
2011-12-26 11:30 9042public abstract class Person { ... -
类设计技巧
2011-12-23 14:13 717(1)一定要将数据设计为私有,绝对不能破坏封装性 (2)一定 ... -
java实现导出excel表
2011-12-22 19:58 968package com.experot; import ja ...
相关推荐
通过使用 Spring Boot 的自动配置机制,我们可以轻松地配置多个 RabbitMQ 实例,并使用它们来发送和接收消息。本文详细介绍了如何在 Spring Boot 中配置多个 RabbitMQ 实例,并提供了相关的代码示例。
在提供的`talk`目录下,可能包含的是这个简单聊天应用的源代码,包括Spring Boot的配置、RabbitMQ的使用以及WebSocket的实现。你可以通过阅读和分析这些代码来学习如何将这三者结合在一起。其中,你可能会看到`...
spring-boot-rabbitmq:spring boot和rabbitmq各种消息应用案例 spring-boot-scheduler:spring boot和定时任务案例 spring-boot-web:web开发综合使用案例 spring-boot-mail:spring boot和邮件服务 spring-boot-...
- 自动配置:Spring Boot会根据项目依赖自动配置相应的bean,如数据源、Web服务器等。 2. **起步依赖(Starter POMs)** - Spring Boot通过一系列的起步依赖来简化Maven或Gradle配置,比如`spring-boot-starter-...
spring-boot-rabbitmq:spring boot和rabbitmq各种消息应用案例 spring-boot-scheduler:spring boot和定时任务案例 spring-boot-web:web开发综合使用案例 spring-boot-mail:spring boot和邮件服务 spring-...
在 `6.spring-boot-examples__ityouknow` 压缩包中,可能包含多个示例项目,每个项目都展示了 Spring Boot 的不同应用场景,比如: - `spring-boot-web-example`:展示如何创建一个简单的 RESTful API。 - `spring-...
标题中的“springboot整合rabbitMq和多数据源动态切换和跨域访问和gradle加maven和shiro安全框架和lombok自动getset生成”表明这是一个关于Spring Boot集成多个技术的项目。以下是对这些技术及其整合的详细解释: 1...
2. **内嵌式Web服务器**:Spring Boot支持内嵌Tomcat、Jetty或Undertow等Web服务器,这使得你可以直接运行一个jar文件启动应用,无需额外部署到外部服务器。 3. **起步依赖(Starter POMs)**:Spring Boot提供了多...
在"Spring Boot 整合 Netty + WebSocket 实时消息推送"项目中,我们主要关注以下几个核心知识点: 1. **Spring Boot集成WebSocket**:Spring Boot提供了Spring WebSocket模块,可以方便地集成WebSocket功能。通过...
【Spring Boot Demo】是一个全面展示Spring Boot应用开发的示例项目,它涵盖了多个核心中间件和技术的集成,如MyBatis、Shiro和RabbitMQ。这个项目旨在帮助开发者快速理解并掌握Spring Boot如何与这些组件协同工作,...
10. **消息队列**:Spring Boot与RabbitMQ、Kafka等消息中间件集成,实现异步通信和事件驱动架构。 通过这55个集成示例,开发者可以深入学习Spring Boot的各种用法,包括如何集成WebSocket、定时任务、缓存、邮件...
Spring Cloud是构建分布式系统的一系列工具集,包括服务发现、配置管理、断路器等,而RabbitMQ则是一个开源的消息代理和队列服务器,用于在分布式系统中传递消息。 描述中提到的博客链接虽然没有提供具体内容,但...
Spring Boot还支持外部化配置,允许开发者通过application.properties或application.yml文件来管理应用的配置,这些配置文件可以在不同的环境中加载不同版本,从而实现环境间的配置隔离。同时,Spring Boot提供了...
本项目提供的源代码展示了如何利用消息队列(Message Queue)、Spring Boot框架以及WebSocket技术来实现一个实时的消息推送模型。以下是关于这些关键技术的详细说明: 1. **消息队列**:消息队列是一种中间件,用于...
在IT行业中,Spring Boot是一个非常流行的Java框架,用于简化微服务开发。RabbitMQ则是一个广泛应用的消息队列系统,常用于解耦应用组件间的通信。WebSocket是一种在客户端和服务器之间建立长连接的协议,适用于实时...
例如,如果你的类路径中有`hibernate-entitymanager`,那么Spring Boot就会自动配置JPA数据源。 4. **Actuator**:这是一个用于生产环境的工具集,提供了健康检查、指标收集、审计、环境信息等功能,帮助监控和管理...
在IT行业中,Spring Boot是一个非常流行的Java框架,它简化了基于Spring的应用开发。RabbitMQ则是一个开源的消息代理和队列服务器,常被用作消息中间件,用于处理应用程序之间的异步通信。当我们谈论"SpringBoot整合...
Spring Boot 将会自动创建一个内嵌的 Tomcat 服务器并启动你的应用。现在,你可以在浏览器中访问 `http://localhost:8080/hello` 来查看 "Hello, Spring Boot!" 的消息。 6. **自动配置** Spring Boot 的核心特性...
- chapter1:[基本项目构建(可作为工程脚手架),引入...由于Spring Cloud偏宏观架构,Spring Boot偏微观细节,内容上越来越多,为了两部分内容不互相干扰,所以迁移Spring Cloud内容到:[SpringCloud-Learning项目]...