I. Registration Center
1.1 service registration and discovery
After using microservices, calls become calls between services.
Inter service calls need to know IP, port and other information.
Before there is no micro service, our call information is usually written in the caller's configuration file (some companies write this information to the database and other public places for maintenance).
Due to the complexity of the business, each service may depend on N other services. If the IP, port and other information of a service changes, the configuration files of all services that depend on the service need to be modified, which is obviously too troublesome.
Some services have multiple instances for load, and the number of instances may be adjusted at any time. If you have to modify the configuration of other services and restart each time you adjust the number of instances, it's too troublesome.
In order to solve this problem, there is a registration center
Suppose we have A service A that needs to call service B, and A service registration discovery component R. The overall process will become three steps:
- When service B starts, register capital with the registry
- Service A pulls the information of service B from the registry
- Service A invokes service B
With the service registration and discovery component, you don't have to modify other related services when modifying service A information

1.2 distributed consistency algorithm
Consistency is the consistency of data. In a distributed system, it can be understood that the values of data in multiple nodes are consistent
Consistency protocol algorithms mainly include Paxos, Raft, ZAB and mission
Specific reference: https://zhuanlan.zhihu.com/p/130332285
Paxos algorithm is a consistency algorithm based on message passing proposed by Leslie Lamport in 1990. It is very difficult to understand. The biggest difference between data synchronization based on Paxos protocol and traditional active and standby methods is that Paxos can ensure the continuous availability of services without data loss by only requiring more than half of the replicas to be online and communicate with each other normally.
Raft is a consistency algorithm designed by Diego Ongaro and John Ousterhout of Stanford University for the purpose of easy understanding. There have been more than a dozen languages of raft algorithm implementation framework, the more famous one is etcd, and Kubernetes of Google also uses etcd as his service discovery framework.
Raft is a simplified version of Paxos. Compared with Paxos, raft emphasizes that it is easy to understand and implement. Like Paxos, raft can provide services as long as more than half of the nodes are normal. This article "ETCD tutorial - 2.Raft Protocol" explains the principle of raft in detail, which is very interesting. Interested students can have a look.
ZooKeeper atomic broadcast (ZAB) is the core algorithm for ZooKeeper to realize distributed data consistency. ZAB draws lessons from Paxos algorithm, but unlike Paxos algorithm, it is a general distributed consistency algorithm. It is an atomic broadcast protocol specially designed for ZooKeeper to support crash recovery
1.3 selection of Registration Center
Five common registries are Zookeeper, Eureka, Nacos, Consul and ETCD
Specific reference: https://blog.csdn.net/lml200701158/article/details/123153513

II. Consul
2.1 introduction
Consul is an open source tool launched by HashiCorp, which is used to realize service discovery and configuration of distributed systems. Compared with other distributed service registration and discovery schemes, consul's scheme is more "one-stop", with built-in service registration and discovery framework, distributed consistency protocol implementation, health check, Key/Value storage and multi data center scheme. It no longer needs to rely on other tools (such as ZooKeeper).
Consul is also relatively simple to use and written in Go language, so it has natural portability (supporting Linux, windows and Mac OS X); The installation package contains only one executable file, which is convenient for deployment and can work seamlessly with lightweight containers such as Docker
Main features of Consul
2.2 consumer features
CP Model, using Raft Algorithm to ensure strong consistency, not availability; Support service registration and discovery, health examination KV Store Function. Supporting multiple data centers can avoid single point of failure of a single data center, and its deployment needs to consider network delay, Segmentation, etc. Support secure service communication, Consul Services can be generated and distributed TLS Certificates to establish mutual TLS connect. support http and dns Protocol interface; Officially provided web Management interface
2.3 installation
General installation
Download address: https://www.consul.io/downloads
win
//1 download //2 add environment variables //3 execution consul agent -dev
mac
//1 download //2 add environment variables //3 execution consul agent -dev
docker installation
//1. Use docker to download consumer //Synchronization time: ntpdate CN pool. ntp. org docker pull docker.io/library/consul //2. Modify the consumer tag docker tag docker.io/library/consul consul //3 start docker run -d -p 8500:8500 -p 8300:8300 -p 8301:8301 -p 8302:8302 -p 8600:8600/udp consul consul agent -dev -client=0.0.0.0 //4 accessing web pages in a browser // 8500 port: http port. We register the service and find that we use HTTP port // 8600 port: dns port //5 dig command to resolve the address and port through the domain name //The dns address of dig command is 10.0.0.102, //Port: 8600, //Domain name: the name of the service service. Automatically generate consumption //The query type is specified as srv dig @10.0.0.102 -p 8600 consul.service.consul SRV

III. common API s
3.1 service registration
Reference documents: https://www.consul.io/commands/services/register
// put request // Address: / v1/agent/service/register // Service Registration Flags -name:The name of the service to register-->Service name -id:The ID of the service. This will default to -name if not set-->id Do not set and name agreement -tag value - Associate a tag with the service instance. This flag can be specified multiples times.-->Label of the service -address - The address of the service. If this isn't specified, it will default to the address registered with the local agent. -port - The port of the service.
Registering a presentation using postman
// towards http://10.0.0.102:8500/v1/agent/service/register Send put request // body data is { "Name":"lqz", "ID":"ddd", "Tags":["lqz","web"], "Address":"127.0.0.1", "Port":8080 }

Go language registration
package main import "fmt" import consulapi "github.com/hashicorp/consul/api" const ( consulAddress = "10.0.0.102:8500" ) func RegisterConsul(localIP string, localPort int, name string,id string, tags []string) error { // Create connection consumer service configuration config := consulapi.DefaultConfig() config.Address = consulAddress client, err := consulapi.NewClient(config) if err != nil { fmt.Println("consul client error : ", err) } // Create a service registered to consumer registration := new(consulapi.AgentServiceRegistration) registration.ID = id registration.Name = name //Find this service by this name registration.Port = localPort //registration.Tags = []string{"lqz", "web"} / / this is a tag. You can find this service according to this, which is equivalent to v1.0 1 this registration.Tags = tags //This is a tag. You can find this service according to this, which is equivalent to V1 1 this registration.Address = localIP // Add consumer health check callback function check := new(consulapi.AgentServiceCheck) check.HTTP = fmt.Sprintf("http://%s:%d/health", registration.Address, registration.Port) check.Timeout = "5s" //overtime check.Interval = "5s" //Health examination frequency check.DeregisterCriticalServiceAfter = "30s" // Consumer will automatically delete the registration service 30 seconds after the failure check registration.Check = check // Register service to consumer err = client.Agent().ServiceRegister(registration) if err != nil { return err } return nil } func main() { // 1 registration RegisterConsul("192.168.1.1",8080,"lqz_web","lqz_web",[]string{"lqz","web"}) }
3.2 service deletion
reference resources: https://www.consul.io/commands/services/deregister
Registering a presentation using postman
// To http://10.0.0.102:8500/v1/agent/service/deregister/ddd Send put request
go code
func DeleteService() error { // Create connection consumer service configuration config := consulapi.DefaultConfig() config.Address = consulAddress client, err := consulapi.NewClient(config) if err != nil { fmt.Println("consul client error : ", err) } err = client.Agent().ServiceDeregister("qqq") if err != nil { return err } return nil }
3.3 set up health check
reference resources: https://www.consul.io/api-docs/agent/check#register-check
3.4 access to services
reference resources: https://www.consul.io/api-docs/agent/service#sample-request
Get all
func GetAllService() (map[string]*consulapi.AgentService, error) { // Create connection consumer service configuration config := consulapi.DefaultConfig() config.Address = consulAddress client, err := consulapi.NewClient(config) if err != nil { fmt.Println("consul client error : ", err) } res, err := client.Agent().Services() if err != nil { return nil, err } return res, nil }
Filtering service
func FilterService() (map[string]*consulapi.AgentService, error) { // Create connection consumer service configuration config := consulapi.DefaultConfig() config.Address = consulAddress client, err := consulapi.NewClient(config) if err != nil { fmt.Println("consul client error : ", err) } //res, err := client.Agent().ServicesWithFilter(`Service=="lqz-web03" `) / / filter by service name res, err := client.Agent().ServicesWithFilter(`Port==8087`) // Filter by port if err != nil { return nil, err } return res, nil }
3.5 multiple registrations of the same service
The same name and different id will put the same name together