Nacos usage tutorial
Nacos is Alibaba’s open source dynamic service discovery, configuration management and service management platform designed to help you build, deliver and manage microservices platforms more easily. It integrates functions such as service registration and discovery, dynamic configuration management, dynamic DNS services and services and metadata management.
1. Introduction to Nacos
Nacos (Dynamic Naming and Configuration Service) is a dynamic service discovery, configuration management and service management platform that is easier to build native applications. It provides the following main features:
- Service Discovery and Service Health Check: Makes the service easier to register and discover other services through the DNS or HTTP interface, providing real-time health checks to prevent sending requests to unhealthy hosts or service instances.
- Dynamic configuration management: Allows you to manage the configuration of all services in a centralized and dynamic manner across all environments, eliminating the need to redeploy applications when updating configurations, making configuration changes more efficient and flexible.
- Dynamic DNS Services: Provides service discovery capabilities based on DNS protocol, supports service discovery in heterogeneous languages, and facilitates third-party applications to review and discover.
- Services and metadata management: Manage all services and metadata in the data center from the perspective of microservice platform construction, including service description, life cycle, static dependency analysis, health status, traffic management, routing and security policies.
2. Nacos deployment
2.1 Download and start Nacos
-
Download Nacos: Download the latest version of Nacos from the GitHub Releases page of Nacos.
-
Unzip the installation package: Unzip the downloaded compressed package to the specified directory.
-
Start Nacos: Enter the decompression
bin
Directory, execute the following command to start Nacos:-
Windows System:
-m standalone
-
Linux/Mac system:
sh -m standalone
-
-
Access the Nacos console: After successful startup, open the browser and access
http://localhost:8848/nacos
The default username and password arenacos
。
2.2 Configuring Nacos
-
Modify the port number: If the default port
8848
Occupated, can be modifiedconf/
In the fileConfiguration Items.
-
Configure the database: By default, Nacos uses embedded database Derby.
-
Switch to MySQL:
- Create a database in MySQL
nacos_config
。 - implement
conf/
Script, initialize the database structure. - Revise
conf/
File, configure database connection information:=mysql =1 .0=jdbc:mysql://localhost:3306/nacos_config?useUnicode=true&characterEncoding=utf-8&useSSL=false =root =
- Create a database in MySQL
-
Switch to MySQL:
-
Enable authentication function:
-
Revise
conf/
File, set the following configuration:=true =your_identity_key =your_identity_value =your_secret_key
-
Restart the Nacos service for the configuration to take effect.
-
3. Nacos as a service registration center
3.1 Introducing dependencies
In Spring Boot ProjectAdd dependencies found by the Nacos service:
<dependency>
<groupId></groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
3.2 Configuring Nacos
existor
Add the following configuration:
=your-service-name
-addr=localhost:8848
3.3 Enable service discovery
Add on the main startup class@EnableDiscoveryClient
annotation:
@SpringBootApplication
@EnableDiscoveryClient
public class YourApplication {
public static void main(String[] args) {
(, args);
}
}
3.4 Service Provider
Create a REST controller to provide the service interface:
@RestController
@RequestMapping("/service")
public class YourServiceController {
@GetMapping("/hello")
public String sayHello() {
return "Hello from Nacos Service!";
}
}
3.5 Serving consumers
In the Service Consumer Project, add the dependencies discovered by the Nacos service and configure the Nacos address:
=your-consumer-service
-addr=localhost:8848
use@LoadBalanced
AnnotatedRestTemplate
Make a service call:
@Configuration
public class AppConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Calling the service in the controller:
@RestController
@RequestMapping("/consumer")
public class YourConsumerController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/hello")
public String sayHello() {
return ("http://your-service-name/service/hello", );
}
}
4. Nacos as configuration center
4.1 Introducing dependencies
existAdd dependencies for Nacos configuration in:
<dependency>
<groupId></groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
4.2 Configuring Nacos
existor
Add the following configuration:
=your-application-name
-addr=localhost:8848
-extension=properties
4.3 Create a configuration file
In the Nacos console, create a configuration file:
-
Data ID:
-
Group:
DEFAULT_GROUP
-
Content:
4.4 Dynamic refresh configuration
Implement dynamic refresh of configuration and need to be added to the classes that require dynamic refresh@RefreshScope
annotation. When a configuration in Nacos changes, the class marked with the annotation will automatically refresh its configuration.
import ;
import ;
import ;
import ;
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {
@Value("${}")
private String configValue;
@GetMapping("/value")
public String getConfigValue() {
return configValue;
}
}
In the above example,ConfigController
On the class@RefreshScope
The annotation ensures that when the configuration in Nacos changes,configValue
Will be updated automatically.
4.5 Configuring Sharing and Environment Isolation
acos supports the implementation of configuration sharing and environmental isolation through namespaces and groupings. Namespaces are used to distinguish different environments, such as development, testing, and production environments; groupings are used to divide configurations into different business modules.
- Namespace: In environmental isolation, such as development environment, test environment and production environment.
-
Group: In terms of business isolation, the configuration of different business modules is divided into different groups.
In the Nacos console, you can create different namespaces and groups and specify corresponding namespaces and groups in configuration management to enable configuration sharing and environmental isolation.
4.6 Configuring encryption and decryption
acos supports encrypted storage and decrypted reading of sensitive configuration items. Configuration items can be encrypted in the Nacos console, and the client will automatically decrypt when read.
To enable the configuration encryption feature, you need to set the encryption key in the Nacos configuration file. Please refer to the official Nacos documentation for the physical configuration method.
5. Nacos cluster deployment
In production environments, it is recommended to deploy Nacos as a cluster to improve the high availability and scalability of the system. acos supports a variety of cluster deployment methods, including:
- Standalone mode: Used for development and testing environments.
-
Cluster mode: Used in production environments, providing high availability and load balancing.
In cluster mode, you need to configure multiple Nacos instances and distribute traffic through a load balancer. For the cluster deployment steps and configuration methods of the body, please refer to the official Nacos documentation.
6. Nacos integration with Spring Cloud
acos can be deeply integrated with Spring Cloud as a service registration and discovery and configuration center. By introducing corresponding dependencies and configurations, you can use Nacos' service discovery and configuration management capabilities in your Spring Cloud project.
For the integration steps and configuration methods of the body, please refer to the official Nacos documentation.
7. Nacos integrates with Spring Boot
acos can also be integrated with Spring Boot as a service registration and discovery and configuration center. By introducing corresponding dependencies and configurations, you can use Nacos' service discovery and configuration management capabilities in your Spring Boot project.
For the integration steps and configuration methods of the body, please refer to the official Nacos documentation.
8. Nacos and Dubbo integration
acos can be integrated with Dubbo as a service registration and discovery center. By introducing corresponding dependencies and configurations, you can use Nacos' service discovery capabilities in your Dubbo project.
For the integration steps and configuration methods of the body, please refer to the official Nacos documentation.
9. Nacos and Kubernetes integration
acos can be integrated with Kubernetes as a service registration and discovery center. By configuring Nacos' integration with Kubernetes, you can use Nacos' service discovery capabilities in a Kubernetes environment.
For the integration steps and configuration methods of the body, please refer to the official Nacos documentation.
10. Nacos' monitoring and alarm
acos provides rich monitoring and alarm functions, and you can view the service health status, configuration change history and other information through the Nacos console. At the same time, Nacos also supports integration with monitoring systems such as Prometheus to enable monitoring of Nacos services.
For monitoring and alarm configuration methods of the body, please refer to the official Nacos documentation.
11. Nacos' security
acos provides a variety of security mechanisms, including authentication, encryption, etc., to ensure the security of your services and configuration. Authentication policies can be configured in the Nacos console to control access to services and configurations.
For the security configuration method of the body, please refer to the official Nacos documentation.
12. Nacos' scalability
acos provides a rich expansion point where you can develop and expand secondaryly according to your business needs. For example, you can develop customized service discovery and configuration management functions through Nacos' plug-in mechanism.
For the extension method of the body, please refer to the official Nacos documentation.
13. Nacos’ community and support
acos has an active community where you can get the latest technology updates, engage in discussions and contribute code. Nacos also provides rich documentation and tutorials to help you better use Nacos.
For more information, please visit the official Nacos website.
14. Summary
Nacos is a powerful service discovery and configuration management platform that integrates functions such as service registration and discovery, dynamic configuration management, dynamic DNS services and services and metadata management. This tutorial allows you to learn how to deploy Nacos, use it as a service registry and configuration center, and integrate with Spring Cloud. Hopefully this tutorial will help you better understand and use Nacos and improve the efficiency and reliability of your microservice architecture.