Microservice Day2 - Getting Started with Nacos Registration Center

Nacos Registration Center

1. Introduction to Nacos

Domestic companies generally respect Alibaba's technology, such as the registration center, and Spring Cloud Alibaba has also launched a registration center called Nacos.

2. Mac installation

  1. Enter the Nacos official website to download the installation package
    http://nacos.io/zh-cn/
    image.png
    Github warehouse address
    image.png

  2. After downloading and decompressing, enter the nacos/bin directory
    image.png

  3. Execute sh startup.sh -m standalonethe command to start nacos
    image.png
    4. Enter the visualization page http://127.0.0.1:8848/nacos/ , the account password is nacos, just log in, the port of nacos is 8848
    image.png

(If you want to close nacos, enter sh shutdown.sh but find that after closing, you can still connect to nacos on the visualization page, so you need to kill the process on port 8848, you can enter lsof-i:8848 kill -9 process number)

3. Register the service to nacos

Nacos is a component of SpringCloudAlibaba, and SpringCloudAlibaba also follows the service registration and service discovery specifications defined in SpringCloud. Therefore, there is not much difference between using Nacos and using Eureka for microservices.

The main differences are:

  • depends on different
  • service address is different

1) Introduce dependencies

<dependencyManagement>Introduce the dependency of SpringCloudAlibaba in the pom file of the cloud-demo parent project :

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

Then introduce nacos-discovery dependencies in the pom files in user-service and order-service:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

Note : Don't forget to comment out the eureka dependency.

2) Configure nacos address

Add nacos address in application.yml of user-service and order-service:

spring:
  cloud:
    nacos:
      server-addr: localhost:8848

Note : Don't forget to comment out the address of eureka

3) reboot

After restarting the microservice, log in to the nacos management page, and you can see the microservice information:
image.png

4. Service hierarchical storage model

Nacos divides the instances in the same computer room into a cluster .

In other words, user-service is a service. A service can contain multiple clusters, such as Hangzhou and Shanghai. Each cluster can have multiple instances, forming a hierarchical model, as shown in the figure: When microservices access each other, they should try to
image.png
access the same cluster instance as much as possible, because the local access speed is faster. Only access other clusters when the cluster is unavailable. For example:
image.png
the order-service in the computer room in Hangzhou should give priority to access the user-service in the same computer room.

4.1 Configure cluster for user-service

Modify the application.yml file of user-service and add the cluster configuration:

spring:
  cloud:
    nacos:
      server-addr: localhost:8848
      discovery:
        cluster-name: HZ # 集群名称

After restarting the two user-service instances, we can see the following results on the nacos console:
image.png

image.png

Nacos service hierarchical storage model

  • The first level is a service, such as userservice
  • The second level is the cluster, such as Hangzhou or Shanghai
  • The third level is an instance, such as a server in a computer room in Hangzhou that deploys userservice

How to set cluster properties for an instance

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

4.2 Load balancing with priority in the same cluster

By default, ZoneAvoidanceRuleit is not possible to achieve load balancing based on the priority of the same cluster.

Therefore, Nacos provides an NacosRuleimplementation that can preferentially select instances from the same cluster.

1) Configure cluster information for order-service

Modify the application.yml file of order-service and add the cluster configuration:

spring:
  cloud:
    nacos:
      server-addr: localhost:8848
      discovery:
        cluster-name: HZ # 集群名称

2) Modify the load balancing rules

Modify the application.yml file of order-service and modify the load balancing rules:

userservice:
  ribbon:
    NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule # 负载均衡规则 

NacosRule load balancing strategy

  • Prioritize the list of service instances in the same cluster
  • The local cluster cannot find the provider, so it goes to other clusters to look for it, and a warning will be reported
  • After determining the list of available instances, random load balancing is used to select instances

5. Weight configuration

In actual deployment, such a scenario will appear:

  • There are differences in the performance of server equipment. The performance of some machines where the instances are located is better, while others are poorer. We hope that the machines with better performance can bear more user requests.

Nacos provides weight configuration to control the access frequency, the greater the weight, the higher the access frequency

In the nacos console, find the instance list of user-service, and click Edit to modify the weight:

image.png

In the edit window that pops up, modify the weight:
image.png

Note : If the weight is modified to 0, the instance will never be visited

6. Environmental isolation

Nacos provides namespace to implement environment isolation.

  • There can be multiple namespaces in nacos
  • There can be group, service, etc. under the namespace
  • Different namespaces are isolated from each other, for example, services in different namespaces are invisible to each other

The outermost layer of service storage and data storage in Nacos is a thing called namespace, which is used for outermost isolation
image.png

6.1 Create a namespace

By default, all services, data, and groups are in the same namespace named public:

image.png

We can click the Add button on the page to add a namespace:
image.png

image.png

6.2 Configure namespace for microservices

Configuring namespaces for microservices can only be achieved by modifying the configuration.

For example, modify the application.yml file of order-service:

spring:
  cloud:
    nacos:
      server-addr: localhost:8848
      discovery:
        cluster-name: HZ
        namespace: fe205277-84c6-402c-be48-ba772cbe7a2f # 命名空间,填ID

After restarting order-service, access the console, and you can see the following results:

image.png

At this time, when accessing order-service, because the namespace is different, the userservice will not be found, and the console will report an error:

image.png

7. The difference between Nacos and Eureka

Nacos service instances are divided into two types:

  • Temporary instance: If the instance is down for more than a certain period of time, it will be removed from the service list, the default type.

  • Non-temporary instance: If the instance goes down, it will not be removed from the service list, and it can also be called a permanent instance.

Configure a service instance as a permanent instance:

spring:
  cloud:
    nacos:
      discovery:
        ephemeral: false # 设置为非临时实例

The overall structure of Nacos and Eureka is similar, service registration, service pull, heartbeat waiting, but there are some differences:

image.png

  • What Nacos and eureka have in common

    • 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 provider status: 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
    • The Nacos cluster adopts the AP mode by default. When there are non-temporary instances in the cluster, the CP mode is adopted; Eureka adopts the AP mode

Guess you like

Origin blog.csdn.net/qq_54351538/article/details/131788432
Recommended