更新和獲取配置中心數據模板代碼

  1. 配置選項
public class ConfigOption<T> {

    private final String key;

    private final T defaultValue;

    private final String description;

    public ConfigOption(String key, T defaultValue) {
        this.key = checkNotNull(key);
        this.description = "";
        this.defaultValue = defaultValue;
    }

    public ConfigOption(String key, String description, T defaultValue) {
        this.key = checkNotNull(key);
        this.description = description;
        this.defaultValue = defaultValue;
    }

    public ConfigOption<T> withDescription(final String description) {
        return new ConfigOption<>(key, description, defaultValue);
    }

    public String key() {
        return key;
    }

    public boolean hasDefaultValue() {
        return defaultValue != null;
    }

    public T defaultValue() {
        return defaultValue;
    }

    public String description() {
        return description;
    }

    public static <T> T checkNotNull(T reference) {
        if (reference == null) {
            throw new NullPointerException();
        }
        return reference;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        else if (o != null && o.getClass() == ConfigOption.class) {
            ConfigOption<?> that = (ConfigOption<?>) o;
            return this.key.equals(that.key) &&
                    (this.defaultValue == null ? that.defaultValue == null :
                            (that.defaultValue != null && this.defaultValue.equals(that.defaultValue)));
        }
        else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        return 31 * key.hashCode() +
                (defaultValue != null ? defaultValue.hashCode() : 0);
    }

    @Override
    public String toString() {
        return String.format("Key: '%s' , default: %s", key, defaultValue);
    }
}
  1. 定義的配置項
public class ConfOptions {

    public static final ConfigOption<String> PROJECT_KEY =
            key("project.key")
                    .defaultValue("")
                    .withDescription("key的描述內容");

    /**
     * 項目中使用的配置中心key的集合
     */
    public static List<ConfigOption> BASIC_OPTION = new ArrayList<>();

    static {
        BASIC_OPTION.add(PROJECT_KEY);
    }

    public static OptionBuilder key(String key) {
        checkNotNull(key);
        return new OptionBuilder(key);
    }

    public static final class OptionBuilder {

        private final String key;

        OptionBuilder(String key) {
            this.key = key;
        }

        public <T> ConfigOption<T> defaultValue(T value) {
            checkNotNull(value);
            return new ConfigOption<>(key, value);
        }

        public ConfigOption<String> noDefaultValue() {
            return new ConfigOption<>(key, null);
        }
    }

}

  1. 配置中心操作服務
@Slf4j
@Service
public class ConfOptionService {
	
	/**
	 * 獲取所有的配置項
	 */
    public List<ConfOptionDto> queryAllConfOption() {
        List<ConfOptionDto> dtoList = Lists.newArrayList();
        for (ConfigOption opts : ConfOptions.BASIC_OPTION) {
            String optionKey = opts.key();
            Object optionValue = opts.defaultValue();
            String value = GaeaConfiguration.getString(opts.key(), "");
            if (StringUtils.isNotBlank(value)) {
                optionValue = value;
            }
            ConfOptionDto dto = new ConfOptionDto();
            dto.setKey(optionKey);
            dto.setValue(String.valueOf(optionValue));
            dto.setDescription(opts.description());
            dtoList.add(dto);
        }
        return dtoList;
    }

	/**
	 * 更新對應的配置項
	 */
    public boolean updateConfOption(ConfOptionDto dto) {
        try {
            // todo更新對應的配置項
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return false;
        }
        return true;
    }

	/**
	 * todo配置項被修改了需要下發大此項目,根據邏輯進行相應的處理
	 */
	
	@Data
	static class ConfOptionDto {

		private String key ;

		private String value ;

		private String description ;
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章