微服务配置中心-Nacos

文章目录

  • 一、Nacos简介
  • 二、微服务配置中心探讨
    • 1.为什么要配置管理
    • 2.配置管理策略
  • 三、Nacos基础
    • 1.Nacos设计原则
    • 2.Nacos的接口
    • 3.完整的客户端实例
  • 四、一些思考
    • 1.Nacos 客户端是怎么实时获取到 Nacos 服务端的最新数据的?
    • 2.Nacos如何兼容apache的configuration
    • 2.AP还是CP,还是mixed

一、Nacos简介

微服务配置中心-Nacos_第1张图片

Nacos是阿里开源的一个微服务配置中心,其官方宣传:

一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。

目前是github的一个明星项目,高达6k+的star。有大量组织在现网使用Nacos,详见官方issue:https://github.com/alibaba/nacos/issues/273

二、微服务配置中心探讨

1.为什么要配置管理

微服务配置中心-Nacos_第2张图片

微服务背景下,配置管理呈现两大特征:分散、动态。这两点都很易于理解: 微服务下是不可能一个配置文件管理多个服务,同时同一个服务会分散在海量机器上。这会带来程序配置管理的碎片化,也就是“分散”。同时微服务需要更灵活地更及时地获取到配置,也就是“动态”。

所以传统的静态配置文件\代码写死的管理方式无法满足现在的要求。

2.配置管理策略

微服务配置中心-Nacos_第3张图片

所以我们的配置管理策略主要是需要这两点

  • 降低运维困难
  • 实时推送能力

微服务配置中心-Nacos_第4张图片

三、Nacos基础

1.Nacos设计原则

微服务配置中心-Nacos_第5张图片

Nacos使用Namespace + Group + DataId 来确定一个配置的内容。

2.Nacos的接口

Nacos支持三类接口:

  • OPEN-API: 纯HTTP接口
  • SDK:目前官方社区已有JAVA版本
  • spring注解

简单的demo如下:

# OPEN-API
curl -x GET "http://serverIp:8848/nacos/v1/cs/configs?dataId=dataIdparam&group=groupParam&tenant=tenantParam
"
public String getConfig(String dataId, String group, long timeoutMs) throws NacosException
@NacosInjected
private ConfigService configService;

@Test
public void testPublishConfig() throws NacosException {
    configService.publishConfig(DATA_ID, DEFAULT_GROUP, "9527");
}

3.完整的客户端实例

  • 生成一个configService
  • configService获取配置(getConfig)(SDK层面看是拉配置)
  • configService调用addListener方法监听服务端的变化(SDK层面看是服务端在推送配置)
String serverAddr = "{serverAddr}";
String dataId = "{dataId}";
String group = "{group}";
Properties properties = new Properties();
properties.put("serverAddr", serverAddr);
ConfigService configService = NacosFactory.createConfigService(properties);
String content = configService.getConfig(dataId, group, 5000);
System.out.println(content);
configService.addListener(dataId, group, new Listener() {
	@Override
	public void receiveConfigInfo(String configInfo) {
		System.out.println("recieve1:" + configInfo);
	}
	@Override
	public Executor getExecutor() {
		return null;
	}
});

// 测试让主线程不退出,因为订阅配置是守护线程,主线程退出守护线程就会退出。 正式代码中无需下面代码
while (true) {
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

四、一些思考

1.Nacos 客户端是怎么实时获取到 Nacos 服务端的最新数据的?

服务端和客户端直接本质上还是通过 http 进行数据通讯的,之所以有“推”的感觉,是因为服务端主动将变更后的数据通过 http 的 response 对象提前写入了。

详见:

https://github.com/all4you/nacos-tutorial/blob/master/nacos-config-realtime-update/nacos-config-realtime-update.md

2.Nacos如何兼容apache的configuration

将Nacos的SDK与apache的configuration结合起来,尽量少地修改我们的业务代码以实现Nacos的接入。以下是一个完整的例子:

			String serverAddr = "localhost";
			String dataId = "DATA_ID";
			String group = "DEFAULT_GROUP";
			Properties properties = new Properties();
			properties.put("serverAddr", serverAddr);
			configService = NacosFactory.createConfigService(properties);
			String content = configService.getConfig(dataId, group, 5000);
			// 加载文件前设置分隔符失效(不使用任何分隔符).
			config.setDelimiterParsingDisabled(true);

			// 将字符串转换成PropertiesConfiguration格式
			config.load(new ByteArrayInputStream(content.getBytes()));

			configService.addListener(dataId, group, new Listener() {
				// 监听变化
				public void receiveConfigInfo(String configInfo) {
					try {
						// 需要先clear再load,不然load不生效
						config.clear();
						config.load(new ByteArrayInputStream(configInfo.getBytes()));
					} catch (ConfigurationException e) {
						e.printStackTrace();
					}
				}
				public Executor getExecutor() {
					return null;
				}
			});
		} catch (ConfigurationException e) {
			logger.error("create conf file error.", e);
			e.printStackTrace();
		}

2.AP还是CP,还是mixed

微服务配置中心-Nacos_第6张图片

详见官方的issue,待深入研究

文章首发:
https://cloud.tencent.com/developer/article/1446931

你可能感兴趣的:(后端)