配置元数据官网
- LuckyZookeeperPropertiesod.class
package com.lucky.config.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "lucky.zookeeper", ignoreInvalidFields = true)
public class LuckyZookeeperProperties {
private String connectString;
private int sessionTimeoutMs;
private int connectionTimeoutMs;
private RetryPolicy retryPolicy;
public String getConnectString() {
return connectString;
}
public void setConnectString(String connectString) {
this.connectString = connectString;
}
public int getSessionTimeoutMs() {
return sessionTimeoutMs;
}
public void setSessionTimeoutMs(int sessionTimeoutMs) {
this.sessionTimeoutMs = sessionTimeoutMs;
}
public int getConnectionTimeoutMs() {
return connectionTimeoutMs;
}
public void setConnectionTimeoutMs(int connectionTimeoutMs) {
this.connectionTimeoutMs = connectionTimeoutMs;
}
public RetryPolicy getRetryPolicy() {
return retryPolicy;
}
public void setRetryPolicy(RetryPolicy retryPolicy) {
this.retryPolicy = retryPolicy;
}
public static class RetryPolicy {
private int baseSleepTimeMs;
private int maxRetries;
public int getBaseSleepTimeMs() {
return baseSleepTimeMs;
}
public void setBaseSleepTimeMs(int baseSleepTimeMs) {
this.baseSleepTimeMs = baseSleepTimeMs;
}
public int getMaxRetries() {
return maxRetries;
}
public void setMaxRetries(int maxRetries) {
this.maxRetries = maxRetries;
}
}
}
- ZookeeperConfig.class
package com.lucky.config;
import com.lucky.config.properties.LuckyZookeeperProperties;
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.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(LuckyZookeeperProperties.class)
public class ZookeeperConfig {
@Bean
public CuratorFramework curatorZookeeperClient(LuckyZookeeperProperties properties) {
RetryPolicy retryPolicy = new ExponentialBackoffRetry(properties.getRetryPolicy().getBaseSleepTimeMs(),
properties.getRetryPolicy().getMaxRetries());
final CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString(properties.getConnectString())
.sessionTimeoutMs(properties.getSessionTimeoutMs())
.connectionTimeoutMs(properties.getConnectionTimeoutMs())
.retryPolicy(retryPolicy)
.build();
client.start();
return client;
}
}
- gradle
annotationProcessor('org.springframework.boot:spring-boot-configuration-processor')
- 执行命令
gradle clear && gradle build
查看生成的自动补全json
build/classes/java/main/META-INF/spring-configuration-metadata.json
{
"groups": [
{
"name": "lucky.zookeeper",
"type": "com.lucky.config.properties.LuckyZookeeperProperties",
"sourceType": "com.lucky.config.properties.LuckyZookeeperProperties"
},
{
"name": "lucky.zookeeper.retry-policy",
"type": "com.lucky.config.properties.LuckyZookeeperProperties$RetryPolicy",
"sourceType": "com.lucky.config.properties.LuckyZookeeperProperties",
"sourceMethod": "getRetryPolicy()"
},
{
"name": "spring.datasource.druid.primary",
"type": "javax.sql.DataSource",
"sourceType": "com.lucky.config.PrimaryDataSourceConfig",
"sourceMethod": "dataSource()"
}
],
"properties": [
{
"name": "lucky.zookeeper.connect-string",
"type": "java.lang.String",
"description": "zookeeper地址格式:Ip1:port1,Ip2:port2.",
"sourceType": "com.lucky.config.properties.LuckyZookeeperProperties"
},
{
"name": "lucky.zookeeper.connection-timeout-ms",
"type": "java.lang.Integer",
"sourceType": "com.lucky.config.properties.LuckyZookeeperProperties",
"defaultValue": 0
},
{
"name": "lucky.zookeeper.retry-policy.base-sleep-time-ms",
"type": "java.lang.Integer",
"sourceType": "com.lucky.config.properties.LuckyZookeeperProperties$RetryPolicy",
"defaultValue": 0
},
{
"name": "lucky.zookeeper.retry-policy.max-retries",
"type": "java.lang.Integer",
"sourceType": "com.lucky.config.properties.LuckyZookeeperProperties$RetryPolicy",
"defaultValue": 0
},
{
"name": "lucky.zookeeper.session-timeout-ms",
"type": "java.lang.Integer",
"sourceType": "com.lucky.config.properties.LuckyZookeeperProperties",
"defaultValue": 0
}
],
"hints": [
{
"name": "lucky.zookeeper.connect-string",
"values": [
{
"value": "localhost:2181"
},
{
"value": "192.168.56.103:2181,192.168.56.104:2181,192.168.56.105:2181"
},
{
"value": "192.168.56.103:2181"
}
]
}
]
}
- 打开application.yml,发现有提示了
lucky:
zookeeper:
session-timeout-ms: 6000
connection-timeout-ms: 6000
retry-policy:
base-sleep-time-ms: 1000
max-retries: 3
connect-string: 192.168.56.103:2181,192.168.56.104:2181,192.168.56.105:2181
- 如果想输入的情况也有提示,在resources目录下面的META-INF中新建additional-spring-configuration-metadata.json
(这里的additional-spring-configuration-metadata.json会自动合并到spring-configuration-metadata.json当中)
{
"properties": [
{
"name": "lucky.zookeeper.connect-string",
"type": "java.lang.String",
"description": "zookeeper地址格式:Ip1:port1,Ip2:port2.",
"sourceType": "com.lucky.config.properties.LuckyZookeeperProperties"
}
],
"hints": [
{
"name": "lucky.zookeeper.connect-string",
"values": [
{
"value": "localhost:2181"
},
{
"value": "192.168.56.103:2181,192.168.56.104:2181,192.168.56.105:2181"
},
{
"value": "192.168.56.103:2181"
}
]
}
]
}
-
连输入都有提示了