Dubbo學習-8-dubbo啟動時檢查


啟動時檢查的作用如下:

(1)Dubbo 缺省會在啟動時檢查依賴的服務是否可用,不可用時會拋出異常,阻止 Spring 初始化完成,以便上線時,能及早發現問題,默認 check="true"

(2)可以通過 check="false" 關閉檢查,比如,測試時,有些服務不關心,或者出現了循環依賴,必須有一方先啟動的情況下

(3)如果你的 Spring 容器是懶加載的,或者通過 API 編程延遲引用服務,請關閉 check,否則服務臨時不可用時,會拋出異常,拿到 null 引用,如果 check="false",總是會返回引用,當服務恢復時,能自動連上。

以前面帖子工程為例:

直接啟動服務的消費者:order-service-consumer工程,結果如下:'

 

 具體錯誤信息如下:提示沒有提供者

 1 Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'orderServiceImpl': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Failed to check the status of the service com.lch.test.service.UserService. No provider available for the service com.lch.test.service.UserService from the url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=order-service-consumer&check=false&dubbo=2.6.2&generic=false&interface=com.lch.test.service.UserService&methods=getUserAddressList&pid=8740&register.ip=192.168.0.110&remote.timestamp=1563206416952&side=consumer&timestamp=1563370847797 to the consumer 192.168.0.110 use dubbo version 2.6.2
 2     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
 3     at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
 4     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
 5     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1268)
 6     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
 7     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
 8     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
 9     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
10     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
11     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
12     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
13     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
14     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
15     at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
16     at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
17     at com.lch.test.MainApplication.main(MainApplication.java:12)
18 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Failed to check the status of the service com.lch.test.service.UserService. No provider available for the service com.lch.test.service.UserService from the url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=order-service-consumer&check=false&dubbo=2.6.2&generic=false&interface=com.lch.test.service.UserService&methods=getUserAddressList&pid=8740&register.ip=192.168.0.110&remote.timestamp=1563206416952&side=consumer&timestamp=1563370847797 to the consumer 192.168.0.110 use dubbo version 2.6.2
19     at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:177)
20     at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)
21     at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1640)
22     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:254)
23     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
24     at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
25     at org.springframework.beans.factory.support.DefaultListableBeanFactory.addCandidateEntry(DefaultListableBeanFactory.java:1316)
26     at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1282)
27     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101)
28     at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
29     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
30     ... 15 more
31 Caused by: java.lang.IllegalStateException: Failed to check the status of the service com.lch.test.service.UserService. No provider available for the service com.lch.test.service.UserService from the url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=order-service-consumer&check=false&dubbo=2.6.2&generic=false&interface=com.lch.test.service.UserService&methods=getUserAddressList&pid=8740&register.ip=192.168.0.110&remote.timestamp=1563206416952&side=consumer&timestamp=1563370847797 to the consumer 192.168.0.110 use dubbo version 2.6.2
32     at com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:422)
33     at com.alibaba.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:333)
34     at com.alibaba.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:163)
35     at com.alibaba.dubbo.config.spring.ReferenceBean.getObject(ReferenceBean.java:66)
36     at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:170)
37     ... 25 more

這是因為,如果沒有配置啟動檢查,dubbo是默認進行啟動檢查的,也就是啟動服務的消費者時,先檢查服務的提供者有沒有啟動起來。

修改配置文件,在dubboLreference標簽里面加上  check=“false” 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 7            http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
 8            http://dubbo.apache.org/schema/dubbo     
 9            http://dubbo.apache.org/schema/dubbo/dubbo.xsd
10            http://www.springframework.org/schema/context
11            http://www.springframework.org/schema/context/spring-context.xsd">
12 
13     <!--掃描包注解 -->
14     <context:component-scan
15         base-package="com.lch.test.service.impl" />
16 
17     <!-- 1.消費方應用名 -->
18     <dubbo:application name="order-service-consumer"  />
19 
20     <!-- 2.指定注冊中心地址 -->
21     <dubbo:registry address="zookeeper://127.0.0.1:2181" />
22 
23     <!-- 生成遠程服務代理dubbo:reference:聲明需要調用的遠程服務的接口 -->
24     <!-- user-service-provider工程里面暴露了一個名為userService的服務,這里要引用這個服務 -->
25    <dubbo:reference id="userService" 26 interface="com.lch.test.service.UserService" check="false"/>
27 
28     <!-- 連接監控中心:使用自動發現的方式 -->
29     <dubbo:monitor protocol="register" />
30     <!-- 直連注冊中心 -->
31     <!-- <dubbo:monitor address="127.0.0.1:7070" /> -->
32 
33 
34 </beans>

然后再啟動服務的消費者:

 

 

 dubbo啟動時檢查可以配置三種作用范圍,上面例子是僅僅關閉對user-service-provider這個服務提供者的檢查,如果要關閉所有服務消費者對 與其對應的服務提供者的檢查,

可以在標簽 dubbo:consumer 中添加check="false" 即:  <dubbo:consumer check="false" />


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM