dubbox消费者启动成功,却无法连接注册中心
使用dubbox作为服务提供端很好实现,因为git的说明和网上有很多的例子可供参考,但是消费端都一笔带过,简单得很,初学者往往以为只要配置如下3样东西就够了:
<?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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config /> <!-- 消费方应用信息,用于计算依赖关系 --> <dubbo:application name="zr_consumer" /> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 消费者接口 --> <dubbo:reference id="mobileZoneResourceService" interface="com.wulf.zoneresource.api.MobileZoneResourceService" /> </beans>
很多例子会写个main方法读取上面这个配置文件,然后调用里面mobileZoneResourceService这个bean里的方法,然后就没了。但我们消费端一般是提供给外部用的,而不是用来写测试类用的。而且这里有个问题:不会到zookeeper注册中心去把消费端注册上去,虽然启动是不会报错的:
INFO: Initializing Spring root WebApplicationContext
[16/08/17 06:24:53:053 CST] localhost-startStop-1 INFO context.ContextLoader: Root WebApplicationContext: initialization started
[16/08/17 06:24:54:054 CST] localhost-startStop-1 INFO support.XmlWebApplicationContext: Refreshing Root WebApplicationContext: startup date [Wed Aug 16 18:24:54 CST 2017]; root of context hierarchy
[16/08/17 06:24:54:054 CST] localhost-startStop-1 INFO xml.XmlBeanDefinitionReader: Loading XML bean definitions from class path resource [META-INF/spring/consumer.xml]
[16/08/17 06:24:54:054 CST] localhost-startStop-1 INFO logger.LoggerFactory: using logger: com.alibaba.dubbo.common.logger.log4j.Log4jLoggerAdapter
[16/08/17 06:24:55:055 CST] localhost-startStop-1 INFO support.PropertySourcesPlaceholderConfigurer: Loading properties file from class path resource [global.properties]
[16/08/17 06:24:55:055 CST] localhost-startStop-1 INFO support.DefaultListableBeanFactory: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@14f205ce: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,zr_consumer,org.springframework.context.support.PropertySourcesPlaceholderConfigurer#0,com.alibaba.dubbo.config.RegistryConfig,mobileZoneResourceService,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
[16/08/17 06:24:55:055 CST] localhost-startStop-1 INFO context.ContextLoader: Root WebApplicationContext: initialization completed in 2188 ms
八月 16, 2017 6:24:56 下午 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8081"]
八月 16, 2017 6:24:56 下午 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
八月 16, 2017 6:24:56 下午 org.apache.catalina.startup.Catalina start
INFO: Server startup in 12599 ms
消费端引入服务端接口的目的就是为了调用,所以这里应该还有一个bean,用来使用引入的接口:
<!-- 对外control --> <bean id="mobileZoneResourceController" class="com.wulf.zoneresource.consumer.control.MobileZoneResourceController" />
此时我们可以看到消费端已经去找zookeeper注册了,但这里我本地没有启动zk,所以会报连接超时:
INFO: Initializing Spring root WebApplicationContext [16/08/17 06:41:05:005 CST] localhost-startStop-1 INFO context.ContextLoader: Root WebApplicationContext: initialization started [16/08/17 06:41:05:005 CST] localhost-startStop-1 INFO support.XmlWebApplicationContext: Refreshing Root WebApplicationContext: startup date [Wed Aug 16 18:41:05 CST 2017];
root of context hierarchy [16/08/17 06:41:05:005 CST] localhost-startStop-1 INFO xml.XmlBeanDefinitionReader: Loading XML bean definitions from class path resource [META-INF/spring/consumer.xml] [16/08/17 06:41:05:005 CST] localhost-startStop-1 INFO logger.LoggerFactory: using logger: com.alibaba.dubbo.common.logger.log4j.Log4jLoggerAdapter [16/08/17 06:41:06:006 CST] localhost-startStop-1 INFO support.PropertySourcesPlaceholderConfigurer: Loading properties file from class path resource [global.properties] ... Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mobileZoneResourceService': FactoryBean threw exception on object creation;
nested exception is org.I0Itec.zkclient.exception.ZkTimeoutException: Unable to connect to zookeeper server within timeout: 5000 at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149) at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102) at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1468) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:249) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:917) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:860) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:775) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489) ... 26 more Caused by: org.I0Itec.zkclient.exception.ZkTimeoutException: Unable to connect to zookeeper server within timeout: 5000 at org.I0Itec.zkclient.ZkClient.connect(ZkClient.java:876) at org.I0Itec.zkclient.ZkClient.<init>(ZkClient.java:98) at org.I0Itec.zkclient.ZkClient.<init>(ZkClient.java:92) at org.I0Itec.zkclient.ZkClient.<init>(ZkClient.java:80) at com.alibaba.dubbo.remoting.zookeeper.zkclient.ZkclientZookeeperClient.<init>(ZkclientZookeeperClient.java:26) at com.alibaba.dubbo.remoting.zookeeper.zkclient.ZkclientZookeeperTransporter.connect(ZkclientZookeeperTransporter.java:10) at com.alibaba.dubbo.remoting.zookeeper.ZookeeperTransporter$Adpative.connect(ZookeeperTransporter$Adpative.java) at com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry.<init>(ZookeeperRegistry.java:69) at com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistryFactory.createRegistry(ZookeeperRegistryFactory.java:37) at com.alibaba.dubbo.registry.support.AbstractRegistryFactory.getRegistry(AbstractRegistryFactory.java:94) at com.alibaba.dubbo.registry.RegistryFactory$Adpative.getRegistry(RegistryFactory$Adpative.java) at com.alibaba.dubbo.registry.integration.RegistryProtocol.refer(RegistryProtocol.java:240) at com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper.refer(ProtocolListenerWrapper.java:63) at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper.refer(ProtocolFilterWrapper.java:60) at com.alibaba.dubbo.rpc.Protocol$Adpative.refer(Protocol$Adpative.java) at com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:392) 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:142) ... 34 more