栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

RocketMQ

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

RocketMQ

1. 设置网络 主机 时间同步

[root@localhost ~]# vi /etc/sysconfig/network
[root@localhost ~]# vi /etc/hosts
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
[root@localhost ~]# date
Sun Jun 12 14:42:31 UTC 2022
[root@localhost ~]# yum install ntp -y                                                    

Complete!
[root@localhost ~]# vi /etc/ntp.conf
[root@localhost ~]# service ntpd start
Redirecting to /bin/systemctl start ntpd.service
[root@localhost ~]# chkconfig ntpd on
Note: Forwarding request to 'systemctl enable ntpd.service'.
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
[root@localhost ~]# date
Sun Jun 12 14:45:16 UTC 2022

2. jdk安装

[root@localhost bin]# cd /soft
[root@localhost soft]# ll
total 198368
-rw-r--r--.  1 root root 170023183 Jun 12 11:39 jdk-8u181-linux-x64.rpm
drwxr-xr-x.  6 root root       103 Feb 22 01:25 rocketmq-4.9.3
-rw-r--r--.  1 root root  26892203 Feb 26 17:10 rocketmq-all-4.9.3-bin-release.zip
[root@localhost soft]# rpm -i jdk-8u181-linux-x64.rpm 
warning: jdk-8u181-linux-x64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
Unpacking JAR files...
    tools.jar...
    plugin.jar...
    javaws.jar...
    deploy.jar...
    rt.jar...
    jsse.jar...
    charsets.jar...
    localedata.jar...
[root@localhost soft]# cd /usr/java
[root@localhost java]# ll
total 0
lrwxrwxrwx. 1 root root  16 Jun 12 12:38 default -> /usr/java/latest
drwxr-xr-x. 8 root root 258 Jun 12 12:38 jdk1.8.0_181-amd64
lrwxrwxrwx. 1 root root  28 Jun 12 12:38 latest -> /usr/java/jdk1.8.0_181-amd64
[root@localhost java]# cd default/
[root@localhost default]# ll
total 25976
drwxr-xr-x. 2 root root     4096 Jun 12 12:38 bin
-rw-r--r--. 1 root root     3244 Jul  7  2018 COPYRIGHT
drwxr-xr-x. 3 root root      132 Jun 12 12:38 include
-rw-r--r--. 1 root root  5207314 Jun 27  2018 javafx-src.zip
drwxr-xr-x. 5 root root      185 Jun 12 12:38 jre
drwxr-xr-x. 5 root root      245 Jun 12 12:38 lib
-rw-r--r--. 1 root root       40 Jul  7  2018 LICENSE
drwxr-xr-x. 4 root root       47 Jun 12 12:38 man
-rw-r--r--. 1 root root      159 Jul  7  2018 README.html
-rw-r--r--. 1 root root      424 Jul  7  2018 release
-rw-r--r--. 1 root root 21099920 Jul  7  2018 src.zip
-rw-r--r--. 1 root root   106782 Jun 27  2018 THIRDPARTYLICENSEREADME-JAVAFX.txt
-rw-r--r--. 1 root root   154222 Jul  7  2018 THIRDPARTYLICENSEREADME.txt
[root@localhost default]# vi /etc/profile
[root@localhost default]# echo $PATH
/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/soft/redis5/bin:/root/bin
[root@localhost default]# source /etc/profile
[root@localhost default]# echo $PATH
/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/soft/redis5/bin:/root/bin:/opt/soft/redis5/bin:/usr/java/default/bin
[root@localhost default]# java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
 

3. RocketMQ 3.1 namesrv

[root@localhost default]# cd /soft
[root@localhost soft]# ll
total 198368
-rw-r--r--.  1 root root 170023183 Jun 12 11:39 jdk-8u181-linux-x64.rpm
drwxr-xr-x.  6 root root       103 Feb 22 01:25 rocketmq-4.9.3
-rw-r--r--.  1 root root   3722978 Feb 26 17:10 rocketmq-all-4.9.3-source-release.zip
[root@localhost soft]# cd rocketmq-4.9.3/
[root@localhost rocketmq-4.9.3]# ll
total 40
drwxr-xr-x. 2 root root   126 Feb 22 01:25 benchmark
drwxr-xr-x. 3 root root  4096 Jun 12 12:37 bin
drwxr-xr-x. 7 root root   201 Feb 22 00:25 conf
drwxr-xr-x. 2 root root  4096 Feb 22 01:25 lib
-rw-r--r--. 1 root root 17327 Feb 22 00:25 LICENSE
-rw-r--r--. 1 root root  1338 Feb 22 00:25 NOTICE
-rw-r--r--. 1 root root  6069 Feb 22 00:25 README.md
[root@localhost rocketmq-4.9.3]# cd bin
[root@localhost bin]# ll
total 116
-rwxr-xr-x. 1 root root 1654 Feb 22 00:25 cachedog.sh
-rwxr-xr-x. 1 root root  845 Feb 22 00:25 cleancache.sh
-rwxr-xr-x. 1 root root 1116 Feb 22 00:25 cleancache.v1.sh
drwxr-xr-x. 2 root root   25 Feb 22 00:25 dledger
-rwxr-xr-x. 1 root root 2523 Feb 22 00:25 export.sh
-rwxr-xr-x. 1 root root 1400 Feb 22 00:25 mqadmin
-rwxr-xr-x. 1 root root 1029 Feb 22 00:25 mqadmin.cmd
-rwxr-xr-x. 1 root root 1394 Feb 22 00:25 mqbroker
-rwxr-xr-x. 1 root root 1084 Feb 22 00:25 mqbroker.cmd
-rwxr-xr-x. 1 root root 1373 Feb 22 00:25 mqbroker.numanode0
-rwxr-xr-x. 1 root root 1373 Feb 22 00:25 mqbroker.numanode1
-rwxr-xr-x. 1 root root 1373 Feb 22 00:25 mqbroker.numanode2
-rwxr-xr-x. 1 root root 1373 Feb 22 00:25 mqbroker.numanode3
-rwxr-xr-x. 1 root root 1396 Feb 22 00:25 mqnamesrv
-rwxr-xr-x. 1 root root 1088 Feb 22 00:25 mqnamesrv.cmd
-rwxr-xr-x. 1 root root 1571 Feb 22 00:25 mqshutdown
-rwxr-xr-x. 1 root root 1398 Feb 22 00:25 mqshutdown.cmd
-rwxr-xr-x. 1 root root 2222 Feb 22 00:25 os.sh
-rwxr-xr-x. 1 root root 1148 Feb 22 00:25 play.cmd
-rwxr-xr-x. 1 root root 1008 Feb 22 00:25 play.sh
-rwxr-xr-x. 1 root root  772 Feb 22 00:25 README.md
-rwxr-xr-x. 1 root root 2159 Feb 22 00:25 runbroker.cmd
-rwxr-xr-x. 1 root root 4528 Feb 22 00:25 runbroker.sh
-rwxr-xr-x. 1 root root 1799 Feb 22 00:25 runserver.cmd
-rwxr-xr-x. 1 root root 4093 Feb 22 00:25 runserver.sh
-rwxr-xr-x. 1 root root 1156 Feb 22 00:25 setcache.sh
-rwxr-xr-x. 1 root root 1408 Feb 22 00:25 startfsrv.sh
-rwxr-xr-x. 1 root root 1543 Feb 22 00:25 tools.cmd
-rwxr-xr-x. 1 root root 1820 Feb 22 00:25 tools.sh
[root@localhost bin]# ./mqnamesrv
Java HotSpot(TM) 64-Bit Server VM warning: Using the DefNew young collector with the CMS collector is deprecated and will likely be removed in a future release
Java HotSpot(TM) 64-Bit Server VM warning: UseCMSCompactAtFullCollection is deprecated and will likely be removed in a future release.
The Name Server boot success. serializeType=JSON
 

3.2 broker

[root@localhost ~]# jps
2502 Jps
2430 NamesrvStartup
[root@localhost ~]# netstat -natp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      680/redis-server 12 
tcp        0      0 127.0.0.1:6380          0.0.0.0:*               LISTEN      683/redis-server 12 
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      398/rpcbind         
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      672/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      889/master          
tcp        0      0 192.168.56.200:22       192.168.56.1:54513      ESTABLISHED 924/sshd: vagrant [ 
tcp        0     52 192.168.56.200:22       192.168.56.1:65247      ESTABLISHED 1254/sshd: vagrant  
tcp6       0      0 :::111                  :::*                    LISTEN      398/rpcbind         
tcp6       0      0 :::9876                 :::*                    LISTEN      2430/java           
tcp6       0      0 :::22                   :::*                    LISTEN      672/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      889/master          
[root@localhost ~]# cd /soft/rocketmq-4.9.3/
[root@localhost rocketmq-4.9.3]# ll
total 40
drwxr-xr-x. 2 root root   126 Feb 22 01:25 benchmark
drwxr-xr-x. 3 root root  4096 Jun 12 12:37 bin
drwxr-xr-x. 7 root root   201 Feb 22 00:25 conf
drwxr-xr-x. 2 root root  4096 Feb 22 01:25 lib
-rw-r--r--. 1 root root 17327 Feb 22 00:25 LICENSE
-rw-r--r--. 1 root root  1338 Feb 22 00:25 NOTICE
-rw-r--r--. 1 root root  6069 Feb 22 00:25 README.md
[root@localhost rocketmq-4.9.3]# cd bin
[root@localhost bin]# ./mqbroker -n localhost:9876 --> 注册到本机namesrv
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000005c0000000, 8589934592, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 8589934592 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /soft/rocketmq-4.9.3/bin/hs_err_pid2773.log
[root@localhost bin]# cat mqbroker
#!/bin/sh

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

if [ -z "$ROCKETMQ_HOME" ] ; then
  ## resolve links - $0 may be a link to maven's home
  PRG="$0"

  # need this for relative symlinks
  while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> (.*)$'`
    if expr "$link" : '/.*' > /dev/null; then
      PRG="$link"
    else
      PRG="`dirname "$PRG"`/$link"
    fi
  done

  saveddir=`pwd`

  ROCKETMQ_HOME=`dirname "$PRG"`/..

  # make it fully qualified
  ROCKETMQ_HOME=`cd "$ROCKETMQ_HOME" && pwd`

  cd "$saveddir"
fi

export ROCKETMQ_HOME

sh ${ROCKETMQ_HOME}/bin/runbroker.sh org.apache.rocketmq.broker.BrokerStartup $@
[root@localhost bin]# vi runbroker.sh  --> 本地测试调整最大内存为1g
[root@localhost bin]# ./mqbroker -n localhost:9876
The broker[localhost.localdomain, 10.0.2.15:10911] boot success. serializeType=JSON and name server is localhost:9876
 

3.3 观察

[root@localhost ~]# jps
3095 Jps
2430 NamesrvStartup
2895 BrokerStartup
[root@localhost ~]# netstat -natp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      680/redis-server 12 
tcp        0      0 127.0.0.1:6380          0.0.0.0:*               LISTEN      683/redis-server 12 
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      398/rpcbind         
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      672/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      889/master          
tcp        0      0 192.168.56.200:22       192.168.56.1:54513      ESTABLISHED 924/sshd: vagrant [ 
tcp        0     52 192.168.56.200:22       192.168.56.1:56264      ESTABLISHED 3019/sshd: vagrant  
tcp        0      0 192.168.56.200:22       192.168.56.1:65247      ESTABLISHED 1254/sshd: vagrant  
tcp6       0      0 :::111                  :::*                    LISTEN      398/rpcbind         
tcp6       0      0 :::9876                 :::*                    LISTEN      2430/java           
tcp6       0      0 :::22                   :::*                    LISTEN      672/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      889/master          
tcp6       0      0 :::10909                :::*                    LISTEN      2895/java           
tcp6       0      0 :::10911                :::*                    LISTEN      2895/java           
tcp6       0      0 :::10912                :::*                    LISTEN      2895/java           
tcp6       0      0 127.0.0.1:33422         127.0.0.1:9876          ESTABLISHED 2895/java           
tcp6       0      0 127.0.0.1:9876          127.0.0.1:33422         ESTABLISHED 2430/java         

4 . 192.168.56.201 同理

[root@localhost bin]# ./mqnamesrv
Java HotSpot(TM) 64-Bit Server VM warning: Using the DefNew young collector with the CMS collector is deprecated and will likely be removed in a future release
Java HotSpot(TM) 64-Bit Server VM warning: UseCMSCompactAtFullCollection is deprecated and will likely be removed in a future release.
The Name Server boot success. serializeType=JSON
 

[root@localhost ~]# cd /soft/rocketmq-4.9.3/bin
[root@localhost bin]# ./mqbroker -n 192.168.56.200:6379
The broker[localhost.localdomain, 10.0.2.15:10911] boot success. serializeType=JSON and name server is 192.168.56.200:6379
 

5. commitlog store

[root@localhost soft]# cd ~
[root@localhost ~]# ll
total 20
-rw-------. 1 root root 5570 Apr 30  2020 anaconda-ks.cfg
drwxr-xr-x. 3 root root   26 Jun 12 12:54 logs
-rw-------. 1 root root 5300 Apr 30  2020 original-ks.cfg
drwxr-xr-x. 5 root root  100 Jun 12 13:02 store
-rwxr-xr-x. 1 root root   73 Jun 11 04:15 test.sh
[root@localhost ~]# cd logs
[root@localhost logs]# ll
total 4
drwxr-xr-x. 2 root root 4096 Jun 12 13:02 rocketmqlogs
[root@localhost logs]# cd ~
[root@localhost ~]# cd store
[root@localhost store]# ll
total 8
-rw-r--r--. 1 root root    0 Jun 12 13:02 abort
-rw-r--r--. 1 root root 4096 Jun 12 13:43 checkpoint
drwxr-xr-x. 2 root root    6 Jun 12 13:02 commitlog
drwxr-xr-x. 2 root root  193 Jun 12 13:43 config
drwxr-xr-x. 2 root root    6 Jun 12 13:02 consumequeue
-rw-r--r--. 1 root root    4 Jun 12 13:02 lock
[root@localhost store]# cd commitlog
[root@localhost commitlog]# ll
total 0
[root@localhost commitlog]# cd .
[root@localhost commitlog]# cd ..
[root@localhost store]# cd consumequeue/
[root@localhost consumequeue]# ll
total 0


6. 代码

Simple Message Example - Apache RocketMQ

@Test
    public void admin() throws MQClientException, RemotingException, InterruptedException {
        DefaultMQAdminExt admin = new DefaultMQAdminExt();
        admin.setNamesrvAddr("192.168.56.200:9876");
        admin.start();

        TopicList topicList = admin.fetchAllTopicList();
        Set sets = topicList.getTopicList();
        sets.forEach(s -> System.out.println(s));

        System.out.println("topic route | info");
        TopicRouteData wula = admin.examineTopicRouteInfo("wula");
        System.out.println(wula);
    }
SCHEDULE_TOPIC_XXXX
RMQ_SYS_TRANS_HALF_TOPIC
DefaultCluster_REPLY_TOPIC
BenchmarkTest
OFFSET_MOVED_EVENT
TBW102
localhost.localdomain
SELF_TEST_TOPIC
DefaultCluster
topic route | info

org.apache.rocketmq.client.exception.MQClientException: CODE: 17  DESC: No topic route info in name server for the topic: wula
See http://rocketmq.apache.org/docs/faq/ for further details.

	at org.apache.rocketmq.client.impl.MQClientAPIImpl.getTopicRouteInfoFromNameServer(MQClientAPIImpl.java:1387)
	at org.apache.rocketmq.client.impl.MQClientAPIImpl.getTopicRouteInfoFromNameServer(MQClientAPIImpl.java:1357)
	at org.apache.rocketmq.tools.admin.DefaultMQAdminExtImpl.examineTopicRouteInfo(DefaultMQAdminExtImpl.java:333)
	at org.apache.rocketmq.tools.admin.DefaultMQAdminExt.examineTopicRouteInfo(DefaultMQAdminExt.java:264)
	at rocketmq.SimpleRocketMQ.admin(SimpleRocketMQ.java:121)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

上述结果:首次运行admin

@Test
    public void consumerPull() throws MQClientException, IOException {
        DefaultLitePullConsumer consumer = new DefaultLitePullConsumer("ooxx");
        consumer.setNamesrvAddr("192.168.56.200:9876");
        consumer.start();

        Collection mqs = consumer.fetchMessageQueues("wula");
        System.out.println("queues:");
        mqs.forEach(messageQueue -> System.out.println(messageQueue));
        System.out.println("poll...");
//        consumer.assign(mqs);

        Collection queue = new ArrayList<>();
        queue.add(new MessageQueue("wula", "node01", 0));
        consumer.assign(queue);
        List poll = consumer.poll();
        poll.forEach(messageExt -> System.out.println(messageExt));
        System.in.read();
    }

    @Test
    public void consumerPush() throws Exception {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer_ox");
        consumer.setNamesrvAddr("192.168.56.200:9876");
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);

        consumer.subscribe("wula", "*");
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                list.forEach(messageExt -> {
                    byte[] body = messageExt.getBody();
                    String message = new String(body);
                    System.out.println(message);
                });
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
        // 自测 加个 无线循环或阻塞
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/969692.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号