rocketmq 单机搭建

  1. 获取安装包
    1. wget http://mirrors.shu.edu.cn/apache/rocketmq/4.4.0/rocketmq-all-4.4.0-bin-release.zip
    2. 挤压 unzip rocketmq-all-4.4.0-bin-releasde.zip
  2. 启动
    1. 启动nameserver 到bin下     ./mqnamesrv
    2. 单独开一个shell   jps    看到NamesrvStartup进程   启动成功
    3. 启动broker           到bin下    ./mqbroker -n ip:9876
    4. jps 看到BrokerStartup 启动成功
    5. rocektmq 依赖java  必须先安装好jdk
  3. 新建topic
    1. 查看所有的topic  在bin下    ./mqadmin topiclist -n ip:9876
    2. 新建topic        ./mqadmin updateTopic -n ip:9876 -c DefaultCluster -t first_topic_test
    3. 执行1   看看有没有新建成功
  4. mqshutdown namesrv
  5. mqshutdown broker
  6. 发送代码
    1. public class SyncProducer {

          public static void main(String[] args) throws Exception {
              //Instantiate with a producer group name.
              DefaultMQProducer producer = new DefaultMQProducer("first_topic_test_group");
              // Specify name server addresses.
              producer.setNamesrvAddr("ip:9876");
              //Launch the instance.
              producer.start();
              for (int i = 0; i < 10; i++) {
                  //Create a message instance, specifying topic, tag and message body.
                  Message msg = new Message("first_topic_test" /* Topic */,
                          "TagA" /* Tag */,
                          ("Hello RocketMQ "+i
                          + i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
                  );
                  
                  //Call send message to deliver message to one of brokers.
                  SendResult sendResult = producer.send(msg);
                  System.out.printf("%s%n", sendResult);
              }
              //Shut down once the producer instance is not longer in use.
              producer.shutdown();
          }
      }

    2. 接收代码

      public class Consumer {

          public static void main(String[] args) throws InterruptedException, MQClientException {

              // Instantiate with specified consumer group name.
              DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("first_topic_test_group");

              // Specify name server addresses.
              consumer.setNamesrvAddr("192.168.195.128:9876");

              // Subscribe one more more topics to consume.
              consumer.subscribe("ip", "*");
              // Register callback to execute on arrival of messages fetched from brokers.
              consumer.registerMessageListener(new MessageListenerConcurrently() {

                  @Override
                  public ConsumeConcurrentlyStatus consumeMessage(List msgs,
                          ConsumeConcurrentlyContext context) {
                      System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
                      System.out.println(new String(msgs.get(0).getBody()));
                      return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                  }

              });

              //Launch the consumer instance.
              consumer.start();
              System.out.printf("Consumer Started.%n");
          }
      }

  7. maven依赖

    1. org.apache.rocketmq rocketmq-client 4.3.0

你可能感兴趣的:(java)