专注Java教育14年 全国咨询/投诉热线:444-1124-454
赢咖4LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 Dubbo消费者找不到服务提供者问题

Dubbo消费者找不到服务提供者问题

更新时间:2021-09-07 11:05:04 来源:赢咖4 浏览1318次

网上有很多类似问题,场景为Dubbo服务端通过监控中心查看是正常注册了的,但是消费者却执行时报错,说找不到服务提供者。异常如下图所示:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).

log4j:WARN Please initialize the log4j system properly.

log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'demoService': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Failed to check the status of the service com.unj.dubbotest.provider.DemoService. No provider available for the service com.unj.dubbotest.provider.DemoService from the url zookeeper://localhost:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=consumer-of-helloworld-app&check=false&dubbo=2.5.3&interface=com.unj.dubbotest.provider.DemoService&loadbalance=roundrobin&methods=sayHello&pid=108320&side=consumer×tamp=1500875956242 to the consumer 192.168.0.101 use dubbo version 2.5.3

at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:175)

at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)

at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1634)

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:254)

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)

at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1078)

at Consumer.main(Consumer.java:11)

Caused by: java.lang.IllegalStateException: Failed to check the status of the service com.unj.dubbotest.provider.DemoService. No provider available for the service com.unj.dubbotest.provider.DemoService from the url zookeeper://localhost:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=consumer-of-helloworld-app&check=false&dubbo=2.5.3&interface=com.unj.dubbotest.provider.DemoService&loadbalance=roundrobin&methods=sayHello&pid=108320&side=consumer×tamp=1500875956242 to the consumer 192.168.0.101 use dubbo version 2.5.3

at com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:420)

at com.alibaba.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:300)

at com.alibaba.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:138)

at com.alibaba.dubbo.config.spring.ReferenceBean.getObject(ReferenceBean.java:65)

at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)

... 6 more

一般由于以下前3种原因,这边是因为第4种,这里一一列举出来:

1.防火墙问题:可以通过telnet尝试连接服务端提供服务的地址加端口,看是否能够正常连接。不行可能需要关闭防火墙,或配置开放端口(大神操作)

2.注册使用的网段ip错误:一般是由于系统有多个ip,结果使用了一个与消费者不通的ip进行注册,可以在监控中心查看注册的url是什么ip,如下图所示,这边使用的192.168.0.101的内部ip,是通的。

如果的确是ip的问题,那么可以通过修改配置文件,在服务端的dubbo:protocol节点配置服务开放端口时,同时指定服务提供ip。这里消费者端好像也可以配置host,我觉得意义不大,这里没有试过。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="hello-world-app"  /> 
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <!-- <dubbo:registry address="multicast://224.5.6.7:1234?unicast=false" />-->
    <!-- zookeeper服务注册中心 -->
    <dubbo:registry address="zookeeper://localhost:2181"  check="true" />    
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" host="192.168.0.101" port="20882"  /> 
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"  version="1.0"/> 
    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />
</beans>

3.版本号问题如上图所示,服务端在dubbo:service的节点下配置了版本1.0,那么客户端也需要在对应的dubbo:reference节点下配置对应版本号,否则也是会报上面错误的。笔者最开始遇到的问题不是前面三种的任何一种,但是发现第4点的问题后发现还是不行,进而基于网上有的几个原因进行排查,结果发现是之前按照网上说的配了下版本号,但是最终没有保持一致。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 
    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="consumer-of-helloworld-app"  /> 
    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <!-- <dubbo:registry address="multicast://224.5.6.7:1234?unicast=false" /> -->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!-- <dubbo:protocol host="192.168.0.101" /> -->
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="demoService"  interface="com.alibaba.dubbo.demo.DemoService"  version="1.0"/>
    <!-- add by liuming to avoid reference 3rd party components -->
</beans>

4.依赖组件没有引用全。通过使用网上现有的demo,我发现居然可以,然后与我自己搭的环境进行对比。最终发现消费者这端我忘记添加netty依赖组件了~~。这里要注意的是,缺少netty组件依赖报错和找不到服是一样的,所以很难辨认原因,缺少其他组件可能也有此现象。这点就要进行仔细排查了。最后吐槽下,缺少netty组件不应该报类找不到异常吗?这个估计是把异常catch重新抛了一个新异常,把真实原因隐藏了,感觉有些不易维护,这里用的2.5.3版本,不知道后面有没有优化。

以上就是赢咖4小编介绍的"Dubbo消费者找不到服务提供者问题",希望对大家有帮助,想了解更多可查看Dubbo教程。赢咖4在线学习教程,针对没有任何Java基础的读者学习,让你从入门到精通,主要介绍了一些Java基础的核心知识,让同学们更好更方便的学习和了解Java编程,感兴趣的同学可以关注一下。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>