This article mainly analyzes the soul gateway framework HTTP long polling data synchronization strategy part of the source code.

background

The soul gateway framework synchronizes data configured by soul-admin to the soul-Bootstrap cluster. By default, websocket synchronization is used and HTTP long polling is supported.

The configuration of HTTP long poll synchronization is relatively simple. For details, see the official website.

The basic principle of HTTP long poll synchronization

  • The soul gateway cluster requests the soul-admin configuration service through HTTP. The soul-admin asynchronously responds to the request. The timeout period of the request is set to 90s.

  • In asynchronous response, soul-admin will put the request into a blocking queue and wait 60 seconds for execution.

  • If the data changes during the wait, the requests are successively removed from the queue and responded to;

  • After receiving the response, the gateway only knows that the data of the Group is changed and needs to request the configuration data of the Group again.

HTTP long polling synchronization source analysis

Soul – the bootstrap analysis

  • When the gateway starts, all Group configurations are pulled first.

  • The gateway application is configured with a thread pool to execute HttpLongPollingTask.

  • HttpLongPollingTask sends all MD5 values configured to soul-admin. This task will continue in a loop (RUNNING control).

  • If the returned Group is not empty, it indicates that the configuration has been changed, and the system will initiate a request to pull the specific configuration.

    String json = this.httpClient.postForEntity(listenerUrl, httpEntity, String.class).getBody();
    groupJson = GSON.fromJson(json, JsonObject.class).getAsJsonArray("data");
    
    if (groupJson != null) {
          // fetch group configuration async.
          ConfigGroupEnum[] changedGroups = GSON.fromJson(groupJson, ConfigGroupEnum[].class);
          if (ArrayUtils.isNotEmpty(changedGroups)) {
              log.info("Group config changed: {}", Arrays.toString(changedGroups));
              this.doFetchGroupConfig(server, changedGroups);
          }
      }
    Copy the code

series

  • Soul source learning [1] – preliminary exploration
  • Soul source learning [2] – divide load balancing plug-in
  • Soul source learning [3] – Dubbo plug-in
  • Soul source learning [4] – RateLimiter stream limiting plug-in
  • Soul source learning [5] – SpringCloud plug-in
  • Soul source learning [6] – Chain of responsibility mode
  • Soul source learning [7] – Data synchronization strategy websocket synchronization
  • [8] – Data synchronization strategy for ZooKeeper synchronization
  • [9] – ZooKeeper Synchronization (2)
  • Soul source learning [10] – Observer mode application