CAP principle in distributed

CAP

C (consistency), A (availability), P (partition tolerance)

insert image description here

AP: Meet availability and partition fault tolerance

When a network partition occurs, in order to ensure availability, system B can return the old value to ensure system availability.

The data can be inconsistent for a short time, but it needs to be consistent in the end. In any case, the availability of the service must be guaranteed.

The AP mode is a temporary instance. By default, after the service is started, a " heartbeat packet " will be sent to nacos every 5 seconds. This heartbeat packet contains the basic information of the current service. If Nacos receives this "heartbeat packet" and finds that the service information is not in the registration list, it will register. If the service information is in the registration list, it indicates that the service is still healthy.

The client notifies the health status of the nacos registration center through the heartbeat report (the default heartbeat interval is 5s, nacos will set the instance that has not received the heartbeat for more than 15s as unhealthy, and delete the instance if it exceeds 30s)

This feature is suitable for scenarios that need to deal with sudden traffic increases. The service can be elastically expanded. When the traffic passes, the service can be automatically logged out when it stops.

CP: Satisfy consistency and partition fault tolerance

When a network partition occurs, in order to ensure consistency, requests must be rejected, otherwise consistency cannot be guaranteed.

Our services may not be available, but we must ensure data consistency.

The CP mode is a permanent instance, which is registered with nacos at startup, and nacos will persist the instance. Nacos actively checks the health status of the client (the default time interval is 20s, and will be set as unhealthy if the health check fails, and will not be deleted immediately). Only the main business of the project will be set as a permanent instance.

Mutual conversion between CP and AP modes

curl -X PUT "$NACOS_SERVER:8848/nacos/v1/ns/operator/switches?entry=serverMode&value=CP"

curl -X PUT "$NACOS_SERVER:8848/nacos/v1/ns/operator/switches?entry=serverMode&value=AP"

Successful example:

insert image description here

The following options need to be configured to indicate that the registration is a temporary/permanent instance.
AP mode does not support data consistency, so only temporary instances of service registration are supported. CP mode supports permanent instances of service registration to meet the consistency of configuration files

spring: 
  cloud:
    nacos:
      discovery:
        # 示例类型(true为临时实例,false为永久实例 ==> CP模式)
        ephemeral: true

tip:

  • This cannot be changed casually, it is recommended to keep the default AP.
  • All services in the cluster environment must be switched
  • You can use postman to simulate, you must use put request. Both get and post are invalid

Guess you like

Origin blog.csdn.net/weixin_49339471/article/details/128933816