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" />

猜你喜欢

转载自www.cnblogs.com/enjoyjava/p/11203975.html