java+创建metaq生产者_微服务架构【技术点4】windows下rocketMQ配置及java端生产者消费者配置-Go语言中文社区...

参考资料

windows下配置rocketMQ

dc35a53f4132af8565b73182dc40b2f1.png

解压缩

7a6a35fe3b5be981fe9f316895bfa4ee.png

6dc96f61dc618e72239554f58e5f8236.png

系统环境变量配置

变量名:ROCKETMQ_HOME

变量值:MQ解压路径MQ文件夹名

d608ab7bbbc2226f235cbde5472e6031.png

6ca56002d1b104fb989a09b77d5e4b07.png

99805060c6b385ad5c18a39dda6842dd.png

c0414648e39e6c1d70dc490abec0b8ab.png

e900e0f9cd82ac92983d7647a6ea0074.png

启动NAMESERVER

Cmd命令框执行进入至‘MQ文件夹bin’下,然后执行‘start mqnamesrv.cmd’,启动NAMESERVER。成功后会弹出提示框,此框勿关闭。

D:

cd D:softwarerocketmq-all-4.2bin

start mqnamesrv.cmd

结果:

05589bb35caa18e3f65eac21a14ff054.png

f24f107450eb4b91ad53e11dde7b96ba.png

暂时忽略警告。

启动BROKER

Cmd命令框执行进入至‘MQ文件夹bin’下,然后执行‘start mqbroker.cmd -n 127.0.0.1:9876 autoCreateTopicEnable=true’,启动BROKER。成功后会弹出提示框,此框勿关闭。

start mqbroker.cmd -n 127.0.0.1:9876 autoCreateTopicEnable=true

d35770b8c916aca55487896d4b54b46c.png

建立项目测试生产者消费者(测试用代码)

该代码只是单纯用于测试,要用于实际环境还需要有相当的规则和编写规范,下一章将补充这个

项目结构如下:

b6c4733dc25a2f5e70f065c92f88b329.png

各个文件内容:

pom.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

net.funfunle

TestMQ

1.0-SNAPSHOT

org.apache.maven.plugins

maven-compiler-plugin

3.3

1.8

1.8

org.apache.rocketmq

rocketmq-common

4.2.0

org.apache.rocketmq

rocketmq-client

4.2.0

mq.properties

# 消费者的组名

apache.rocketmq.consumer.PushConsumer=PushConsumer

# 生产者的组名

apache.rocketmq.producer.producerGroup=Producer

# NameServer地址

apache.rocketmq.namesrvAddr=localhost:9876

MQConfig.java

package net.funfunle.TestMQ.config;

import org.apache.commons.lang3.StringUtils;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.util.Properties;

/**

* Created by DGDL-08 on 2017/3/24.

*/

public class MQConfig {

private static Properties config;

private static final String producerGroup;

private static final String namesrvAddr;

private static final String consumerGroup;

static {

//InputStream inputStream = MyClass.class.getClassLoader().getResourceAsStream("com/john/basis/conf.properties");

config=new Properties();

InputStream inputStream=null;

try{

InputStream in = ClassLoader.getSystemResourceAsStream("mq.properties");

InputStreamReader is=new InputStreamReader(in,"utf-8");

config.load(is);

is.close();

in.close();

// config.load(inputStream);

}

catch (Exception ed){

ed.printStackTrace();

}

consumerGroup=config.getProperty("apache.rocketmq.consumer.PushConsumer");

producerGroup=config.getProperty("apache.rocketmq.producer.producerGroup");

namesrvAddr=config.getProperty("apache.rocketmq.namesrvAddr");

}

public static String getProducerGroup() {

return producerGroup;

}

public static String getNamesrvAddr() {

return namesrvAddr;

}

public static String getConsumerGroup() {

return consumerGroup;

}

}

RocketMQClient.java

package net.funfunle.TestMQ;

import net.funfunle.TestMQ.config.MQConfig;

import org.apache.commons.lang3.time.StopWatch;

import org.apache.rocketmq.client.producer.DefaultMQProducer;

import org.apache.rocketmq.client.producer.SendResult;

import org.apache.rocketmq.common.message.Message;

import org.apache.rocketmq.remoting.common.RemotingHelper;

public class RocketMQClient {

/**

* 生产者的组名

*/

private String producerGroup= MQConfig.getProducerGroup();

/**

* NameServer 地址

*/

private String namesrvAddr=MQConfig.getNamesrvAddr();

public void defaultMQProducer() {

//生产者的组名

DefaultMQProducer producer = new DefaultMQProducer(producerGroup);

//指定NameServer地址,多个地址以 ; 隔开

producer.setNamesrvAddr(namesrvAddr);

try {

/**

* Producer对象在使用之前必须要调用start初始化,初始化一次即可

* 注意:切记不可以在每次发送消息时,都调用start方法

*/

producer.start();

//创建一个消息实例,包含 topic、tag 和 消息体

//如下:topic 为 "TopicTest",tag 为 "push"

Message message = new Message("TopicTest", "push", "发送消息----zhisheng-----".getBytes(RemotingHelper.DEFAULT_CHARSET));

StopWatch stop = new StopWatch();

stop.start();

for (int i = 0; i < 10000; i++) {

SendResult result = producer.send(message);

System.out.println("发送响应:MsgId:" + result.getMsgId() + ",发送状态:" + result.getSendStatus());

}

stop.stop();

System.out.println("----------------发送一万条消息耗时:" + stop.getTime());

} catch (Exception e) {

e.printStackTrace();

} finally {

producer.shutdown();

}

}

public static void main(String[] args){

new RocketMQClient().defaultMQProducer();

}

}

RocketMQServer.java

package net.funfunle.TestMQ;

import net.funfunle.TestMQ.config.MQConfig;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;

import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;

import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;

import org.apache.rocketmq.common.consumer.ConsumeFromWhere;

import org.apache.rocketmq.common.message.MessageExt;

import org.apache.rocketmq.remoting.common.RemotingHelper;

/**

* Created by zhisheng_tian on 2018/2/6

*/

public class RocketMQServer {

/**

* 消费者的组名

*/

private String consumerGroup= MQConfig.getConsumerGroup();

/**

* NameServer 地址

*/

private String namesrvAddr=MQConfig.getNamesrvAddr();

public void defaultMQPushConsumer() {

//消费者的组名

DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(consumerGroup);

//指定NameServer地址,多个地址以 ; 隔开

consumer.setNamesrvAddr(namesrvAddr);

try {

//订阅PushTopic下Tag为push的消息

consumer.subscribe("TopicTest", "push");

//设置Consumer第一次启动是从队列头部开始消费还是队列尾部开始消费

//如果非第一次启动,那么按照上次消费的位置继续消费

consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);

consumer.registerMessageListener((MessageListenerConcurrently) (list, context) -> {

try {

for (MessageExt messageExt : list) {

System.out.println("messageExt: " + messageExt);//输出消息内容

String messageBody = new String(messageExt.getBody(), RemotingHelper.DEFAULT_CHARSET);

System.out.println("消费响应:msgId : " + messageExt.getMsgId() + ", msgBody : " + messageBody);//输出消息内容

}

} catch (Exception e) {

e.printStackTrace();

return ConsumeConcurrentlyStatus.RECONSUME_LATER; //稍后再试

}

return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; //消费成功

});

consumer.start();

} catch (Exception e) {

e.printStackTrace();

}

}

public static void main(String[] args){

new RocketMQServer().defaultMQPushConsumer();

}

}

分别执行生产者和消费者,可以得到结果:

f2064faa8342acdb45280b872060ece0.png

cbab6224bce00edd4a930aecc525cc94.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值