配置服务客户端, 定义接口读取apollo/diamond配置.
<dependency>
<groupId>com.github.charlemaznable</groupId>
<artifactId>char-configservice</artifactId>
<version>2024.0.0</version>
</dependency>
<dependency>
<groupId>com.github.charlemaznable</groupId>
<artifactId>char-configservice</artifactId>
<version>2024.0.1-SNAPSHOT</version>
</dependency>
1.1.1. 简单配置
# namespace:XXX propertyName:YYY
ZZZ
@ApolloConfig(namespace = "XXX")
public interface MyConfig {
@ApolloConfig("YYY")
String value();
}
MyConfig myConfig = ApolloFactory.getApollo(MyConfig.class);
String value = myConfig.value(); // value: "ZZZ"
1.1.2. Properties配置
# namespace:XXX propertyName:YYY
key1=value1
key2=value2
@ApolloConfig(namespace = "XXX")
public interface MyConfig {
@ApolloConfig("YYY")
Properties value();
}
MyConfig myConfig = ApolloFactory.getApollo(MyConfig.class);
Properties value = myConfig.value();
String value1 = value.getProperty("key1"); // value1: "value1"
String value2 = value.getProperty("key2"); // value2: "value2"
1.1.3. Properties配置按键读取
# namespace:XXX propertyName:YYY
key1=value1
key2=value2
@ApolloConfig(namespace = "XXX", propertyName = "YYY")
public interface MyConfig {
@ApolloConfig("key1")
String key1();
@ApolloConfig("key2")
String key2();
}
MyConfig myConfig = ApolloFactory.getApollo(MyConfig.class);
String value1 = myConfig.key1(); // value1: "value1"
String value2 = myConfig.key2(); // value2: "value2"
与apollo配置类似, diamond配置的group:dataId
对应apollo配置的namespace:propertyName
.
将java代码中的@ApolloConfig
替换为@DiamondConfig
, 将ApolloFactory.getApollo()
替换为DiamondFactory.getDiamond()
即可.
与apollo配置类似, etcd配置的namespace:key
对应apollo配置的namespace:propertyName
.
将java代码中的@ApolloConfig
替换为@EtcdConfig
, 将ApolloFactory.getApollo()
替换为EtcdFactory.getEtcd()
即可.
编写配置客户端接口时, 需要使用@ApolloConfig
/@DiamondConfig
/@EtcdConfig
注解, 在接口上标识其为配置客户端接口, 在接口或方法上指定配置读取的坐标.
- 当注解在接口上时,
namespace
/group
/namespace
的默认值为"application"
/"DEFAULT_GROUP"
/"application"
,propertyName
/dataId
/key
/value
的默认值为""
. - 当注解在方法上时, 上述注解属性的默认值都是
""
.
@ApolloConfig
@DiamondConfig
@EtcdConfig
public interface MyConfig {
@ApolloConfig // 可省略
@DiamondConfig // 可省略
@EtcdConfig // 可省略
String value();
}
读取配置:
# apollo
namespace: application
propertyName: value
# diamond
group: DEFAULT_GROUP
dataId: value
# etcd
namespace: application
key: value
String value();方法返回配置完整内容字符串
@ApolloConfig(namespace = "XXX")
@DiamondConfig(group = "XXX")
@EtcdConfig(namespace = "XXX")
public interface MyConfig {
@ApolloConfig // 可省略
@DiamondConfig // 可省略
@EtcdConfig // 可省略
String value();
}
读取配置:
# apollo
namespace: XXX
propertyName: value
# diamond
group: XXX
dataId: value
# etcd
namespace: XXX
key: value
String value();方法返回配置完整内容字符串
@ApolloConfig(propertyName = "YYY")
@DiamondConfig(dataId = "YYY")
@EtcdConfig(key = "YYY")
public interface MyConfig {
@ApolloConfig // 可省略
@DiamondConfig // 可省略
@EtcdConfig // 可省略
String value();
}
读取配置:
# apollo
namespace: application
propertyName: YYY
# diamond
group: DEFAULT_GROUP
dataId: YYY
# etcd
namespace: application
key: YYY
String value();方法返回配置内容按Properties解析后的键value的配置值
@ApolloConfig
@DiamondConfig
@EtcdConfig
public interface MyConfig {
@ApolloConfig(namespace = "ZZZ")
@DiamondConfig(group = "ZZZ")
@EtcdConfig(namespace = "ZZZ")
String value();
}
读取配置:
# apollo
namespace: ZZZ
propertyName: value
# diamond
group: ZZZ
dataId: value
# etcd
namespace: ZZZ
key: value
String value();方法返回配置完整内容字符串
@ApolloConfig
@DiamondConfig
@EtcdConfig
public interface MyConfig {
@ApolloConfig(propertyName = "abc")
@DiamondConfig(dataId = "abc")
@EtcdConfig(key = "abc")
String value();
}
读取配置:
# apollo
namespace: application
propertyName: abc
# diamond
group: DEFAULT_GROUP
dataId: abc
# etcd
namespace: application
key: abc
String value();方法返回配置完整内容字符串
@ApolloConfig(namespace = "XXX", propertyName = "YYY")
@DiamondConfig(group = "XXX", dataId = "YYY")
@EtcdConfig(namespace = "XXX", key = "YYY")
public interface MyConfig {
@ApolloConfig // 可省略
@DiamondConfig // 可省略
@EtcdConfig // 可省略
String value();
}
读取配置:
# apollo
namespace: XXX
propertyName: YYY
# diamond
group: XXX
dataId: YYY
# etcd
namespace: XXX
key: YYY
String value();方法返回配置内容按Properties解析后的键value的配置值
@ApolloConfig
@DiamondConfig
@EtcdConfig
public interface MyConfig {
@ApolloConfig(namespace = "ZZZ", propertyName = "abc")
@DiamondConfig(group = "ZZZ", dataId = "abc")
@EtcdConfig(namespace = "ZZZ", key = "abc")
String value();
}
读取配置:
# apollo
namespace: ZZZ
propertyName: abc
# diamond
group: ZZZ
dataId: abc
# etcd
namespace: ZZZ
key: abc
String value();方法返回配置完整内容字符串
@ApolloConfig(namespace = "XXX")
@DiamondConfig(group = "XXX")
@EtcdConfig(namespace = "XXX")
public interface MyConfig {
@ApolloConfig(namespace = "ZZZ")
@DiamondConfig(group = "ZZZ")
@EtcdConfig(namespace = "ZZZ")
String value();
}
读取配置:
# apollo
namespace: ZZZ
propertyName: value
# diamond
group: ZZZ
dataId: value
# etcd
namespace: ZZZ
key: value
String value();方法返回配置完整内容字符串
@ApolloConfig(propertyName = "YYY")
@DiamondConfig(dataId = "YYY")
@EtcdConfig(key = "YYY")
public interface MyConfig {
@ApolloConfig(propertyName = "abc")
@DiamondConfig(dataId = "abc")
@EtcdConfig(key = "abc")
String value();
}
读取配置:
# apollo
namespace: application
propertyName: YYY
# diamond
group: DEFAULT_GROUP
dataId: YYY
# etcd
namespace: application
key: YYY
String value();方法返回配置内容按Properties解析后的键abc的配置值
@ApolloConfig(namespace = "XXX")
@DiamondConfig(group = "XXX")
@EtcdConfig(namespace = "XXX")
public interface MyConfig {
@ApolloConfig(propertyName = "abc")
@DiamondConfig(dataId = "abc")
@EtcdConfig(key = "abc")
String value();
}
读取配置:
# apollo
namespace: XXX
propertyName: abc
# diamond
group: XXX
dataId: abc
# etcd
namespace: XXX
key: abc
String value();方法返回配置完整内容字符串
@ApolloConfig(propertyName = "YYY")
@DiamondConfig(dataId = "YYY")
@EtcdConfig(key = "YYY")
public interface MyConfig {
@ApolloConfig(namespace = "ZZZ")
@DiamondConfig(group = "ZZZ")
@EtcdConfig(namespace = "ZZZ")
String value();
}
读取配置:
# apollo
namespace: application
propertyName: YYY
# diamond
group: DEFAULT_GROUP
dataId: YYY
# etcd
namespace: application
key: YYY
String value();方法返回配置内容按Properties解析后的键ZZZ.value的配置值
@ApolloConfig(propertyName = "YYY")
@DiamondConfig(dataId = "YYY")
@EtcdConfig(key = "YYY")
public interface MyConfig {
@ApolloConfig(namespace = "ZZZ", propertyName = "abc")
@DiamondConfig(group = "ZZZ", dataId = "abc")
@EtcdConfig(namespace = "ZZZ", key = "abc")
String value();
}
读取配置:
# apollo
namespace: application
propertyName: YYY
# diamond
group: DEFAULT_GROUP
dataId: YYY
# etcd
namespace: application
key: YYY
String value();方法返回配置内容按Properties解析后的键ZZZ.abc的配置值
@ApolloConfig(namespace = "XXX")
@DiamondConfig(group = "XXX")
@EtcdConfig(namespace = "XXX")
public interface MyConfig {
@ApolloConfig(namespace = "ZZZ", propertyName = "abc")
@DiamondConfig(group = "ZZZ", dataId = "abc")
@EtcdConfig(namespace = "ZZZ", key = "abc")
String value();
}
读取配置:
# apollo
namespace: ZZZ
propertyName: abc
# diamond
group: ZZZ
dataId: abc
# etcd
namespace: ZZZ
key: abc
String value();方法返回配置完整内容字符串
@ApolloConfig(namespace = "XXX", propertyName = "YYY")
@DiamondConfig(group = "XXX", dataId = "YYY")
@EtcdConfig(namespace = "XXX", key = "YYY")
public interface MyConfig {
@ApolloConfig(propertyName = "abc")
@DiamondConfig(dataId = "abc")
@EtcdConfig(key = "abc")
String value();
}
读取配置:
# apollo
namespace: XXX
propertyName: YYY
# diamond
group: XXX
dataId: YYY
# etcd
namespace: XXX
key: YYY
String value();方法返回配置内容按Properties解析后的键abc的配置值
@ApolloConfig(namespace = "XXX", propertyName = "YYY")
@DiamondConfig(group = "XXX", dataId = "YYY")
@EtcdConfig(namespace = "XXX", key = "YYY")
public interface MyConfig {
@ApolloConfig(namespace = "ZZZ")
@DiamondConfig(group = "ZZZ")
@EtcdConfig(namespace = "ZZZ")
String value();
}
读取配置:
# apollo
namespace: XXX
propertyName: YYY
# diamond
group: XXX
dataId: YYY
# etcd
namespace: XXX
key: YYY
String value();方法返回配置内容按Properties解析后的键ZZZ.value的配置值
@ApolloConfig(namespace = "XXX", propertyName = "YYY")
@DiamondConfig(group = "XXX", dataId = "YYY")
@EtcdConfig(namespace = "XXX", key = "YYY")
public interface MyConfig {
@ApolloConfig(namespace = "ZZZ", propertyName = "abc")
@DiamondConfig(group = "ZZZ", dataId = "abc")
@EtcdConfig(namespace = "ZZZ", key = "abc")
String value();
}
读取配置:
# apollo
namespace: XXX
propertyName: YYY
# diamond
group: XXX
dataId: YYY
# etcd
namespace: XXX
key: YYY
String value();方法返回配置内容按Properties解析后的键ZZZ.abc的配置值
指定坐标值时, 可使用${key}
标识环境变量.
环境变量源为类路径中的configservice.env.props
配置文件和Arguments
启动参数.
其中Arguments
变量的优先级高于配置文件.
默认根据方法返回类型解析读取到的配置值, 并做类型转换.
- 基本类型和字符串会直接转换并返回
- Map及其子类型, 将返回Properties类型对象
- 非集合类型的泛型类型, 将使用
DiamondUtils.parseObject()
方法进行解析 - 集合类型将使用
DiamondUtils.parseObjects()
方法进行解析 - 可在方法上添加
@ConfigValueParse
注解, 进行自定义解析
当未读取到指定坐标的配置值时, 可使用配置默认值返回缺省配置值.
可使用注解中的defaultValue
属性, 或方法调用的第一个入参配置默认值.
方法入参的优先级高于defaultValue
属性.
defaultValue
属性配置的默认值也可使用使用环境变量, 也会进行类型转换.
可在方法上添加@DefaultEmptyValue
注解, 配置默认值为""
而非null
.
可使用注解中的cacheSeconds
属性, 指定配置读取到内存后的缓存时长(单位: 秒).
配置客户端接口继承ConfigGetter
接口, 即可使用通用方法按键读取配置值.
当配置内容以# toml
开头时, 将按TOML格式读取配置为Properties对象.
配置客户端接口同时添加@ApolloConfig
, @DiamondConfig
和@EtcdConfig
注解, 或添加@Config
注解.
使用ConfigFactory.getConfig()
方法获取客户端实例.
当类路径中仅包含apollo-client或diamond-client或etcdconf-client时, 将自动选取对应的配置实现.
当类路径中同时包含apollo-client/diamond-client/etcdconf-client时, 根据环境变量中设置的ConfigService
值选取对应的配置实现.
# configservice.env.props
ConfigService=apollo
# Arguments
--ConfigService=apollo
使用@ApolloScan
/@DiamondScan
/@EtcdScan
/@ConfigScan
指定扫描加载包路径.
包路径中所有添加@ApolloConfig
/@DiamondConfig
/@EtcdConfig
/@Config
注解的接口都将生成对应的配置客户端实例并注入SpringContext.
使用ApolloModular
/DiamondModular
/EtcdModular
/ConfigModular
按类或包路径扫描加载.
创建的Module
中将包含对应的配置客户端实例.