Zookeeper, Apollo and so on are representatives of the configuration center, but most of them are Based on JAVA. Golang is the main development language used by the author. Of course, there are components like ETCD, but it is not easy to manage and cannot be visualized. I accidentally found that Ali Dad open source nacOS, the service discovery + configuration center component, and after a period of time, I share it with you here

Attach:

A meow blog :w-blog.cn

Nacos official Git address :github.com/alibaba/nac…

Nacos official document address :nacos. IO/zh-CN /docs/

Go language SDK address :github.com/sunmi-OS/go…

PS: The latest version is V1.1.3. Alicloud provides configuration center service. ACM is used in the same way as Nacos

I. Introduction to Nacos

Nacos is an open source project of Ali Cloud middleware team, which is independent based on the ACM configuration management service provided by Ali Cloud. Up to now, Github has more than 8K star, although the maturity is not equal to Ctrip’s Apollo. Comparing the stability of components that provide services on Ali Cloud is also worth believing, of course, to use the introduction of Nacos must introduce the idea of configuration center.

Configuration center is a cliche topic, starting with software programming configuration management is a important step in the project, with a single application, of course, need only a single way to manage the configuration file or environment variable configuration is good so no longer within the scope of this article, under the service configuration center is a major service to solve or micro configuration management of the following questions:

  • Effective password management, development does not touch the password configuration, unified management of operation and maintenance personnel and architecture team to avoid leakage;
  • Configuration in multiple projects is absolutely uniform, and there are no bugs caused by configuration write errors
  • Complete capability of configuration editing, storage, distribution, change management, historical version management, change audit
  • Configure grouping and grayscale publishing

There are pros and cons to using configuration files. We also need to solve the following problems:

  • How to ensure the service availability in the case of abnormality of the configuration center? (SDK provides Cache function. When the service of the configuration center is unavailable, the Cache configuration loaded last time will be used.)
  • Program execution logic after configuration change (SDK provides configuration change subscription logic to subscribe configuration change writing processing logic)
  • Configuration file debugging during development (framework design required)
  • Configuration central performance issues for some languages (PHP) (Throughput of Nacos 8C16G 15K concurrent)

By contrast, we can conclude that the advantages of the configuration center outweigh the disadvantages

Second, Nacos deployment

Nacos supports not only binary deployment but also Docker and K8S deployment, Because Nacos is a stateful service whose data needs to be stored by Mysql and the clustering method needs to specify the IP address of slave, it is not a good choice to use K8S (K8S uses StatefulSet to run stateful service). Here, the author uses Docker-Composer to run Nacos

Git address: github.com/nacos-group…

Git Clone https://github.com/nacos-group/nacos-docker.git Standalone deployment (memory mode) : Docker-comement-f example/standalone-derby.yaml up standalone deployment (Mysql mode) : Docker-compose -f example/standalone-mysql.yaml up Cluster deployment: docker-compose -f example/cluster-hostname.yaml upCopy the code

Visit: http://localhost:8848/nacos/ you can see the login screen

PS: The default user name and password are both nacos

Alicloud ACM service

Of course, there are many challenges to deploying Nacos yourself, such as:

  • The cluster structures,
  • The stability of
  • Mysql Database Maintenance
  • Configure security protection (Nacos has no password, but ACM needs to use ali Cloud key to improve security)

PS: In accordance with the principle of not building a service by myself, the author finally uses ACM service of Aliyun (currently ACM service is free).

PS: It should be noted that ACM and Nacos are linked differently in SDK

Three, basic use

Nacos has several basic concepts that we need to understand before we can better integrate them into business scenarios:

  • Namespace namespace
  • Group Configuring a Group
  • DataID Specifies the configuration name

Generally, namespace is used to distinguish different projects or environments, and Group is used to distinguish configuration differences. For example, there are some slight differences between the configuration obtained by A business and that of B team. Finally, DataId is used to distinguish specific configuration items

Add a Namespace

Add a configuration

Support a variety of configuration formats, you can use custom formats or even store code directly

There is also sample code for the Corresponding JAVA family

Iv. SDK and OpenApi usage configuration

Nacos supports SDKS for the following languages:

  • Java
  • go
  • cpp
  • python
  • nodejs

You can view the specific usage in the official documentation

The configuration already configured above can be accessed using OpenApi

The curl "http://172.16.0.13:8848/nacos/v1/cs/configs? dataId=test_config&group=DEFAULT_GROUP&tenant=b58ea912-e564-4958-b21f-3098ad15daf9" { "name":"sunmi" }Copy the code

Tenant is the name of the namespace, but the original name is not the following

The open source version of Nacos can also be used to discover related problems, which is left for you to explore, in terms of configuration has been able to solve most of the problems, next time I will give configuration center best practices based on Go, please remember to follow me.