Technology Sharing

SpringCloudAlibaba Nacos configuration center and service discovery

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Table of contents

1. Configuration

1.1 Configuration features

Read-only

Accompanying the entire life cycle of the application

Multiple loading methods

Configuration requires governance

1.2 Configuration Center

2. Introduction to Nacos

2.1 Features

Service discovery and service health check

Dynamic Configuration Management

Dynamic DNS Service

Service and metadata management

3. Service Discovery


1. Configuration

When an application is started and running, it often needs to read some configuration information. The configuration basically accompanies the entire life cycle of the application, such as: database connection parameters, startup parameters, etc.

1.1 Configuration features

Read-only

It is read-only for programmers. The program changes its behavior by reading the configuration, but the program should not change the configuration.

Accompanying the entire life cycle of the application

Initialize by reading configuration at startup, and adjust behavior according to configuration at runtime. For example, you need to read the service port number at startup, and read custom policies to execute scheduled tasks during system operation.

Multiple loading methods

Common ones include hard code within the program, configuration files, environment variables, startup parameters, and database-based ones.

Configuration requires governance

The same program needs to have different configurations in different environments (development, testing, production) and different clusters (such as different data centers), so a complete environment and cluster configuration management is required.

1.2 Configuration Center

In a microservice architecture, when a system is split from a monolithic application into service nodes on a distributed system, the configuration files must be migrated (separated) so that the configuration is dispersed.

The configuration center separates configuration from each application and manages configuration in a unified manner, so the applications themselves do not need to manage configuration themselves.

2. Introduction to Nacos

Alibaba's open source product is a comprehensive solution for service discovery, configuration management, and service governance in microservice architecture.

Official website:https://nocas.io/

2.1 Features

Four major functions

Service discovery and service health check

Nacos makes it easier to register services and discover other services through DNS or HTTP interfaces. Nacos also provides real-time inspection of services to prevent requests from being sent to unhealthy hosts or service instances.

Dynamic Configuration Management

Dynamic configuration services allow you to manage the configuration of all services in a centralized and dynamic manner across all environments. Nacos eliminates the need to redeploy applications when updating configurations, which makes configuration changes more efficient and flexible.

Automatically define the extended Data Id configuration. The internal configuration has the highest priority. Configuration priority: the larger the n in the extended Data Id, the higher the priority. The loading order of bootstrap.yml takes precedence over application.yml.

Completely turn off configuration: Completely turn off Spring Cloud Nacos Config by setting spring.cloud.nacos.config.enable=false.

nacos can be deployed in a cluster.

The @Value annotation is an annotation provided by the Spring framework. It is used to obtain the value in the configuration file. It can be used to obtain the configuration items in any configuration file (such as application.properties, application.yml). When using the @Value annotation, you need to specify the full path of the configuration item, for example: @Value("${config.key}").

nacos startup command (Windows): startup.cmd -m standalone

Open the browser and enter http://localhost:8848/nacos to access the service. The default account and password are nacos, nacos

Dynamic DNS Service

Nacos provides service discovery capabilities based on the DNS protocol, which aims to support service discovery in heterogeneous languages ​​and expose endpoints of services registered on Nacos in the form of domain names, so that third-party applications can easily query and discover them.

Service and metadata management

Nacos allows you to manage all services and metadata in the data center from the perspective of microservice platform construction, including management service description, life cycle, static dependency analysis of services, service health status, service traffic management, routing and security policies.

3. Service Discovery

In a microservice architecture, the entire system is divided into multiple services according to their responsibilities and capabilities, and business goals are achieved through collaboration between services. In this way, remote calls between services are required in our code. The consumer of the service needs to call the producer of the service. In order to complete a request,Consumers need to know the network location of service producers(IP address and port number).

·