最近研究了一下apollo和nacos,比对了一下nacos和apollo的配置发布能力,这里不讲原理,直接上实操。需要使用代码给配置中心发布 yaml格式的数据。注意apollo的yaml格式需要特殊配置一下。
- 对外提供发布配置的接口
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;
}
}
- 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() {
}
}
- 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);
}
}