【小程序课程设计/毕业设计】移动端智能预约报名与商品订购系统 便民生活预约订购数字化服务小程序 基于 SpringBoot 的订单预约生成与后台管理系统设计【附源码、数据库、万字文档】
2026/7/22 16:56:16
namesrv和broker<!-- Spring Cloud Stream RocketMQ依赖 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-stream-rocketmq</artifactId></dependency>创建接口定义输入输出通道:
publicinterfaceChannel{StringOUTPUT_EXAMPLE="output_example";StringINPUT_EXAMPLE="input_example";@Output(OUTPUT_EXAMPLE)MessageChanneloutputExample();@Input(INPUT_EXAMPLE)MessageChannelinputExample();}配置通道与MQ主题映射及服务器连接:
spring:cloud:stream:rocketmq:binder:name-server:xx.xx.xx.xx:9876bindings:output_example:destination:example-topicinput_example:destination:example-topicgroup:example-group@EnableBinding(Channel.class)@ComponentpublicclassMessageProcessor{// 消息处理逻辑}@EnableBinding 注解的作用
启用通道绑定功能:告诉Spring Cloud Stream框架需要绑定消息通道
扫描通道定义:框架会扫描 Channel 接口中定义的所有 @Input 和 @Output 通道
创建通道实例:为每个定义的通道创建相应的实例,使得可以通过 @Autowired 注入使用
建立消息连接:将应用程序与消息中间件(RocketMQ)建立连接
所以无论是消息发送方还是接收方,都需要使用 @EnableBinding(Channel.class) 注解来启用消息通道功能。
@Autowired@Qualifier(Channel.OUTPUT_EXAMPLE)privateMessageChanneloutputExampleChannel;publicvoidsendExampleMessage(Stringcontent){Message<String>message=MessageBuilder.withPayload(content).build();outputExampleChannel.send(message);}@StreamListener(Channel.INPUT_EXAMPLE)publicvoidhandleExampleMessages(Stringcontent){// 处理接收到的消息}