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都能極大地提升數據處理的速度和可靠性。
原創文章,作者:YOCNM,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/373961.html