SpringBoot讀取ZooKeeper(ZK)屬性的方法實現(xiàn)
1. 在配置文件中定義 ZK 屬性
在 application.properties
或 application.yml
中添加 ZK 相關配置:
application.properties
# 單源配置示例 zookeeper.source.default.rootnode=/democonfig zookeeper.source.default.servers=192.168.124.1:2181,192.168.124.2:2181 zookeeper.source.default.acls=root:iiot!@#zk$ # 多源配置示例(可選) zookeeper.source.backup.rootnode=/backup-config zookeeper.source.backup.servers=192.168.124.3:2181
application.yml
zookeeper: source: default: rootnode: /democonfig servers: 192.168.124.1:2181,192.168.124.2:2181 acls: root:iiot!@#zk$ backup: rootnode: /backup-config servers: 192.168.124.3:2181
2. 創(chuàng)建配置類綁定屬性
使用 @ConfigurationProperties
綁定 ZK 配置:
import lombok.Getter; import lombok.Setter; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Map; @Component @ConfigurationProperties(prefix = "zookeeper") @Getter @Setter public class ZookeeperConfig { private Map<String, SourceProperties> source = new HashMap<>(); @Getter @Setter public static class SourceProperties { private String rootnode; private String servers; private String acls; } }
3. 初始化 ZK 客戶端
在 Spring 容器中初始化 ZK 客戶端,確保配置已注入:
import org.apache.zookeeper.ZooKeeper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.io.IOException; @Component public class ZookeeperClient { private static final int SESSION_TIMEOUT = 5000; private final ZooKeeper zooKeeper; @Autowired public ZookeeperClient(ZookeeperConfig zookeeperConfig) { try { // 獲取默認源配置 ZookeeperConfig.SourceProperties source = zookeeperConfig.getSource().get("default"); if (source == null) { throw new IllegalArgumentException("ZooKeeper source 'default' not configured"); } this.zooKeeper = new ZooKeeper( source.getServers(), SESSION_TIMEOUT, null ); } catch (IOException e) { throw new RuntimeException("Failed to connect to ZooKeeper", e); } } public ZooKeeper getZooKeeper() { return zooKeeper; } }
4. 使用配置屬性
在任意 Spring Bean 中注入 ZookeeperConfig
或 ZookeeperClient
,并讀取屬性:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class MyService { @Autowired private ZookeeperConfig zookeeperConfig; @Autowired private ZookeeperClient zookeeperClient; public void printZkConfig() { // 直接讀取配置 ZookeeperConfig.SourceProperties source = zookeeperConfig.getSource().get("default"); System.out.println("ZK Root Node: " + source.getRootnode()); System.out.println("ZK Servers: " + source.getServers()); // 通過客戶端使用 ZK 連接 ZooKeeper zk = zookeeperClient.getZooKeeper(); System.out.println("ZK Session ID: " + zk.getSessionId()); } }
5. 多源配置支持
如果需要切換不同的 ZK 源(如 default
和 backup
),可擴展 ZookeeperClient
:
@Component public class ZookeeperClient { private final Map<String, ZooKeeper> clients = new HashMap<>(); @Autowired public ZookeeperClient(ZookeeperConfig zookeeperConfig) { zookeeperConfig.getSource().forEach((name, config) -> { try { clients.put(name, new ZooKeeper( config.getServers(), SESSION_TIMEOUT, null )); } catch (IOException e) { throw new RuntimeException("Failed to connect to ZooKeeper source: " + name, e); } }); } public ZooKeeper getClient(String sourceName) { return clients.getOrDefault(sourceName, clients.get("default")); } }
6. 注意事項
- 配置文件路徑:確保
application.properties
或application.yml
在類路徑下(src/main/resources
)。 - 屬性鍵命名規(guī)則:
- 使用
kebab-case
(如zookeeper.source.default.servers
)。 - 綁定到
@ConfigurationProperties
時,自動轉換為駝峰命名(如source.default.servers
→source.get("default").getServers()
)。
- 使用
- 優(yōu)先級:
- 系統(tǒng)屬性(
-D
參數(shù)) > 配置文件 > 代碼默認值。
- 系統(tǒng)屬性(
- 異常處理:在 ZK 客戶端初始化時捕獲
IOException
,并提供友好的錯誤提示。 - 單例與依賴注入:避免在 Spring 容器初始化前手動創(chuàng)建
ZookeeperClient
(如在main
方法中),應通過@Autowired
注入。
對比:傳統(tǒng)屬性讀取 vs Spring Boot 原生方式
方式 | 優(yōu)點 | 缺點 |
---|---|---|
傳統(tǒng) PropertyConfig | 簡單直接,無需 Spring 依賴 | 不支持自動刷新、類型安全、配置校驗 |
Spring Boot @ConfigurationProperties | 類型安全、支持校驗、自動刷新(需配置) | 需要定義配置類,學習成本略高 |
通過以上步驟,你可以在 Spring Boot 中優(yōu)雅地讀取和管理 ZooKeeper 的配置屬性。
到此這篇關于SpringBoot讀取ZooKeeper(ZK)屬性的方法實現(xiàn)的文章就介紹到這了,更多相關SpringBoot讀取ZooKeeper內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- 淺談Java(SpringBoot)基于zookeeper的分布式鎖實現(xiàn)
- 使用dubbo+zookeeper+spring boot構建服務的方法詳解
- SpringBoot中dubbo+zookeeper實現(xiàn)分布式開發(fā)的應用詳解
- SpringBoot集成Curator實現(xiàn)Zookeeper基本操作的代碼示例
- SpringBoot系列教程之dubbo和Zookeeper集成方法
- SpringBoot整合Dubbo+Zookeeper實現(xiàn)RPC調用
- springboot應用訪問zookeeper的流程
- SpringBoot整合Zookeeper詳細教程
- Java Spring Boot 集成Zookeeper
相關文章
Spring之InitializingBean接口和DisposableBean接口的使用
這篇文章主要介紹了Spring之InitializingBean接口和DisposableBean接口的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01mybatis插入數(shù)據(jù)后如何返回新增數(shù)據(jù)的id值
當往mysql數(shù)據(jù)庫插入一條數(shù)據(jù)時,有時候需要知道剛插入的信息,下面這篇文章主要給大家介紹了關于mybatis插入數(shù)據(jù)后如何返回新增數(shù)據(jù)id值的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-06-06SpringBoot 編程式事務使用及兩種實現(xiàn)方式
編程式事務管理是通過編寫代碼來管理事務,相對于聲明式事務(@Transactional注解),它提供了更細粒度的事務控制,這篇文章主要介紹了SpringBoot 編程式事務使用及兩種實現(xiàn)方式,需要的朋友可以參考下2024-12-12springboot?max-http-header-size最大長度的那些事及JVM調優(yōu)方式
這篇文章主要介紹了springboot?max-http-header-size最大長度的那些事及JVM調優(yōu)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09