Chapter 1: introduction to consumer and service registration and discovery


What is consumer?
Consul is a distributed and highly available system, which is mainly used for service discovery and service registration of microservice architecture.
It has the following characteristics:
1) Service discovery: used to register services and find services. For example, register a product service.
The client obtains and invokes the service through the gateway to obtain the list of goods.
2) Health check: consumer can regularly check whether the registered service is available. Of course.
It can be actively detected when calling the service.
3) Key / value storage: consumer can use the key / value structure to store registered services
Data storage.
4) Multiple data centers: support multiple data centers.

How to use consumer?
Start mode: start in agent mode.
1) Consumer agent – dev / / development mode
2) Consumer agent – server – bootstrap expect 1 – Data dir / TMP / consumer / – node = ali_1 – UI – client – bind intranet IP
-Join intranet IP

Parameter details:
-Server: the role is server, used to store data.
-Bootstrap expect 1: wait for how many nodes to start. One indicates that one of them can start.
-Data dir: data storage directory.
-Node: node name.
-UI: launches the default UI interface.
-Client: This indicates the IP address of a series of clients such as registration or query. If this IP address is not specified, the default is
-Join: start to join the cluster
-Bind: bind intranet IP

Someone wants to ask what the server and client parameters mean? This is the role of consumer
There are three roles in consumer

Client role: it is the client mode. It is a mode of the consumer node. In this mode, only the service registration is accepted and no information is saved. The registered service information will be forwarded to the server itself and will not be persisted.

Server role: indicates the server mode of the consumer, indicating that the consumer is a server. In this mode, the function is the same as that of client. But it will persist information locally
And can also participate in the leader election of the server.

Server leader role: indicates that this server is the leader of the cluster. It is different from other servers in that the leader needs to be responsible for synchronizing the registration information to other servers, as well as the health check of each node.

What are the different functions of the external ports provided by consumer? Just understand.
TCP / 8300 8300 port is used for server node, through which client calls server node through RPC Protocol. Server nodes call each other.
TCP / UDP / 8301 8301 port is used for the mutual communication between all nodes in a single data center, and the data synchronization of nodes in the cluster,
It enables the entire data center to automatically discover server addresses, detect node faults in a distributed way, and broadcast events (such as leader election)
8500 HTTP protocol access

View agent members
curl -X GET http://localhost:8500/v1/agent/members?pretty

How to register a service.
You can register services through the HTTP protocol.
curl -X PUT
http://localhost:8500/v1/agent/service/register -d “{
“Id”: “user”, / / service ID
“Name”: “username”, / / service name
“Tags”: [


“Address”: “”, / / IP of the service
“Port”: 8000, / / the port of the service
“EnableTagOverride”: false,
“Check”: {/ / health check section

"DeregisterCriticalServiceAfter": "90m",
"HTTP": " / RPC / health. PHP", // specifies the URL of the health check. After the call, as long as 20x is returned, the consumer is considered healthy
"Interval": "10s" // the interval time of health check. Call the above URL once every 10s

Unregistered services:

curl -X PUT http://localhost:8500/v1/agent/service/deregister/user {ID}

How to discover services:
Find by name
curl -X GET {Name}
Access to local health services
curl -X GET {Name}
Query service list

Recommended Today

Summary: NPM common commands and operations

The full name of NPM is (node package manager), which is a package management and distribution tool installed with nodejs. It is very convenient for JavaScript developersDownload, install, upload and manage installed packages。 First of all, the following variables will be used: < name > | < PKG > module name < version > version […]