Spring Cloud Stream报错:Invalid bean definition with name:bean definition with this name already exist
当想使用 Spring Cloud Stream 让微服务自己生产的消息自己消费时,如果使用的@Input("xxx")
与@Output("xxx")
注解中的"xxx"名称相同,启动程序将会报错:
Invalid bean definition with name 'xxx' defined in xxx: bean definition with this name already exists
。
完整错误记录及解决方法:
首先,为了生产和消费消息,需要定义一个输入通道、一个输出通道:
public interface StreamClient {String INPUT = "myMessage";String OUTPUT = "myMessage";@Input(StreamClient.INPUT)SubscribableChannel input();@Output(StreamClient.OUTPUT)MessageChannel output();
}
通过 INPUT 和 OUTPUT 使用相同的名称,让生产消息和消费消息指向相同的Topic,从而实现消费自己发出的消息。
接下来,创建一个HTTP接口,并通过上面定义的输出通道触来生产消息:
@RestController
public class SendMessageController {@Autowiredprivate StreamClient streamClient;@GetMapping("/sendMessage")public void process() {String message = "now " + new Date();streamClient.output().send(MessageBuilder.withPayload(message).build());}
}
已经有生产消息的实现,下面来创建对输入通道的监听,以实现消息的消费逻辑,需在该类名上标记 @EnableBinding(xxx.class)
注解:
@Component
@EnableBinding(StreamClient.class)
@Slf4j
public class StreamReceiver {@StreamListener(value = StreamClient.INPUT)public void process(String message) {log.info("StreamReceiver: {}", message);}
}
这看似天衣无缝的操作,如果你在这时启动,将会看到如下报错信息:
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'myMessage' defined in com.imooc.order.server.message.StreamClient: bean definition with this name already exists - Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=com.imooc.order.server.message.StreamClient; factoryMethodName=input; initMethodName=null; destroyMethodName=nullat org.springframework.cloud.stream.binding.BindingBeanDefinitionRegistryUtils.registerBindingTargetBeanDefinition(BindingBeanDefinitionRegistryUtils.java:64) ~[spring-cloud-stream-2.0.1.RELEASE.jar:2.0.1.RELEASE]
以下省略...
解决方法:
根据错误提示:Invalid bean definition with name 'myMessage' defined in com.imooc.order.server.message.StreamClient: bean definition with this name already exists
,没有启动成功的原因是:已经存在了一个名为myMessage的Bean,那么为什么会重复创建这个Bean呢?
当我们使用@Output
和@Input
注解来定义消息通道时,都会根据传入的通道名称来创建一个Bean,而在上面的例子中,因为我们需要实现对自己生产的消息进行消费,所以我们需要定义的 @Output 和 @Input 名称相同的Topic,这样一来,便会生成相同Bean,以至报错bean definition with this name already exists
。
既然这样,我们定义相同的通道名是行不通了,那么我们只能通过定义不同的通道名,并为这两个通道配置相同的目标Topic来将这一对输入输出指向同一个实际的Topic。
对于上面的错误程序,只需要做如下两处改动:
第一步:修改通道名,使用不同的名字
public interface StreamClient {String INPUT = "myMessageTopicInput";String OUTPUT = "myMessageTopicOutput";@Input(StreamClient.INPUT)SubscribableChannel input();@Output(StreamClient.OUTPUT)MessageChannel output();
}
第二步:在配置文件中,为这两个通道设置相同的Topic名称
spring:cloud:stream:bindings:myMessageTopicInput:group: orderdestination: same-topiccontent-type: application/jsonmyMessageTopicOutput:group: orderdestination: same-topiccontent-type: application/json
这样,这两个输入输出通道就会都指向名为same-topic
的Topic了。
最后,再启动该程序,没有报错。然后访问controller:localhost:8080/sendMessage
,可以在控制台中看到如下输出信息:
2020-03-17 22:09:56.490 INFO 84 --- [e-topic.order-1] c.i.order.server.message.StreamReceiver : StreamReceiver: now Tue Mar 17 22:09:56 CST 2020
至此,已解决错误,希望能帮助到更多的伙伴们!
Spring Cloud Stream报错:Invalid bean definition with name:bean definition with this name already exist相关推荐
- Spring Cloud RestTemplate报错:java.lang.IllegalStateException: No instances available for xxx
java.lang.IllegalStateException: No instances available for XXXX 一.问题复现 最近搭建springCloud项目,搭建Eureka,多 ...
- IDEA2020.1基于Maven开发spring cloud项目报错 程序包 com.xxx.xxx.xxx不存在
原因分析 在dependency中有相应的包,但是Maven找不到,那么最根本的原因是没引入依赖 通常,一个微服务开发中会创建两个子模块,一个用于写接口和实体类与其他微服务相互调用,另一个写业务逻辑 ...
- IDEA JAVA 新建Spring Cloud项目报错问题解决记录
1.找不到 主类Application IDEA的原因 删除.idea 重启 2.项目标红 能启动 清除idea缓存 3.找不到新项目的swagger 配置网关 报错信息 Please indic ...
- Spring Cloud Stream多RabbitMQ实例配置时报错no default binder has been set
当前Spring Cloud Rabbit的版本为2.1.2 <dependency><groupId>org.springframework.cloud</groupI ...
- spring boot 启动报错:No qualifying bean of type org.springframework.security.oauth2.provider.token.Toke
spring boot启动报错: Exception encountered during context initialization - cancelling refresh attempt: o ...
- Spring Cloud Stream如何消费自己生产的消息?
在上一篇<Spring Cloud Stream如何处理消息重复消费?>中,我们通过消费组的配置解决了多实例部署情况下消息重复消费这一入门时的常见问题.本文将继续说说在另外一个被经常问到的 ...
- Spring Cloud Stream如何消费自己生产的消息
在上一篇<Spring Cloud Stream如何处理消息重复消费>中,我们通过消费组的配置解决了多实例部署情况下消息重复消费这一入门时的常见问题.本文将继续说说在另外一个被经常问到的问 ...
- Spring Cloud Stream的使用(上)
操作消息队列的另一种方法,SpringCloud是Spring的组件之一,官方定义Spring Cloud Stream,给微服务应用构建消息驱动能力的框架,下面我简称Stream,应用程序通过Inp ...
- Spring Cloud Stream消费失败后的处理策略(三):使用DLQ队列(RabbitMQ)
应用场景 前两天我们已经介绍了两种Spring Cloud Stream对消息失败的处理策略: 自动重试:对于一些因环境原因(如:网络抖动等不稳定因素)引发的问题可以起到比较好的作用,提高消息处理的 ...
最新文章
- 换个视角,从产品线管理看过去
- MySQL 8.0 CentOS 7安装手册
- MFC六大核心机制之二:运行时类型识别(RTTI)
- 多用户企业文件管理系统源码_固定资产管理系统的细节分析
- 爬空气质量MySQL,mysql
- 1115. Counting Nodes in a BST (30) 数据结构
- netty 游戏服务器框图_基于Netty和WebSocket协议实现Web端自动打印订单服务方法与流程...
- (转)数字格式化函数:Highcharts.numberFormat()
- ngnix高并发的原理实现(转)
- 一年之计:如何构建知识体系?
- laravel-echo-server 不接收失败_6所高校公布报名不合格名单!这些问题最容易出错...
- Linux中find命令-path -prune用法详解
- Android 中的转场动画及兼容处理
- spring security3 使用自定义MD5或者其他加密规则
- win7一点计算机就卡死,win7系统电脑经常卡住假死页面关不掉的解决方法
- VSCode Conventional Commits 插件
- 2006东京游戏展游历
- 单元格的边框没有被显示出来
- 关于“城市超脑DIGITAL TWIN”的一些保姆级干货
- Windows7重装系统后文件夹权限的混乱
热门文章
- 一个画板十年工程师的PCB设计经验分享
- Android 实现SKU选择通用方式
- 如何解决打开浏览器跳转MSN中国页面
- VBA中worksheets/sheets/workbook	区别
- php 成语接龙api,再谈Weiphp公众平台开发——1、成语接龙插件
- 什么是期货、现货?//2021-2-1
- 谈谈第一次Web项目的感想(上)
- hdu 5238 Calculator(线段树+中国剩余定理)
- 最强大脑《多米诺效应》
- c语言烧录芯片,入门扫盲!芯片程序的烧录过程,没想象那么难跟着走就行!...