感興趣的胖友能夠體驗一哈新的閱讀地址:http://www.zhouhong.icu/post/142 (*^▽^*)
服務器IP
|
hostname
|
節點說明
|
端口
|
管控臺地址
|
192.168.2.121
|
zhouhong121
|
rabbitmq master
|
5672
|
http://192.168.2.121:15672
|
192.168.2.122
|
zhouhong122
|
rabbitmq slave
|
5672
|
http://192.168.2.122:15672
|
192.168.2.123
|
zhouhong123
|
rabbitmq slave
|
5672
|
http://192.168.2.123:15672
|
前提條件:修改12一、12二、123三臺服務器的 hostname 而且可使用hostname 兩兩之間 ping 通。
vim /etc/hostname
## 修改對應的名字,好比:
zhouhong121
vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.2.121 zhouhong121 192.168.2.122 zhouhong122 192.168.2.123 zhouhong123
rpm -ivh erlang-23.0.4-1.el7.x86_64.rpm rpm -ivh socat-1.7.3.2-5.el7.lux.x86_64.rpm rpm -ivh rabbitmq-server-3.8.5-1.el7.noarch.rpm
連接:https://pan.baidu.com/s/1diapYC19UlDy4G-4lgZWHA 提取碼:jf5r 複製這段內容後打開百度網盤手機App,操做更方便哦
1、安裝 rpm -ivh erlang-23.0.4-1.el7.x86_64.rpm rpm -ivh socat-1.7.3.2-5.el7.lux.x86_64.rpm rpm -ivh rabbitmq-server-3.8.5-1.el7.noarch.rpm 2、啓動 systemctl start rabbitmq-server 3、安裝web管控臺 rabbitmq-plugins enable rabbitmq_management 4、添加用戶 sudo rabbitmqctl add_user admin admin sudo rabbitmqctl set_user_tags admin administrator sudo rabbitmqctl set_permissions -p / admin "." "." ".*" 5、重啓 systemctl start rabbitmq-server rabbitmq-plugins enable rabbitmq_management
選擇12一、12二、123任意一個節點爲Master(這裏選擇71爲Master),也就是說咱們須要把121的Cookie文件同步到12二、123節點上去,進入/var/lib/rabbitmq目錄下,把/var/lib/rabbitmq/.erlang.cookie文件的權限修改成777,原來是400;而後把.erlang.cookie文件copy到各個節點下;最後把全部cookie文件權限還原爲400便可。
//進入目錄修改權限;遠程copy12二、123節點 cd /var/lib/rabbitmq/ chmod 777 /var/lib/rabbitmq/.erlang.cookie scp /var/lib/rabbitmq/.erlang.cookie 192.168.2.122:/var/lib/rabbitmq/ scp /var/lib/rabbitmq/.erlang.cookie 192.168.2.123:/var/lib/rabbitmq/ // 每臺服務器爲默認修改權限 chmod 400 /var/lib/rabbitmq/.erlang.cookie
rabbitmqctl stop
rabbitmq-server -detached
//注意作這個步驟的時候:須要配置/etc/hosts 必須相互可以尋址到 //在122節點上執行如下操做 rabbitmqctl stop_app rabbitmqctl join_cluster --ram rabbit@zhouhong121 rabbitmqctl start_app //一樣在123節點上執行如下操做 rabbitmqctl stop_app rabbitmqctl join_cluster rabbit@zhouhong121 rabbitmqctl start_app //在另外其餘節點上操做要移除的集羣節點 //rabbitmqctl forget_cluster_node rabbit@zhouhong12二、12二、123
rabbitmqctl set_cluster_name rabbitmq_cluster1
rabbitmqctl cluster_status
如圖:121爲dics 12二、123爲 RAMhtml
rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
將全部隊列設置爲鏡像隊列,即隊列會被複制到各個節點,各個節點狀態一致,RabbitMQ高可用集羣就已經搭建好了,咱們能夠重啓服務,查看其隊列是否在從節點同步。java
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zhouhong</groupId> <artifactId>rabbit-producer</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- springboot rabbitmq(amqp) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> </dependencies> </project>
server.servlet.context-path=/ server.port=8011 ## 鏡像隊列地址 spring.rabbitmq.addresses=192.168.2.121,192.168.2.122,192.168.2.123 spring.rabbitmq.username=admin spring.rabbitmq.password=admin ## 默認虛擬主機 spring.rabbitmq.virtual-host=/ ## 鏈接超時 spring.rabbitmq.connection-timeout=15000 ## 是否使用啓用消息確認模式(可靠性投遞) spring.rabbitmq.publisher-confirms=true ## 設置reture消息模式,注意要和mandatory一塊兒配合使用 ## spring.rabbitmq.publisher-returns=true ## spring.rabbitmq.template.mandatory=true spring.application.name=rabbit-producer spring.http.encoding.charset=UTF-8 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.time-zone=GMT+8 spring.jackson.default-property-inclusion=NON_NULL
package com.zhouhong.rabbit.producer.component; import java.util.Map; import java.util.UUID; import org.springframework.amqp.AmqpException; import org.springframework.amqp.core.MessagePostProcessor; import org.springframework.amqp.rabbit.connection.CorrelationData; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.core.RabbitTemplate.ConfirmCallback; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.messaging.Message; import org.springframework.messaging.MessageHeaders; import org.springframework.messaging.support.MessageBuilder; import org.springframework.stereotype.Component; @Component public class RabbbitSender { @Autowired private RabbitTemplate rabbitTemplate; /** * 這裏是確認消息的回調監聽接口,用於確認消息是否被 broker 所收到 */ final ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback() { /** * @param CorrelationData 做爲一個惟一的標識 * @param ack broker是否落盤成功 * @param cause 失敗的一些異常信息 */ @Override public void confirm(CorrelationData correlationData, boolean ack, String cause) { // TODO Auto-generated method stub } }; /** * 對外發送消息的方法 * @param massage 具體的消息內容 * @param properties 額外的屬性 * @throws Exception */ public void send(Object message, Map<String, Object> properties) throws Exception{ MessageHeaders mhs = new MessageHeaders(properties); Message<?> msg = MessageBuilder.createMessage(message, mhs); /** * 使用的是confirms模式,因此在發消息以前須要監控 */ rabbitTemplate.setConfirmCallback(confirmCallback); //指定業務惟一的ID CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString()); MessagePostProcessor mpp = new MessagePostProcessor() { @Override public org.springframework.amqp.core.Message postProcessMessage(org.springframework.amqp.core.Message message) throws AmqpException { System.out.println("post todo: "+ message); return message; } }; rabbitTemplate.convertAndSend("exchange-1", "rabbitmq.*", msg, correlationData); } }
server.servlet.context-path=/ server.port=8012 ## 鏡像隊列地址 spring.rabbitmq.addresses=192.168.2.121,192.168.2.122,192.168.2.123 spring.rabbitmq.username=admin spring.rabbitmq.password=admin ## 默認虛擬主機 spring.rabbitmq.virtual-host=/ ## 鏈接超時 spring.rabbitmq.connection-timeout=15000 ## 表示消費者消息消費成功之後,須要手工的進行簽收(ACK) 默認爲 auto spring.rabbitmq.listener.simple.acknowledge-mode=manual ## 線程數 spring.rabbitmq.listener.simple.concurrency=5 spring.rabbitmq.listener.simple.max-concurrency=10 ## 一條一條消費 spring.rabbitmq.listener.simple.prefetch=1 ## 最好不要在代碼裏寫死配置信息,儘可能使用這種方式也就是配置文件的方式 ## 在代碼裏使用 ${} 方式進行設置配置: ${spring.rabbitmq.listener.order.exchange.name} ## 交換機名稱 ## spring.rabbitmq.listener.order.exchange.name=order-exchange ## 是否持久化 ## spring.rabbitmq.listener.order.exchange.durable=true ## type 類型 ## spring.rabbitmq.listener.order.exchange.type=topic ## 規則 ## spring.rabbitmq.listener.order.exchange.key=rabbitmq.* spring.application.name=rabbit-producer spring.http.encoding.charset=UTF-8 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.time-zone=GMT+8 spring.jackson.default-property-inclusion=NON_NULL
package com.zhouhong.rabbit.consumer.component; 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.amqp.support.AmqpHeaders; import org.springframework.messaging.Message; import org.springframework.stereotype.Component; import com.rabbitmq.client.Channel; @Component public class RabbbitReceive { /** * 組合使用監聽 * @param message * @param channel * @throws Exception */ @RabbitListener(bindings = @QueueBinding( value = @Queue(value = "queue-1", durable = "true"), exchange = @Exchange(name = "exchange-1", durable = "true", type = "topic", ignoreDeclarationExceptions = "true"), key = "rabbitmq.*" ) ) @RabbitHandler public void onMessage(Message message, Channel channel) throws Exception { //一、收到消息之後進行業務端消費處理 System.err.println("======================"); System.err.println("消息消費:" + message.getPayload()); //二、處理成功以後獲取deliveryTay 而且進行手工的ACK操做,由於咱們配置文件裏面配置的是手工簽收 Long deliveryTay = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG); channel.basicAck(deliveryTay, false); } }
package com.zhouhong.rabbit.producer.test; import java.util.HashMap; import java.util.Map; 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.zhouhong.rabbit.producer.component.RabbbitSender; @RunWith(SpringRunner.class) @SpringBootTest public class ApplicationTest { @Autowired private RabbbitSender rabbbitSender; @Test public void testSender() throws Exception{ Map<String , Object> properties = new HashMap<String, Object>(); properties.put("key1", "你好呀,RabbitMQ!!"); properties.put("key2", "你好呀,Kafka!!"); rabbbitSender.send("rabbitmq-test", properties); Thread.sleep(10000); } }
創建了一個咱們代碼裏面指定的交換機 exchange-1,而且綁定了咱們指定的隊列queue-1,路由規則爲 rabbitmq.*node
咱們發現會有一條未消費的消息。web