spring/springboot整合curator遇到的坑及解決
近期本人在搭建自己的調(diào)度平臺項目使用到了zookeeper做執(zhí)行器自動注冊中心時,使用到了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可以查看節(jié)點信息,但創(chuàng)建節(jié)點會導致程序進程阻塞,根據(jù)zookeeper版本不同報出不同的問題,我使用的是默認的curator4.0.0自帶的zookeeper版本,3.5.+-beta版,添加節(jié)點報,并且程序阻塞:
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>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring MVC中使用Controller如何進行重定向
這篇文章主要介紹了Spring MVC中使用Controller如何進行重定向操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
MyEclipse+Tomcat+MAVEN+SVN項目完整環(huán)境搭建(圖文教程)
這篇文章主要介紹了MyEclipse+Tomcat+MAVEN+SVN項目完整環(huán)境搭建(圖文教程),非常具有實用價值,需要的朋友可以參考下2017-12-12
SpringBoot接收數(shù)組參數(shù)和集合參數(shù)方式
這篇文章主要介紹了SpringBoot接收數(shù)組參數(shù)和集合參數(shù)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03
一文搞懂Java?ScheduledExecutorService的使用
JUC包(java.util.concurrent)中提供了對定時任務的支持,即ScheduledExecutorService接口。本文主要對ScheduledExecutorService的使用進行簡單的介紹,需要的可以參考一下2022-11-11

