SpringCloud source code analysis (2)-Nacos registration center

1 Overview

Nacos is one of the core components in Spring Cloud Alibaba. It provides functions such as service discovery, service configuration and service management, and can be used as a registration center and configuration center. The registration center is one of the core components in the microservice architecture system. Nacos has more powerful functions than Eureka. They can both provide service registration and service pull, and support service providers to monitor health in the form of heartbeats. Nacos supports message push mode for service list changes, and service updates are more timely; the Nacos server will actively monitor the client status (temporary instances adopt heartbeat mode, and non-temporary instances adopt active monitoring mode). This article will explain in detail the use of nacos and some precautions.

2.Use and analysis

2.1 Nacos usage

The registration center generally contains three roles: service provider (Server), service consumer (Client), and registration center (Registry). A service can be both a provider and a consumer at the same time. Nacos includes server installation packages and service dependency packages. The specific usage is as follows:

2.1.1 Server package installation

This article uses centos7 system as the server installation environment,download address. I choose version 1.4.2 here, download and upload it to the server, as follows:
Insert image description here
The unzipped directory is as follows:
Insert image description here
The nacos configuration file is stored in the conf folder , which can be modified as needed. Next, you can start nacos. The startup instructions for the stand-alone version of nacos are as follows:

startup.sh -m standalone

Enter the bin folder, execute the above command, and get the following results (provided that the jdk installation is completed and can run normally, the default jdk address is /usr/local/java, which can be specified in the configuration file): a>
Insert image description here
After successful startup, you can access the web page (ip+port number+/nacos/index.html) through ip+port number, as shown below:
Insert image description here

2.1.2 spingboot integrates nacos

1.Introduce dependencies

			<dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.3.7.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR3</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <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>

Among the above dependencies, the first three are spring-boot, spring-cloud, and spring-cloud-alibaba. If you want to introduce the service discovery package of nacos, you also need to introduce spring-boot-starter-web. Package, otherwise an error will be reported.
2. Add the @EnableDiscoveryClient annotation to the startup file

@Slf4j
@EnableDiscoveryClient
@SpringBootApplication
public class OrderApplication {
    
    
    public static void main(String[] args) {
    
    
        log.info("这是新的OrderApplication");
        SpringApplication.run(OrderApplication.class, args);
    }
}

3. Add configuration file

#服务端口号
server.port=8082
#服务名称
spring.application.name=order-service
#注册中心地址
spring.cloud.nacos.discovery.server-addr=http://localhost:8848

4. Start the service

Insert image description here
Insert image description here
View through the graphical interface of nacos. If the service can be viewed in the service list, the registration is successful.

2.2 Core configuration analysis

2.2.1 Nacos hierarchical storage model

The design model of Nacos service discovery is as follows:
Insert image description here
The model of traditional service discovery (Consul, Eureka, etc.) is generally directly related to Instance under Service, and nacos adds here Understand the concept of cluster. The purpose of adding Cluster is also to improve access efficiency and isolate abnormal services. For example, clusters are divided by region. Services in the Shanghai computer room are all registered to the SH cluster, and services in the Hangzhou computer room are registered to the HZ cluster. Services in the Shanghai computer room are called to Shanghai first. For other services in the cluster, the services in the Hangzhou computer room have priority in calling other services in the Hangzhou cluster. Only when all called services in the Shanghai cluster are abnormal, it is possible to call the service in the Hangzhou cluster.
According to the design model of Nacos, service isolation mainly includes Cluster, Group, and Namespace. The explanation is as follows:

Cluster (cluster isolation): The machines mounted under the service belong to different environments. It is hoped that under certain circumstances, all traffic in a certain environment can be cut away, so that You can achieve one-time traffic cut through cluster isolation.

Group (group isolation): Refer to Dubbo's service isolation strategy. One thing to note is: when Dubbo registers the triplet (interface name + group + version), where Dubbo The group is included in the Nacos service name and is not mapped to the Nacos group. Generally, the services registered by Nacos are registered to the DEFAULT_GROUP group by default.

Namespace (namespace isolation): It is also commonly used in actual scenarios. It is generally used for the isolation of multiple environments, such as daily, dev, test, uat, prod, etc. Environmental isolation. Especially when there are many environments, using namespaces for logical isolation is a cost-saving strategy.

1. Configure cluster
You can specify a service cluster through the following configuration,

spring.cloud.nacos.discovery.cluster-name=beijing

Insert image description here

2.Configure group

spring.cloud.nacos.discovery.group=AAA

Insert image description here
3. Configure namespace
Add a namespace on the web side of nacos, as shown below:
Insert image description here
After the addition is completed, you can click on the namespace menu bar Check:
Insert image description here
Configure namespace in the configuration file as follows:

spring.cloud.nacos.discovery.namespace=a6a062e0-085d-4eb0-936d-ad9f9bbc0d74

namespace is the generated unique id or specified value. Start the service. After successful registration, you can get the following results:
Insert image description here
Namespace environment isolation, please note the following:

1. Each namespace has a unique id;
2. Services under different namespaces are not visible.

2.2.2 The difference between Nacos and Eureka

1.Nacos supports the server to actively detect the provider status: temporary instances adopt heartbeat mode, and non-temporary instances adopt active detection mode;
2. Temporary instances with abnormal heartbeats will be removed , non-temporary instances will not be eliminated;
3.Nacos supports the message push mode for service list changes, and the service list is updated more timely;
4.Nacos cluster The AP mode is used by default. When there are non-temporary instances in the cluster, the CP mode is used; Eureka uses the AP mode.

3. Summary

1.nacos can be used as a registration center and configuration center;
2. As a registration center, nacos can improve more comprehensive registration discovery and isolation functions than consul and eureka; < /span>
3. With the service isolation model of cluster·, group, and namespace, and support for the push-pull model, nacos can be quickly integrated by the microservice framework.

4. References

1.https://zhuanlan.zhihu.com/p/364527885
2.https://www.bilibili.com/video/BV1LQ4y127n4

5. Appendix

https://gitee.com/Marinc/nacos.git

Guess you like

Origin blog.csdn.net/qq_33479841/article/details/128947063