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("==================關(guān)閉成功==================");
? ? }
?
? ? 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的版本沖突導(dǎo)致。
2)、curator可以查看節(jié)點信息,但創(chuàng)建節(jié)點會導(dǎo)致程序進(jìn)程阻塞,根據(jù)zookeeper版本不同報出不同的問題,我使用的是默認(rèn)的curator4.0.0自帶的zookeeper版本,3.5.+-beta版,添加節(jié)點報,并且程序阻塞:
Unable to read additional data from server sessionid 0x1002fd7768a015f
解決辦法
修改pom依賴,第一解決log4j和slaf4j依賴版本沖突問題,第二curator依賴的zookeeper版本修改成你服務(wù)器安裝的zookeeper服務(wù)的版本,完美解決。
<!-- 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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java學(xué)習(xí)之JasperReport踩坑
本篇文章介紹的是在JAVA學(xué)習(xí)中JasperReport遇到的坑以及解決辦法,有需要的朋友參考下吧。2018-01-01
SpringBoot JWT實現(xiàn)token登錄刷新功能
JWT本身是無狀態(tài)的,這點有別于傳統(tǒng)的session,不在服務(wù)端存儲憑證。這種特性使其在分布式場景,更便于擴(kuò)展使用。接下來通過本文給大家分享SpringBoot JWT實現(xiàn)token登錄刷新功能,感興趣的朋友一起看看吧2021-09-09
SpringBoot使用AOP實現(xiàn)防重復(fù)提交功能
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何使用AOP實現(xiàn)防重復(fù)提交功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
Java8接口中引入default關(guān)鍵字的本質(zhì)原因詳析
Default方法是在java8中引入的關(guān)鍵字,也可稱為Virtual extension methods—虛擬擴(kuò)展方法,這篇文章主要給大家介紹了關(guān)于Java8接口中引入default關(guān)鍵字的本質(zhì)原因,需要的朋友可以參考下2022-01-01
Java開發(fā)之普通web項目轉(zhuǎn)為Maven項目的方法
這篇文章主要給大家介紹了關(guān)于Java開發(fā)之普通web項目轉(zhuǎn)為Maven項目的相關(guān)資料,文中通過圖文將轉(zhuǎn)換的方法步驟介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
利用Spring?Boot和JPA創(chuàng)建GraphQL?API
這篇文章主要介紹了利用Spring?Boot和JPA創(chuàng)建GraphQL?API,GraphQL既是API查詢語言,也是使用當(dāng)前數(shù)據(jù)執(zhí)行這些查詢的運行時,下文更多相關(guān)內(nèi)容介紹需要的小伙伴可以參考一下2022-04-04
SpringBoot利用自定義注解實現(xiàn)隱私數(shù)據(jù)脫敏(加密顯示)的解決方案
這兩天在整改等保測出的問題,里面有一個“用戶信息泄露”的風(fēng)險項(就是后臺系統(tǒng)里用戶的一些隱私數(shù)據(jù)直接明文顯示了),其實指的就是要做數(shù)據(jù)脫敏,本文給大家介紹了SpringBoot利用自定義注解實現(xiàn)隱私數(shù)據(jù)脫敏(加密顯示)的解決方案,需要的朋友可以參考下2023-11-11

