当前位置: 首页 > news >正文

公司网站设计有哪些使用技巧呢物流专线网站建设

公司网站设计有哪些使用技巧呢,物流专线网站建设,万博法务网站,一个备案可以做几个网站目录 概述使用准备工作引入依赖创建Topic代码应用启动消息接收再扩展一个 结束 概述 github 文档地址 rocket mq example RocketMQ 版本为 5.1.4 使用 准备工作 阅读此文需要事先准备 RocketMQ ,如有疑问,请移步 RocketMQ 服务搭建 引入依赖 此处…

目录

  • 概述
  • 使用
    • 准备工作
    • 引入依赖
    • 创建Topic
    • 代码
    • 应用启动
    • 消息接收
    • 再扩展一个
  • 结束

概述

github 文档地址
rocket mq example

RocketMQ 版本为 5.1.4

使用

准备工作

阅读此文需要事先准备 RocketMQ ,如有疑问,请移步 RocketMQ 服务搭建

引入依赖

此处,多依赖了一些依赖。

<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
</dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-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-server=10.xx.xx.143:9876
# 定义name为output的binding
spring.cloud.stream.bindings.output.destination=test-topic
spring.cloud.stream.bindings.output.content-type=application/json
# 定义name为input的binding
spring.cloud.stream.bindings.input.destination=test-topic
spring.cloud.stream.bindings.input.content-type=application/json
spring.cloud.stream.bindings.input.group=test-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: 9700

2.启动应用,支持 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.yayakq.cn/news/395095/

相关文章:

  • 哪个行业最容易做网站无锡做网站优化价格
  • 网站开发关联下拉列表2021最火营销方案
  • 高端网站制作如何实现网站的纯静态化
  • 建立网站建设公司官网制作
  • 怎么推广外贸网站海沧区建设局网站
  • 网站建设的时间蛋糕网站建设的目的
  • 合肥电脑网站建站寿光营销型网站建设
  • 快速搭建网站系统中英双语网站程序
  • thinkphp 网站设置功能青岛房地产团购网站建设
  • 昆明seo网站排名小说网站源码带采集
  • 学校网站建设运行情况简介网站设计与运营
  • 网站建设 海拉尔seo建设
  • 在线查看qq空间网站12306网站能不能用银河二计算机做服务器啊慢得要死
  • 黑龙江做网站公司网站菜单导航
  • 致力于网站建设网站开发 发表文章
  • 长沙哪里可以做网站建设优化网站
  • 有什么网站可以做团购深圳人才市场招聘信息
  • 做网站必须有框架么徐州市徐州市城乡建设局网站首页
  • vpn网站模板如何做百度搜索推广
  • 网站建设企业建站要多久tp5手机网站开发
  • 山西旅游网站建设深圳网站备案拍照点
  • 网站地图定位怎么做个人社保缴费app下载
  • 保健品网站dede模板上海东方网首页
  • 菏泽 做网站 多少钱wordpress html调用php
  • 做的视频发到哪个网站山西cms建站系统价格
  • 怎么查网站是那个公司做的美丽乡村网站建设模板
  • 如何打破违法网站wordpress过时了吗
  • 宿州网站开发建设中山网站建设文化教程
  • 网站安全检测软件个人主页网页设计源代码
  • wix做的网站在国内访问不了口碑营销属于什么营销