Introduction to Nacos, installation, deployment and configuration priorities

Table of contents

1. What is Nacos

2. Nacos installation and deployment

Environmental preparation

installation method

DownloadNacos

Initialize database

Change setting

Set up MySQL data source connection

Turn on authentication

Start the server

Verification log

Verify login

Shut down the server

Configure systemctl management service

Nacos configuration priority

Multiple configuration loading priorities

Local profile priority

Configuration file priority modification


1. What is Nacos

The full name of Nacos/nɑ:kəʊs/ is Dynamic Naming and Configuration Service. It is a basic service platform open sourced by Alibaba that integrates dynamic service discovery, configuration management and service management. Service is a first-class citizen in the Nacos world. It provides functions such as service registration and discovery, configuration management, dynamic DNS services, service metadata, and traffic management to meet the needs of microservice architecture and cloud native applications.

Its key features are as follows:

  1. Service registration and discovery: Nacos supports multiple service registration and discovery, including DNS-based, RPC-based and K8s-based service discovery.

  2. Dynamic configuration service: Nacos provides a centralized configuration management platform with a simple and easy-to-use UI that supports dynamic configuration, eliminating the need to redeploy applications when configuration changes, thereby improving application flexibility and manageability.

  3. Dynamic DNS service: Nacos provides a lightweight dynamic DNS service, which can map service names to actual network addresses, achieve dynamic access and load balancing of services, support weighted routing, and make it easier to implement flexible routing strategies and traffic control.

  4. Service and metadata management: Nacos provides management functions for all services and metadata in the data center, including service description, life cycle, static dependency analysis, health status, traffic management, routing and security policies, service SLA and metrics statistics. And provide visual interface display.

  5. Service health check: Nacos has a built-in health check function that supports health checks at the transport layer (PING or TCP) and application layer (such as HTTP, MySQL, user-defined), and can monitor and manage the health status of service instances.

2. Nacos installation and deployment

Nacos supports three deployment modes: stand-alone (development and testing environment), cluster (recommended for production environment) and multi-cluster (recommended for multi-data center scenarios). This article takes stand-alone deployment as an example to introduce the Nacos installation and deployment method in detail.

Environmental preparation

  • Recommended hardware configuration is at least 2CCPU/4G memory/60G disk.
  • Install 64bit JDK 1.8+ Nacos relies on the Java runtime environment.
  • Install database MySQL5.7 requires version 5.6.5+ to facilitate observation of data storage.

installation method

DownloadNacos

Download package fromNacos Github Assets and unzipnacos-server-2.2.0.tar.gz安装

# 下载
wget https://github.com/alibaba/nacos/releases/download/2.2.0/nacos-server-2.2.0.tar.gz
# 解压
tar xvf nacos-server-2.2.0.tar.gz

Initialize database

First create the database nacos_config and user nacos, and grant all permissions to user nacos.

# 创建数据库nacos_config
CREATE DATABASE nacos_config DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
# 授权
grant all privileges on nacos_config.* to 'nacos'@'%' identified by 'nacos' with grant option;
# 刷新权限
flush privileges;

The database initialization sql file is located in ./nacos/conf/mysql-schema.sql. Execute the following command to import the table structure and data.

# 导入sql文件,输入密码nacos
mysql -u nacos -p -D nacos_config < ./nacos/conf/mysql-schema.sql

Change setting

Nacos is essentially a SpringBoot application, and the global configuration is located in ./nacos/conf/application.properties.

Set up MySQL data source connection

Be sure to enable the data source MySQL first, set spring.datasource.platform=mysql, and set spring.sql.init.platform=mysql starting from version 2.2.1. Otherwise, it will not take effect and will be saved to the embedded derby database by default.

db.num=1
db.url.0=jdbc:mysql://192.168.5.10:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=nacos
db.password.0=nacos

Nacos supports multiple data source configurations

db.num represents the number of data sources.

db.url.<index> indicates the index+1 data source Url configuration, and index starts from 0 by default.

db.user.<index>表示第index+1个Url的用户名。

db.password.<index>表示第index+1个Url的密码。

Note: Ifdb.user和db.password is not setindex, all URLs use db.user and db.password For authentication, multiple usernames and passwords will be separated by commas. If the database name, user name or password is incorrect, a No DataSource set exception will be reported. Please check whether the connection is correct. 

Turn on authentication

Nacos is suitable for intranet operation and cannot be exposed to the public network environment, otherwise it will bring security risks. Nacos has a built-in default authentication plug-in, and you can enable service authentication in the following ways.

# 开启默认鉴权插件
nacos.core.auth.system.type=nacos
nacos.core.auth.enabled=true
# key和value可以自定义,本例中请求api时header需要携带serverIdentity=security
nacos.core.auth.server.identity.key=serverIdentity
nacos.core.auth.server.identity.value=security
# 2.1.0版本后还需要配置插件密钥
nacos.core.auth.plugin.nacos.token.secret.key=VGhpc0lzTXlDdXN0b21TZWNyZXRLZXkwMTIzNDU2Nzg=
Start the server

standalone means running in stand-alone mode, not cluster mode.

# 启动
bin/startup.sh -m standalone
Verification log

The startup log is located in /opt/nacos/logs/start.out. If Nacos started successfully appears, it means the startup is successful.

2023-08-28 17:16:06,648 INFO Exposing 1 endpoint(s) beneath base path '/actuator'

2023-08-28 17:16:06,678 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos'

2023-08-28 17:16:06,694 INFO Nacos started successfully in stand alone mode. use external storage
Verify login

The browser opens the login interface: http://192.168.5.10:8848/nacos/index.html#/login. The default login username/password is nacos/nacos.

Shut down the server
bin/shutdown.sh
Configure systemctl management service

Create the Unit configuration file /lib/systemd/system/nacos.service of the Nacos service

可能异常:ERROR: Please set the JAVA_HOME variable in your environment, We need java(x64)! jdk8。

Solution: Load the JAVA_HOME environment variable by setting Environment=JAVA_HOME=/path/to/jdk
.

[root@node1 nacos]# vim /lib/systemd/system/nacos.service
[Unit]
Description=nacos
After=network.target

[Service]
Environment=JAVA_HOME=/usr/java/jdk1.8.0_181-cloudera
Type=forking
ExecStart=/opt/nacos/bin/startup.sh -m standalone
ExecReload=/opt/nacos/bin/shutdown.sh
ExecStop=/opt/nacos/bin/shutdown.sh
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Load Unit configuration file

systemctl daemon-reload

Start nacos.service

systemctl status nacos.service

Set up startup

systemctl enable nacos.service

Nacos configuration priority

Multiple configuration loading priorities

Local profile priority

spring:
  application:
    name: nacos-config-multi
  main:
    allow-bean-definition-overriding: true
  cloud:
    nacos:
      username: ${nacos.username}
      password: ${nacos.password}
      config:
        server-addr: ${nacos.server-addr}
        namespace: ${nacos.namespace}
        # 用于共享的配置文件
        shared-configs:
          - data-id: common-1.yaml
            group: SPRING_CLOUD_EXAMPLE_GROUP
 
          - data-id: common-2.yaml
            group: SPRING_CLOUD_EXAMPLE_GROUP
 
          - data-id: common-3.yaml
            group: SPRING_CLOUD_EXAMPLE_GROUP
		......
 
        # 常规配置文件
        # 优先级大于 shared-configs,在 shared-configs 之后加载
        extension-configs:
          - data-id: nacos-config-1.yaml
            group: SPRING_CLOUD_EXAMPLE_GROUP
            refresh: true
 
          - data-id: nacos-config-2.yaml
            group: SPRING_CLOUD_EXAMPLE_GROUP
            refresh: true

          - data-id: nacos-config-3.yaml
            group: SPRING_CLOUD_EXAMPLE_GROUP
            refresh: true
  • Priority: extension configuration (extension-configs) > shared configuration (shared-configs)
  • They are both extension configurations. The larger the subscript, the higher the priority: extension-configs[3] > extension-configs[2] > extension-configs[1] > extension-configs[0]
  • Both are shared configurations, the larger the subscript, the higher the priority: shared-configs[3] > shared-configs[2] > shared-configs[1] > shared-configs[0]

Configuration file priority modification

spring:
  cloud:
    config:
      # 如果本地配置优先级高,那么 override-none 设置为 true,包括系统环境变量、本地配置文件等配置
      override-none: true
      # 如果想要远程配置优先级高,那么 allow-override 设置为 false,如果想要本地配置优先级高那么 allow-override 设置为 true
      allow-override: true
      # 只有系统环境变量或者系统属性才能覆盖远程配置文件的配置,本地配置文件中配置优先级低于远程配置;注意本地配置文件不是系统属性
      override-system-properties: false

Guess you like

Origin blog.csdn.net/BlogPan/article/details/132528345