河北網(wǎng)站建設(shè)排名優(yōu)化哪家專業(yè)
【RocketMQ入門-安裝部署與Java API測試】
- 一、環(huán)境說明
- 二、安裝部署
- 三、Java API 編寫Producer和Consumer進(jìn)行測試
- 四、小結(jié)
一、環(huán)境說明
- 虛擬機(jī)VWMare:安裝centos7.6操作系統(tǒng)
- 源碼包:rocketmq-all-5.1.3-source-release.zip
- 單master部署,在一臺(tái)虛擬機(jī)上安裝部署name server和proxy以及broker
- 流程圖:
二、安裝部署
-
源碼包安裝需要事先安裝部署maven,下載apache-maven-3.6.3-bin.tar.gz安裝包,然后解壓并配置環(huán)境變量,如下命令:
tar -zvxf apache-maven-3.6.3-bin.tar.gz -C /training/
配置環(huán)境變量(此處是用root安裝),編輯:
vi ~/.bash_profile
,在文件末尾添加如下內(nèi)容:#maven export MVN_HOME=/training/apache-maven-3.6.3 export PATH=$MVN_HOME/bin:$PATH
執(zhí)行:
source ~/.bash_profile
使環(huán)境生效。 -
進(jìn)入/training/apache-maven-3.6.3/conf目錄下,配置maven的倉庫為阿里云和華為云倉庫,執(zhí)行如下命令:
cd /training/apache-maven-3.6.3/conf/ mv settings.xml settings.xml.backup vi settings.xml
在打開的settings.xml中,粘貼如下內(nèi)容即可:
<?xml version="1.0" encoding="utf-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation=" http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"><mirrors><mirror><id>aliyunmaven</id><mirrorOf>*</mirrorOf><name>阿里云公共倉庫</name><url>https://maven.aliyun.com/repository/public</url></mirror><mirror><id>huaweicloud</id><mirrorOf>central</mirrorOf><name>huaweicloud maven</name><url>https://mirrors.huaweicloud.com/repository/maven/</url></mirror></mirrors><profiles><profile><repositories><repository><id>central</id><url>https://maven.aliyun.com/repository/central</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository></repositories></profile></profiles> </settings>
-
由于CentOS7.6最小模式安裝沒有unzip命令,需要事先安裝,執(zhí)行如下命令安裝:
yum install unzip -y
-
解壓源碼包rocketmq-all-5.1.3-source-release.zip,進(jìn)入到解壓后的目錄下,然后編譯安裝,執(zhí)行如下命令:
unzip rocketmq-all-5.1.3-source-release.zip cd rocketmq-all-5.1.3-source-release/ mvn -Prelease-all -DskipTests -Dspotbugs.skip=true clean install -U
-
第5步驟正確后,進(jìn)入到 /rocketmq-all-5.1.3-source-release/distribution/target/rocketmq-5.1.3/rocketmq-5.1.3目錄下,然后啟動(dòng)NameServer,執(zhí)行如下命令:
cd /root/rocketmq-all-5.1.3-source-release/distribution/target/rocketmq-5.1.3/rocketmq-5.1.3 nohup sh bin/mqnamesrv &
-
驗(yàn)證NameServer是否啟動(dòng)成功,執(zhí)行如下命令:
tail -f ~/logs/rocketmqlogs/namesrv.log
會(huì)看到如下內(nèi)容,說明已經(jīng)正常啟動(dòng)了
The Name Server boot success. serializeType=JSON, address 0.0.0.0:9876
或者執(zhí)行jps命令查看是否已經(jīng)有了NameServer進(jìn)程:NamesrvStartup
,如有說明ok -
第5、6步驟正確后,進(jìn)入到 /rocketmq-all-5.1.3-source-release/distribution/target/rocketmq-5.1.3/rocketmq-5.1.3目錄下,然后啟動(dòng)Broker和Proxy,執(zhí)行如下命令:
注意:
NameServer成功啟動(dòng)后,我們啟動(dòng)Broker和Proxy,5.x 版本下我們建議使用 Local 模式部署,即 Broker 和 Proxy 同進(jìn)程部署。5.x 版本也支持 Broker 和 Proxy 分離部署以實(shí)現(xiàn)更靈活的集群能力。詳情參考其他教程。cd /root/rocketmq-all-5.1.3-source-release/distribution/target/rocketmq-5.1.3/rocketmq-5.1.3 nohup sh bin/mqbroker -n localhost:9876 --enable-proxy &
-
驗(yàn)證NameServer是否啟動(dòng)成功,執(zhí)行如下命令:
tail -f ~/logs/rocketmqlogs/proxy.log
會(huì)看到如下內(nèi)容,說明已經(jīng)正常啟動(dòng)了
The broker[broker-a, 192.168.36.132:10911] boot success. serializeType=JSON and name server is localhost:9876
或者執(zhí)行jps命令查看是否已經(jīng)有了:ProxyStartup
進(jìn)程,如有說明ok
三、Java API 編寫Producer和Consumer進(jìn)行測試
- 上述正常啟動(dòng)NameServer和Broker及Proxy后,首先需要?jiǎng)?chuàng)建名為
TestTopic
的Topic,執(zhí)行如下命令:
查看新創(chuàng)建的Topic,驗(yàn)證是否已經(jīng)創(chuàng)建好,執(zhí)行:cd /root/rocketmq-all-5.1.3-source-release/distribution/target/rocketmq-5.1.3/rocketmq-5.1.3 sh bin/mqadmin updatetopic -n localhost:9876 -t TestTopic -c DefaultCluster
結(jié)果如下:sh bin/mqadmin topicList -n localhost:9876
- 創(chuàng)建消費(fèi)者組,執(zhí)行如下命令:
執(zhí)行命令無任何錯(cuò)誤即說明已經(jīng)創(chuàng)建成功。cd /root/rocketmq-all-5.1.3-source-release/distribution/target/rocketmq-5.1.3/rocketmq-5.1.3 sh bin/sh mqadmin updateSubGroup -g testgroup -c DefaultCluster -n localhost:9876
- 在Idea中創(chuàng)建Maven工程,添加rocketmq依賴,添加如下依賴到pom.xml中:
<properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><rocketmq-client-java-version>5.0.5</rocketmq-client-java-version><slf4j.version>1.7.25</slf4j.version> </properties><dependencies><dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-client-java</artifactId><version>${rocketmq-client-java-version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${slf4j.version}</version></dependency> </dependencies>
- 編寫ProducerTest生產(chǎn)者,代碼如下:
import org.apache.rocketmq.client.apis.ClientConfiguration; import org.apache.rocketmq.client.apis.ClientConfigurationBuilder; import org.apache.rocketmq.client.apis.ClientException; import org.apache.rocketmq.client.apis.ClientServiceProvider; import org.apache.rocketmq.client.apis.message.Message; import org.apache.rocketmq.client.apis.producer.Producer; import org.apache.rocketmq.client.apis.producer.SendReceipt; import org.slf4j.Logger; import org.slf4j.LoggerFactory;import java.io.IOException;public class ProducerTest {private static final Logger logger = LoggerFactory.getLogger(ProducerTest.class);public static void main(String[] args) throws Exception {testMain();}public static void testMain() throws ClientException, IOException {// 接入點(diǎn)地址,需要設(shè)置成Proxy的地址和端口列表,一般是xxx:8081;xxx:8081。String endpoint = "192.168.36.132:8081";// 消息發(fā)送的目標(biāo)Topic名稱,需要提前創(chuàng)建。// 執(zhí)行:sh bin/mqadmin updatetopic -n localhost:9876 -t TestTopic -c DefaultClusterString topic = "TestTopic";ClientServiceProvider provider = ClientServiceProvider.loadService();ClientConfigurationBuilder builder = ClientConfiguration.newBuilder().setEndpoints(endpoint);ClientConfiguration configuration = builder.build();// 初始化Producer時(shí)需要設(shè)置通信配置以及預(yù)綁定的Topic。Producer producer = provider.newProducerBuilder().setTopics(topic).setClientConfiguration(configuration).build();int temp = 0;while (true) {String msg = "第 " + temp + " 條消息,我喜歡rocketmq!!";temp++;// 普通消息發(fā)送。Message message = provider.newMessageBuilder().setTopic(topic)// 設(shè)置消息索引鍵,可根據(jù)關(guān)鍵字精確查找某條消息。.setKeys("messageKey")// 設(shè)置消息Tag,用于消費(fèi)端根據(jù)指定Tag過濾消息。.setTag("messageTag")// 消息體。.setBody(msg.getBytes()).build();try {// 發(fā)送消息,需要關(guān)注發(fā)送結(jié)果,并捕獲失敗等異常。SendReceipt sendReceipt = producer.send(message);Thread.sleep(1000);logger.info("Send message successfully, messageId={}", sendReceipt.getMessageId());} catch (Exception e) {logger.error("Failed to send message", e);}}// producer.close();} }
- 編寫CommonUtils工具類,用于將ByteBuffer轉(zhuǎn)成String,代碼如下:
import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets;public class CommonUtils {public static void main(String[] args) {System.out.println("Hello world!");}public static String decodeKey(ByteBuffer bytes) {Charset charset = StandardCharsets.UTF_8;return charset.decode(bytes).toString();}public static byte[] decodeValue(ByteBuffer bytes) {int len = bytes.limit() - bytes.position();byte[] bytes1 = new byte[len];bytes.get(bytes1);return bytes1;}public static ByteBuffer encodeKey(String key) {return ByteBuffer.wrap(key.getBytes(StandardCharsets.UTF_8));}public static ByteBuffer encodeValue(byte[] value) {ByteBuffer byteBuffer = ByteBuffer.allocate(value.length);byteBuffer.clear();byteBuffer.get(value, 0, value.length);return byteBuffer;} }
- 編寫ConsumerTest生產(chǎn)者,代碼如下:
import java.util.Collections; import org.apache.rocketmq.client.apis.ClientConfiguration; import org.apache.rocketmq.client.apis.ClientServiceProvider; import org.apache.rocketmq.client.apis.consumer.ConsumeResult; import org.apache.rocketmq.client.apis.consumer.FilterExpression; import org.apache.rocketmq.client.apis.consumer.FilterExpressionType; import org.apache.rocketmq.client.apis.consumer.PushConsumer; import org.rocketmq.producer.CommonUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory;public class PushConsumerTest {private static final Logger logger = LoggerFactory.getLogger(PushConsumerTest.class);private PushConsumerTest() {}public static void main(String[] args) throws Exception {final ClientServiceProvider provider = ClientServiceProvider.loadService();// 接入點(diǎn)地址,需要設(shè)置成Proxy的地址和端口列表,一般是xxx:8081;xxx:8081。String endpoints = "192.168.36.132:8081";ClientConfiguration clientConfiguration = ClientConfiguration.newBuilder().setEndpoints(endpoints).build();// 訂閱消息的過濾規(guī)則,表示訂閱所有Tag的消息。String tag = "*";FilterExpression filterExpression = new FilterExpression(tag, FilterExpressionType.TAG);// 為消費(fèi)者指定所屬的消費(fèi)者分組,Group需要提前創(chuàng)建。// 執(zhí)行:sh bin/sh mqadmin updateSubGroup -g testgroup -c DefaultCluster -n localhost:9876String consumerGroup = "testgroup";// 指定需要訂閱哪個(gè)目標(biāo)Topic,Topic需要提前創(chuàng)建。String topic = "TestTopic";// 初始化PushConsumer,需要綁定消費(fèi)者分組ConsumerGroup、通信參數(shù)以及訂閱關(guān)系。PushConsumer pushConsumer = provider.newPushConsumerBuilder().setClientConfiguration(clientConfiguration)// 設(shè)置消費(fèi)者分組。.setConsumerGroup(consumerGroup)// 設(shè)置預(yù)綁定的訂閱關(guān)系。.setSubscriptionExpressions(Collections.singletonMap(topic, filterExpression))// 設(shè)置消費(fèi)監(jiān)聽器。.setMessageListener(messageView -> {// 處理消息并返回消費(fèi)結(jié)果。logger.info("Consume message successfully, messageId={},messageBody={}", messageView.getMessageId(), CommonUtils.decodeKey(messageView.getBody()));return ConsumeResult.SUCCESS;}).build();Thread.sleep(Long.MAX_VALUE);// 如果不需要再使用 PushConsumer,可關(guān)閉該實(shí)例。// pushConsumer.close();} }
- 為了能查看到控制臺(tái)日志輸入,需要在resources目錄下新建log4j.properties、log4j2.properties,具體內(nèi)容如下:
log4j.properties
內(nèi)容:log4j.rootLogger=INFO,consolelog4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.target=System.out log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j2.properties
內(nèi)容:name = PropertiesConfig property.filename = target/logs#appenders = console, file #配置值是appender的類型,并不是具體appender實(shí)例的name appenders = rollingappender.rolling.type = RollingFile appender.rolling.name = RollingLogFile appender.rolling.fileName=${filename}/automationlogs.log appender.rolling.filePattern = ${filename}/automationlogs-%d{MM-dd-yy-HH-mm-ss}-%i.log appender.rolling.layout.type = PatternLayout appender.rolling.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n appender.rolling.policies.type = Policies appender.rolling.policies.size.type = SizeBasedTriggeringPolicy appender.rolling.policies.size.size=100MB appender.rolling.strategy.type = DefaultRolloverStrategy appender.rolling.strategy.max = 5rootLogger.level = INFO,console rootLogger.appenderRef.rolling.ref = RollingLogFile
- 到此,完成了所有準(zhǔn)備工作了,整個(gè)工程如下所示:
- 運(yùn)行ProducerTest程序進(jìn)行消息的發(fā)送,控制臺(tái)中會(huì)看到如下內(nèi)容:
- 運(yùn)行ConsumerTest程序接收消息,控制臺(tái)中會(huì)看到如下內(nèi)容:
四、小結(jié)
至此,一個(gè)單節(jié)點(diǎn)副本的 RocketMQ 集群已經(jīng)部署起來了,我們也通過編寫Java程序進(jìn)行簡單的消息收發(fā)。如本文對您有幫助,麻煩您動(dòng)動(dòng)發(fā)財(cái)?shù)氖种更c(diǎn)個(gè)贊~~~~~,謝謝您的閱讀!!!