This is the sixth day of my participation in the First Challenge 2022. For details: First Challenge 2022.
Nacos is an important part of Spring Cloud Alibaba and provides two important functions: service registration and discovery and unified configuration center functionality.
The service registration and discovery function solves the connection management and request forwarding functions of the caller and service provider in the microservice cluster, so that the application developer does not have to worry too much about the stability and health of the service provider and the call address, because these can be monitored, managed and automatically forwarded by Nacos.
Imagine, when a system is deployed multiple times, how do you decide which instance to invoke? When something goes wrong with one invoked instance, how can requests be forwarded to other instances? These problems are real problems in microservices architecture, but they can be easily solved with Nacos. Nacos also provides a unified configuration center to solve the security problems of project configuration files stored locally or on Github, and supports dynamic modification and unified maintenance of configuration files.
1. Nacos profile
Nacos is DynamicNaming and Configuration SAn acronym for ervice; It is a platform for dynamic service discovery, configuration management, and service management that makes it easier to build cloud native applications.Website:nacos.io/
Warehouse source: github.com/alibaba/nac…
2. Nacos ecology
Nacos supports almost all major languages, among which Java/Golang/Python already supports Nacos 2.0 long link protocol, which can maximize the performance of Nacos. Ali micro service DNS (Dubbo+Nacos+ Spring-Cloud-Alibaba /Seata/Sentinel) best practice, is the best solution of Java micro service ecosystem.
3. Rapid deployment of Nacos
Nacos supports three deployment modes:
- Single deployment
- Cluster deployment
- Multi-cluster Deployment
Nacos can be installed in the following two modes:
- Install using source code.
- Install using the compiled installation package.
In this article we will use the first method, plus Docker, to quickly deploy the Nacos server side.
Because it is convenient to install and uninstall the Docker, this article uses this method to demonstrate the installation of Nacos. Other installation methods, please refer to the official website: nacos. IO /zh-cn/docs/…
3.1 Preparing the Environment Preparations
Nacos relies on the Java environment to run it. If you build and run Nacos from code, you also need to configure the Maven environment for this. Make sure you install it in the following versions:
- The 64-bit OS supports Linux, Unix, Mac, and Windows. Linux, Unix, and Mac are recommended.
- 64-bit JDK 1.8+.
- Maven 3.2 x +.
Because this article uses the way of Nacos + Docker deployment, so the students who have not installed Docker, search for installation oh.
Install Open JDK 8
Yum -y install Java — 1.8.0 comes with its – devel. X86_64
After the installation, use Java -version to check whether the installation is successful, as shown in the following figure:If the Java version is displayed, the installation is successful.
3.2 download Nacos
This article uses git to download the Nacos source package, so use the following command to install Git first:
yum -y install git
Download Nacos source code:
Git clone github.com/nacos-group…
Download completed as shown below:Enter the Nacos directory:
cd nacos-docker
3.3 Starting the Nacos Service
Nacos requires a database to run, and it supports two types of databases: Derby, a local database, and MySQL. Therefore, the corresponding startup commands are as follows:
- The Derby database runs in standalone mode: docker-comement-f example/standalone-derby.yaml up
- MySQL 5.7 database run standalone mode: docker-comement-f example/standalone-mysql-5.7.yaml up
- MySQL 8 database run standalone mode: docker-comement-f example/standalone-mysql-8.yaml up
Yaml up: docker-compose -f example/cluster-hostname.yaml up
The successful startup is shown in the following figure:
3.3 access Nacos
Once successfully started, it is ready to usehttp://127.0.0.1:8848/nacos/#/loginTo access the Nacos admin console, as shown below:Enter both username and password nacOS to enter the system, as shown below:
3.4 Common Error Messages
3.4.1 Docker-compose command not found
Docker-compose is an orchestration tool for defining and running multi-container Docker applications. Docker-compose eliminates the need to create and start containers one by one. You can use the YML file to configure all the services your application needs, and then create and start all the services from the YML file configuration with a single command. Docker-compose needs to be installed separately, otherwise the following error will occur:Solution:
cd /usr/localhttps://github.com/docker/compose/releases/download/1.14.0-rc2/docker-compose-Linux-x86_64 / bin wget rename docker-compose-Linux-x86_64 docker-compose docker-compose-Linux-x86_64 chmod +x /usr/local/bin/docker-compose
docker-compose version
Copy the code
The installation is successful, as shown below:
3.4.2 Image: Invalid reference format
Docker-compose may prompt “ERROR: no such image: nacOS/nacOS-server :: invalid reference format”, as shown in the following figure.Example /standalone-mysql-5.7.yaml: image: Nacos /nacos-server:{{NACOS_VERSION}} Nacos/nacos – server: latest “.
4. Registration of Spring Cloud Alibaba service
Service registrars, also known as producers, are service providers in microservices. It is created on the basis of the Spring Boot project. The steps are as follows:Create projects using aliyun addressstart.aliyun.comTo create the Spring Cloud Alibaba Nacos project, click Next, as shown below:Added Nacos Service Discovery framework support, as shown in the following figure:Add support for the Spring Web (Spring MVC) framework and click Finish to complete the project.
4.1 Project Dependency
The Spring Cloud Alibaba Nacos project mainly has two dependencies, as shown below:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
Copy the code
4.2 Modifying the Configuration File
In the configuration file application.properties, fill in the following information about Nacos:
Name =spring-cloud-nacos-producer # Server. port=8082 # Nacos authentication information Spring. Cloud. Nacos. Discovery. The username = nacos spring. Cloud. Nacos. Discovery. The password = # nacos nacos service discovery and register configuration, The sub-attribute server-addr specifies the Nacos server host and port Spring.cloud.nacos.discovery.server-addr=mse-6d50f4f0-p.nacos-ans.mse.aliyuncs.com: 8848 # registered to nacos specified namespace, The default to public spring. Cloud. Nacos. Discovery. The namespace = publicCopy the code
4.3 Adding Code
Then add a Restful API interface for later service callers to use, the specific implementation code is as follows:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@EnableDiscoveryClient
public class SpringCloudNacosProducerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudNacosProducerApplication.class, args);
}
@RequestMapping("/sayhi/{name}")
public String sayHi(@PathVariable String name) {
return "Hi Nacos Discovery "+ name; }}Copy the code
After writing the code, run the project and look at the list of services in Nacos to see the project, as shown below:After the above operation, the project of Spring Cloud Alibaba created by us is registered in Nacos, and other programs can also call it through Nacos.
5.Spring Cloud Alibaba service discovery
Similarly, create a service caller, also called a consumer, to invoke the sayhi method in the framework as follows.
5.1 Creating a Project
5.2 Modifying a Configuration File
Name =springcloud-nacos-consumer server.port=8082 https://nacos.io/zh-cn/docs/concepts.html # Nacos authentication information spring. Cloud. Nacos. Discovery. The username = Nacos Spring. Cloud. Nacos. Discovery. Password = # nacos nacos service discovery and register configuration, The child property server - addr Nacos server host and port specified spring. Cloud. Nacos. Discovery. The server - addr = 82.157.146.10:8848 # registered to Nacos specified Namespace, default to public spring. Cloud. Nacos. Discovery. The namespace = publicCopy the code
5.3 Adding Code
The implementation code of the consumer has two classes, add a RestTemplate Bean object to the start class, used to provide Restful API requests, the specific implementation code of the start class is as follows:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class SpringcloudNacosConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudNacosConsumerApplication.class, args);
}
@LoadBalanced
@Bean
public RestTemplate restTemplate(a) {
return newRestTemplate(); }}Copy the code
The specific implementation code of the consumer is as follows:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class TestController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/hi")
public String hi(String name) {
// Call the producer sayhi method and return the result
return restTemplate.getForObject("http://spring-cloud-nacos-producer/sayhi/"+ name, String.class); }}Copy the code
After adding the code, run the program and you can see the following in Nacos’s list of services:We then call the consumer’s HI method and have it call the producer as follows:It can be seen from the above results that consumers have successfully called the Sayhi method in spring-Cloud-nacos-producer through Nacos.
summary
Nacos provides two important functions: service registration and discovery and unified configuration center. It provides three deployment modes: single-machine deployment, cluster deployment and multi-cluster deployment, and two installation modes: source installation and installation package installation. We use Docker plus Nacos source code to install and deploy Nacos. The registration and Discovery of Nacos services must be supported by the Nacos Service Discovery framework, and corresponding Nacos information must be configured in the configuration file, so that the functions of registration and Discovery of Nacos services can be correctly realized.
Reference & acknowledgements
Nacos Framework and Principles
Judge right and wrong from yourself, praise to listen to others, gain and loss in the number.
Public account: Java Chinese Community
Java Interview Collection: gitee.com/mydb/interv…