From 4fc1b493c7ea4e49ff2e51c9e609c4d45b4e91c2 Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819.com> Date: Tue, 8 Sep 2020 17:35:34 +0800 Subject: [PATCH 1/4] =?UTF-8?q?:memo:=20=E6=B7=BB=E5=8A=A0=20MyBatisCodeHe?= =?UTF-8?q?lper-Pro=20=E9=B8=A3=E8=B0=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.en.md | 3 ++- README.md | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/README.en.md b/README.en.md index 79a80a77b..8d7286454 100644 --- a/README.en.md +++ b/README.en.md @@ -28,7 +28,8 @@ This project has integrated actuator (`monitoring`), admin (`visual monitoring`) ## Thanks -jetbrains**Thanks JetBrains Offer Open Source Free License** +- jetbrains**Thanks JetBrains Offer Open Source Free License** +- [Thanks MyBatisCodeHelper-Pro(The Best Code Generator Plugin) Offer Permanent Activation Code](https://gejun123456.github.io/MyBatisCodeHelper-Pro/#/?id=mybatiscodehelper-pro) ## Branch Introduction diff --git a/README.md b/README.md index 86ec1e484..18b98ddb7 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,9 @@ ## 感谢 -jetbrains**感谢 JetBrains 提供的免费开源 License** +- jetbrains**感谢 JetBrains 提供的免费开源 License** + +- [感谢史上最牛的代码生成插件 MyBatisCodeHelper-Pro 提供的永久激活码](https://gejun123456.github.io/MyBatisCodeHelper-Pro/#/?id=mybatiscodehelper-pro) ## 分支介绍 From cbca4d36c6fb355aa59f1274795b2b6361aa7221 Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Sat, 14 Aug 2021 18:14:04 +0800 Subject: [PATCH 2/4] =?UTF-8?q?:memo:=20=E6=9B=B4=E6=96=B0=20README?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2d9b99a36..cb5b73171 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,8 @@ ### 开源推荐 +![11628591293_.pic_hd](https://static.aliyun.xkcoding.com/2021/08/14/11628591293pichd.jpg?image/auto-orient,1/interlace,1/quality,q_80/watermark,text_eGtjb2Rpbmc,type_d3F5LXplbmhlaQ,color_ffffff,size_20,x_10,y_10) + - `JustAuth`:史上最全的整合第三方登录的开源库,https://github.com/justauth/JustAuth - `Mica`:SpringBoot 微服务高效开发工具集,https://github.com/lets-mica/mica - `awesome-collector`:https://github.com/P-P-X/awesome-collector @@ -152,4 +154,4 @@ [MIT](http://opensource.org/licenses/MIT) -Copyright (c) 2018 Yangkai.Shen \ No newline at end of file +Copyright (c) 2018 Yangkai.Shen From f10dc0a45be84e89acb48f6cf259620f52838b6e Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Thu, 7 Oct 2021 09:29:49 +0800 Subject: [PATCH 3/4] =?UTF-8?q?:memo:=20=E6=9B=B4=E6=96=B0=20README?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cb5b73171..93ce3f22d 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ ### 开源推荐 -![11628591293_.pic_hd](https://static.aliyun.xkcoding.com/2021/08/14/11628591293pichd.jpg?image/auto-orient,1/interlace,1/quality,q_80/watermark,text_eGtjb2Rpbmc,type_d3F5LXplbmhlaQ,color_ffffff,size_20,x_10,y_10) +![11628591293_.pic_hd](https://static.aliyun.xkcoding.com/2021/08/14/11628591293pichd.jpg?x-oss-process=style/tag_compress) - `JustAuth`:史上最全的整合第三方登录的开源库,https://github.com/justauth/JustAuth - `Mica`:SpringBoot 微服务高效开发工具集,https://github.com/lets-mica/mica From d52a477431b562204eac26f4495fad4fa97d1ee2 Mon Sep 17 00:00:00 2001 From: samwellwang Date: Wed, 27 Oct 2021 17:11:50 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=AE=98=E6=96=B9=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E5=8F=91=E9=80=81=E6=B6=88=E6=81=AF=E6=A0=B7=E4=BE=8B=20?= =?UTF-8?q?=E5=8F=91=E9=80=81=E6=99=AE=E9=80=9A=E6=B6=88=E6=81=AF=E6=A0=B7?= =?UTF-8?q?=E4=BE=8B=20=E5=8F=91=E9=80=81=E5=BB=B6=E8=BF=9F=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E6=A0=B7=E4=BE=8B=20=E6=B6=88=E8=B4=B9=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E6=A0=B7=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo-mq-rocketmq/pom.xml | 14 ++++ .../controller/SendMsgController.java | 69 ++++++++++++++++++ .../mq/rocketmq/message/MessageConsumer.java | 16 +++++ .../mq/rocketmq/message/MessageProducer.java | 70 +++++++++++++++++++ .../src/main/resources/application.yml | 9 +++ 5 files changed, 178 insertions(+) create mode 100644 demo-mq-rocketmq/src/main/java/com/xkcoding/mq/rocketmq/controller/SendMsgController.java create mode 100644 demo-mq-rocketmq/src/main/java/com/xkcoding/mq/rocketmq/message/MessageConsumer.java create mode 100644 demo-mq-rocketmq/src/main/java/com/xkcoding/mq/rocketmq/message/MessageProducer.java create mode 100644 demo-mq-rocketmq/src/main/resources/application.yml diff --git a/demo-mq-rocketmq/pom.xml b/demo-mq-rocketmq/pom.xml index 0369f8fc9..71182f11d 100644 --- a/demo-mq-rocketmq/pom.xml +++ b/demo-mq-rocketmq/pom.xml @@ -28,11 +28,25 @@ spring-boot-starter + + org.apache.rocketmq + rocketmq-spring-boot-starter + 2.0.4 + + org.springframework.boot spring-boot-starter-test test + + org.springframework.boot + spring-boot-starter-web + + + org.projectlombok + lombok + diff --git a/demo-mq-rocketmq/src/main/java/com/xkcoding/mq/rocketmq/controller/SendMsgController.java b/demo-mq-rocketmq/src/main/java/com/xkcoding/mq/rocketmq/controller/SendMsgController.java new file mode 100644 index 000000000..dab7f4392 --- /dev/null +++ b/demo-mq-rocketmq/src/main/java/com/xkcoding/mq/rocketmq/controller/SendMsgController.java @@ -0,0 +1,69 @@ +package com.xkcoding.mq.rocketmq.controller; + +import com.xkcoding.mq.rocketmq.message.MessageProducer; +import org.apache.rocketmq.client.exception.MQBrokerException; +import org.apache.rocketmq.client.exception.MQClientException; +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; +import org.apache.rocketmq.remoting.exception.RemotingException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.io.UnsupportedEncodingException; + +@RestController +@RequestMapping("sendMessage") +public class SendMsgController { + @Autowired + MessageProducer messageProducer; + + /** + * 官方发送消息示例 + */ + @RequestMapping("official") + public void sendMsgOfficial() throws Exception { + //测试发送消息,实例化生产者producer + // 实例化消息生产者Producer + DefaultMQProducer producer = new DefaultMQProducer("test_message"); + // 设置NameServer的地址 + producer.setNamesrvAddr("192.168.1.18:9876"); + // 启动Producer实例 + producer.start(); + for (int i = 0; i < 50; i++) { + // 创建消息,并指定Topic,Tag和消息体 + Message msg = new Message("TopicTest" /* Topic */, + "TagA" /* Tag */, + ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */ + ); + // 发送消息到一个Broker + SendResult sendResult = producer.send(msg); + // 通过sendResult返回消息是否成功送达 + System.out.printf("%s%n", sendResult); + } + // 如果不再发送消息,关闭Producer实例。 + producer.shutdown(); + } + + /** + * 发送普通消息 + */ + @RequestMapping("plain") + public SendResult sendPlainMsg(){ + return messageProducer.sendPlainMsg("发送普通消息"); + } + + /** + * 发送延迟消息, + * messageDelayLevel=1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h + */ + @RequestMapping(value = "delay",method = RequestMethod.POST) + public SendResult sendDelayMsg(@RequestParam(name = "delayLevel") int delayLevel){ + return messageProducer.sendDelayMsg("发送延迟消息",delayLevel); + } + +} diff --git a/demo-mq-rocketmq/src/main/java/com/xkcoding/mq/rocketmq/message/MessageConsumer.java b/demo-mq-rocketmq/src/main/java/com/xkcoding/mq/rocketmq/message/MessageConsumer.java new file mode 100644 index 000000000..6127ccefd --- /dev/null +++ b/demo-mq-rocketmq/src/main/java/com/xkcoding/mq/rocketmq/message/MessageConsumer.java @@ -0,0 +1,16 @@ +package com.xkcoding.mq.rocketmq.message; + +import org.apache.rocketmq.spring.annotation.RocketMQMessageListener; +import org.apache.rocketmq.spring.core.RocketMQListener; +import org.springframework.stereotype.Component; + +@Component +@RocketMQMessageListener(topic = "TopicTest",consumerGroup="consumer-test") +public class MessageConsumer implements RocketMQListener { + + @Override + + public void onMessage(String message) { + System.out.printf("consumer name is %s data is %s%n", this.getClass().getSimpleName(), message); + } +} diff --git a/demo-mq-rocketmq/src/main/java/com/xkcoding/mq/rocketmq/message/MessageProducer.java b/demo-mq-rocketmq/src/main/java/com/xkcoding/mq/rocketmq/message/MessageProducer.java new file mode 100644 index 000000000..8d26e8caa --- /dev/null +++ b/demo-mq-rocketmq/src/main/java/com/xkcoding/mq/rocketmq/message/MessageProducer.java @@ -0,0 +1,70 @@ +package com.xkcoding.mq.rocketmq.message; + +import lombok.extern.slf4j.Slf4j; +import org.apache.rocketmq.client.producer.DefaultMQProducer; +import org.apache.rocketmq.client.producer.MessageQueueSelector; +import org.apache.rocketmq.client.producer.SendCallback; +import org.apache.rocketmq.client.producer.SendResult; +import org.apache.rocketmq.common.message.Message; +import org.apache.rocketmq.common.message.MessageQueue; +import org.apache.rocketmq.remoting.common.RemotingHelper; +import org.apache.rocketmq.spring.core.RocketMQTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.messaging.support.MessageBuilder; +import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import java.util.List; + +@Service +@Slf4j +public class MessageProducer { + @Resource + RocketMQTemplate rocketMQTemplate; + + /** + * 发送普通消息 + * @param msg 消息内容 + */ + public SendResult sendPlainMsg(String msg){ + return rocketMQTemplate.syncSend("TopicTest", MessageBuilder.withPayload(msg).build()); + } + + /** + * 发送延迟消息 + * @param msg 消息内容 + * @param delayLevel 延迟级别,在broker配置文件里定义 + */ + public SendResult sendDelayMsg(String msg,int delayLevel){ + return rocketMQTemplate.syncSend("TopicTest",MessageBuilder.withPayload(msg).build(),2000,delayLevel); + } + public void sendAsyncMsg(String msg){ + rocketMQTemplate.asyncSend("TopicTest", MessageBuilder.withPayload(msg).build(), new SendCallback() { + @Override + public void onSuccess(SendResult sendResult) { + log.info("消息发送成功"+sendResult.getMsgId()); + } + + @Override + public void onException(Throwable throwable) { + log.error("消息发送失败",throwable); + } + }); + } + + /** + * 发送顺序消息 + * @param msg 消息体 + * @param index hashKey + * @return 发送结果 + */ + public SendResult sendOrderMsg(String msg,int index){ + rocketMQTemplate.setMessageQueueSelector((list, message, o) -> { + //分组规则 + return null; + }); + return rocketMQTemplate.syncSendOrderly("TopicTest",MessageBuilder.withPayload(msg).build(),String.valueOf(index)); + } +} diff --git a/demo-mq-rocketmq/src/main/resources/application.yml b/demo-mq-rocketmq/src/main/resources/application.yml new file mode 100644 index 000000000..f4eb968ab --- /dev/null +++ b/demo-mq-rocketmq/src/main/resources/application.yml @@ -0,0 +1,9 @@ +server: + port: 8080 + servlet: + context-path: /demo +rocketmq: + name-server: 192.168.1.18:9876 # Rocketmq name-server地址 + producer: + group: service-producer-group #生产者 +