nacos和apollo的配置主动发布功能对比

最近研究了一下apollo和nacos,比对了一下nacos和apollo的配置发布能力,这里不讲原理,直接上实操。需要使用代码给配置中心发布 yaml格式的数据。注意apollo的yaml格式需要特殊配置一下。

  1. 对外提供发布配置的接口
public interface ConfigCenterService extends SmartLifecycle {
    /**
     * 配置发布
     * @param value 需要发布的数据
     */
    void publish(Map value) throws JsonProcessingException;
}

接口的抽象实现类

@Slf4j
public abstract class AbstractConfigCenterService implements ConfigCenterService {
    protected YAMLMapper YAML_MAPPER = new YAMLMapper();

    {
        // null的属性不序列化
        YAML_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }
	
	@Override
    public void publish() throws Exception {
        // TODO 业务代码,组装配置内容
        
        // 发布配置
        publish(xxx);

        log.info("发布配置信息成功!");
    }

	@Override
    public void start() {
        checkConfigCenterIsOk();
        try {
            publish();
        } catch (Exception e) {
            throw new RuntimeException("初始化,发布配置信息失败!", e);
        }
    }

    public abstract void checkConfigCenterIsOk();

    @Override
    public void stop() {

    }

    @Override
    public boolean isRunning() {
        return false;
    }
	
}
  1. nacos配置发布实现
@Configuration
@ConditionalOnProperty(
        prefix = "spring.cloud.nacos.config",
        name = {"enabled"},
        havingValue = "true"
)
public class NacosConfigAutoConfiguration {

    @Bean
    public ConfigCenterService configCenterService(NacosConfigManager nacosConfigManager) {
        return new NacosConfigCenterServiceImpl(nacosConfigManager);
    }
}
@Slf4j
public class NacosConfigCenterServiceImpl extends AbstractConfigCenterService {

	
    private NacosConfigManager nacosConfigManager;

    public NacosConfigCenterServiceImpl(NacosConfigManager nacosConfigManager)
        this.nacosConfigManager = nacosConfigManager;
    }

    @Override
    public void publish(Map value) throws JsonProcessingException {
        try {
            NacosConfigProperties nacosConfigProperties = nacosConfigManager.getNacosConfigProperties();
            nacosConfigManager.getConfigService().publishConfig("appid", nacosConfigProperties.getGroup(), YAML_MAPPER.writeValueAsString(value), ConfigType.YAML.getType());
        } catch (NacosException e) {
            log.error("nacos 配置中心发布失败!", e);
        }
    }

	/**
     * 检查nacos 是否连接正常
     * @return
     */
    @Override
    public void checkConfigCenterIsOk() {
    }

}
  1. apollo配置发布实现
@Configuration
@ConditionalOnProperty(
        prefix = "apollo",
        name = {"appid"}
)
public class ApolloAutoConfiguration {
    @Bean
    public ApolloProperties apolloProperties() {
    	// 这个属性就不贴出来了,这个配置很简单。
        return new ApolloProperties();
    }

    @Bean
    public ApolloOpenApiClient apolloOpenApiClient(ApolloProperties apolloProperties){
        return ApolloOpenApiClient.newBuilder()
                .withPortalUrl(apolloProperties.getPortalUrl())
                // 注意,这里需要使用apollo的第三方token
                .withToken(apolloProperties.getToken())
                .build();
    }

    @Bean
    public ConfigCenterService configCenterService(ApolloOpenApiClient apolloOpenApiClient,
                                                   ApolloProperties apolloProperties){
        return new ApolloConfigCenterServiceImpl(apolloOpenApiClient, apolloProperties);
    }

}
public class ApolloConfigCenterServiceImpl extends AbstractConfigCenterService{

    private ApolloOpenApiClient apolloOpenApiClient;

    private ApolloProperties properties;

    public ApolloConfigCenterServiceImpl(ApolloOpenApiClient apolloOpenApiClient,
                                         ApolloProperties properties){
        this.apolloOpenApiClient = apolloOpenApiClient;
        this.properties = properties;
    }

    /**
     * 检查配置终检是否连接正常
     * @return
     */
    @Override
    public void checkConfigCenterIsOk() {
        try {
            apolloOpenApiClient.getNamespace(properties.getAppid(),
                    properties.getEnv(),
                    properties.getClusterName(),
                    properties.getNamespaceName());
        } catch (Exception e){
            throw new RuntimeException("请检查Apollo配置," + JSONObject.toJSONString(properties));
        }
    }

    @Override
    public void publish(Map value) throws JsonProcessingException {
        apolloUpdateYaml(value);
        apolloPublish();
    }

    /**
     * apollo发布功能
     */
    private void apolloPublish() {
        NamespaceReleaseDTO namespaceReleaseDTO = new NamespaceReleaseDTO();
        namespaceReleaseDTO.setReleaseTitle(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + "-release");
        namespaceReleaseDTO.setReleasedBy("apollo");
        apolloOpenApiClient.publishNamespace(properties.getAppid(),
                properties.getEnv(),
                properties.getClusterName(),
                properties.getNamespaceName(),
                namespaceReleaseDTO);
    }

    /**
     * 修改 apollo 的参数
     *
     * @param value
     */
    private void apolloUpdateYaml(Map value) throws JsonProcessingException {
        OpenItemDTO openItemDTO = new OpenItemDTO();
        openItemDTO.setKey("content");
        openItemDTO.setValue(YAML_MAPPER.writeValueAsString(value));
        openItemDTO.setDataChangeLastModifiedBy("apollo");
        openItemDTO.setDataChangeLastModifiedTime(new Date());
        apolloOpenApiClient.updateItem(properties.getAppid(),
                properties.getEnv(),
                properties.getClusterName(),
                properties.getNamespaceName(), openItemDTO);
    }
}
  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值