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. 创建配置类绑定属性
使用 @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
)。 - 属性键命名规则:
- 使用
kebab-case
(如zookeeper.source.default.servers
)。 - 绑定到
@configurationproperties
时,自动转换为驼峰命名(如source.default.servers
→source.get("default").getservers()
)。
- 使用
- 优先级:
- 系统属性(
-d
参数) > 配置文件 > 代码默认值。
- 系统属性(
- 异常处理:在 zk 客户端初始化时捕获
ioexception
,并提供友好的错误提示。 - 单例与依赖注入:避免在 spring 容器初始化前手动创建
zookeeperclient
(如在main
方法中),应通过@autowired
注入。
对比:传统属性读取 vs spring boot 原生方式
方式 | 优点 | 缺点 |
---|---|---|
传统 propertyconfig | 简单直接,无需 spring 依赖 | 不支持自动刷新、类型安全、配置校验 |
spring boot @configurationproperties | 类型安全、支持校验、自动刷新(需配置) | 需要定义配置类,学习成本略高 |
通过以上步骤,你可以在 spring boot 中优雅地读取和管理 zookeeper 的配置属性。
到此这篇关于springboot读取zookeeper(zk)属性的方法实现的文章就介绍到这了,更多相关springboot读取zookeeper内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论