Client registration process

How many places does a client register with a server?

  • Initiate registration while initiating initialization
  • If the server returns 404 while sending heartbeat, initiate registration
  • The client cache instance lease information changed and initiated registration. Procedure

The client code of the registration process is relatively simple. It sends an HTTP request of the post type with the request parameter InstanceInfo

boolean register(a) throws Throwable {
  EurekaHttpResponse<Void> httpResponse;
  try {
    httpResponse = eurekaTransport.registrationClient.register(instanceInfo);
  } catch (Exception e) {
    throw e;
  }
  return httpResponse.getStatusCode() == Status.NO_CONTENT.getStatusCode();
}
/ / AbstractJerseyEurekaHttpClient method in a class
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);
    returnanEurekaHttpResponse(response.getStatus()).headers(headersOf(response)).build(); }}Copy the code

Client Heartbeat

The client periodically sends heartbeat information to EurekaServer

private class HeartbeatThread implements Runnable {
  public void run(a) {
    / / contract
    if (renew()) {
      // The timestamp of the last successful heartbeatlastSuccessfulHeartbeatTimestamp = System.currentTimeMillis(); }}}boolean renew(a) {
  EurekaHttpResponse<InstanceInfo> httpResponse;
    // Send put request
    httpResponse = eurekaTransport.registrationClient.sendHeartBeat(instanceInfo.getAppName(),    
                                                      instanceInfo.getId(), instanceInfo, null);
    // The server responds 404 to initiate registration,
    if (httpResponse.getStatusCode() == Status.NOT_FOUND.getStatusCode()) {
      REREGISTER_COUNTER.increment();
      // Marks the local instanceInfo as dirty, returns the timestamp of the last dirty flag set
      long timestamp = instanceInfo.setIsDirtyWithTime();
      // Service registration
      boolean success = register();
      if (success) {
        instanceInfo.unsetIsDirty(timestamp);
      }
      returnsuccess; }}public synchronized void unsetIsDirty(long unsetDirtyTimestamp) {
  // Check that the lastDirtyTimestamp held by instanceInfo is smaller than the timestamp passed in by the login
  // For the purpose of checking, there are other operations that set dirty flags such as the timed task of configuring refresh. If the instance configuration of the client is updated,
  // The local cache instanceInfo is set to dirty and lastDirtyTimestamp is updated,
  // The local instanceInfo file is not the same as the instanceInfo file on the server, so it is still dirty
  if (lastDirtyTimestamp <= unsetDirtyTimestamp) {
    isInstanceInfoDirty = false; }}Copy the code