JMS是Java Message Service的简称,是Java定义的一套消息服务标准,类似于JDBC。只要是符合JMS规范标准的,都是通用的Java消息服务
MOM是Message Oriented Middleware的简称
RabbitMQ是实现了AMQP(Advanced Message Queuing Protocol - 高级消息队列协议)的开源消息代理软件
RabbitMQ服务器是用Erlang语言编写的
将RabbitMQ部署到CentOS中
预装环境
yum install ncurses ncurses-base ncurses-devel ncurses-libs ncurses-static ncurses-term ocaml-curses ocaml-curses-devel -y
yum install openssl-devel zlib-devel -y
yum -y install make ncurses-devel gcc gcc-c++ unixODBC unixODBC-devel openssl openssl-devel
Erlang的安装
# 创建安装目录
mkdir /usr/local/erlang
# 进入目录
cd /usr/local/erlang
# 下载安装包
wget http://erlang.org/download/otp_src_20.0.tar.gz
# 解压
tar -xf otp_src_20.0.tar.gz
# 设置安装环境
./configure --prefix=/opt/erlang --with-ssl -enable-threads -enable-smmp-support -enable-kernel-poll --enable-hipe --without-javac
# 编译并安装
make&&make install
# 设置软连接
ln -s /opt/erlang/bin/erl /usr/local/bin/erl
# 进入erlang应用环境
/usr/local/bin/erl
# 测试
EvenN = lists:filter (fun (N) -> N rem 2 == 0 end, lists:seq (1,100)).
# 退出erlang应用环境
halt().
# 设置环境变量
vim /etc/profile
# 添加内容
# ERLANG_HOME=/opt/erlang
# PATH=$ERLANG_HOME/bin:$PATH
# export ERLANG_HOME
# export PATH
# 保存后重载
source /etc/profile
# 检查是否加载成功
echo $ERLANG_HOME
echo $PATH
安装RabbitMQ
# 进入安装目录
cd /usr/local/erlang
# 下载安装包
http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.10/rabbitmq-server-generic-unix-3.6.10.tar.xz
# 安装xz解压插件
yum -y install xz
# 解压xz文件
xz -d rabbitmq-server-generic-unix-3.6.10.tar.xz
# 解压tar文件
tar -xvf rabbitmq-server-generic-unix-3.6.10.tar
# 解压后移动到/opt/rabbitmq目录
mv rabbitmq_server-3.6.10 /opt/rabbitmq/
# 进入rabbitmq的可执行目录
cd /opt/rabbitmq/sbin
# 启动rabbitmq
# 直接启动使用
./rabbitmq-server
# 后台启动使用
./rabbit-server -detached
# 开启插件管理页面
./rabbitmq-plugins enable rabbitmq_management
# 创建用户密码
./rabbitmqctl add user admin 123
# 设置用户角色
./rabbitmqctl set_user_tags admin administrator
# 用户授权
./rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
# 访问
# 在浏览器输入ip+端口号
http://192.168.54.121:15672
# 关闭服务
./rabbitmqctl stop
<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.0modelVersion>
<packaging>pompackaging>
<modules>
<module>messagesmodule>
<module>consumermodule>
<module>publishermodule>
modules>
<parent>
<artifactId>spring-boot-parentartifactId>
<groupId>org.springframework.bootgroupId>
<version>2.1.10.RELEASEversion>
parent>
<groupId>cn.khuegroupId>
<artifactId>rabbitmq-test-01artifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-amqpartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
dependency>
dependencies>
project>
package cn.khue.message;
import java.io.Serializable;
import java.util.Objects;
public class MyMessage implements Serializable {
private Integer id;
private String remark;
private Object data;
public MyMessage(Integer id, String remark, Object data) {
this.id = id;
this.remark = remark;
this.data = data;
}
public MyMessage() {
}
@Override
public String toString() {
return "MyMessage{" +
"id=" + id +
", remark='" + remark + '\'' +
", data=" + data +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyMessage myMessage = (MyMessage) o;
return Objects.equals(id, myMessage.id) &&
Objects.equals(remark, myMessage.remark) &&
Objects.equals(data, myMessage.data);
}
@Override
public int hashCode() {
return Objects.hash(id, remark, data);
}
}
<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">
<parent>
<artifactId>rabbitmq-test-01artifactId>
<groupId>cn.khuegroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>consumerartifactId>
<dependencies>
<dependency>
<groupId>cn.khuegroupId>
<artifactId>messagesartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
project>
@SpringBootApplication
//导入spring-boot-starter-amqp,该注解自动生效(可以不写),但不会扫描@RabbitListener和@RabbitHandler等注解,薛涛提供@Component注解
//@EnableRabbit
public class ConsumerApp{
public static void main(String[] args){
SpringApplication.run(ConsumerApp.class,args);
}
}
# 自定义队列、交换器、路由键参数
mq.exchange.name=my-exchange
mq.queue.name=my-queue
mq.queue.routing-key=my-routing-key
# 配置RabbitMQ路径
spring.rabbitmq.host=192.168.54.121
spring.rabbitmq.port=5672
spring.rabbitmq.virtual-host=/
spring.rabbitmq.username=admin
spring.rabbitmq.password=123
package cn.khue.consumer;
import cn.khue.message.MyMessage;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;
@RabbitListener(bindings = {
@QueueBinding(
name = @Queue(
name = "${mq.queue.name}",
autoDelete = "false"),
exchange = @Exchange(
name = "${mq.exchange.name}",
type = "direct",
autoDelete = "false"),
key = "${mq.queue.routing-key}")
})
@Component
public class MyConsumer {
//处理消息
@RabbitHandler
public void message(MyMessage message){
System.out.println("\033[36m"+message+"\033[m");
}
}
<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">
<parent>
<artifactId>rabbitmq-test-01artifactId>
<groupId>cn.khuegroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>publisherartifactId>
<dependencies>
<dependency>
<groupId>cn.khuegroupId>
<artifactId>messagesartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
project>
package cn.khue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PublisherApp {
public static void main(String[] args) {
SpringApplication.run(PublisherApp.class,args);
}
}
# 配置交换器、路由键参数
mq.exchange.name=my-exchange
mq.queue.routing-key=my-routing-key
# 配置RabbitMQ路径
spring.rabbitmq.host=192.168.54.121
spring.rabbitmq.port=5672
spring.rabbitmq.virtual-host=/
spring.rabbitmq.username=admin
spring.rabbitmq.password=123
package cn.khue.publisher;
import cn.khue.message.MyMessage;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class MyPublisher {
@Autowired
private AmqpTemplate template;
@Value("${mq.exchange.name}")
//发送到哪一个交换器
private String exchangeName;
@Value("${mq.queue.routing-key}")
//路由键是什么
private String routingKey;
public void sendMessage(Integer id,String remark,Object data){
//创建消息主体
MyMessage message=new MyMessage(id,remark,data);
//发送消息
this.template.convertAndSend(exchangeName,routingKey,message);
}
}
package cn.khue.test;
import cn.khue.PublisherApp;
import cn.khue.publisher.MyPublisher;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {
PublisherApp.class})
public class PublisherAppTest {
@Autowired
private MyPublisher publisher;
@Test
public void sendTest(){
//输出10个消息
for (int i = 0; i <10 ; i++) {
this.publisher.sendMessage(i,"消息"+i,"内容是"+i);
}
}
}
RabbitMQ常用交换器有:direct交换器、topic交换器及fanout交换器
direct交换器是一种点对点、实现发布/订阅标准的交换器,根据路由键信息决定消息存储在哪个队列
topic交换器也叫主题交换器,或规则匹配交换器,它是通过自定义的模糊匹配规则来决定消息存储在哪些队列