Springboot 集成dubbo 解决@Reference 注解为空问题汇总

Springboot 集成dubbo,是目前主流的微服务架构之一,两者在集成过程中经常遇到生产者提供的服务类,在消费者服务里依赖注入时启动报错或者注入为空的情况,今天我分享几种情况产生的原因:

首先在查看问题汇总之前需要先了解Springboot 集成dubbo的各种配置和依赖:参考

一、dubbo的注册中心ZK地址配错,导致启动后依赖注入的对象为空:

1、 错误配置:忘记前缀或者后面的端口号,导致启动时就报错

spring:
  dubbo:
    application:
      name: nandao-admin-dubbo #应用名
    registry:
      address: zk1-test.nandao.org;zk2-test.nandao.org; #zookeeper地址
      port: 2181 #提供注册的端口
    protocol:
      name: dubbo
      port: 21887 #dubbo服务暴露的端口
    scan: com.nandao.admin #扫描的包名

 正确的  address 应该为:即域名前面必须带上  zookeeper://  ,初学者很容易忽略这一点。

 address: zookeeper://zk1-test.nandao.org:2181;zookeeper://zk2-test.nandao.org:2181; #zookeeper地址

2、 启动时报错日志:

 2020-11-02 21:23:36.596 [] [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'applyService': Injection of @Reference dependencies failed; nested exception is java.lang.IllegalStateException: Failed to subscribe consumer://172.11.08.9/com.alibaba.dubbo.registry.RegistryService?application=earning-admin-dubbo&callbacks=10000&connect.timeout=10000&dubbo=2.6.2&interface=com.alibaba.dubbo.registry.RegistryService&lazy=true&methods=lookup,subscribe,unsubscribe,unregister,register&pid=28168&reconnect=false&sticky=true&subscribe.1.callback=true&timeout=10000&timestamp=1641129785996&unsubscribe.1.callback=false, cause: Failed to invoke the method subscribe in the service com.alibaba.dubbo.registry.RegistryService. Tried 3 times of the providers [zk-cluster-earning-1.tc-jp1.nandao.com:2181] (1/1) from the registry zk-cluster-earning-1.tc-jp1.nandao.com:2181 on the consumer 172.11.08.9 using the dubbo version 2.6.2. Last error is: Invoke remote method timeout. method: subscribe, provider: dubbo://zk-cluster-earning-1.tc-jp1.nandao.com:2181/com.alibaba.dubbo.registry.RegistryService?application=earning-admin-dubbo&callbacks=10000&check=false&connect.timeout=10000&dubbo=2.6.2&interface=com.alibaba.dubbo.registry.RegistryService&lazy=true&methods=lookup,subscribe,unsubscribe,unregister,register&pid=28168&reconnect=false&remote.timestamp=1641129785996&sticky=true&subscribe.1.callback=true&timeout=10000&timestamp=1641129785996&unsubscribe.1.callback=false, cause: Waiting server-side response timeout by scan timer. start time: 2020-11-02 21:23:26.543, end time: 2020-11-02 21:23:36.559, client elapsed: 1 ms, server elapsed: 10015 ms, timeout: 10000 ms, request: Request [id=4, version=2.0.0, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=subscribe, parameterTypes=[class com.alibaba.dubbo.common.URL, interface com.alibaba.dubbo.registry.NotifyListener], arguments=[consumer://172.11.08.9/com.alibaba.dubbo.registry.RegistryService?application=earning-admin-dubbo&callbacks=10000&connect.timeout=10000&dubbo=2.6.2&interface=com.alibaba.dubbo.registry.RegistryService&lazy=true&methods=lookup,subscribe,unsubscribe,unregister,register&pid=28168&reconnect=false&sticky=true&subscribe.1.callback=true&timeout=10000&timestamp=1641129785996&unsubscribe.1.callback=false, com.alibaba.dubbo.registry.integration.RegistryDirectory@3185fa6b], attachments={path=com.alibaba.dubbo.registry.RegistryService, sys_callback_arg-1=830863979, interface=com.alibaba.dubbo.registry.RegistryService, version=0.0.0, timeout=10000}]], channel: 172.11.08.9:0 -> zk-cluster-earning-1.tc-jp1.nandao.com/172.11.08.18:2181
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:324)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202)
	at com.nandao.earning.admin.EarningAdminApplication.main(EarningAdminApplication.java:28)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:47)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:86)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
	at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'applyService': Injection of @Reference dependencies failed; nested exception is java.lang.IllegalStateException: Failed to subscribe consumer://172.11.08.9/com.alibaba.dubbo.registry.RegistryService?application=earning-admin-dubbo&callbacks=10000&connect.timeout=10000&dubbo=2.6.2&interface=com.alibaba.dubbo.registry.RegistryService&lazy=true&methods=lookup,subscribe,unsubscribe,unregister,register&pid=28168&reconnect=false&sticky=true&subscribe.1.callback=true&timeout=10000&timestamp=1641129785996&unsubscribe.1.callback=false, cause: Failed to invoke the method subscribe in the service com.alibaba.dubbo.registry.RegistryService. Tried 3 times of the providers [zk-cluster-earning-1.tc-jp1.nandao.com:2181] (1/1) from the registry zk-cluster-earning-1.tc-jp1.nandao.com:2181 on the consumer 172.11.08.9 using the dubbo version 2.6.2. Last error is: Invoke remote method timeout. method: subscribe, provider: dubbo://zk-cluster-earning-1.tc-jp1.nandao.com:2181/com.alibaba.dubbo.registry.RegistryService?application=earning-admin-dubbo&callbacks=10000&check=false&connect.timeout=10000&dubbo=2.6.2&interface=com.alibaba.dubbo.registry.RegistryService&lazy=true&methods=lookup,subscribe,unsubscribe,unregister,register&pid=28168&reconnect=false&remote.timestamp=1641129785996&sticky=true&subscribe.1.callback=true&timeout=10000&timestamp=1641129785996&unsubscribe.1.callback=false, cause: Waiting server-side response timeout by scan timer. start time: 2020-11-02 21:23:26.543, end time: 2020-11-02 21:23:36.559, client elapsed: 1 ms, server elapsed: 10015 ms, timeout: 10000 ms, request: Request [id=4, version=2.0.0, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=subscribe, parameterTypes=[class com.alibaba.dubbo.common.URL, interface com.alibaba.dubbo.registry.NotifyListener], arguments=[consumer://172.11.08.9/com.alibaba.dubbo.registry.RegistryService?application=earning-admin-dubbo&callbacks=10000&connect.timeout=10000&dubbo=2.6.2&interface=com.alibaba.dubbo.registry.RegistryService&lazy=true&methods=lookup,subscribe,unsubscribe,unregister,register&pid=28168&reconnect=false&sticky=true&subscribe.1.callback=true&timeout=10000&timestamp=1641129785996&unsubscribe.1.callback=false, com.alibaba.dubbo.registry.integration.RegistryDirectory@3185fa6b], attachments={path=com.alibaba.dubbo.registry.RegistryService, sys_callback_arg-1=830863979, interface=com.alibaba.dubbo.registry.RegistryService, version=0.0.0, timeout=10000}]], channel: 172.11.08.9:0 -> zk-cluster-earning-1.tc-jp1.nandao.com/172.11.08.18:2181
	at com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.postProcessPropertyValues(ReferenceAnnotationBeanPostProcessor.java:96)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1383)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:525)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:496)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:630)
	at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:180)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:321)
	... 25 common frames omitted
Caused by: java.lang.IllegalStateException: Failed to subscribe consumer://172.11.08.9/com.alibaba.dubbo.registry.RegistryService?application=earning-admin-dubbo&callbacks=10000&connect.timeout=10000&dubbo=2.6.2&interface=com.alibaba.dubbo.registry.RegistryService&lazy=true&methods=lookup,subscribe,unsubscribe,unregister,register&pid=28168&reconnect=false&sticky=true&subscribe.1.callback=true&timeout=10000&timestamp=1641129785996&unsubscribe.1.callback=false, cause: Failed to invoke the method subscribe in the service com.alibaba.dubbo.registry.RegistryService. Tried 3 times of the providers [zk-cluster-earning-1.tc-jp1.nandao.com:2181] (1/1) from the registry zk-cluster-earning-1.tc-jp1.nandao.com:2181 on the consumer 172.11.08.9 using the dubbo version 2.6.2. Last error is: Invoke remote method timeout. method: subscribe, provider: dubbo://zk-cluster-earning-1.tc-jp1.nandao.com:2181/com.alibaba.dubbo.registry.RegistryService?application=earning-admin-dubbo&callbacks=10000&check=false&connect.timeout=10000&dubbo=2.6.2&interface=com.alibaba.dubbo.registry.RegistryService&lazy=true&methods=lookup,subscribe,unsubscribe,unregister,register&pid=28168&reconnect=false&remote.timestamp=1641129785996&sticky=true&subscribe.1.callback=true&timeout=10000&timestamp=1641129785996&unsubscribe.1.callback=false, cause: Waiting server-side response timeout by scan timer. start time: 2020-11-02 21:23:26.543, end time: 2020-11-02 21:23:36.559, client elapsed: 1 ms, server elapsed: 10015 ms, timeout: 10000 ms, request: Request [id=4, version=2.0.0, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=subscribe, parameterTypes=[class com.alibaba.dubbo.common.URL, interface com.alibaba.dubbo.registry.NotifyListener], arguments=[consumer://172.11.08.9/com.alibaba.dubbo.registry.RegistryService?application=earning-admin-dubbo&callbacks=10000&connect.timeout=10000&dubbo=2.6.2&interface=com.alibaba.dubbo.registry.RegistryService&lazy=true&methods=lookup,subscribe,unsubscribe,unregister,register&pid=28168&reconnect=false&sticky=true&subscribe.1.callback=true&timeout=10000&timestamp=1641129785996&unsubscribe.1.callback=false, com.alibaba.dubbo.registry.integration.RegistryDirectory@3185fa6b], attachments={path=com.alibaba.dubbo.registry.RegistryService, sys_callback_arg-1=830863979, interface=com.alibaba.dubbo.registry.RegistryService, version=0.0.0, timeout=10000}]], channel: 172.11.08.9:0 -> zk-cluster-earning-1.tc-jp1.nandao.com/172.11.08.18:2181
	at com.alibaba.dubbo.registry.support.FailbackRegistry.subscribe(FailbackRegistry.java:207)
	at com.alibaba.dubbo.registry.integration.RegistryDirectory.subscribe(RegistryDirectory.java:159)
	at com.alibaba.dubbo.registry.dubbo.DubboRegistryFactory.createRegistry(DubboRegistryFactory.java:97)
	at com.alibaba.dubbo.registry.support.AbstractRegistryFactory.getRegistry(AbstractRegistryFactory.java:96)
	at com.alibaba.dubbo.registry.RegistryFactory$Adaptive.getRegistry(RegistryFactory$Adaptive.java)
	at com.alibaba.dubbo.registry.integration.RegistryProtocol.refer(RegistryProtocol.java:272)
	at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper.refer(ProtocolFilterWrapper.java:106)
	at com.alibaba.dubbo.qos.protocol.QosProtocolWrapper.refer(QosProtocolWrapper.java:63)
	at com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper.refer(ProtocolListenerWrapper.java:65)
	at com.alibaba.dubbo.rpc.Protocol$Adaptive.refer(Protocol$Adaptive.java)
	at com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:399)
	at com.alibaba.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:333)
	at com.alibaba.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:163)
	at com.alibaba.dubbo.config.spring.ReferenceBean.getObject(ReferenceBean.java:66)
	at com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor$ReferenceFieldElement.inject(ReferenceAnnotationBeanPostProcessor.java:367)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
	at com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.postProcessPropertyValues(ReferenceAnnotationBeanPostProcessor.java:92)
	... 38 common frames omitted
Caused by: com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method subscribe in the service com.alibaba.dubbo.registry.RegistryService. Tried 3 times of the providers [zk-cluster-earning-1.tc-jp1.nandao.com:2181] (1/1) from the registry zk-cluster-earning-1.tc-jp1.nandao.com:2181 on the consumer 172.11.08.9 using the dubbo version 2.6.2. Last error is: Invoke remote method timeout. method: subscribe, provider: dubbo://zk-cluster-earning-1.tc-jp1.nandao.com:2181/com.alibaba.dubbo.registry.RegistryService?application=earning-admin-dubbo&callbacks=10000&check=false&connect.timeout=10000&dubbo=2.6.2&interface=com.alibaba.dubbo.registry.RegistryService&lazy=true&methods=lookup,subscribe,unsubscribe,unregister,register&pid=28168&reconnect=false&remote.timestamp=1641129785996&sticky=true&subscribe.1.callback=true&timeout=10000&timestamp=1641129785996&unsubscribe.1.callback=false, cause: Waiting server-side response timeout by scan timer. start time: 2020-11-02 21:23:26.543, end time: 2020-11-02 21:23:36.559, client elapsed: 1 ms, server elapsed: 10015 ms, timeout: 10000 ms, request: Request [id=4, version=2.0.0, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=subscribe, parameterTypes=[class com.alibaba.dubbo.common.URL, interface com.alibaba.dubbo.registry.NotifyListener], arguments=[consumer://172.11.08.9/com.alibaba.dubbo.registry.RegistryService?application=earning-admin-dubbo&callbacks=10000&connect.timeout=10000&dubbo=2.6.2&interface=com.alibaba.dubbo.registry.RegistryService&lazy=true&methods=lookup,subscribe,unsubscribe,unregister,register&pid=28168&reconnect=false&sticky=true&subscribe.1.callback=true&timeout=10000&timestamp=1641129785996&unsubscribe.1.callback=false, com.alibaba.dubbo.registry.integration.RegistryDirectory@3185fa6b], attachments={path=com.alibaba.dubbo.registry.RegistryService, sys_callback_arg-1=830863979, interface=com.alibaba.dubbo.registry.RegistryService, version=0.0.0, timeout=10000}]], channel: 172.11.08.9:0 -> zk-cluster-earning-1.tc-jp1.nandao.com/172.11.08.18:2181
	at com.alibaba.dubbo.rpc.cluster.support.FailoverClusterInvoker.doInvoke(FailoverClusterInvoker.java:102)
	at com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker.invoke(AbstractClusterInvoker.java:238)
	at com.alibaba.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker.invoke(MockClusterInvoker.java:75)
	at com.alibaba.dubbo.rpc.proxy.InvokerInvocationHandler.invoke(InvokerInvocationHandler.java:52)
	at com.alibaba.dubbo.common.bytecode.proxy0.subscribe(proxy0.java)
	at com.alibaba.dubbo.registry.dubbo.DubboRegistry.doSubscribe(DubboRegistry.java:144)
	at com.alibaba.dubbo.registry.support.FailbackRegistry.subscribe(FailbackRegistry.java:190)
	... 54 common frames omitted
Caused by: com.alibaba.dubbo.remoting.TimeoutException: Waiting server-side response timeout by scan timer. start time: 2020-11-02 21:23:26.543, end time: 2020-11-02 21:23:36.559, client elapsed: 1 ms, server elapsed: 10015 ms, timeout: 10000 ms, request: Request [id=4, version=2.0.0, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=subscribe, parameterTypes=[class com.alibaba.dubbo.common.URL, interface com.alibaba.dubbo.registry.NotifyListener], arguments=[consumer://172.11.08.9/com.alibaba.dubbo.registry.RegistryService?application=earning-admin-dubbo&callbacks=10000&connect.timeout=10000&dubbo=2.6.2&interface=com.alibaba.dubbo.registry.RegistryService&lazy=true&methods=lookup,subscribe,unsubscribe,unregister,register&pid=28168&reconnect=false&sticky=true&subscribe.1.callback=true&timeout=10000&timestamp=1641129785996&unsubscribe.1.callback=false, com.alibaba.dubbo.registry.integration.RegistryDirectory@3185fa6b], attachments={path=com.alibaba.dubbo.registry.RegistryService, sys_callback_arg-1=830863979, interface=com.alibaba.dubbo.registry.RegistryService, version=0.0.0, timeout=10000}]], channel: 172.11.08.9:0 -> zk-cluster-earning-1.tc-jp1.nandao.com/172.11.08.18:2181
	at com.alibaba.dubbo.remoting.exchange.support.DefaultFuture.returnFromResponse(DefaultFuture.java:220)
	at com.alibaba.dubbo.remoting.exchange.support.DefaultFuture.get(DefaultFuture.java:139)
	at com.alibaba.dubbo.remoting.exchange.support.DefaultFuture.get(DefaultFuture.java:112)
	at com.alibaba.dubbo.rpc.protocol.dubbo.DubboInvoker.doInvoke(DubboInvoker.java:95)
	at com.alibaba.dubbo.rpc.protocol.AbstractInvoker.invoke(AbstractInvoker.java:148)
	at com.alibaba.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:75)
	at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
	at com.alibaba.dubbo.rpc.protocol.dubbo.filter.FutureFilter.invoke(FutureFilter.java:54)
	at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
	at com.alibaba.dubbo.rpc.filter.ConsumerContextFilter.invoke(ConsumerContextFilter.java:48)
	at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
	at com.alibaba.dubbo.rpc.listener.ListenerInvokerWrapper.invoke(ListenerInvokerWrapper.java:77)
	at com.alibaba.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:56)
	at com.alibaba.dubbo.rpc.cluster.support.FailoverClusterInvoker.doInvoke(FailoverClusterInvoker.java:78)
	... 60 common frames omitted

3、 启动后现象:调试接口时 userService 显示为 null 

   @Reference
   UserService userService;

二、客户端消费者 注解使用错误:

1、用的是:@Resource 或者  @Autowired 注解,

应该用 @Reference 注解:

import com.alibaba.dubbo.config.annotation.Reference;

此时启动是不能创建对象,导致启动失败,失败日志如下:

2020-11-05 15:31:02.973 [] [main] ERROR o.s.b.diagnostics.LoggingFailureAnalysisReporter - 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field userService in com.nandao.admin.web.controller.UserManageController required a bean of type 'com.nandao.user.rpc.api.user.UserService' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.nandao.user.rpc.api.user.UserService' in your configuration.

三、扫描的包名一定要正确无误,也即能扫描到:

1、此包名一定注意:

scan: com.nandao.admin #扫描的包名

如果配置不对,就会扫描不到,当然也就依赖注入不了,肯定报错。

四、上面的问题均是使用注解作为配置的,如何使用xml文件配置 Dubbo也会出现类似的问题:

1、就服务消费者Consumer而言,是需要先扫描dubbo bean,然后再扫描  SpringBoot bean/controller,就服务提供者而言 恰好相反;

SpringBoot/springMVC的配置与dubbo配置写在一个配置文件中,注意顺序:

<mvc:annotation-driven />  
  
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方同样 -->    
<dubbo:application name="dubbo-consumer" owner="programmer" organization="dubbox" />     
  
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>  
  
<dubbo:consumer timeout="30000" />  
  
<!-- <dubbo:reference id="demoService" interface="com.dubbo.nandao.service.DemoService"/> -->  
<dubbo:annotation package="com.dubbo.nandao.controller" />  
  
<!-- 把标记了@Controller注解的类转换为bean -->  
<context:component-scan base-package="com.dubbo.nandao.controller" /> 

具体可以参考这篇文章,我觉得写的不错:参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寅灯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值