- 浏览: 110788 次
- 性别:
- 来自: 上海
最新评论
-
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 1352<!-- 安装ifconfig --> yum i ... -
SpringBoot注入多实例,多线程处理方式
2018-06-19 14:21 2449https://www.cnblogs.com/qindong ... -
原 局域网笔记本ping不通台式机,MYSQL错误 NO.1130 ERROR
2018-06-14 15:16 678同一局域网 笔记本ping不同台式机,mysq客户端连接数据库 ... -
几句良言 胜过十年代码
2013-12-02 08:53 719转载原文出处 http://qindongliang1922 ... -
时间字符串转换
2012-06-13 15:23 1061字符串转换成时间 DateFormat f = new Sim ... -
正则表达式,常用正则
2012-04-18 15:11 1138//邮箱正则表达式 ... -
oracle客户端连数据的配置文件
2012-04-13 10:17 990安装oracle客户端的时候,配置数据 都在这个安装路径下 o ... -
多线程理解,线程
2012-03-31 18:12 1028最近在玩安卓,要提到多线程, 总感觉那东西有点不明白,总 ... -
struts1.2 和spring ,hibernate整合一起
2012-03-20 17:10 1095struts里面的配置 这个是从spring里拿到的bean ... -
struts1.2 和spring ,hibernate整合
2012-05-25 10:24 1253项目结构图: jsp部分为: ... -
从Word读出写入txt文本
2012-02-16 17:13 1118package com.purang.pdf; import ... -
从PDF读写到TXT文本
2012-02-16 17:13 1070package com.purang.pdf; import ... -
Grails1.3.7 初学者注意的问题
2012-02-03 17:02 1123开发环境:IntelliJ IDEA 11.0.1, Gra ... -
Random随机生成ID
2011-12-30 14:12 4901[size=medium]常用UUID public s ... -
JAVA简单抽象实现方法
2011-12-26 11:30 9016public abstract class Person { ... -
类设计技巧
2011-12-23 14:13 688(1)一定要将数据设计为私有,绝对不能破坏封装性 (2)一定 ... -
java实现导出excel表
2011-12-22 19:58 943package com.experot; import ja ...
相关推荐
spring-boot-rabbitmq:spring boot和rabbitmq各种消息应用案例 spring-boot-scheduler:spring boot和定时任务案例 spring-boot-web:web开发综合使用案例 spring-boot-mail:spring boot和邮件服务 spring-boot-...
spring-boot-rabbitmq:spring boot和rabbitmq各种消息应用案例 spring-boot-scheduler:spring boot和定时任务案例 spring-boot-web:web开发综合使用案例 spring-boot-mail:spring boot和邮件服务 spring-...
RabbitMQ作为AMQP的代表性产品,在项目中大量使用。结合现在主流的spring boot,极大简化了开发过程中所涉及到的消息通信问题。
- chapter1:[基本项目构建(可作为工程脚手架),引入...由于Spring Cloud偏宏观架构,Spring Boot偏微观细节,内容上越来越多,为了两部分内容不互相干扰,所以迁移Spring Cloud内容到:[SpringCloud-Learning项目]...
其中,每个版本都有其对应的多数据源解决方案。 - springboot-caches - Spring Boot 缓存,包括redis、ehcache、spring-cache、memcached、使用redis实现session共享 等。 - springboot-templates - Spring ...
SpringBoot-RabbitMQ消息层次 这个指南将引导你建立一个RabbitMQ AMQP服务器发布和订阅消息的过程。 声明 可以使用本人阿里云安装好的RabbitMQ服务器 host:http://120.27.114.229 username:root password:root port...
- [spring-boot-rabbitmq](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-rabbitmq):Spring Boot 和 Rabbitmq 各种消息应用案例 - [spring-boot-scheduler]...
chapter3-2-4:多数据源配置(二):Spring-data-jpa chapter3-2-5:使用NoSQL数据库(一):Redis chapter3-2-6:使用NoSQL数据库(二):MongoDB chapter3-2-7:整合MyBatis chapter3-2-8:MyBatis注解配置详解 ...
`spring boot demo` 是一个用来深度学习并实战 `spring boot` 的项目,目前总共包含 **`63`** 个集成demo,已经完成 **`51`** 个。 该项目已成功集成 actuator(`监控`)、admin(`可视化监控`)、logback(`日志`)、aop...
但是在分布式多节点架构中,我们不能让每个节点都去执行同一个任务。并且在任务执行过程中一旦遇到失败,必须具备一种回滚机制来保证分布式事务调度的一致性与完整性。本项目采用了rabbitmq与kafka两种消息队列,将...
plus(快速操作Mybatis)、BeetlSQL(强大的ORM框架)、upload(本地文件上传和七牛云文件上传)、redis(缓存)、ehcache(缓存)、email(发送各种类型邮件)、task(基础定时任务)、quartz(动态管理定时任务)、xxl-job(分布式...
该项目已成功集成batis-aop(AOP自定义多数据源)、multi-datasource-mybatis(使用Mybatis集成多数据源)、quartz(定时任务)、rabbitmq-access(Rabbitmq手动确认模式)、rabbitmq-availability(RabbitMQ消息100%可靠性...
消息总线:Rabbitmq 动态网关:Spring Cloud Gateway 授权认证:Spring Security OAuth2 服务容错:Spring Cloud Hystrix 服务调用:Spring Cloud OpenFeign 文档管理:Swagger2 服务监控:Spring Boot Admin ...
- 使用最新技术栈,社区资源丰富,基于Java 21(Core Module Support 17-21)、Spring Boot 3.2。 (Support Virtual Threads/fibre/loom) - 基于注解的动态查询(Specification),可根据需要扩充查询注解。 - 支持...
以下是对这个资源的简要介绍:后端(Spring Boot): Spring Boot是一个开源的Java基础项目,用来简化Spring应用的初始搭建以及开发过程。它提供了大量的起步依赖,可以快速地构建出企业级的应用程序。在工厂车间管理...
每个项目有自带的Readme, 点击即可阅读. 花点心思 (常用工具类模块) (负载均衡的主从数据源) (整合Spring Boot和Elastic Job分布式定时任务框架) (整合JWT实现权限拦截) (Redis方法级缓存和工具类) (Spring Boot整合...
该资源包含完整的项目源代码、配置文件以及相关依赖,可以帮助开发者快速搭建和部署一个基于SpringBoot的秒杀系统。适用于Java后端开发人员、全栈开发人员以及对高并发系统设计感兴趣的学习者。通过学习和实践该项目...
使用到SMS短信注册验证,JMail进行找回密码邮箱验证,OSS阿里云图片存储服务器,RabbitMQ消息队列以及redis缓存等小技术。整个系统采用Restful风格。以json进行数据的传输。前端页面采用BootStrap以及Layui。 软件...
消息队列:RabbitMQ(spring-boot-starter-amqp) 持久层框架:MyBatis(mybaits-spring-boot-starter) 缓存:Redis(spring-boot-starter-data-redis) 数据库:MYSQL 数据库连接池:Alibaba Druid 日志管理...