linux安装rabbitmq_springcloud——rabbitmq

什么是 RabbitMQ

v2-644511d75b889764e26c21e5fb2bc8a8_b.png

安装rabbitmq(linux系统,<win系统管网下载exe文件进行安装即可>)

首先要更新wget

sudo yum -y install wget

安装OpenSSL环境

wget ftp://ftp.openssl.org/source/openss-1.0.0c.tar.gz

tar -zxv openssl-1.0.0c.tar.gz

cd openssl-1.0.0c/

./config --prefix=/usr/local --openssldir=/usr/local/ssl

make && make install

./config shared --prefix=/usr/local --openssldir=/usr/local/ssl

make clean

make && make install

下载安装文件

wget https://packages.erlang-solutions.com/erlang-solutions-1.0-1.noarch.rpm

rpm -Uvh erlang-solutions-1.0-1.noarch.rpm

安装 erlang

yum install erlang

安装完成后可以用 erl 命令查看是否安装成功

erl -version

安装 RabbitMQ Server

安装准备,下载 RabbitMQ Server

wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.5.1/rabbitmqserver-3.5.1-1.noarch.rpm(这个链接可能失效,到官网找到对应的版本的网址下载即可)

安装 RabbitMQ Server

rpm --import http://www.rabbitmq.com/rabbitmq-signing-key-public.asc

yum install rabbitmq-server-3.5.1-1.noarch.rpm

启动 RabbitMQ

配置为守护进程随系统自动启动,root 权限下执行:

chkconfig rabbitmq-server on

启动 rabbitMQ 服务

/sbin/service rabbitmq-server start

安装 Web 管理界面插件

安装命令

rabbitmq-plugins enable rabbitmq_management

——安装成功后会显示如下内容:

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 登录

这里我们以创建个 zc 帐号,密码 123456 为例,创建一个账号并支持远程 ip 访问

1创建账号

rabbitmqctl add_user zc 123456

2设置用户角色

rabbitmqctl set_user_tags oldlu administrator

3设置用户权限

rabbitmqctl set_permissions -p "/" oldlu ".*" ".*" ".*"

4设置完成后可以查看当前用户和角色(需要开启服务)

rabbitmqctl list_users

浏览器输入:serverip:15672。其中 serverip 是 RabbitMQ-Server 所在主机的 ip

消息队列基础知识

Provider:消息生产者,就是投递消息的程序

Consumer:消息消费者,就是接受消息的程序

没有使用消息队列时消息传递方式

v2-f9550e803ceb4702d16a73731b3d230d_b.png

使用消息队列后消息传递方式

v2-1e36d30abc7415064e84e98c626b759e_b.png

什么是队列?

队列就像存放了商品的仓库或者商店,是生产商品的工厂和购买商品的用户之间的中转站

队列里存储了什么?

在 rabbitMQ 中,信息流从你的应用程序出发,来到 Rabbitmq 的队列,所有信息可以只存储在一个队列中。队列可以存储很多信息,因为它基本上是一个无限制的缓冲区,前提是你的机器有足够的存储空间

队列和应用程序的关系?

多个生产者可以将消息发送到同一个队列中,多个消息者也可以只从同一个队列接收数据

入门案例

搭建项目环境

v2-b9d5203ab893e6beebb9c83a16e17f07_b.jpg

修改 pom 文件添加 RabbitMQ 坐标

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

修改全局配置文件,添加 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

创建队列

package com.bjsxt;

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//创建消息队列
@Configuration
public class QueueConfig {
	//创建队列
	@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;

//消息发送者
@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;
//消息接收者
@Component
public class Receiver {

	//接收消息的方法,采用消息队列监听机制
	@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 App {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

测试代码

package com.bjsxt.test;

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;

import com.bjsxt.App;
import com.bjsxt.Sender;

@RunWith(SpringRunner.class)
@SpringBootTest(classes=App.class)
public class QueueTest {

	@Autowired
	private Sender sender;
	
	//测试消息队列
	@Test
	public void test1() {
		this.sender.send("hello rabbitmq");
	}
}

运行原理:

v2-cbf851b8f32823a26a2df45282933ba5_b.jpg

v2-7c376aa189cfc374ce5b480c16b275cc_b.jpg

v2-d43a20634057d29779c3080bd1664f8e_b.jpg

v2-9984260d827434fe49f17553649b2a23_b.jpg

Rabbit 交换器

用来接收生产者发送的消息并将这些消息路由给服务器中的队列

常见的交换器类型有哪些?
direct、fanout、topic

什么是Routing-key?
路由键。RabbitMQ 决定消息该投递到哪个队列的规则、队列通过路由键绑定到交换器。消息发送到 MQ 服务器时,消息将拥有一个路由键,即便是空的,RabbitMQ 也会将其和绑定使用的路由键进行匹配、如果相匹配,消息将会投递到该队列、如果不匹配,消息将会进入黑洞

什么是Channel?
Channel 中文叫做信道,是 TCP 里面的虚拟链接。例如:电缆相当于 TCP,信道是一个独立光纤束,一条 TCP 连接上创建多条信道是没有问题的、TCP 一旦打开,就会创建 AMQP 信道、无论是发布消息、接收消息、订阅队列,这些动作都是通过信道完成的

交换器与队列的关系是什么?
交换器是通过路由键和队列绑定在一起的,如果消息拥有的路由键跟队列和交换器的路由键匹配,那么消息就会被路由到该绑定的队列中、也就是说,消息到队列的过程中,消息首先会经过交换器,接下来交换器在通过路由键匹配分发消息到具体的队列中、路由键可以理解为匹配的规则。

RabbitMQ为什么需要信道?为什么不使用TCP协议直接通信
TCP 的创建和销毁开销特别大。创建需要 3 次握手,销毁需要 4 次分手。
如果不用信道,那应用程序就会以 TCP 链接 Rabbit,高峰时每秒成千上万条链接会造成资源巨大的浪费,而且操作系统每秒处理 TCP 链接数也是有限制的,必定造成性能瓶颈
信道的原理是一条线程一条通道,多条线程多条通道同用一条 TCP 链接。一条 TCP链接可以容纳无限的信道,即使每秒成千上万的请求也不会成为性能的瓶颈

搭建环境:(以direct为例)

consumer配置文件:

spring.application.name=springcloud-mq
spring.rabbitmq.host=192.168.70.131
spring.rabbitmq.port=5672
spring.rabbitmq.username=oldlu
spring.rabbitmq.password=123456
#设置交换器的名称
mq.config.exchange=log.direct
#info 队列名称
mq.config.queue.info=log.info
#info 路由键
mq.config.queue.info.routing.key=log.info.routing.key
#error 队列名称
mq.config.queue.error=log.error
#error 路由键
mq.config.queue.error.routing.key=log.error.routing.key

Provider 的配置文件:

spring.application.name=springcloud-mq
spring.rabbitmq.host=192.168.70.131
spring.rabbitmq.port=5672
spring.rabbitmq.username=oldlu
spring.rabbitmq.password=123456
#设置交换器的名称
mq.config.exchange=log.direct
#info 路由键
mq.config.queue.info.routing.key=log.info.routing.key
#error 路由键
mq.config.queue.error.routing.key=log.error.routing.key
#error 队列名称
mq.config.queue.error=log.error

编写consumer的errorReceiver类:

package com.bjsxt;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 消息接收者
 * @author Administrator
 * @RabbitListener bindings:绑定队列
 * @QueueBinding  value:绑定队列的名称
 *                exchange:配置交换器
 * 
 * @Queue value:配置队列名称
 *        autoDelete:是否是一个可删除的临时队列
 * 
 * @Exchange value:为交换器起个名称
 *           type:指定具体的交换器类型
 */
@Component
@RabbitListener(
			bindings=@QueueBinding(
					value=@Queue(value="${mq.config.queue.error}",autoDelete="true"),
					exchange=@Exchange(value="${mq.config.exchange}",type=ExchangeTypes.DIRECT),
					key="${mq.config.queue.error.routing.key}"
			)
		)
public class ErrorReceiver {

	/**
	 * 接收消息的方法。采用消息队列监听机制
	 * @param msg
	 */
	@RabbitHandler
	public void process(String msg){
		System.out.println("Error..........receiver: "+msg);
	}
}

编写provider的消息发送类:

package com.bjsxt;

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;

/**
 * 消息发送者
 * @author Administrator
 *
 */
@Component
public class Sender {

	@Autowired
	private AmqpTemplate rabbitAmqpTemplate;
	
	//exchange 交换器名称
	@Value("${mq.config.exchange}")
	private String exchange;
	
	//routingkey 路由键
	@Value("${mq.config.queue.error.routing.key}")
	private String routingkey;
	/*
	 * 发送消息的方法
	 */
	public void send(String msg){
		//向消息队列发送消息
		//参数一:交换器名称。
		//参数二:路由键
		//参数三:消息
		this.rabbitAmqpTemplate.convertAndSend(this.exchange, this.routingkey, msg);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值