zookeeper作为注册中心

1.配置如下:

<?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:reg="http://www.dangdang.com/schema/ddframe/reg"
	xmlns:job="http://www.dangdang.com/schema/ddframe/job"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.dangdang.com/schema/ddframe/reg
        http://www.dangdang.com/schema/ddframe/reg/reg.xsd
        http://www.dangdang.com/schema/ddframe/job
        http://www.dangdang.com/schema/ddframe/job/job.xsd
        ">

	<!-- 配置注册中心 ,任务的信息都会在zk中存储 -->
	<reg:zookeeper id="regCenter" server-lists="192.168.206.144:2181"
		namespace="test-job" base-sleep-time-milliseconds="1000"
		max-sleep-time-milliseconds="3000" max-retries="4" />
	<!-- 配置简单作业 -->
	<!-- 分片为1,即不需要分片;支持覆盖,即会用本次的配置覆盖缓存在zk中的配置 -->
	<job:simple id="testTask" class="cn.sanishan.ss.job.demo.DemoSimpleJob"
		registry-center-ref="regCenter" cron="0 10 * * * ?"
		sharding-total-count="1" overwrite="true">
	</job:simple>
</beans>

2.启动报错如下:

2017-08-31 17:22:12,614 [main] [org.springframework.beans.factory.support.DefaultListableBeanFactory]-[DEBUG] Eagerly caching bean 'regCenter' to allow for resolving potential circular references
2017-08-31 17:22:12,614 [main] [org.springframework.beans.factory.support.DefaultListableBeanFactory]-[DEBUG] Invoking init method  'init' on bean with name 'regCenter'
2017-08-31 17:22:31,230 [main] [org.springframework.beans.factory.support.DefaultListableBeanFactory]-[DEBUG] Retrieved dependent beans for bean '(inner bean)#6fd02e5': [regCenter]
2017-08-31 17:22:31,230 [main] [org.springframework.context.support.ClassPathXmlApplicationContext]-[WARN] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'regCenter': Invocation of init method failed; nested exception is com.dangdang.ddframe.job.reg.exception.RegException: org.apache.zookeeper.KeeperException$OperationTimeoutException: KeeperErrorCode = OperationTimeout
2017-08-31 17:22:31,231 [main] [org.springframework.beans.factory.support.DefaultListableBeanFactory]-[DEBUG] Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@35fb3008: defining beans [regCenter,com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler#0]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'regCenter': Invocation of init method failed; nested exception is com.dangdang.ddframe.job.reg.exception.RegException: org.apache.zookeeper.KeeperException$OperationTimeoutException: KeeperErrorCode = OperationTimeout
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
	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:93)
	at cn.sanishan.ss.job.demo.StartBySpring.main(StartBySpring.java:8)
Caused by: com.dangdang.ddframe.job.reg.exception.RegException: org.apache.zookeeper.KeeperException$OperationTimeoutException: KeeperErrorCode = OperationTimeout
	at com.dangdang.ddframe.job.reg.exception.RegExceptionHandler.handleException(RegExceptionHandler.java:52)
	at com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter.init(ZookeeperRegistryCenter.java:107)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1758)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1695)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
	... 12 more
Caused by: org.apache.zookeeper.KeeperException$OperationTimeoutException: KeeperErrorCode = OperationTimeout
	at com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter.init(ZookeeperRegistryCenter.java:102)
	... 19 more
原因分析:

1.zookeeper服务器防火墙问题,解决方案:

(1)关闭防火墙
命令:

service iptables stop
(2)如果不想关闭防火墙,在防火墙中添加2181端口

参考:http://blog.csdn.net/u013274055/article/details/77542143

2.修改连接重试次数,一般建议配置3次,如果连接失败,增加重试次数就可以了

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐