SpringBoot使用RabbitMQ做消息中间件
2018-03-25 10:39
831 查看
说明
MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法。应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们。消息传递指的是程序之间通过在消息中发送数据进行通信,而不是通过直接调用彼此来通信,直接调用通常是用于诸如远程过程调用的技术。排队指的是应用程序通过 队列来通信。队列的使用除去了接收和发送应用程序同时执行的要求。MQ是消费-生产者模型的一个典型的代表,一端往消息队列中不断写入消息,而另一端则可以读取或者订阅队列中的消息。MQ和JMS类似,但不同的是JMS是SUN JAVA消息中间件服务的一个标准和API定义,而MQ则是遵循了AMQP协议的具体实现和产品。RabbitMQ是一个在AMQP(高级消息队列协议)基础上完成的,可复用的企业消息系统。他遵循Mozilla Public License开源协议。作用:在项目中,将一些无需即时返回且耗时的操作提取出来,进行了异步处理,而这种异步处理的方式大大的节省了服务器的
4000
请求响应时间,从而提高了系统的吞吐量
安装
环境:CentOS 7.21 安装Erlang -y参数表示忽略回答安装yes的过程
yum install -y erlang2 安装RabbitMQ 切换到根目录,然后下载(这里演示下载3.6.6版,可在官网找不同版本的rpm的链接,戳->点击打开链接)
cd /root/ wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.6/rabbitmq-server-3.6.6-1.el7.noarch.rpm下载完成后安装
yum install rabbitmq-server-3.6.6-1.el7.noarch.rpm创建rabbitmq.config文件
cd /etc/rabbitmq/ vi rabbitmq.config输入:(注意最后一行有一个'.'号)
[{rabbit, [{loopback_users, []}]}].这里的意思是开放使用,rabbitmq默认创建的用户guest,密码也是guest,这个用户默认只能是本机访问,localhost或者127.0.0.1,从外部访问需要添加上面的配置。
启动
service rabbitmq-server start查看状态
service rabbitmq-server status
开启管理UI:默认账户是guest,密码是guest,访问端口:15672
rabbitmq-plugins enable rabbitmq_management firewall-cmd --zone=public --add-port=15672/tcp --permanent firewall-cmd --reload也可以直接使用命令行添加用户:
rabbitmqctl add_user yunlingfly yunlingfly //添加用户,后面两个参数分别是用户名和密码 rabbitmqctl set_permissions -p / yunlingfly ".*" ".*" ".*" //添加权限 rabbitmqctl set_user_tags yunlingfly administrator //修改用户角色注:如果出现FirewallD is not running的问题则是防火墙没有开启,使用如下命令:
systemctl start firewalld systemctl status firewalld然后就可以远程访问了,然后可直接配置用户权限等信息。
登录:http://ip:15672,输入用户名guest密码guest或者是我配置的yunlingfly密码yunlingfly,见到下面的图说明配置正确,可以进行下一步操作了
注意事项:需要开启服务器相应的端口号,然后添加进防火墙,否则外网无法访问
附RabbitMQ常用操作命令:开启:
service rabbitmq-server strat关闭:
service rabbitmq-server stop重启:service rabbitmq-server restart查看状态:service rabbitmq-server status开机自启:chkconfig rabbitmq-server on添加用户:rabbitmqctl add_user username password //添加用户,后面两个参数分别是用户名和密码
rabbitmqctl set_permissions -p / username ".*" ".*" ".*" //添加权限
rabbitmqctl set_user_tags username administrator //修改用户角色
使用SpringBoot集成
环境:IDEA2017,jdk1.8,SpringBoot1.5.10.RELEASE说明:一般来说消息发送者和消息接收者不是同一个,为了演示方便将二者放在了一个项目下,但是可以发现二者没有耦合,随时可以分开成两个项目
首先给出项目结构:
1 配置pom.xml<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>yunlingfly</groupId>
<artifactId>springcloud-rabbitmq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud-rabbitmq</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2 编写启动类package yunlingfly.springcloudrabbitmq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringcloudRabbitmqApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudRabbitmqApplication.class, args);
}
}3 配置文件application.ymlspring:
application:
name: rabbitMQ
rabbitmq:
#配置连接IP地址,本机安装的RabbitMQ的话可以用localhost
host: xxx.xxx.xxx.xxx
#这里配置的是RabbitMQ的连接端口,是5672哦不是15672
port: 5672
username: yunlingfly
password: yunlingfly
server:
#这里配置的是该项目的启动端口
port: 8765
info:
app:
name: RabbitMQ测试连接
version: 0.0.14 编写消息发送者package yunlingfly.springcloudrabbitmq.mqsender;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class Sender1 {
/**
* 注入AmqpTemplate,然后利用AmqpTemplate向一个名为hello的消息队列中发送消息。
*/
@Autowired
private AmqpTemplate rabbitTemplate;
public void send(){
String msg="hello rabbitMQ:"+new Date()+" 你好,高级消息队列使用ing~";
System.out.println("单对单发送参数。Sender发出了消息:"+msg);
this.rabbitTemplate.convertAndSend("hello",msg);
// 第一个参数表示交换机,第二个参数表示routing key,第三个参数即消息,Topic方式
// this.rabbitTemplate.convertAndSend("topicExchange","key.da",msg);
}
}5 编写队列package yunlingfly.springcloudrabbitmq.mqqueue;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitConfig {
@Bean
public Queue helloQueue(){
return new Queue("hello");
}
}
6 编写消息接收者
package yunlingfly.springcloudrabbitmq.mqreceiver; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @RabbitListener(queues = "hello") public class Receiver1 { @RabbitHandler public void process(String msg){ System.out.println("Receiver收到了消息:"+msg); } }7 编写控制层
package yunlingfly.springcloudrabbitmq.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import yunlingfly.springcloudrabbitmq.mqsender.Sender1; @RestController public class RabbitMQController { @Autowired private Sender1 sender; @RequestMapping(value = "/helloRabbit",method = RequestMethod.GET) public String sendMQ(){ System.out.println("准备发送消息。。。"); sender.send(); return "success"; } }8 运行
[INFO]Created new connection: rabbitConnectionFactory表示连接成功
9 外网访问输入:http://localhost:8765/helloRabbit
看到控制台输出:
查看RabbitMQ可以看到有了一个hello queue
附:可以在官网找到更多用法和例子,戳->点击打开链接
相关文章推荐
- spring boot / cloud (九) 使用rabbitmq消息中间件
- spring boot / cloud (九) 使用rabbitmq消息中间件
- SpringBoot 系列 | 第十篇:使用RabbitMQ收发消息
- Spring Boot 构建应用——整合消息中间件 RabbitMQ
- Spring Cloud版——电影售票系统<五>Hystrix使用消息中间件RabbitMQ收集数据
- Spring Boot 入门之消息中间件的使用
- 以ActiveMQ为例JAVA消息中间件学习【3】——SpringBoot中使用ActiveMQ
- Spring Boot 2.0.0.M7 使用异步消息服务-AMQP(RabbitMQ)
- springboot(3)-Spring Boot中使用RabbitMQ
- python中使用rabbitmq消息中间件
- RabbitMQ的Java应用(3) -- 使用spring-boot-starter-amqp开发生产者应用
- SpringBoot的RabbitMQ消息队列: 六、第五模式"Topics"
- 使用rabbitmq消息中间件
- 【消息中间件RabbitMQ】Spring AMQP分析与实战视频教程
- rabbitmq学习10:使用spring-amqp发送消息及异步接收消息
- Spring Boot中使用RabbitMQ
- 消息队列 概念 配合SpringBoot使用Demo
- Spring Boot 揭秘与实战(六) 消息队列篇 - RabbitMQ
- 在Spring Boot框架下使用WebSocket实现消息推送
- SpringBoot的RabbitMQ消息队列: 四、第三模式"Publish/Subscribe"