一、 什么是 RabbitMQ
二、 安装 RabbitMQ
系统版本:CentOS 6.5
RabbitMQ-Server:3.5.1
一、安装erlang
1.安装准备,下载安装文件
wget https://packages.erlang-solutions.com/erlang-solutions-1.0-1.noarch.rpm
rpm -Uvh erlang-solutions-1.0-1.noarch.rpm
修改primary.xml.gz的sha的加密值
cd /var/cache/yum/x86_64/6/erlang-solutions
sha1sum primary.xml.gz
vim repomd.xml
修改
<data type="primary">
<checksum type="sha">结果为sha1sum命令结果</checksum>
2.安装erlang
yum install erlang
3.安装完成后可以用erl命令查看是否安装成功
erl -version
二、安装RabbitMQ Server
1.安装准备,下载RabbitMQ Server
wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.5.1/rabbitmq-server-3.5.1-1.noarch.rpm
2.安装RabbitMQ Server
rpm --import http://www.rabbitmq.com/rabbitmq-signing-key-public.asc
yum install rabbitmq-server-3.5.1-1.noarch.rpm
三、启动RabbitMQ
1.配置为守护进程随系统自动启动,root权限下执行:
chkconfig rabbitmq-server on
2.启动rabbitMQ服务
/sbin/service rabbitmq-server start
四、安装Web管理界面插件
1.安装命令
rabbitmq-plugins enable rabbitmq_management
2.安装成功后会显示如下内容
The following plugins have been enabled:
mochiweb
webmachine
rabbitmq_web_dispatch
amqp_client
rabbitmq_management_agent
rabbitmq_management
Plugin configuration has changed. Restart RabbitMQ for changes to take effect.
五、设置RabbitMQ远程ip登录
这里我们以创建个oldlu帐号,密码123456为例,创建一个账号并支持远程ip访问。
1.创建账号
rabbitmqctl add_user oldlu 123456
2.设置用户角色
rabbitmqctl set_user_tags oldlu administrator
3.设置用户权限
rabbitmqctl set_permissions -p "/" oldlu ".*" ".*" ".*"
4.设置完成后可以查看当前用户和角色(需要开启服务)
rabbitmqctl list_users
浏览器输入:serverip:15672。其中serverip是RabbitMQ-Server所在主机的ip。
三、 为什么要使用 RabbitMQ?他解决了什么问题?
四、 消息队列基础知识
五、 编写 RabbitMQ 的入门案例
1 搭建项目环境
1.2修改 pom 文件添加 RabbitMQ 坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
1.3修改全局配置文件,添加 RabbitMQ 相关的配置
spring.application.name=springcloud-mq
spring.rabbitmq.host=192.168.70.131
spring.rabbitmq.port=5672
spring.rabbitmq.username=oldlu
spring.rabbitmq.password=123456
2 编写代码
创建队列
package com.bjsxt;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 创建消息队列
* @author Administrator
*
*/
@Configuration
public class QueueConfig {
/**
* 创建队列
* @return
*/
@Bean
public Queue createQueue(){
return new Queue("hello-queue");
}
}
创建消息提供者
package com.bjsxt;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 消息发送者
* @author Administrator
*
*/
@Component
public class Sender {
@Autowired
private AmqpTemplate rabbitAmqpTemplate;
/*
* 发送消息的方法
*/
public void send(String msg){
//向消息队列发送消息
//参数一:队列的名称。
//参数二:消息
this.rabbitAmqpTemplate.convertAndSend("hello-queue", msg);
}
}
消息接收者
package com.bjsxt;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 消息接收者
* @author Administrator
*
*/
@Component
public class Receiver {
/**
* 接收消息的方法。采用消息队列监听机制
* @param msg
*/
@RabbitListener(queues="hello-queue")
public void process(String msg){
System.out.println("receiver: "+msg);
}
}
启动类
package com.bjsxt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootServerApplication.class, args);
}
}
测试代码
/**
* 消息队列测试类
* @author Administrator
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes=SpringbootServerApplication.class)
public class QueueTest {
@Autowired
private Sender sender;
/*
* 测试消息队列
*/
@Test
public void test1(){
this.sender.send("Hello RabbitMQ");
}
}
六、 RabbitMQ 原理图