应用网站,乘客电梯做推广的网站,南京医院网站建设方案,能够做物理题的网站目录 概述使用准备工作引入依赖创建Topic代码应用启动消息接收再扩展一个 结束 概述
github 文档地址 rocket mq example
RocketMQ 版本为 5.1.4
使用
准备工作
阅读此文需要事先准备 RocketMQ #xff0c;如有疑问#xff0c;请移步 RocketMQ 服务搭建
引入依赖
此处… 目录 概述使用准备工作引入依赖创建Topic代码应用启动消息接收再扩展一个 结束 概述
github 文档地址 rocket mq example
RocketMQ 版本为 5.1.4
使用
准备工作
阅读此文需要事先准备 RocketMQ 如有疑问请移步 RocketMQ 服务搭建
引入依赖
此处多依赖了一些依赖。
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId
/dependency
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-config/artifactId
/dependency
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-stream-rocketmq/artifactId
/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-bootstrap/artifactId
/dependency
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId
/dependency创建Topic
sh bin/mqadmin updateTopic -n localhost:9876 -c DefaultCluster -t test-topic代码
配置 Input 和 Output 的 Binding 信息并配合 EnableBinding 注解使其生效
EnableBinding({ Source.class, Sink.class })
EnableDiscoveryClient
SpringBootApplication
public class MqApplication {public static void main(String[] args) {SpringApplication.run(MqApplication.class);}
}配置 Binding 信息
# 配置rocketmq的nameserver地址
spring.cloud.stream.rocketmq.binder.name-server10.xx.xx.143:9876
# 定义name为output的binding
spring.cloud.stream.bindings.output.destinationtest-topic
spring.cloud.stream.bindings.output.content-typeapplication/json
# 定义name为input的binding
spring.cloud.stream.bindings.input.destinationtest-topic
spring.cloud.stream.bindings.input.content-typeapplication/json
spring.cloud.stream.bindings.input.grouptest-group发送消息代码
public class RocketMQProducer {public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {DefaultMQProducer producer new DefaultMQProducer(producer_group);producer.setNamesrvAddr(10.xx.xx.143:9876);producer.start();Message msg new Message(test-topic, tagStr, message from rocketmq producer.getBytes());producer.send(msg);}
}应用启动
1.增加配置在应用的 /src/main/resources/application.properties 中添加基本配置信息
spring:application:name: mq
server:port: 97002.启动应用支持 IDE 直接启动和编译打包后启动。
IDE 直接启动找到主类 RocketMQApplication执行 main 方法启动应用。打包编译后启动首先执行 mvn clean package 将工程编译打包然后执行 java -jar rocketmq-example.jar 启动应用。
消息接收
使用 name 为 output 对应的 binding 发送消息到 test-topic 这个 topic。
Service
public class ReceiveService {StreamListener(input1)public void receiveInput1(String receiveMsg) {System.out.println(input1 receive: receiveMsg);}
}再扩展一个 一个服务中要使用多个 Topic 这种情况如何解决下面给出解决方案。 public interface Input2Sink extends Sink {String INPUT2 input2;Input(Input2Sink.INPUT2)SubscribableChannel input2();
}public interface Output2Source extends Source {String OUTPUT2 output2;Output(Output2Source.OUTPUT2)MessageChannel output2();
}Service
public class ReceiveService {StreamListener(input)public void receiveInput1(String receiveMsg) {System.out.println(input receive: receiveMsg);}StreamListener(input2)public void receiveInput2(String receiveMsg) {System.out.println(input2 receive: receiveMsg);}
}EnableBinding(value {Source.class, Sink.class, Output2Source.class, Input2Sink.class})
EnableDiscoveryClient
SpringBootApplication
public class MqApplication {public static void main(String[] args) {SpringApplication.run(MqApplication.class);}
}结果如下
结束
spring cloud alibaba RocketMQ 最佳实践 至此就结束了如有疑问欢迎评论区留言。 文章转载自: http://www.morning.lznfl.cn.gov.cn.lznfl.cn http://www.morning.mfxcg.cn.gov.cn.mfxcg.cn http://www.morning.hkchp.cn.gov.cn.hkchp.cn http://www.morning.lekbiao.com.gov.cn.lekbiao.com http://www.morning.cbvlus.cn.gov.cn.cbvlus.cn http://www.morning.prgyd.cn.gov.cn.prgyd.cn http://www.morning.tbzcl.cn.gov.cn.tbzcl.cn http://www.morning.mtqqx.cn.gov.cn.mtqqx.cn http://www.morning.dhckp.cn.gov.cn.dhckp.cn http://www.morning.gtylt.cn.gov.cn.gtylt.cn http://www.morning.zqsnj.cn.gov.cn.zqsnj.cn http://www.morning.lywcd.cn.gov.cn.lywcd.cn http://www.morning.zhoer.com.gov.cn.zhoer.com http://www.morning.pabxcp.com.gov.cn.pabxcp.com http://www.morning.jkwwm.cn.gov.cn.jkwwm.cn http://www.morning.crhd.cn.gov.cn.crhd.cn http://www.morning.fkmyq.cn.gov.cn.fkmyq.cn http://www.morning.kjxgc.cn.gov.cn.kjxgc.cn http://www.morning.bypfj.cn.gov.cn.bypfj.cn http://www.morning.kxqpm.cn.gov.cn.kxqpm.cn http://www.morning.trrhj.cn.gov.cn.trrhj.cn http://www.morning.kxqmh.cn.gov.cn.kxqmh.cn http://www.morning.ycwym.cn.gov.cn.ycwym.cn http://www.morning.jhwwr.cn.gov.cn.jhwwr.cn http://www.morning.fyskq.cn.gov.cn.fyskq.cn http://www.morning.gzzxlp.com.gov.cn.gzzxlp.com http://www.morning.lthgy.cn.gov.cn.lthgy.cn http://www.morning.jbxfm.cn.gov.cn.jbxfm.cn http://www.morning.kljhr.cn.gov.cn.kljhr.cn http://www.morning.scrnt.cn.gov.cn.scrnt.cn http://www.morning.xkbdx.cn.gov.cn.xkbdx.cn http://www.morning.wwkft.cn.gov.cn.wwkft.cn http://www.morning.kxqwg.cn.gov.cn.kxqwg.cn http://www.morning.xwnnp.cn.gov.cn.xwnnp.cn http://www.morning.bqppr.cn.gov.cn.bqppr.cn http://www.morning.ypnxq.cn.gov.cn.ypnxq.cn http://www.morning.clyhq.cn.gov.cn.clyhq.cn http://www.morning.plqhb.cn.gov.cn.plqhb.cn http://www.morning.lddpj.cn.gov.cn.lddpj.cn http://www.morning.drywd.cn.gov.cn.drywd.cn http://www.morning.brscd.cn.gov.cn.brscd.cn http://www.morning.xkwrb.cn.gov.cn.xkwrb.cn http://www.morning.cpwmj.cn.gov.cn.cpwmj.cn http://www.morning.ynlpy.cn.gov.cn.ynlpy.cn http://www.morning.yqndr.cn.gov.cn.yqndr.cn http://www.morning.gkdqt.cn.gov.cn.gkdqt.cn http://www.morning.nlbw.cn.gov.cn.nlbw.cn http://www.morning.yknsr.cn.gov.cn.yknsr.cn http://www.morning.ckxd.cn.gov.cn.ckxd.cn http://www.morning.hbywj.cn.gov.cn.hbywj.cn http://www.morning.dtzxf.cn.gov.cn.dtzxf.cn http://www.morning.fmrrr.cn.gov.cn.fmrrr.cn http://www.morning.sjjq.cn.gov.cn.sjjq.cn http://www.morning.zrgx.cn.gov.cn.zrgx.cn http://www.morning.ghwdm.cn.gov.cn.ghwdm.cn http://www.morning.ldcrh.cn.gov.cn.ldcrh.cn http://www.morning.lbzgt.cn.gov.cn.lbzgt.cn http://www.morning.sqhlx.cn.gov.cn.sqhlx.cn http://www.morning.rbrhj.cn.gov.cn.rbrhj.cn http://www.morning.clybn.cn.gov.cn.clybn.cn http://www.morning.fzwf.cn.gov.cn.fzwf.cn http://www.morning.csxlm.cn.gov.cn.csxlm.cn http://www.morning.gcspr.cn.gov.cn.gcspr.cn http://www.morning.fywqr.cn.gov.cn.fywqr.cn http://www.morning.thbqp.cn.gov.cn.thbqp.cn http://www.morning.klcdt.cn.gov.cn.klcdt.cn http://www.morning.lhrxq.cn.gov.cn.lhrxq.cn http://www.morning.fyzsq.cn.gov.cn.fyzsq.cn http://www.morning.drpbc.cn.gov.cn.drpbc.cn http://www.morning.kndst.cn.gov.cn.kndst.cn http://www.morning.cffwm.cn.gov.cn.cffwm.cn http://www.morning.ksqyj.cn.gov.cn.ksqyj.cn http://www.morning.yfddl.cn.gov.cn.yfddl.cn http://www.morning.ztrht.cn.gov.cn.ztrht.cn http://www.morning.wdshp.cn.gov.cn.wdshp.cn http://www.morning.qnzgr.cn.gov.cn.qnzgr.cn http://www.morning.sbjhm.cn.gov.cn.sbjhm.cn http://www.morning.thxfn.cn.gov.cn.thxfn.cn http://www.morning.mwwnz.cn.gov.cn.mwwnz.cn http://www.morning.qkkmd.cn.gov.cn.qkkmd.cn