SpringCloud Eureka cluster registry is how consistent the data?

SpringCloud Eureka cluster registry is how consistent the data?

Registration Service Center can not be a single point, there will be a cluster, the cluster service registration information of how to maintain consistency in the cluster do?

First, to be clear is that Eureka is weak data consistency is.

Here will be described from two aspects:

  1. What is the weak data consistency
  2. Eureka is how to synchronize data

1. Data Consistency weak

We know ZooKeeper can achieve data center, ZooKeeper is strong consistency.

Distributed systems have an important theory: CAP.

SpringCloud Eureka cluster registry is how consistent the data?

The distributed system theory mentioned three characteristics:

  • Consistency Data consistency

Distributed system, multiple copies of the data exist, there are some problems can result when data is written, a copy of part of the success, some of the copies failed, resulting in inconsistent data.

After the update operation to meet the requirements of consistency on the success of the data, multiple copies of the data must be consistent.

  • Availability Availability

Cluster client read and write operations at any time, the normal response to a request.

  • Partition Tolerance partition tolerance

When a communication failure occurs, the cluster is divided into multiple partitions can not communicate, the cluster is still available.

CAP theory states that: these three properties can not simultaneously meet up meet two.

P  is an objective reality, can not be bypassed , it is the choice of  C  chose  A .

ZooKeeper chose  C , it is to ensure data consistency as much as possible, under certain circumstances may be sacrificing usability.

Eureka chose  A , so the Eureka high availability, at any time, the service consumer can obtain a list of services normally, but does not guarantee strong consistency of the data, consumers may get a list of service expired.

SpringCloud Eureka cluster registry is how consistent the data?

Eureka design philosophy: keep the data available and expired better than losing a good usable data.

2. Eureka way data synchronization

2.1 复制方式

分布式系统的数据在多个副本之间的复制方式,主要有:

  • 主从复制

就是 Master-Slave 模式,有一个主副本,其他为从副本,所有写操作都提交到主副本,再由主副本更新到其他从副本。

写压力都集中在主副本上,是系统的瓶颈,从副本可以分担读请求。

  • 对等复制

就是 Peer to Peer 模式,副本间不分主从,任何副本都可以接收写操作,然后每个副本间互相进行数据更新。

对等复制模式,任何副本都可以接收写请求,不存在写压力瓶颈,但各个副本间数据同步时可能产生数据冲突。

Eureka 采用的就是 Peer to Peer 模式。

2.2 同步过程

Eureka Server 本身依赖了 Eureka Client,也就是每个 Eureka Server 是作为其他 Eureka Server 的 Client。

Eureka Server 启动后,会通过 Eureka Client 请求其他 Eureka Server 节点中的一个节点,获取注册的服务信息,然后复制到其他 peer 节点。

Eureka Server 每当自己的信息变更后,例如 Client 向自己发起注册、续约、注销请求, 就会把自己的最新信息通知给其他 Eureka Server,保持数据同步。

SpringCloud Eureka cluster registry is how consistent the data?

如果自己的信息变更是另一个Eureka Server同步过来的,这是再同步回去的话就出现数据同步死循环了。

SpringCloud Eureka cluster registry is how consistent the data?

Eureka Server in the implementation of the copy operation, use  HEADER_REPLICATION the http header to distinguish between normal request general application instance, stated that this is a copy request, so that other peer node receives a request, it will not be a copy operation, so as to avoid an endless loop.

Another problem is that data conflicts , such as server A initiates synchronization request to server B, if the data A is older than B, B can not accept data A, then B is A, how do you know the data is old it? A then how should we do?

The new and old data typically by a version number defined, Eureka is through  lastDirtyTimestamp this property is similar to the version number to achieve.

lastDirtyTimestamp Inside the center is a registered service instance of a property, it represents the first instance of this service time of the last change.

Such as Eureka Server A copy data to the Eureka Server B, there are two kinds of data conflict situation:

Data (1) A ratio of the new B, B returns 404, A re-registration to the application instance B.

Data (2) A ratio of the old B, B returns 409, the synchronization request data A is B.

SpringCloud Eureka cluster registry is how consistent the data?

There is an important mechanism: Hearbeat heartbeat , that renewal operations, to carry out the final restoration of data, because replication between nodes may be wrong, the heartbeat can be found errors, compensate.

For example, find an application and a server instance data is inconsistent, then the server back into the 404, examples can be re-registered.

3. Summary

  • Eureka is a weak consistency of data, selected in the CAP AP.
  • Eureka using Peer to Peer mode data replication.
  • Eureka to resolve replication conflicts lastDirtyTimestamp.
  • Eureka to achieve data recovery through a heartbeat mechanism.

Guess you like

Origin blog.51cto.com/14570694/2457308