agreement

A network protocol is a set of conventions that both sides of a communicating computer must follow. How to establish a connection, how to recognize each other, etc. Only by following this convention can computers communicate with each other.

In order for data to travel from source to destination over the network, participants in the network communication must follow the same rules, called protocols, which are ultimately reflected in the format of packets transmitted over the network.

In layman’s terms, it’s a set of rules that everyone follows.

Whether it’s a 4-tier model or a 7-tier model, at each tier there’s a function to be done. In order to implement these functions, everyone needs to follow the same rules.

And the rules that everyone follows are called protocols.

The protocol has many features:

  • Everyone in the agreement must understand the agreement and know in advance all the steps to be completed.
  • Everyone in the agreement must agree to it and abide by it.
  • The agreement must be clear, each step must be clearly defined and not lead to misunderstanding.

Redis protocol

An excerpt from the Redis document describes the agreement as follows:

Redis defines its own set of rules, such as different values for the first byte.

The Redis protocol class in Spring

Protocol is a Protocol class in Spring Boot Redis.

public final class Protocol {

  private static final String ASK_RESPONSE = "ASK";
  private static final String MOVED_RESPONSE = "MOVED";
  private static final String CLUSTERDOWN_RESPONSE = "CLUSTERDOWN";
  private static final String BUSY_RESPONSE = "BUSY";
  private static final String NOSCRIPT_RESPONSE = "NOSCRIPT";

  public static final String DEFAULT_HOST = "localhost";
  public static final int DEFAULT_PORT = 6379;
  public static final int DEFAULT_SENTINEL_PORT = 26379;
  public static final int DEFAULT_TIMEOUT = 2000;
  public static final int DEFAULT_DATABASE = 0;

  public static final String CHARSET = "UTF-8";

  public static final byte DOLLAR_BYTE = '$';
  public static final byte ASTERISK_BYTE = The '*';
  public static final byte PLUS_BYTE = '+';
  public static final byte MINUS_BYTE = The '-';
  public static final byte COLON_BYTE = ':';

  public static final String SENTINEL_MASTERS = "masters";
  public static final String SENTINEL_GET_MASTER_ADDR_BY_NAME = "get-master-addr-by-name";
  public static final String SENTINEL_RESET = "reset";
  public static final String SENTINEL_SLAVES = "slaves";
  public static final String SENTINEL_FAILOVER = "failover";
  public static final String SENTINEL_MONITOR = "monitor";
  public static final String SENTINEL_REMOVE = "remove";
  public static final String SENTINEL_SET = "set";

  public static final String CLUSTER_NODES = "nodes";
  public static final String CLUSTER_MEET = "meet";
  public static final String CLUSTER_RESET = "reset";
  public static final String CLUSTER_ADDSLOTS = "addslots";
  public static final String CLUSTER_DELSLOTS = "delslots";
  public static final String CLUSTER_INFO = "info";
  public static final String CLUSTER_GETKEYSINSLOT = "getkeysinslot";
  public static final String CLUSTER_SETSLOT = "setslot";
  public static final String CLUSTER_SETSLOT_NODE = "node";
  public static final String CLUSTER_SETSLOT_MIGRATING = "migrating";
  public static final String CLUSTER_SETSLOT_IMPORTING = "importing";
  public static final String CLUSTER_SETSLOT_STABLE = "stable";
  public static final String CLUSTER_FORGET = "forget";
  public static final String CLUSTER_FLUSHSLOT = "flushslots";
  public static final String CLUSTER_KEYSLOT = "keyslot";
  public static final String CLUSTER_COUNTKEYINSLOT = "countkeysinslot";
  public static final String CLUSTER_SAVECONFIG = "saveconfig";
  public static final String CLUSTER_REPLICATE = "replicate";
  public static final String CLUSTER_SLAVES = "slaves";
  public static final String CLUSTER_FAILOVER = "failover";
  public static final String CLUSTER_SLOTS = "slots";
  public static final String PUBSUB_CHANNELS = "channels";
  public static final String PUBSUB_NUMSUB = "numsub";
  public static final String PUBSUB_NUM_PAT = "numpat";

  public static final byte[] BYTES_TRUE = toByteArray(1);
  public static final byte[] BYTES_FALSE = toByteArray(0);

  public static final byte[] POSITIVE_INFINITY_BYTES = "+inf".getBytes();
  public static final byte[] NEGATIVE_INFINITY_BYTES = "-inf".getBytes();
}
Copy the code

Custom protocol

The most commonly used protocol is HTTP, and many distributed frameworks define their own protocols to achieve high performance. For example, Dubbo defines its own protocol for interaction between services.

See the Dubbo protocol for details

In our daily development, we often use some conventions, such as the unification of API return results, which is also an embodiment of the protocol.

{
    "code": 200."message": "success"."data": {}}Copy the code