spring springboot整合curator的坑
lishentao_1122 人气:0近期本人在搭建自己的调度平台项目使用到了zookeeper做执行器自动注册中心时,使用到了springboot2.0+curator4.0版本整合
整个代码
pom.xml
<dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.0.0</version> </dependency>
zookeeper的config类:
application.properties ################################## zookeeper ################################## kafka.zookeeper.host=127.0.0.1:2181 kafka.zookeeper.maxRetry=3 kafka.zookeeper.sessionTimeout=60000 kafka.zookeeper.connectTimeout=10000 kafka.zookeeper.namespace=sensecrowd
@Configuration @ConfigurationProperties(prefix = "kafka.zookeeper") public class ZookeeperConfig { private final Logger LOGGER = LoggerFactory.getLogger(ZookeeperConfig.class); private String host; private int maxRetry; private int sessionTimeout; private int connectTimeout; private String namespace; @Bean public CuratorFramework curatorFramework(){ RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 1); CuratorFramework client = CuratorFrameworkFactory.builder() .connectString(host) .sessionTimeoutMs(sessionTimeout) .connectionTimeoutMs(connectTimeout) .retryPolicy(retryPolicy) /*.aclProvider(new ACLProvider() { private List<ACL> acl ; @Override public List<ACL> getDefaultAcl() { if(acl ==null) { ArrayList<ACL> acl = ZooDefs.Ids.CREATOR_ALL_ACL; acl.clear(); acl.add(new ACL(ZooDefs.Perms.ALL, new Id("auth", "admin:admin"))); this.acl = acl; } return acl; } @Override public List<ACL> getAclForPath(String s) { return acl; } })*/ // .namespace(namespace) .build(); client.start(); client.getConnectionStateListenable().addListener(new ZookeeperConnectionListener(host,maxRetry,sessionTimeout,connectTimeout,namespace)); return client; } @PreDestroy private void destroyClient(){ curatorFramework().close(); LOGGER.info("==================关闭成功=================="); } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getMaxRetry() { return maxRetry; } public void setMaxRetry(int maxRetry) { this.maxRetry = maxRetry; } public int getSessionTimeout() { return sessionTimeout; } public void setSessionTimeout(int sessionTimeout) { this.sessionTimeout = sessionTimeout; } public int getConnectTimeout() { return connectTimeout; } public void setConnectTimeout(int connectTimeout) { this.connectTimeout = connectTimeout; } public String getNamespace() { return namespace; } public void setNamespace(String namespace) { this.namespace = namespace; } }
可项目遇到了两个问题
1)、项目运行控制台会报Log4j日志警告,原因是我的项目使用的springboot整合的log4j模块而curator包含slf4j的日志包,zookeeper包含log4j的日志包,所以log4j的版本冲突导致。
2)、curator可以查看节点信息,但创建节点会导致程序进程阻塞,根据zookeeper版本不同报出不同的问题,我使用的是默认的curator4.0.0自带的zookeeper版本,3.5.+-beta版,添加节点报,并且程序阻塞:
Unable to read additional data from server sessionid 0x1002fd7768a015f
解决办法
修改pom依赖,第一解决log4j和slaf4j依赖版本冲突问题,第二curator依赖的zookeeper版本修改成你服务器安装的zookeeper服务的版本,完美解决。
<!-- zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.0.0</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.0.0</version> </dependency>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
加载全部内容