1服务注册发现
服务注册发现使服务能够动态发现其他服务的位置。常用方案:Consul、Etcd、Nacos。
go
1// Consul 服务注册2func registerService(client *consul.Client, name string, port int) error {3 return client.Agent().ServiceRegister(&consul.AgentServiceRegistration{4 ID: fmt.Sprintf("%s-%d", name, port),5 Name: name,6 Port: port,7 Address: "localhost",8 Check: &consul.AgentServiceCheck{9 HTTP: fmt.Sprintf("http://localhost:%d/health", port),10 Interval: "10s",11 },12 })13}