Four, eureka synchronization server registration operations

All articles

https://www.cnblogs.com/lay2017/p/11908715.html

 

text

In eureka server registration services in one article, we mentioned that the register method does two things

1) registration service instance information to the current node

2) Copy of service instances to other nodes

This article focuses on the second point, copying service instance information to other nodes. To this end, we briefly look at the code register method

Open class register method PeerAwareInstanceRegistryImpl

public void register(final InstanceInfo info, final boolean isReplication) {
    // ...
    super.register(info, leaseDuration, isReplication);
    // 复制到其它节点
    replicateToPeers(Action.Register, info.getAppName(), info.getId(), info, null, isReplication);
}

 

replicateToPeers responsible for the copy function, follow it

Private  void replicateToPeers (the Action Action, appName String, String ID, 
                              InstanceInfo info / * optional * / , 
                              InstanceStatus newStatus / * optional * / , Boolean isReplication) { 
    The Stopwatch Tracer = action.getTimer () Start ();.
     the try {
         IF ( isReplication) { 
            numberOfReplicationsLastMin.increment (); 
        } 
        // if this register is to copy operation itself, is no longer replicated to the other nodes 
        IF (peerEurekaNodes == Collections.EMPTY_LIST the ||isReplication) {
             return ; 
        } 

        // iterate around node 
        for ( Final PeerEurekaNode Node: peerEurekaNodes.getPeerEurekaNodes ()) {
             // if the current node, skip 
            IF (peerEurekaNodes.isThisMyUrl (node.getServiceUrl ())) {
                 Continue ; 
            } 
            // copy operation triggers 
            replicateInstanceActionsToPeers (Action, appName, ID, info, newStatus, Node); 
        } 
    } the finally { 
        tracer.stop (); 
    } 
}

Here is actually sending a registration request to all other nodes

 

Follow replicateInstanceActionsToPeers, you can see that in addition register and some other operations as is the need to be synchronized to other nodes. Here we are only concerned with the operation register

private void replicateInstanceActionsToPeers(Action action, String appName,
                                             String id, InstanceInfo info, InstanceStatus newStatus,
                                             PeerEurekaNode node) {
    try {
        InstanceInfo infoFromRegistry = null;
        CurrentRequestVersion.set(Version.V2);
        switch (action) {
            case Cancel:
                node.cancel(appName, id);
                break;
            case Heartbeat:
                InstanceStatus overriddenStatus = overriddenInstanceStatusMap.get(id);
                infoFromRegistry = getInstanceByAppAndId(appName, id, false);
                node.heartbeat(appName, id, infoFromRegistry, overriddenStatus, false);
                break;
            case Register:
                node.register(info);
                break;
            case StatusUpdate:
                infoFromRegistry = getInstanceByAppAndId(appName, id, false);
                node.statusUpdate(appName, id, newStatus, infoFromRegistry);
                break;
            case DeleteStatusOverride:
                infoFromRegistry = getInstanceByAppAndId(appName, id, false);
                node.deleteStatusOverride(appName, id, infoFromRegistry);
                break;
        }
    } catch (Throwable t) {
        // ... 
    }
}

 

Follow the register method, copy the instance information is structured into a task threw batchingDispatcher to asynchronous execution, if it fails will try again.

public  void Register ( Final InstanceInfo info) throws Exception {
     Long ExpiryTime = System.currentTimeMillis () + getLeaseRenewalOf (info);
     // asynchronous tasks 
    batchingDispatcher.process ( 
            taskld ( "Register" , info),
             // construct a replicated instance task information 
            new new InstanceReplicationTask (targetHost, Action.Register, info, null , to true ) {
                 public EurekaHttpResponse <Void> Execute () {
                     return replicationClient.register (info) ; 
                }
            },
            expiryTime
    );
}

 

The main logic InstanceReplicationTask is called replicationClient the register method, follow it

Here's an example to achieve Jersey

public EurekaHttpResponse<Void> register(InstanceInfo info) {
    String urlPath = "apps/" + info.getAppName();
    ClientResponse response = null;
    try {
        Builder resourceBuilder = jerseyClient.resource(serviceUrl).path(urlPath).getRequestBuilder();
        addExtraHeaders(resourceBuilder);
        response = resourceBuilder
                .header("Accept-Encoding", "gzip")
                .type(MediaType.APPLICATION_JSON_TYPE)
                .accept(MediaType.APPLICATION_JSON)
                .post(ClientResponse.class, info);
        return anEurekaHttpResponse(response.getStatus()).headers(headersOf(response)).build();
    } finally {
        // ...
    }
}

You can see, in fact, it is to send a http request to the other end eureka Server

 

to sum up

eureka Server will register, cancel, heartbeat and other operations synchronous transmission from one node to other nodes, thereby realizing the copy function. eureka and zookeeper is not the same, it is to follow ap, it adopted the final consistency, did not like the zookeeper selected as strong uniform. The final point to maintain the consistency of detail between the eureka Server or many, such as failure retry timeout, the heartbeat, the version number of instances, lock control and so on the same node. We are interested can learn more about it.

 

Guess you like

Origin www.cnblogs.com/lay2017/p/11919370.html