Dubbo学习-8-dubbo启动时检查
2024-08-29 23:28:46
启动时检查的作用如下:
(1)Dubbo 缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止 Spring 初始化完成,以便上线时,能及早发现问题,默认 check="true"
。
(2)可以通过 check="false"
关闭检查,比如,测试时,有些服务不关心,或者出现了循环依赖,必须有一方先启动的情况下
(3)如果你的 Spring 容器是懒加载的,或者通过 API 编程延迟引用服务,请关闭 check,否则服务临时不可用时,会抛出异常,拿到 null 引用,如果 check="false"
,总是会返回引用,当服务恢复时,能自动连上。
以前面帖子工程为例:
直接启动服务的消费者:order-service-consumer工程,结果如下:'
具体错误信息如下:提示没有提供者
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®ister.ip=192.168.0.110&remote.timestamp=1563206416952&side=consumer×tamp=1563370847797 to the consumer 192.168.0.110 use dubbo version 2.6.2
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1268)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.lch.test.MainApplication.main(MainApplication.java:12)
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®ister.ip=192.168.0.110&remote.timestamp=1563206416952&side=consumer×tamp=1563370847797 to the consumer 192.168.0.110 use dubbo version 2.6.2
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:177)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1640)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:254)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.addCandidateEntry(DefaultListableBeanFactory.java:1316)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1282)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
... 15 more
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®ister.ip=192.168.0.110&remote.timestamp=1563206416952&side=consumer×tamp=1563370847797 to the consumer 192.168.0.110 use dubbo version 2.6.2
at com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:422)
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 org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:170)
... 25 more
这是因为,如果没有配置启动检查,dubbo是默认进行启动检查的,也就是启动服务的消费者时,先检查服务的提供者有没有启动起来。
修改配置文件,在dubboLreference标签里面加上 check=“false”
<?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://dubbo.apache.org/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!--扫描包注解 -->
<context:component-scan
base-package="com.lch.test.service.impl" /> <!-- 1.消费方应用名 -->
<dubbo:application name="order-service-consumer" /> <!-- 2.指定注册中心地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 生成远程服务代理dubbo:reference:声明需要调用的远程服务的接口 -->
<!-- user-service-provider工程里面暴露了一个名为userService的服务,这里要引用这个服务 -->
<dubbo:reference id="userService"
26 interface="com.lch.test.service.UserService" check="false"/> <!-- 连接监控中心:使用自动发现的方式 -->
<dubbo:monitor protocol="register" />
<!-- 直连注册中心 -->
<!-- <dubbo:monitor address="127.0.0.1:7070" /> --> </beans>
然后再启动服务的消费者:
dubbo启动时检查可以配置三种作用范围,上面例子是仅仅关闭对user-service-provider这个服务提供者的检查,如果要关闭所有服务消费者对 与其对应的服务提供者的检查,
可以在标签 dubbo:consumer 中添加check="false" 即: <dubbo:consumer check="false" />
最新文章
- JSP基础语法---九九乘法表-java jsp
- 关于input/textarea提交内容空格回车转换问题,以及ng-model去除空格问题
- BZOJ1798——[Ahoi2009]Seq维护序列seq
- drop,delete,truncate区别
- Jedis操作Redis数据库
- hdu 4597 Play Game(区间dp,记忆化搜索)
- java本地方法
- 关于用jQuery的animate方法实现的动画在IE中失效的原因以及解决方法
- WebService应用--使用java开发WebService程序
- JBoss 主要模块
- 使用FsCheck编写Property-based测试
- [Swift]LeetCode287. 寻找重复数 | Find the Duplicate Number
- FL studio的循环模式简介
- kafka分区及副本在broker的分配
- JAVA8之函数式接口
- 使用Asp.Net Core MVC 开发项目实践[第五篇:缓存的使用]
- ul+li水平居中的几种方法
- (已解决) eclipse提示报错";serializing cdt project settings";解决方案
- Linux的软中断处理实现 【转】
- PHP 使用 MongoDB
热门文章
- leetcode 695 Max Area of Island 岛的最大面积
- Hbase——HA搭建
- springmvc 读写分离
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_08 转换流_1_字符编码和字符集
- MVC 源码系列之控制器激活(一)
- 基于nginx实现二维码下载安装apk文件
- python接口自动化:调试接口的代码(无token情况下)
- 应用安全-安全设备-Waf系列-软Waf-安全狗(Safedog)
- Mac016--安装kubernetes(k8s)
- ftp服务端