com.chinagoods.framework.thinkcloud.zookeeper.config.ZookeeperAutoConfigure Maven / Gradle / Ivy
The newest version!
package com.chinagoods.framework.thinkcloud.zookeeper.config;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author : ZHANG.Q
* @author : [email protected]
* @author : 2022/1/12 9:36
*/
@Slf4j
@Configuration
@EnableConfigurationProperties(ZookeeperProperties.class)
public class ZookeeperAutoConfigure {
@Resource
private ZookeeperProperties zookeeperProperties;
@Bean
@ConditionalOnMissingBean
public CuratorFramework client() {
// 重试策略,初试时间1秒,重试10次
RetryPolicy policy = new ExponentialBackoffRetry(zookeeperProperties.getBaseSleepTimeMs(),
zookeeperProperties.getMaxRetries());
// 通过工厂创建Curator
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(zookeeperProperties.getServer())
// .authorization("digest",zookeeperProperties.getDigest().getBytes())
.connectionTimeoutMs(zookeeperProperties.getConnectionTimeoutMs())
.sessionTimeoutMs(zookeeperProperties.getSessionTimeoutMs()).retryPolicy(policy).build();
// 开启连接
client.start();
log.info("think cloud zookeeper init success...");
return client;
}
}