RabbitMQ Server 3.8.0使用指南
RabbitMQ Server 3.8.0使用指南
RabbitMQ Server 3.8.0是一个开源的消息队列软件,官方网站为https://www.rabbitmq.com,本文将为你讲解如何使用RabbitMQ Server 3.8.0实现消息通信。
一、安装RabbitMQ Server 3.8.0
RabbitMQ Server 3.8.0能支持跨平台而开发的服务器版本,可以安装在Linux(Fedora, RHEL, CentOS)、 macOS、Windows,核心的代码使用Erlang语言编写,不同平台的安装方式略有不同,下面我们以CentOS操作系统为例进行说明。
1.安装Erlang
sudo yum install erlang
2.安装RabbitMQ Server 3.8.0
sudo wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.0/rabbitmq-server-3.8.0-1.el7.noarch.rpm sudo rpm --import https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc sudo yum install rabbitmq-server-3.8.0-1.el7.noarch.rpm
3.启动RabbitMQ Server
sudo systemctl start rabbitmq-server.service
二、RabbitMQ Server 3.8.0的使用
1.连接到RabbitMQ Server
RabbitMQ Server的默认端口为5672,许多客户端和库都支持RabbitMQ Server与服务器之间的要求,下面是Python和Java两种语言的示例代码:
Python代码:
import pika connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel()
Java代码:
import java.io.IOException; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; public class Producer { private final static String QUEUE_NAME = "hello"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) { channel.queueDeclare(QUEUE_NAME, false, false, false, null); String message = "Hello World!"; channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8")); System.out.println(" [x] Sent '" + message + "'"); } } }
2.创建、发送和接收消息
RabbitMQ Server的基本概念是在Producers和Consumers之间传递消息,下面是创建,发送和接收消息的例子:
Python代码:
import pika connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel() channel.queue_declare(queue='hello') channel.basic_publish(exchange='', routing_key='hello', body='Hello World!') print(" [x] Sent 'Hello World!'")
Java代码:
import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.DeliverCallback; public class Consumer { private final static String QUEUE_NAME = "hello"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); DeliverCallback deliverCallback = (consumerTag, delivery) -> { String message = new String(delivery.getBody(), "UTF-8"); System.out.println(" [x] Received '" + message + "'"); }; channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { }); } }
三、集成RabbitMQ Server 3.8.0
RabbitMQ Server 3.8.0不仅能够实现生产者的功能,还可以与Spring Boot等框架进行集成,下面是使用Spring Boot集成RabbitMQ的例子:
spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest spring.rabbitmq.virtual-host=/
1.发送消息
@Autowired private AmqpTemplate rabbitTemplate; ... rabbitTemplate.convertAndSend("hello", message);
2.接收消息
@RabbitListener(queues = "hello") public void receive(String message) { System.out.println("Received message: " + message); }
四、安全性
RabbitMQ Server 3.8.0提供了许多安全性措施来确保服务器和客户端之间的安全通信,包括SSL、SASL和防止DDoS攻击等,下面是一些安全性示例:
1.SSL
ssl_options = { "ca_certs": "/path/to/ca_certificate", "ssl_certfile": "/path/to/client_certificate", "ssl_keyfile": "/path/to/client_key", "verify_hostname": False } connection_params = pika.ConnectionParameters(host="localhost", port=5671, ssl=True, ssl_options=ssl_options) connection = pika.BlockingConnection(connection_params)
2.SASL
credentials = pika.PlainCredentials("username", "password") connection_params = pika.ConnectionParameters(host="localhost", port=5672, credentials=credentials) connection = pika.BlockingConnection(connection_params)
3.DDoS攻击防范
sudo rabbitmqctl set_connection_backpressure_threshold
总结
本文对RabbitMQ Server 3.8.0进行了全面概述,包括安装与使用,集成Spring Boot,安全性等。在许多场景下,使用RabbitMQ Server 3.8.0都能极大地提升数据处理的速度和可靠性。
评论关闭