The registry

What is a registry

To figure this out, we need to learn from an actual situation. Let’s take a shopping situation as an example. In the shopping situation and the scene, there are three roles involved: the consumer, the store, and the mall.

In this scenario, we solved it by drawing

Why use a registry

Figure resolution

advantages

1, the decoupling

Service consumers and service providers are decoupled and vary independently without affecting each other

2, extension,

The addition and removal of new services by service consumers and service providers have no impact on either side

3. Intermediary design pattern

This is a model of a many-to-many relationship

Registry Type

zookeeper

A widely used distributed high-performance service

consul

Consul, a tool for discovering and configuring services, provides API registration and discovery services. Consul performs health checks to ensure operability

etcd

A highly available, distributed, consistent key-value structure for sharing configuration information and service discovery K8s uses ETCD

eureka

This registry has been closed. Do not use it

How do I use Consul in microservices

What is a Consul

Consul is an open source tool for service discovery and configuration for distributed systems. Is developed by the GO language. It mainly consists of several components:

  • ** Service discovery: ** Clients provide services through Consul, similar to how APIS,MySQL, or other clients can use Consul to discover service providers. Using services like DNS or HTTP, applications and services that they depend on can be easily discovered.
  • ** Check health: **Consul client can provide a health check related to a given service (Web server returns 200 OK) or a local node (” memory utilization is less than 90% “). This information can monitor the health of the cluster and keep access away from unhealthy host components.
  • ** Key-value pair storage: ** Applications can use Cousul’s hierarchical key-value pairs.
  • ** MULTI-data Center: **Consul has an unpacked and used multi-data center.

The role of the Consul

Client: a stateless client that forwards HTTP and DNS interface requests to the server cluster on the LAN. Server: a server that saves configuration information and is a high availability cluster that communicates with local clients on the LAN and with other data centers on the WAN. The recommended number of servers for each data center is 3 or 5.

agent

An agent must be running on each member of the Consul cluster, which can be started using the Consul Agent command. The Agent can run in the Server or client state. Naturally, nodes running in the server state are called server nodes; Nodes running in client state are called client nodes.

The client node

Responsible for forwarding all RPCS to the server node. Being stateless and lightweight, it is possible to deploy a large number of client nodes.

Server node

Responsible for the complex work that makes up the cluster (elections, state maintenance, forwarding requests to Leads), and the services consul provides (responding to RCP requests). Considering fault tolerance and convergence, it is generally appropriate to deploy 3 to 5.

Consul insider

The term

  • Agent: The agent is the daemon of each member on Consul. It is started by Consul Agent. The agent can run in client or server mode. Since all nodes must run agents, it is easier to reference nodes as clients or servers, but there are agents for other instances. All proxies can run DNS or HTTP interfaces and are responsible for running checks and keeping services in sync.
  • Client: The proxy that the client can forward all RPC requests to the server. The client is relatively stateless. The only background activity performed by the client is the LANgossip pool. It consumes minimal resource overhead and a small amount of network bandwidth.
  • Server side: The server side is the agent with extended functions that are involved in maintaining cluster state, responding to RPC queries, exchanging WAN Gossip with other data centers, and forwarding queries to upper-level or remote data centers.
  • ** Data center: ** While the definition of a data center may seem obvious, there are some fine details that must be considered. We define a data center as a private, low latency, and high bandwidth network environment. This does not include communication over the public Internet, but for our purposes, multiple available areas within a single EC2 area will be treated as part of a single data center
  • Gossip: Consul is built on top of Serf and provides a complete Gossip protocol that is used in many places. Serf provides membership, fault detection and event broadcasting. The communication between nodes in Gossip uses UDP protocol.
  • LAN Gossip: Refers to the LAN Gossip pool on nodes in the same LAN or data center.
  • **WAN Gossip: ** refers to the WAN Gossip pool that contains servers in different data centers that communicate over the network.
  • Consistency protocol adopts Raft algorithm to ensure high availability of services.
  • The GOSSIP protocol is adopted for member management and message broadcast, and ACL access control is supported.

ACL technology is widely used in routers. It is a flow control technology based on packet filtering. The control list uses the source IP address, destination IP address, and port number as the basic elements for packet check and determines whether the packets that meet the conditions are allowed to pass.

Gossip is the P2P protocol. The main thing he wants to do is decentralize.

This protocol mimics human rumor spreading behavior. The first way to spread rumors is to have seed nodes. A seed node randomly sends a list of its own nodes to the other nodes every second, along with messages to propagate. Any newly added nodes are quickly known to the whole network in this way.

Consul Operation Flow Chart

For illustration, please refer to the figure

Consul microservice practices (how to register, discover)

How does Consul register for discovery service?

steps

Consul download address

Official website: www.consul.io/

Download address: releases.hashicorp.com/consul/1.7…. Or https://www.consul.io/downloads.html

2. Start the server

1.1 Startup Commands in Development Mode

consul.exe agent -dev
Copy the code

Version: Consul Version

Node ID: Consul Current boot Node ID (GUID)

Node Name: Node Name (default computer Name)

Datacenter: indicates a Datacenter

Server: The startup mode is the Server mode, otherwise, the client mode is the client mode

Client Addr: indicates the Client connection address. The value can be HTTP, HTTPS,gRPC, or DNS. By default, we use HTTP

Cluster Addr: indicates the Cluster address, which is the boot mode in Server mode

Encrypt: safety

1.2 Commands for starting production Mode:

consul agent -server -bootstrap-expect 1  -data-dir d:/consul/data 
Copy the code

1.2.1 Error: prompt

Main reason: When server mode is started, the default binding address is 0.0.0.0. You want to bind the default IP address

Consul agent-server-bind =127.0.0.1 -bootstrap-expect 1-data-dir d:/consul/dataCopy the code

1.3 Starting the client Mode

Use net programs directly to start

1.4 conclusion:

-bind must be a private IP address, 0.0.0.0 is the default value. When you encounter problems, you must first start from environmental differences or root causes. If the root causes cannot be found, you can use heuristic method to solve the problem.

Consul operates in three modes, Client, Server, and Dev.

Note: Dev mode does not persist data, so the saved configuration information will be lost after the restart.

Consul start parameters are described as follows:

Agent Consul’s core command, – Server stands for server mode. – UI stands for enabling the Web console. – Bootstrap-expect stands for the number of clusters to be created. – Node indicates the name of the current node. – Client should be a registered address of the client service, which can be the same as that of the current server or another host address. The default value is 127.0.0.1

Run cosnul agent in server mode: -server: define agent to run in server mode -bootstrap-expect: Expect to offer in a datacenter server node number, when the value is to provide consul until reach the specified number of sever will guide the whole cluster, the tag can’t share the bootstrap – bind: All nodes in the cluster must be accessible to the address. The default value is 0.0.0.0-node: The name of the node in the cluster, which must be unique within a cluster. The default value is the host name of the node -uI-dir: -rejoin: Causes Consul to ignore consul’s previous departure and attempt to join the cluster after restarting. -config-dir: configuration file directory, in which all files ending in. Json will be loaded. -client: Consul service Specifies the listening address of the consul service. This address provides HTTP, DNS, and RPC services. The default value is 127.0.0.1 and therefore does not provide external services

Download Consul package

Aspnetcore nuget for download

3. Then the microservice provider registers with the code as follows

Var consulClient = new consulClient (configuration => {// establish a configuration between client and server The Uri (" http://127.0.0.1:8500 "); }); Var registration = new AgentServiceRegistration() {ID = guid.newguid ().tostring (), Name = "teamservice", Address = "http://localhos", Port = "5001", Tags = new string[], Check = new AgentServiceCheck {// 3.1, Consul health Check Timeout = timespan. FromSeconds(10), / / 3.2, service stopped for 5 seconds after the cancellation of service DeregisterCriticalServiceAfter = TimeSpan. FromSeconds (5), / / = 3.3, consul health check address HTTP serviceNode. HealthCheckAddress, / / 3.4 consul health check Interval time Interval = TimeSpan. FromSeconds (10), }}; / / 4, the registration service consulClient. Agent. ServiceRegister (registration), Wait ();Copy the code

4. Finally, the microservice discoverer acquires the service discovery code as follows

Var consulClient = new consulClient (configuration => {// establish a configuration between client and server The Uri (" http://127.0.0.1:8500 "); }); / / 2, consul query Service, depending on the Service name lookup var queryResult = await consulClient. Catalog. The Service (" teamservice "); Var list = new list <ServiceUrl>(); foreach (var service in queryResult.Response) { list.Add(new ServiceUrl { Url = service.ServiceAddress + ":" + service.ServicePort }); }Copy the code

How does Consul do a heartbeat check?

steps

1. Use AgentServiceCheck to do this

New AgentServiceCheck {// 3.1, Consul health check Timeout = timespan. FromSeconds(10), / / 3.2, service stopped for 5 seconds after the cancellation of service DeregisterCriticalServiceAfter = TimeSpan. FromSeconds (5), / / = 3.3, consul health check address HTTP serviceNode. HealthCheckAddress, / / 3.4 consul health check Interval time Interval = TimeSpan. FromSeconds (10),}Copy the code

How to encapsulate Consul?

steps

1. First, the service is abstracted into provider and discoverer based on the service role

2. Then use ioc container for conditional preparation and APP builder for service registration

How does Consul build a cluster?

Parameter interpretation
Command line arguments
-bind: binds an address to the node. -enable-script-checks=true: sets the check service as available. - Join: adds the node to an existing cluster. -config-file - Configuration file to load -config-dir: Specifies the configuration file that defines the service. By default, all files ending in. Json will be read. -datacenter: The data center does not have a name. The default value is DC-client. -ui: -data-dir Directory where data is stored on Consul -bootstrap: This command is used to control whether a server is in Bootstrap mode. Only one server can be in Bootstrap mode in a datacenter. When a server is in Bootstrap mode, you can elect the raft Leader. - the bootstrap - expect: Expect to offer in a datacenter server node number, when the value is to provide consul until reach the specified number of sever will guide the whole cluster, the tag can't and the bootstrap public these two parameters is very important, a choice, if you don't use two parameters, 2018/10/14 15:40:00 [ERR] Agent: failed to sync remote state: No cluster leaderCopy the code
Configuration file parameters
UI: Equivalent to the -ui command line flag. Acl_token: indicates that the agent will use this token to request consul server. Acl_ttl: indicates the cache that controls the TTL. The default value is 30s addresses. DNS, HTTP, and RPC advertise_ADDR: Equal to -advertise bootstrap: Equal to -bootstrap bootstrap_expect: Equal to -bootstrap-expect bind_ADDR: Equivalent to -bindca_file: provides the path of the CA file used to check the links between the client and the server. Cert_file: must be used together with key_file. Check_update_interval: client_ADDR: Equivalent to -client datacenter: Equivalent to -dc datA_DIR: equivalent to -data-dir disable_ANONYMous_signature: disables the anonymous signature during update check enable_debug: Enable debug mode enable_syslog: equal to -syslog Encrypt: Equal to -encrypt key_file: path for providing the private key leave_ON_TERMINATE: The default is false. If true, the agent will send leave messages to other nodes in the cluster when it receives a TERM signal. Log_level: the same as -log-level node_name: the same as -node ports: this is a nested object. The following keys can be set: DNS (DNS address: 8600), HTTP (HTTP API address: 8500), RPC (RPC :8400), SERF_LAN (LAN Port :8301), SERF_WAN (WAN Port :8302), server(Server RPC :8300) Protocol: Equivalent to -protocol rejoin_after_leave: Equivalent to -rejoin retry_join: Equivalent to -retry-join retry_interval: equivalent to -retry-interval Server: Equivalent to -server syslog_facility: When enable_syslog is provided, this parameter controls the level of information to be sent. By default, Local0 UI_DIR: equivalent to -uI-dirCopy the code
Cluster Construction (single-node)

Because there are no resources, you can only install the pseudo cluster on one machine, if it is three servers to do it, do not need to write json configuration file, directly start from the command line can be

$mkdir -pv /data/app/consul/{node1,node2,node3} mkdir: created directory '/data/app/consul/node1' mkdir: created directory '/data/app/consul/ Created directory '/data/app/consul/node2' mkdir: created directory '/data/app/consul/node3'Copy the code
Node 1 Configuration
$ vim /data/app/consul/node1/basic.json { "datacenter": "dc1", "data_dir": "/data/app/consul/node1", "log_level": "INFO", "server" : true, "node_name" : "node1", "UI" : true, "bind_addr" : "10.208.1.10", "client_addr" : "" advertise_addr 10.208.1.10", ":" 10.208.1.10 ", "bootstrap_expect" : 3, "ports" : {" HTTP: "8500," DNS ": 8600," server ": 8300, "serf_lan": 8301, "serf_wan": 8302 } } $ nohup /usr/bin/consul agent -config-file=/data/app/consul/node1/basic.json > /data/app/consul/node1/consul.log 2>&1 & $ tail -100f /data/app/consul/node1/consul.logCopy the code
Node 2 Configuration
$ vim /data/app/consul/node2/basic.json { "datacenter": "dc1", "data_dir": "/data/app/consul/node2", "log_level": "INFO", "server" : true, "node_name" : "2", "bind_addr" : "10.208.1.10", "client_addr" : "" advertise_addr 10.208.1.10", ":" 10.208.1.10 ", "ports" : {" HTTP: "8510," DNS ": 8610," server ": 8310," serf_lan ": 8311, "serf_wan": 8312}} $nohup/usr/bin/consul agent -- config file = / data/app/consul / 2 / basic json - retry - join = 10.208.1.10:8301 > /data/app/consul/node2/consul.log 2>&1 & $ tail -100f /data/app/consul/node2/consul.logCopy the code
Node 3 Configuration
$ vim /data/app/consul/node3/basic.json { "datacenter": "dc1", "data_dir": "/data/app/consul/node3", "log_level": "INFO", "server" : true, "node_name" : "node3", "bind_addr" : "10.208.1.10", "client_addr" : "" advertise_addr 10.208.1.10", ":" 10.208.1.10 ", "ports" : {" HTTP: "8520," DNS ": 8620," server ": 8320," serf_lan ": 8321, "serf_wan": 8322}} $nohup/usr/bin/consul agent -- config file = / data/app/consul/node3 / basic json - retry - join = 10.208.1.10:8301 > /data/app/consul/node3/consul.log 2>&1 & $ tail -100f /data/app/consul/node3/consul.logCopy the code
View log changes on controller 1
2019/01/24 22:48:58 [INFO] serf: EventMemberJoin: node2.dc1 10.208.1.10 2019/01/24 22:49:59 [INFO] serf: EventMemberJoin: node3. Dc1 10.208.1.10... 2019/01/24 22:49:59 [INFO] consul: Found expected number of peers, attempting bootstrap: 10.208.1.10:8320,10.208. 1.10:8300,10.208. 1.10:8310 2019/01/24 22:49:59 [INFO] consul: Handled member-join event for server "node3.dc1" in area "wan" 2019/01/24 22:50:05 [WARN] raft: Heartbeat timeout from "" reached, starting election 2019/01/24 22:50:05 [INFO] raft: Node at 10.208.1.10:8300 [Candidate] entering Candidate state in term 2 2019/01/24 22:50:05 [INFO] raft: Election won. Tally: 2 2019/01/24 22:50:05 [INFO] raft: Node at 10.208.1.10:8300 [Leader] entering Leader state 2019/01/24 22:50:05 [INFO] raft: Added peer faa05ada-4e06-6d5a-f35b-286c57826231, starting replication 2019/01/24 22:50:05 [INFO] raft: Added peer be2837bd-3b87-07f9-a776-863ed5966ffb, starting replication 2019/01/24 22:50:05 [INFO] consul: cluster leadership acquired 2019/01/24 22:50:05 [INFO] consul: New leader elected: node1 2019/01/24 22:50:05 [WARN] raft: AppendEntries to {Voter BE2837BD-3b87-07f9-a776-863ed5966ffb 10.208.1.10:8310} Til, sending older logs (next: 1) 2019/01/24 22:50:05 [INFO] raft: Pipelining replication to peer {Voter BE2837BD-3b87-07f9-a776-863ed5966FFB 10.208.1.10:8310} 2019/01/24 22:50:05 [INFO] consul: member 'node1' joined, marking health alive 2019/01/24 22:50:05 [INFO] consul: member 'node2' joined, marking health alive 2019/01/24 22:50:05 [INFO] agent: Synced node info 2019/01/24 22:50:05 [INFO] consul: member 'node3' joined, marking health alive 2019/01/24 22:50:06 [WARN] raft: AppendEntries to {Voter faa05ada-4e06-6D5a-f35b-286c57826231 10.208.1.10:8320} AppendEntries to {Voter faa05ada-4e06-6D5a-f35b-286c57826231 10.208.1.10:8320} 1) 2019/01/24 22:50:07 [INFO] raft: Pipelining replication to peer {Voter FAa05ada-4e06-6D5A-f35b-57826231 10.208.1.10:8320}Copy the code
Visit the UI
Viewing Cluster Information
$/usr/bin/consul members-http-addr =10.208.1.10:8500 Node Address Status Type Build Protocol DC Segment node1 10.208.1.10:8301 Alive server 1.3.1 2dc1 <all> node2 10.208.1.10:8311 Alive server 1.3.1 2dc1 <all> node3 10.208.1.10:8321 alive server 1.3.1 2dc1 <all> $/usr/bin/consul info-http-addr =10.208.1.10:8500 Agent: Check_monitors = 0 check_TTls = 0 checks = 0 services = 0Build: prerelease = revision = f2b13f30 version = 1.3.1 Consul: Bootstrap = false Known_datacenters = 1 leader = true Leader_ADDR = 10.208.1.10:8300 server = Trueraft: applied_index = 80 commit_index = 80 fsm_pending = 0 last_contact = 0 last_log_index = 80 last_log_term = 2 last_snapshot_index = 0 last_snapshot_term = 0 latest_configuration = [{Suffrage:Voter ID: e06 faa05ada - 4-6 d5a - f35b - 286 c57826231 Address: 10.208.1.10:8320} {Suffrage: Voter ID: 5 aee898c - ead4 - f844-0 d70-37 ee7d9e9fb3 Address: 10.208.1.10:8300} {Suffrage: Voter ID: BE2837BD-3B87-07F9-A776-863ED5966FFB Address:10.208.1.10:8310}] latEST_configuration_index = 1 NUM_peers = 2 protocol_version = 3 protocol_version_max = 3 protocol_version_min = 0 snapshot_version_max = 1 snapshot_version_min = 0  state = Leader term = 2runtime: Arch = AMd64 CPU_count = 4 goroutines = 104 MAX_procs = 4 OS = Linux version = GO1.11.1 serF_LAN: coordinate_resets = 0 encrypted = false event_queue = 0 event_time = 2 failed = 0 health_score = 0 intent_queue = 0 left  = 0 member_time = 3 members = 3 query_queue = 0 query_time = 1serf_wan: coordinate_resets = 0 encrypted = false event_queue = 0 event_time = 1 failed = 0 health_score = 0 intent_queue = 0 left  = 0 member_time = 5 members = 3 query_queue = 0 query_time = 1Copy the code