解决java连接zookeeper很慢的问题

 更新时间:2021年11月11日 13:02  点击:1864 作者:才爱上

java连接zookeeper很慢

最近在学习zookeeper,但是在连接zookeeper服务端时很慢,每次都超过18s

在头条尝试搜索了一下这个问题,找到了答案

原因是在服务器端集群时,设置的host是使用hostname,而不是ip,且客户端没有配置hostname和ip的映射导致的问题,

在客户端加上第一张图中的ip映射关系即可,或者把服务端的server中的hostname换成ip即可

记一次惨痛的zookeeper连接教训

因为工作需要导致需要重启tomcat,结果重启一直不成功,tomcat每次都卡在一个地方,也没用报错信息。由于一直没头绪,所以暂时先放着,让tomcat自己在那卡着,哈哈哈~~~

后面,大概半小时过去查看tomcat日志发现出现了一个错误(第一次看到报错这么激动)

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘appInvestConsultaApiImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.linewell.invest.api.IInvestConsultaApi com.linewell.guide.api.invest.impl.AppInvestConsultaApiImpl.investConsultaApi; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘investConsultaAPI': FactoryBean threw exception on object creation; nested exception is org.I0Itec.zkclient.exception.ZkTimeoutException: Unable to connect to zookeeper server within timeout: 2000000
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory 1. g e t O b j e c t ( A b s t r a c t B e a n F a c t o r y . j a v a : 303 ) a t o r g . s p r i n g f r a m e w o r k . b e a n s . f a c t o r y . s u p p o r t . D e f a u l t S i n g l e t o n B e a n R e g i s t r y . g e t S i n g l e t o n ( D e f a u l t S i n g l e t o n B e a n R e g i s t r y . j a v a : 230 ) a t o r g . s p r i n g f r a m e w o r k . b e a n s . f a c t o r y . s u p p o r t . A b s t r a c t B e a n F a c t o r y . d o G e t B e a n ( A b s t r a c t B e a n F a c t o r y . j a v a : 299 ) a t o r g . s p r i n g f r a m e w o r k . b e a n s . f a c t o r y . s u p p o r t . A b s t r a c t B e a n F a c t o r y . g e t B e a n ( A b s t r a c t B e a n F a c t o r y . j a v a : 194 ) a t o r g . s p r i n g f r a m e w o r k . b e a n s . f a c t o r y . s u p p o r t . D e f a u l t L i s t a b l e B e a n F a c t o r y . p r e I n s t a n t i a t e S i n g l e t o n s ( D e f a u l t L i s t a b l e B e a n F a c t o r y . j a v a : 755 ) a t o r g . s p r i n g f r a m e w o r k . c o n t e x t . s u p p o r t . A b s t r a c t A p p l i c a t i o n C o n t e x t . f i n i s h B e a n F a c t o r y I n i t i a l i z a t i o n ( A b s t r a c t A p p l i c a t i o n C o n t e x t . j a v a : 757 ) a t o r g . s p r i n g f r a m e w o r k . c o n t e x t . s u p p o r t . A b s t r a c t A p p l i c a t i o n C o n t e x t . r e f r e s h ( A b s t r a c t A p p l i c a t i o n C o n t e x t . j a v a : 480 ) a t o r g . s p r i n g f r a m e w o r k . w e b . c o n t e x t . C o n t e x t L o a d e r . c o n f i g u r e A n d R e f r e s h W e b A p p l i c a t i o n C o n t e x t ( C o n t e x t L o a d e r . j a v a : 434 ) a t o r g . s p r i n g f r a m e w o r k . w e b . c o n t e x t . C o n t e x t L o a d e r . i n i t W e b A p p l i c a t i o n C o n t e x t ( C o n t e x t L o a d e r . j a v a : 306 ) a t o r g . s p r i n g f r a m e w o r k . w e b . c o n t e x t . C o n t e x t L o a d e r L i s t e n e r . c o n t e x t I n i t i a l i z e d ( C o n t e x t L o a d e r L i s t e n e r . j a v a : 106 ) a t o r g . a p a c h e . c a t a l i n a . c o r e . S t a n d a r d C o n t e x t . l i s t e n e r S t a r t ( S t a n d a r d C o n t e x t . j a v a : 5157 ) a t o r g . a p a c h e . c a t a l i n a . c o r e . S t a n d a r d C o n t e x t . s t a r t I n t e r n a l ( S t a n d a r d C o n t e x t . j a v a : 5680 ) a t o r g . a p a c h e . c a t a l i n a . u t i l . L i f e c y c l e B a s e . s t a r t ( L i f e c y c l e B a s e . j a v a : 145 ) a t o r g . a p a c h e . c a t a l i n a . c o r e . C o n t a i n e r B a s e . a d d C h i l d I n t e r n a l ( C o n t a i n e r B a s e . j a v a : 1018 ) a t o r g . a p a c h e . c a t a l i n a . c o r e . C o n t a i n e r B a s e . a d d C h i l d ( C o n t a i n e r B a s e . j a v a : 994 ) a t o r g . a p a c h e . c a t a l i n a . c o r e . S t a n d a r d H o s t . a d d C h i l d ( S t a n d a r d H o s t . j a v a : 652 ) a t o r g . a p a c h e . c a t a l i n a . s t a r t u p . H o s t C o n f i g . d e p l o y D e s c r i p t o r ( H o s t C o n f i g . j a v a : 712 ) a t o r g . a p a c h e . c a t a l i n a . s t a r t u p . H o s t C o n f i g 1.getObject(AbstractBeanFactory.java:303) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:434) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5157) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5680) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1018) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:994) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652) at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:712) at org.apache.catalina.startup.HostConfig 1.getObject(AbstractBeanFactory.java:303)atorg.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)atorg.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)atorg.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)atorg.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)atorg.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)atorg.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:434)atorg.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)atorg.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)atorg.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5157)atorg.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5680)atorg.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)atorg.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1018)atorg.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:994)atorg.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)atorg.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:712)atorg.apache.catalina.startup.HostConfigDeployDescriptor.run(HostConfig.java:2003)
at java.util.concurrent.Executors R u n n a b l e A d a p t e r . c a l l ( E x e c u t o r s . j a v a : 511 ) a t j a v a . u t i l . c o n c u r r e n t . F u t u r e T a s k . r u n ( F u t u r e T a s k . j a v a : 266 ) a t j a v a . u t i l . c o n c u r r e n t . T h r e a d P o o l E x e c u t o r . r u n W o r k e r ( T h r e a d P o o l E x e c u t o r . j a v a : 1149 ) a t j a v a . u t i l . c o n c u r r e n t . T h r e a d P o o l E x e c u t o r RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor RunnableAdapter.call(Executors.java:511)atjava.util.concurrent.FutureTask.run(FutureTask.java:266)atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)atjava.util.concurrent.ThreadPoolExecutorWorker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.linewell.invest.api.IInvestConsultaApi com.linewell.guide.api.invest.impl.AppInvestConsultaApiImpl.investConsultaApi; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘investConsultaAPI': FactoryBean threw exception on object creation; nested exception is org.I0Itec.zkclient.exception.ZkTimeoutException: Unable to connect to zookeeper server within timeout: 2000000
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor A u t o w i r e d F i e l d E l e m e n t . i n j e c t ( A u t o w i r e d A n n o t a t i o n B e a n P o s t P r o c e s s o r . j a v a : 561 ) a t o r g . s p r i n g f r a m e w o r k . b e a n s . f a c t o r y . a n n o t a t i o n . I n j e c t i o n M e t a d a t a . i n j e c t ( I n j e c t i o n M e t a d a t a . j a v a : 88 ) a t o r g . s p r i n g f r a m e w o r k . b e a n s . f a c t o r y . a n n o t a t i o n . A u t o w i r e d A n n o t a t i o n B e a n P o s t P r o c e s s o r . p o s t P r o c e s s P r o p e r t y V a l u e s ( A u t o w i r e d A n n o t a t i o n B e a n P o s t P r o c e s s o r . j a v a : 331 ) . . . 26 m o r e C a u s e d b y : o r g . s p r i n g f r a m e w o r k . b e a n s . f a c t o r y . B e a n C r e a t i o n E x c e p t i o n : E r r o r c r e a t i n g b e a n w i t h n a m e ′ i n v e s t C o n s u l t a A P I ′ : F a c t o r y B e a n t h r e w e x c e p t i o n o n o b j e c t c r e a t i o n ; n e s t e d e x c e p t i o n i s o r g . I 0 I t e c . z k c l i e n t . e x c e p t i o n . Z k T i m e o u t E x c e p t i o n : U n a b l e t o c o n n e c t t o z o o k e e p e r s e r v e r w i t h i n t i m e o u t : 2000000 a t o r g . s p r i n g f r a m e w o r k . b e a n s . f a c t o r y . s u p p o r t . F a c t o r y B e a n R e g i s t r y S u p p o r t . d o G e t O b j e c t F r o m F a c t o r y B e a n ( F a c t o r y B e a n R e g i s t r y S u p p o r t . j a v a : 175 ) a t o r g . s p r i n g f r a m e w o r k . b e a n s . f a c t o r y . s u p p o r t . F a c t o r y B e a n R e g i s t r y S u p p o r t . g e t O b j e c t F r o m F a c t o r y B e a n ( F a c t o r y B e a n R e g i s t r y S u p p o r t . j a v a : 103 ) a t o r g . s p r i n g f r a m e w o r k . b e a n s . f a c t o r y . s u p p o r t . A b s t r a c t B e a n F a c t o r y . g e t O b j e c t F o r B e a n I n s t a n c e ( A b s t r a c t B e a n F a c t o r y . j a v a : 1523 ) a t o r g . s p r i n g f r a m e w o r k . b e a n s . f a c t o r y . s u p p o r t . A b s t r a c t B e a n F a c t o r y . d o G e t B e a n ( A b s t r a c t B e a n F a c t o r y . j a v a : 251 ) a t o r g . s p r i n g f r a m e w o r k . b e a n s . f a c t o r y . s u p p o r t . A b s t r a c t B e a n F a c t o r y . g e t B e a n ( A b s t r a c t B e a n F a c t o r y . j a v a : 194 ) a t o r g . s p r i n g f r a m e w o r k . b e a n s . f a c t o r y . s u p p o r t . D e f a u l t L i s t a b l e B e a n F a c t o r y . f i n d A u t o w i r e C a n d i d a t e s ( D e f a u l t L i s t a b l e B e a n F a c t o r y . j a v a : 1120 ) a t o r g . s p r i n g f r a m e w o r k . b e a n s . f a c t o r y . s u p p o r t . D e f a u l t L i s t a b l e B e a n F a c t o r y . d o R e s o l v e D e p e n d e n c y ( D e f a u l t L i s t a b l e B e a n F a c t o r y . j a v a : 1044 ) a t o r g . s p r i n g f r a m e w o r k . b e a n s . f a c t o r y . s u p p o r t . D e f a u l t L i s t a b l e B e a n F a c t o r y . r e s o l v e D e p e n d e n c y ( D e f a u l t L i s t a b l e B e a n F a c t o r y . j a v a : 942 ) a t o r g . s p r i n g f r a m e w o r k . b e a n s . f a c t o r y . a n n o t a t i o n . A u t o w i r e d A n n o t a t i o n B e a n P o s t P r o c e s s o r AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ... 26 more Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'investConsultaAPI': FactoryBean threw exception on object creation; nested exception is org.I0Itec.zkclient.exception.ZkTimeoutException: Unable to connect to zookeeper server within timeout: 2000000 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:1523) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)atorg.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)...26moreCausedby:org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname′investConsultaAPI′:FactoryBeanthrewexceptiononobjectcreation;nestedexceptionisorg.I0Itec.zkclient.exception.ZkTimeoutException:Unabletoconnecttozookeeperserverwithintimeout:2000000atorg.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:175)atorg.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)atorg.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1523)atorg.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)atorg.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120)atorg.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044)atorg.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessorAutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
… 28 more
Caused by: org.I0Itec.zkclient.exception.ZkTimeoutException: Unable to connect to zookeeper server within timeout: 2000000
at org.I0Itec.zkclient.ZkClient.connect(ZkClient.java:876)
at org.I0Itec.zkclient.ZkClient.(ZkClient.java:98)
at org.I0Itec.zkclient.ZkClient.(ZkClient.java:92)
at org.I0Itec.zkclient.ZkClient.(ZkClient.java:80)
at com.alibaba.dubbo.remoting.zookeeper.zkclient.ZkclientZookeeperClient.(ZkclientZookeeperClient.java:26)
at com.alibaba.dubbo.remoting.zookeeper.zkclient.ZkclientZookeeperTransporter.connect(ZkclientZookeeperTransporter.java:10)
at com.alibaba.dubbo.remoting.zookeeper.ZookeeperTransporter A d p a t i v e . c o n n e c t ( Z o o k e e p e r T r a n s p o r t e r Adpative.connect(ZookeeperTransporter Adpative.connect(ZookeeperTransporterAdpative.java)
at com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry.(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 A d p a t i v e . g e t R e g i s t r y ( R e g i s t r y F a c t o r y Adpative.getRegistry(RegistryFactory Adpative.getRegistry(RegistryFactoryAdpative.java)
at com.alibaba.dubbo.registry.integration.RegistryProtocol.refer(RegistryProtocol.java:240)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper.refer(ProtocolFilterWrapper.java:60)
at com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper.refer(ProtocolListenerWrapper.java:63)
at com.alibaba.dubbo.rpc.Protocol A d p a t i v e . r e f e r ( P r o t o c o l Adpative.refer(Protocol Adpative.refer(ProtocolAdpative.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:168)
… 36 more

然后确认是zookeeper连接的问题,确认问题后该找原因咯,为什么之前没问题现在出现了,由于项目是采用dubbo框架,所以就去dubbo框架的配置文件中找问题

配置文件

因为是在一个tomcat上跑多个项目,前两个项目部署成功到第三个就出错了,于是比较3个配置文件的差异,发现问题就在id为tzydZk这个上面了,于是ping了这个地址发现ping不通,说明服务器那边出问题了。结果一问,是服务器地址改动了。。。。。

简直无语。修改地址为新的服务器地址,搞定。

总结:dubbo+zookeeper在部署项目时,若出现卡主无响应情况,可以检查项目中的zookeeper是否能正常运行,此处会卡主很久是因为zookeeper设置的连接超时限制很长。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持猪先飞。

原文出处:https://blog.csdn.net/tt_rsn/article/details/106954735

[!--infotagslink--]

相关文章