04-Microservice-Nacos

Nacos registration center

Domestic companies generally respect Alibaba's technologies, such as registration centers. Spring Cloud Alibaba also launched a registration center called Nacos.

1.1. Understand and install Nacos

Nacos is a product of Alibaba and is now a component in Spring Cloud . Compared with Eureka , it has richer functions and is more popular in China.

image-20210713230444308

1.2. Service registration 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:

  • Dependencies are different
  • The service address is different

1) Introduce dependencies

<dependencyManagement>Introduce the SpringCloudAlibaba dependency into 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 the nacos-discovery dependency into 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 addresses 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) Restart

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

image-20210713231439607

1.3. Service hierarchical storage model

A service can have multiple instances . For example, our user-service can have:

  • 127.0.0.1:8081
  • 127.0.0.1:8082
  • 127.0.0.1:8083

If these instances are distributed in different computer rooms across the country, for example:

  • 127.0.0.1:8081, in Shanghai computer room
  • 127.0.0.1:8082, in Shanghai computer room
  • 127.0.0.1:8083, in Hangzhou computer room

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:

image-20210713232522531

When microservices access each other, they should try to access the same cluster instance because local access is faster. Only when the current cluster is unavailable will other clusters be accessed. For example:

image-20210713232658928

The order-service in the Hangzhou computer room should give priority to the user-service in the same computer room.

1.3.1. Configure the cluster for user-service

Modify the application.yml file of user-service and add 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 in the nacos console:

image-20210713232916215

We copy a user-service startup configuration again and add properties:

-Dserver.port=8083 -Dspring.cloud.nacos.discovery.cluster-name=SH

The configuration is shown in the figure:

image-20210713233528982

Check the nacos console again after starting UserApplication3:

image-20210713233727923

1.3.2. Load balancing with priority in the same cluster

By default, ZoneAvoidanceRuleload balancing cannot be achieved based on priority within the same cluster.

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

1) Configure cluster information for order-service

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

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

2) Modify 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 # 负载均衡规则 

1.4. Weight configuration

In actual deployment, the following scenario will occur:

The performance of server equipment varies. Some machines where instances are located have better performance, while others have worse performance. We hope that machines with good performance will bear more user requests.

But by default, NacosRule is randomly selected within the same cluster and does not consider machine performance issues.

Therefore, Nacos provides weight configuration to control 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-20210713235133225

In the pop-up editing window, modify the weight:

image-20210713235235219

NOTE : If the weight is modified to 0, the instance will never be accessed

1.5. Environmental isolation

Nacos provides namespace to implement environment isolation function.

  • There can be multiple namespaces in nacos
  • There can be groups, services, etc. under the namespace.
  • Different namespaces are isolated from each other. For example, services in different namespaces are not visible to each other.

image-20210714000101516

1.5.1.Create namespace

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

image-20210714000414781

We can click the new button on the page to add a namespace:

image-20210714000440143

Then, fill out the form:

image-20210714000505928

You will see a new namespace on the page:

image-20210714000522913

1.5.2. Configure namespace for microservices

Configuring namespace 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: 492a7d5d-237b-46a1-a99a-fa8e98e4b0f9 # 命名空间,填ID

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

image-20210714000830703

image-20210714000837140

When accessing order-service at this time, because the namespace is different, userservice cannot be found, and the console will report an error:

image-20210714000941256

1.6.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. This is the default type.

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

Configure a service instance as a permanent instance:

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

The overall structures of Nacos and Eureka are similar, including service registration, service pulling, and heartbeat waiting, but there are also some differences:

image-20210714001728017

  • What Nacos and eureka have in common

    • Both support service registration and service pulling.
    • All support service providers’ heartbeat method for health testing.
  • The difference between Nacos and Eureka

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

Study notes, compiled from the Dark Horse Programmer Tutorial

Guess you like

Origin blog.csdn.net/weixin_57486248/article/details/135368547