《SpringCloud微服务实战》学习笔记之消息总线BUS-RabbitMQ
来源:互联网 发布:时时彩宝宝计划软件 编辑:程序博客网 时间:2024/05/18 11:49
Spring Cloud Bus支持两款消息中间件:RabbitMQ、Kafka。
一、RabbitMQ实现消息总线
1、RabbitMQ简介
RabbitMQ是实现了高级消息对列协议(AMQP)的开源消息代理软件,它是用高性能、可伸缩而闻名的Erlang语言编写而成的,其集群和故障转移是构建在开放电信平台框架上的。
2、RabbitMQ的基本概念
- Broker:可以理解为消息队列服务器实体,负责接收消息生产者的消息,然后将消息发送至消息接收者或者其他的Broker。
- Exchange:消息交换机,是消息第一个到达的地方,消息通过它指定的路由规则,分发到不同的消息队列中去。
- Queue:消息队列,消息通过发送和路由之后最终到达的地方。
- Binding:绑定,作用是吧Exchange和Queue按照路由规则绑定起来。
- Routing Key:路由关键字,Exchange根据这个关键字进行消息投递。
- Virtual host:虚拟主机,他是对Broker的虚拟划分,将消费者、生产这和他们依赖的AMQP相关结构进行隔离,一般是为了安全考虑。
- Connection:连接,代表生产者、消费者、Broker之间惊喜通信的物理网络。
- Channel:消息通道,用于连接生产者和消费者的逻辑结构。
- Producer:消息生产者
- Consumer:消息消费者
3、安装RabbitMQ
http://blog.csdn.net/u012343297/article/details/78874237
4、RabbitMQ下创建user: springcloud/springcloud
5、分配权限
6、创建springboot项目SpringCloud-RabbitMq项目
7、pom中添加依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> </dependencies>
8、创建消息发送类sender和消息接受类reciver
@Componentpublic class Sender { @Autowired private AmqpTemplate rabbitTemplate; public void send(){ String context = "hello "+new Date(); this.rabbitTemplate.convertAndSend("hello",context); }}
@Component@RabbitListener(queues="hello")public class Reciver { @RabbitHandler public void process(String hello){ System.out.print("reciver:"+hello); }}
9、创建主类RabbitApplication和测试类ApplicationTest
@SpringBootApplicationpublic class RabbitApplication { public static void main(String[] args) { SpringApplication.run(RabbitApplication.class, args); }}
@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes=RabbitApplication.class)public class ApplicationTest { @Autowired private Sender sender; @Test public void hello(){ sender.send(); }}
10、创建application.properties
spring.application.name=rabbitmqspring.rabbitmq.host=127.0.0.1spring.rabbitmq.port=5672spring.rabbitmq.username=springcloudspring.rabbitmq.password=springcloud
11.启动项目,并运行测试类,登录http://localhost:15672, springcloud/springcloud
12、Spring Cloud中使用RabbitMq
a、在SpringCloud-ConfigClient项目的pom文件中添加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
b、在application.properties中添加
spring.rabbitmq.host=127.0.0.1spring.rabbitmq.port=5672spring.rabbitmq.username=springcloudspring.rabbitmq.password=springcloud
c、依次启动SpringCloud-EurakaServer、SpringCloud-Config,然后启动SpringCloud-ConfigClient两次(端口分别为9002、9003)
启动configClient时,控制台输出
分别访问http://localhost:9002/hello、http://localhost:9003/hello
d、修改demo-dev.properties
greeting=hello,RabbitMQ
e、发送post请求至:http://localhost:9003/bus/refresh
此时9002和9003的控制台输出
f、访问http://localhost:9002/hello、http://localhost:9003/hello,返回:hello,RabbitMQ
g、/bus/refresh接口可以指定服务,即使用”destination”参数,比如 “/bus/refresh?destination=customers:**” 即刷新服务名为customers的所有服务,不管ip。
原理:
- 《SpringCloud微服务实战》学习笔记之消息总线BUS-RabbitMQ
- 《SpringCloud微服务实战》学习笔记之消息驱动Stream
- 《SpringCloud微服务实战》学习笔记之断路器Hystrix
- 《SpringCloud微服务实战》学习笔记之Feign
- 《SpringCloud微服务实战》学习笔记之网关服务zuul
- 《SpringCloud微服务实战》学习笔记之配置中心Config
- springcloud实战之13 rabbitmq消息总线(bus)
- 《SpringCloud微服务实战》学习笔记之服务治理Eureka(单节点)
- 《SpringCloud微服务实战》学习笔记之服务消费者Ribbon+RestTemplate
- 《SpringCloud微服务实战》学习笔记之分布式服务跟踪Sleuth
- SpringCloud微服务实战
- SpringCloud教程八:消息总线(SpringCloud Bus)
- 10.消息总线(bus+rabbitmq)
- 基于springcloud的微服务实战
- SpringCloud微服务实战-SpringCloudConfig配置中心
- SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)
- springcloud微服务实战:Eureka+Zuul+Ribbon+Hystrix+SpringConfig
- 微服务实战之微服务介绍
- 最近公司项目总结
- 微信公众平台开发之获取用户地理位置
- 贝叶斯公式
- 使用變數型別的良好習慣
- Spring的@Autowired注入规则
- 《SpringCloud微服务实战》学习笔记之消息总线BUS-RabbitMQ
- 【传智播客郑州校区分享】职场小白到传智播客郑州校区学习变身技术达人
- go语言随便玩玩(linux环境)
- 计算机屏幕上的代码列
- Object类的这三个方法---equals、hashCode和clone方法关键技术剖析
- 递归查询 START WITH CONNECT BY PRIOR
- React D3.js pie chart | 解决label重叠的问题
- 裸辞之回炉重造(牛客网刷题总结)
- 最短路径导航系统