rocketMq5.0没有找到比较规范、可使用的使用gRpc协议的文章,这里也是试了很久才调通。
很早之前安装的,可能有问题,安装失败联系我
RocketMq官网
{
"rocketMQClusterName": "DefaultCluster",
"remotingListenPort":28080,
"grpcServerPort":28081
}
RocketMq官网
maven编译,maven安装省略,不会百度,再不会建议转行
需修改的application.yml(端口自定义,别被占用)
MessageServiceImpl.java(去掉+1,新版本不修改,所有的数据都显示两条,阿里群找的答案,都是大佬,钉钉群21982288)
编译,启动,java -jar ***.jar
启动后页面
针对5.0.5版本的具体消息无法查看(消息查看有延迟)
<dependency>
<groupId>org.apache.rocketmqgroupId>
<artifactId>rocketmq-client-javaartifactId>
<version>5.0.5version>
dependency>
rocketmq:
enabled: true
accessKey: rocketmq2
secretKey: 12345678
proxy: 192.168.25.1:28080
topic: lo-topic
consumer:
group: consumer-group
import lombok.extern.slf4j.Slf4j;
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.consumer.ConsumeResult;
import org.apache.rocketmq.client.apis.consumer.FilterExpression;
import org.apache.rocketmq.client.apis.consumer.FilterExpressionType;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
@Slf4j
@Component
public class RocketMqConsumer {
@Value("${rocketmq.proxy}")
private String proxy;
@Value("${rocketmq.consumer.group}")
private String consumerGroup;
@Value("${rocketmq.topic}")
private String topic;
@Bean(name = "MqConsumer")
@ConditionalOnProperty(value = "rocketmq.enabled", havingValue = "true") //启用禁用
public void MqConsumer() {
ClientServiceProvider provider = ClientServiceProvider.loadService();
ClientConfigurationBuilder builder = ClientConfiguration.newBuilder().setEndpoints(proxy);
ClientConfiguration configuration = builder.build();
// 初始化Producer时需要设置通信配置以及预绑定的Topic。
try {
log.info("构建mq5.0消费者:proxy:{}, topic:{}, group:{}", proxy, topic, consumerGroup);
// 订阅消息的过滤规则,表示订阅所有Tag的消息。
String tag = "*";
FilterExpression filterExpression = new FilterExpression(tag, FilterExpressionType.TAG);
provider.newPushConsumerBuilder()
.setClientConfiguration(configuration)
// 设置消费者分组。
.setConsumerGroup(consumerGroup)
// 设置预绑定的订阅关系。
.setSubscriptionExpressions(Collections.singletonMap(topic, filterExpression))
// 设置消费监听器。
.setMessageListener(messageView -> {
log.info("消费消息:{}", messageView);
String str = StandardCharsets.UTF_8.decode(messageView.getBody()).toString();
System.out.println("消费:"+str);
return ConsumeResult.SUCCESS;
}).build();
log.info("构建mq5.0消费者成功:proxy:{}, topic:{}, group:{}", proxy, topic, consumerGroup);
} catch (ClientException e) {
log.error("构建mq5.0消费者异常:proxy:{}, topic:{}, group:{}", proxy, topic, consumerGroup, e);
}
}
}
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.apis.*;
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.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Slf4j
@Component
public class RocketMqProducer {
@Value("${rocketmq.accessKey}")
private String accessKey;
@Value("${rocketmq.secretKey}")
private String secretKey;
@Value("${rocketmq.proxy}")
private String endpoints;
//异步
public void asyncSendMessage(String topic, String messageStr) throws ClientException, InterruptedException, IOException {
ClientServiceProvider provider = ClientServiceProvider.loadService();
SessionCredentialsProvider sessionCredentialsProvider = new StaticSessionCredentialsProvider(accessKey, secretKey);
ClientConfiguration clientConfiguration = ClientConfiguration.newBuilder().setEndpoints(endpoints).setCredentialProvider(sessionCredentialsProvider).build();
Producer producer = provider.newProducerBuilder().setClientConfiguration(clientConfiguration).setTopics(new String[]{topic}).build();
String tag = "*";
Message message = provider.newMessageBuilder().setTopic(topic).setTag(tag).setBody(messageStr.getBytes(StandardCharsets.UTF_8)).build();
CompletableFuture<SendReceipt> future = producer.sendAsync(message);
ExecutorService sendCallbackExecutor = Executors.newCachedThreadPool();
future.whenCompleteAsync((sendReceipt, throwable) -> {
if (null != throwable) {
log.error("Failed to send message", throwable);
} else {
log.info("Send message successfully, messageId={}", sendReceipt.getMessageId());
}
}, sendCallbackExecutor);
Thread.sleep(Long.MAX_VALUE);
producer.close();
}
//同步
public void sendMessage(String topic, String messageStr) throws ClientException, IOException {
ClientServiceProvider provider = ClientServiceProvider.loadService();
SessionCredentialsProvider sessionCredentialsProvider = new StaticSessionCredentialsProvider(accessKey, secretKey);
ClientConfiguration clientConfiguration = ClientConfiguration.newBuilder().setEndpoints(endpoints).setCredentialProvider(sessionCredentialsProvider).build();
Producer producer = provider.newProducerBuilder().setClientConfiguration(clientConfiguration).setTopics(new String[]{topic}).build();
String tag = "*";
System.out.println("消息内容"+messageStr);
Message message = provider.newMessageBuilder().setTopic(topic).setTag(tag).setBody(messageStr.getBytes(StandardCharsets.UTF_8)).build();
try {
SendReceipt sendReceipt = producer.send(message);
log.info("Send message successfully, messageId={}", sendReceipt.getMessageId());
} catch (Throwable var13) {
log.error("Failed to send message", var13);
}
producer.close();
}
}
import org.apache.rocketmq.client.apis.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
@RestController
public class RocketMqController {
@Value("${rocketmq.topic}")
private String topic;
@Autowired
private RocketMqProducer rocketMqProducer;
@GetMapping("/rq/send/{message}")
public String sendMessage(@PathVariable String message) throws ClientException, IOException {
rocketMqProducer.sendMessage(topic, message);
System.out.println("测试");
return "success:message->"+message;
}
}