The use of Spirng Cloud Alibaba Nacos registration center (environment isolation, service hierarchical storage model, weight configuration, temporary instances and persistent instances)

1. Environmental isolation

In Nacos, related concepts of data model include namespace, group, services and dataId.

data model

1. Namespace:

  • Namespace is the highest level isolation unit of Nacos, used to isolate different applications or environments.
  • Each Namespace is isolated from each other and has independent configuration and service registry.
  • Configuration and service management of different environments can be achieved through Namespace. For example, development environment, test environment and production environment can use different Namespaces.

2. Group:

  • Group is a logical grouping of services or configurations under the same Namespace.
  • Services or configurations can be classified and managed through Groups, which facilitates operations such as version control and grayscale publishing.
  • A Group can contain multiple services or configurations.

3. Services:

  • Services represent a collection of instances that provide the same service.
  • In Nacos, the service registration center is used to register service instances into the service list and provide functions such as service discovery and load balancing.
  • Each service has a unique service name (Service Name), and service instances can be found and managed based on the service name.

4. DataId:

  • DataId refers to a string that uniquely identifies a configuration in Nacos.
  • For configuration management, DataId corresponds to the file name of the configuration file or the unique identifier of the configuration item.
  • For service registration and discovery, the DataId corresponds to the unique identifier of the service. A configuration or service can be uniquely located through DataId.

The organizational structure and management of these concepts in Nacos can help users better perform configuration management and service registration and discovery.

5. Practical demonstration:

5.1 Default (public):

In Nacos, by default, all services, configurations (data) and groups are in the same Namespace, and the name of the Namespace is public. This means that by default, all services and configurations are managed and isolated under the same namespace.

default

5.2 Creation of Namespace:

Find the namespace menu:

new
Create a new namespace:
Insert image description here

If the namespace is not filled in, it will be automatically generated by default. After filling in, click the OK button.

sex

As you can see, a new namespace has been created on the page.

5.3 Microservice access namespace:

You can read this article first: Spring Cloud Alibaba integrates Nacos in practice

5.3.1 The consumer is in the public namespace by default:
public
5.3.2 The provider modifies the yml configuration:

  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        namespace: c431c2a3-91bc-40ed-a99c-6fe856ac1e7d

5.3.3 The corresponding namespace for starting the provider is as follows:
provider
5.3.4 The consumer initiates a call to the provider:

The console error is as follows:

Insert image description here
Because the namespace is different, the consumer cannot find the provider.

2. Service hierarchical storage model

In a distributed system, the relationships between instances can be divided into the following levels:

1. Service level:

Each instance belongs to the same service, the consumer service.

2. Cluster level:

Multiple instances can form a cluster.

For example, consumer-1, consumer-2 and consumer-3 can form a consumer-service cluster.

127.0.0.1:8101  # 北京机房
127.0.0.1:8102  # 上海机房
127.0.0.1:8103  # 上海机房

3. Regional level:

If instances are spread across different regions or data centers, they can be divided into different regional levels.

For example, consumer-1 and consumer-2 are deployed in the Beijing computer room, while consumer-3 is deployed in Shanghai.

The service grading model is as follows:
hierarchical model
When microservices access each other, it is best to prioritize access to instances in the same cluster because local access is faster. Only when this cluster is unavailable will access to instances in other clusters be considered.

For example: the consumer service in the Beijing computer room should give priority to calling the provider service in the same computer room in Beijing. If the call fails, it should access the provider service in the Shanghai computer room.
Same computer room
Next, configure the unreachable cluster for the consumer.

In Nacos, you can configurecluster-name attributeto specify the name of the cluster. Instances in the same computer room can be configured with the same cluster name, so that they will be considered members of the same cluster.

Modify yml as follows:

spring:
  application:
    name: consumer  #微服务名称
  #配置nacos注册中心地址
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        cluster-name: SH   

Start at this time, three consumer corresponding instances, open the service list of the nacos console:

detail

Click on details and you can see that there are two consumer clusters in Shanghai and one in Beijing:

Shanghai

3. Weight configuration

The service weight function in Nacos can help solve some problems, including service upgrade and service selection.

1. Service upgrade:

When the system needs to perform service upgrades or version iterations, you can achieve a smooth upgrade process by setting the weight of the service. For example, if you want to upgrade a new version of a service, you can first set the weight of the instance of the new version to a lower value, and then gradually increase the weight while reducing the weight of the instance of the old version to achieve a smooth transition between the old and new versions.

2. Service selection:

By setting the weights of different instances, the selection and allocation of different instances can be achieved. For example, you can set higher weights for certain instances with better performance or more sufficient resources, so that requests are more likely to be routed to these instances. This can achieve load balancing and resource optimization, and improve system performance and availability.

Through the weight function of Nacos, the weight of services can be flexibly controlled and adjusted to meet different scenarios and needs. This provides more control and flexibility in the management and operation of services.

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

weight
Here we change the provider weight to 0:

Insert image description here

If the provider is called through the consumer, the instance will never be accessed and the error will be as follows:

providera

A weight of 0 means that the instance's service is unavailable or does not participate in load balancing, so requests will not be routed to this instance.

4. Temporary instances and persistent instances

1. Temporary instance:

By default, nacos is a temporary instance. A temporary instance means that after the service is registered, when the instance goes offline or is disconnected, Nacos will immediately delete it from the service list.

This means that the temporary instance will not be called by other services after it is offline.

Insert image description here

2. Persistence example:

In contrast, a persistent instance will remain in the service list after it goes offline, and other services can still call it.

In Nacos, you can specify whether an instance is a temporary instance or a persistent instance through configuration.

Modify the ephemeral attribute in the yml file to false:

spring:
  cloud:
    nacos:
      discovery:
        ephemeral: false

consumer-prov

Observe that the temporary instance of consumer has changed to false

consumer1
Observe that the temporary instance of provider has changed to false

provider1

Guess you like

Origin blog.csdn.net/qq_39939541/article/details/131745494