Microservice-Nacos (registration center)

Nacos is a very powerful component of SpringCloud, and it wants to be more functional than eureka

Official nacos profile

Nacos (full name: Naming and Configuration Service) is an open source dynamic service discovery, configuration management and service management platform. It was developed by Alibaba Group and contributed to the open source community, aiming to help developers better build and manage microservice architecture.

Nacos provides the following core functions:

  1. Service discovery and registration: Nacos acts as a service registry , allowing developers to easily register, discover and manage microservice instances. It uses a consistent hash algorithm to achieve load balancing of services , and provides a variety of ways to discover and call services.

  2. Dynamic configuration management: Nacos can centrally manage configuration information of applications. Developers can use Nacos to dynamically update and publish configurations without redeploying or restarting applications. This increases configuration flexibility and maintainability while reducing the complexity of configuration management.

  3. Service health monitoring: Nacos can monitor and report the health status of microservices . It can perform regular heartbeat checks and use the heartbeat data to determine whether the service is available. When a service is unavailable, Nacos can issue an alert in time and assist in troubleshooting and recovery.

  • Dynamic routing and load balancing: Nacos provides dynamic routing and load balancing functions, enabling developers to flexibly manage and adjust request routing strategies. It supports a variety of load balancing algorithms based on weight, consistent hashing, and least active number, and can also be integrated with other open source components (such as Nginx and OpenResty).

        All in all, Nacos provides a complete set of microservice infrastructure to help developers better build, manage and adjust microservice architecture. It has the characteristics of open source, easy to use, high availability and scalability , and has been widely used and recognized in the industry.

Nacos installation

Windows installation

  • Download the installation package

On the GitHub page of Nacos, there is a download link to download the compiled Nacos server or source code:

GitHub homepage: https://github.com/alibaba/nacos

Release download page of GitHub: Releases · alibaba/nacos · GitHub

  • Unzip (any non-Chinese directory)
  • port configuration
  • The default port of Nacos is 8848. If other processes on your computer occupy port 8848, enter the conf directory of nacos to modify it.
  • Start (enter the bin directory)

Open cmd to execute the command:

startup.cmd -m standalone

Linux installation

  • install jdk

You can download the jdk jar package on the java official website, and upload the downloaded jar package to a directory (such as /user/local/)

Rename it to java after decompression :

tar -xvf jdk-8u144-linux-x64.tar.gz

Configure environment variables:

export JAVA_HOME=/usr/local/java
export PATH=$PATH:$JAVA_HOME/bin

Set environment variables:

source /etc/profile
  • Upload the installation package

Upload to a directory of the Linux server (for example: /user/local/src)

  • decompress

Command to decompress the compressed installation package :

tar -xvf nacos-server-1.4.1.tar.gz

Remove the installation package:

rm -rf nacos-server-1.4.1.tar.gz
  • port configuration
  • start up
sh startup.sh -m standalone

Spring Cloud introduces nacos

1. Introduce the corresponding dependencies in the parent project:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
    <version>2.2.5.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

2. Comment out the eureka dependency in the original AB service

3. Introduce the corresponding dependencies in the nacos client:

<!-- nacos客户端依赖包 -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

4. Modify the yml files in A and B services, comment out the eureka address, and add the nacos address

spring:
  cloud:
    nacos:
      server-addr: nacos:8848 # nacos服务地址

Summarize:

1. Nacos service construction

  • Download the installation package
  • decompress
  • Run the command in the bin directory: startup.cmd -m standalone

2. Nacos service registration or discovery

  • Introduce nacos.discovery dependency
  • Configure nacos address spring.cloud.nacos.server-addr

Nacos service hierarchical storage model

 Service Cluster Properties

1. Modify the yml file of the corresponding service and add the following content

spring:
  cloud:
    nacos:
      server-addr: nacos:8848 # nacos服务地址
      discovery:
         cluster-name:SM #配置集群名称,也就是说机房的位置

Summarize:

1. Nacos service hierarchical storage model

  • first level service
  • The second level is cluster
  • Level 3 is an example

2. How to set the cluster properties of the instance

  • Modify the corresponding yml file and add the property spring.cloud.nacos.discovery.cluster-name

        In our production process, load balancing must give priority to finding the service closest to us (the same cluster), so how should we configure in Spring to give priority to finding services in the same cluster as ourselves?

Load balancing according to the cluster

1. Modify the yml file in service A and set the cluster to SM

spring:
  cloud:
    nacos:
      server-addr: nacos:8848 # nacos服务地址
      discovery:
        cluster-name: SM

2. Set the IRule of load balancing in service A to NacosRule. This rule will give priority to finding services in the same cluster as itself

userservice:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRul

Load balancing based on weight

       In actual application, we will encounter such a situation, some server equipment is poor, we hope it bears less user requests, some server equipment is good, we hope it bears more user requests

Nacos supports weight configuration to control access frequency. The greater the weight, the higher the access frequency [0,1]

Environment isolation-namespace

The outermost layer of service storage and data storage in Nacos is a thing called namespqce, which is used for outermost isolation

 In the Nacos console, namespaces can be created to isolate different environments (namespace->new namespace)

 cluster-name: SM
      discovery:
        #命名空间ID
        namespace: 4d6ce343-9e1b-44df-a90f-2cf2b6b3d177 # dev环境
        

Each isolation environment has a unique id, services in different environments are not visible to each other

Principle of Nacos Registration Center

 We can set whether the service is a temporary instance:

spring:
  cloud:
    nacos:
      discovery:
        ephemeral: false # 是否是临时实例

When the temporary instance goes down, it will be removed from the nacos service list, but the non-temporary instance will not


The common responsibility of Nacos and eureka:

  • Both support service registration and service pull
  • Both support the service provider's heartbeat method for health detection

The difference between Nacos and Eureka:

  • Nacos supports the server to actively detect the status of the provider: the temporary instance adopts the heartbeat mode, and the non-temporary instance adopts the active detection mode
  • Temporary instances with abnormal heartbeat will be removed, while non-temporary instances will not be removed
  • Nacos supports the message push mode of service list changes, and the service list updates more timely
  • By default, the Nacos cluster adopts the ap mode (weak consistency and strong availability). When there are non-temporary instances in the cluster, it adopts the cp mode (strong consistency and weak availability), and Eureka adopts the ap mode.

Guess you like

Origin blog.csdn.net/dfdbb6b/article/details/132337659