解決java連接zookeeper很慢的問題

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設置的連接超時限制很長。

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: