網上有很多類似問題,場景為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版本,不知道后面有沒有優化。