spring/springboot整合curator遇到的坑

近期本人在搭建自己的调度平台项目使用到了zookeeper做执行器自动注册中心时,使用到了springboot2.0+curator4.0版本整合,

整个代码:

pom.xml


     org.apache.curator
     curator-framework
     4.0.0


      org.apache.curator
      curator-recipes
      4.0.0

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 ;
                            @Override
                            public List getDefaultAcl() {
                                if(acl ==null) {
                                    ArrayList 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 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服务的版本,完美解决。


            
                org.apache.zookeeper
                zookeeper
                3.4.10
                
                    
                        org.slf4j
                        slf4j-log4j12
                    
                    
                        org.slf4j
                        slf4j-api
                    
                    
                        log4j
                        log4j
                    
                
            

            
                org.apache.curator
                curator-framework
                4.0.0
                
                    
                        org.slf4j
                        slf4j-api
                    
                
            
            
                org.apache.curator
                curator-recipes
                4.0.0
            

 

你可能感兴趣的:(zookeeper,spring,随笔)