Spring Cloud 2-RabbitMQ 集成(八)
通过消息队列MQ做为通信中心,这里采用RabbitMQ.安装方参考: https://www.cnblogs.com/linyufeng/p/9883905.html
也可以采用windows环境(建议初学者采用)
启动服务并启动web访问界面
- linux启动
rabbitmq-plugins enable rabbitmq_management
- windows启动
rabbitmq-plugins.bat enable rabbitmq_management
访问 http://localhost:15672/ 并登陆, 默认用户名和密码都是guest
Hello World 代码示例
pom.xml
<!-- rabbitmq 客户端 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
application.yml
spring:
application:
name: rabbitmq-hello
# 以下都是默认配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
提供者
@Component
public class Sender {
@Autowired
private AmqpTemplate template;
public void send(){
String msg = "hello: "+new Date().toString();
System.out.println("Sender: " + msg);
this.template.convertAndSend("hello", msg);
}
}
消费者
@Component
public class Receiver {
@RabbitListener(queues = "hello")
public void process(String hello){
System.out.println("Reciver: "+ hello);
}
}
队列配置
@Configuration
public class RabbitConfig {
@Bean
public Queue queue(){
return new Queue("hello");
}
}
单元测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqHelloApplicationTests {
@Autowired
private Sender sender;
@Test
public void contextLoads() throws InterruptedException {
for (int i = 0; i < 1000; i++) {
Thread.sleep((long) (Math.random()*3000));
sender.send();
}
}
}
运行单元测试, 控制台打印:
Sender: hello: Wed Dec 26 13:46:35 GMT+08:00 2018
Reciver: hello: Wed Dec 26 13:46:35 GMT+08:00 2018
Sender: hello: Wed Dec 26 13:46:37 GMT+08:00 2018
Reciver: hello: Wed Dec 26 13:46:37 GMT+08:00 2018
Sender: hello: Wed Dec 26 13:46:38 GMT+08:00 2018
Reciver: hello: Wed Dec 26 13:46:38 GMT+08:00 2018

Spring Cloud 2-RabbitMQ 集成(八)的更多相关文章
- Spring cloud 基础框架集成
Spring cloud 基础框架集成 1. 注册中心 -eurekar 1. pom依赖 <?xml version="1.0" encoding="UTF-8& ...
- spring cloud系列教程第八篇-修改服务名称及获取注册中心注册者的信息
spring cloud系列教程第八篇-修改服务名称及获取注册中心注册者的信息 本文主要内容: 1:管理页面主机名及访问ip信息提示修改 2:获取当前注册中心的服务列表及每个服务对于的服务提供者列表 ...
- spring cloud 项目相关集成简介
Spring Cloud Config 配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储.Git以及Subversion. Spring Cloud Bus 事件.消 ...
- 消息驱动式微服务:Spring Cloud Stream & RabbitMQ
1. 概述 在本文中,我们将向您介绍Spring Cloud Stream,这是一个用于构建消息驱动的微服务应用程序的框架,这些应用程序由一个常见的消息传递代理(如RabbitMQ.Apache Ka ...
- Spring Cloud微服务集成配置中心
1. 搭建Spring Cloud Config配置中心(见上一篇博客) 2. 创建微服务项目bounter-simon-app,pom文件如下: <?xml version="1.0 ...
- Spring Cloud学习笔记【八】服务网关 Zuul(过滤器)
在上篇文章中我们了解了 Spring Cloud Zuul 作为网关所具备的最基本功能:路由(Router),下面我们将关注 Spring Cloud Zuul 的另一核心功能:过滤器(Filter) ...
- Spring Cloud架构教程 (八)消息驱动的微服务(消费组)【Dalston版】
使用消费组实现消息消费的负载均衡 通常在生产环境,我们的每个服务都不会以单节点的方式运行在生产环境,当同一个服务启动多个实例的时候,这些实例都会绑定到同一个消息通道的目标主题(Topic)上. 默认情 ...
- Spring Cloud第八篇 | Hystrix集群监控Turbine
本文是Spring Cloud专栏的第八篇文章,了解前七篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Clo ...
- Nacos Config客户端与Spring Boot、Spring Cloud深度集成
目录 Nacos与Spring Boot集成 @NacosPropertySource和@NacosValue com.alibaba.nacos.spring.core.env.NacosPrope ...
- spring cloud 集成分布式配置中心 apollo(单机部署apollo)
一.什么是apollo? Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性,适用 ...
随机推荐
- Django Rest Framework(一)
•基于Django 先创建一个django项目,在项目中创建一些表,用来测试rest framework的各种组件 class UserInfo(models.Model): "" ...
- Docker 核心技术之容器与镜像
Docker容器与镜像的关系 容器提交 – docker commit docker commit -h 作用: 根据容器生成一个新的镜像 命令格式: docker commit [OPTIONS] ...
- npm install报错
npm ERR! C:\Users\xxxxxx\AppData\Roaming\npm-cache\_logs\2019-02-27T07_48_47_283Z-debug.log " ...
- Nginx HTTP框架提供的请求相关变量
L73 binary_remote_addr 对端二进制IPV4或IPV6 一般用作限制用户请求缓存key connection 递增链接序号 connection_requests 一条TCP链接 ...
- ImageMagick 安装 window10与错误总结
一般默认安装最新的版本(目前是7.0.8),但是通过测试发现这个版本会报错(ImportError: MagickWand shared library not found.)所以建议安装 im6版本 ...
- 洛谷P1122最大子树和题解
题目 一道比较好想的树形\(DP\) 完全可以用树形DP的基本思路,递归,然后取最优的方法. \(Code\) #include <iostream> #include <cstri ...
- Oracle下查看索引的语句
1. 查询一张表里面索引 select * from user_indexes where table_name=upper('bills'); 2. 查询被索引字段 select * from ...
- ACM-自学之旅
分类 知识清单 数据结构 链式前向星 树状数组 线段树 线段树的区间合并 基于ST表格的RMQ 图论 最近公共祖先 树的直径.树的重心与树的点分治 树的最小支配集,最小点覆盖与最大独立集 求无向连通图 ...
- C#调用Java的WebService添加SOAPHeader验证
C#调用Java的WebService添加SOAPHeader验证(2) 1.问题描述 调用的Java的webservice string Invoke(string func, string req ...
- html css笔记zht
第3章 Img标签 路径问题 绝对路径:从盘符(C:\)出发的路径 (C:\Users\......) linux(绝对路径以 / 开头) 相对路径:( ./ 当前文件所在的目录)( ../上一级目录 ...