在微服务架构里面,每个小服务都是由很多节点组成,节点的添加删除故障希望能对下游透明,因此有必要引入一种服务的自动注册和发现机制,而 consul 提供了完整的解决方案,并且内置了对 GRPC 以及 HTTP 服务的支持
总体架构
- 服务调用: client 直连 server 调用服务
- 服务注册: 服务端将服务的信息注册到 consul 里
- 服务发现: 客户端从 consul 里发现服务信息,主要是服务的地址
- 健康检查: consul 检查服务器的健康状态
服务注册
服务端将服务信息注册到 consul 里,这个注册可以在服务启动可以提供服务的时候完成
完整代码参考: https://github.com/hatlonely/hellogolang/blob/master/sample/addservice/internal/grpcsr/consul_register.go
config := api.DefaultConfig() config.Address = r.Address client, err := api.NewClient(config) if err != nil { panic(err) } agent := client.Agent() IP := localIP() reg := &api.AgentServiceRegistration{ ID: fmt.Sprintf("%v-%v-%v", r.Service, IP, r.Port), // 服务节点的名称 Name: fmt.Sprintf("grpc.health.v1.%v", r.Service), // 服务名称 Tags: r.Tag, // tag,可以为空 Port: r.Port, // 服务端口 Address: IP, // 服务 IP Check: &api.AgentServiceCheck{ // 健康检查 Interval: r.Interval.String(), // 健康检查间隔 // grpc 支持,执行健康检查的地址,service 会传到 Health.Check 函数中 GRPC: fmt.Sprintf("%v:%v/%v", IP, r.Port, r.Service), DeregisterCriticalServiceAfter: r.DeregisterCriticalServiceAfter.String(), // 注销时间,相当于过期时间 }, } if err := agent.ServiceRegister(reg); err != nil { panic(err) }
服务发现
客户端从 consul 里发现服务信息,主要是服务的地址
完整代码参考: https://github.com/hatlonely/hellogolang/blob/master/sample/addservice/internal/grpclb/consul_resolver.go
services, metainfo, err := w.client.Health().Service(w.service, "", true, &api.QueryOptions{ WaitIndex: w.lastIndex, // 同步点,这个调用将一直阻塞,直到有新的更新 }) if err != nil { logrus.Warn("error retrieving instances from Consul: %v", err) } w.lastIndex = metainfo.LastIndex addrs := map[string]struct{}{} for _, service := range services { addrs[net.JoinHostPort(service.Service.Address, strconv.Itoa(service.Service.Port))] = struct{}{} }
健康检查
consul 检查服务器的健康状态,consul 用 google.golang.org/grpc/health/grpc_health_v1.HealthServer 接口,实现了对 grpc健康检查的支持,所以我们只需要实现先这个接口,consul 就能利用这个接口作健康检查了
完整代码参考: https://github.com/hatlonely/hellogolang/blob/master/sample/addservice/cmd/server/main.go
// HealthImpl 健康检查实现 type HealthImpl struct{} // Check 实现健康检查接口,这里直接返回健康状态,这里也可以有更复杂的健康检查策略,比如根据服务器负载来返回 func (h *HealthImpl) Check(ctx context.Context, req *grpc_health_v1.HealthCheckRequest) (*grpc_health_v1.HealthCheckResponse, error) { return &grpc_health_v1.HealthCheckResponse{ Status: grpc_health_v1.HealthCheckResponse_SERVING, }, nil } grpc_health_v1.RegisterHealthServer(server, &HealthImpl{})
参考链接
完整工程代码: https://github.com/hatlonely/hellogolang/tree/master/sample/addservice
consul 健康检查 api: https://www.consul.io/api/agent/check.html
consul 服务注册 api: https://www.consul.io/api/agent/service.html
grpc 健康检查: https://github.com/grpc/grpc/blob/master/doc/health-checking.md
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新动态
- ChrisBotti-ChrisBottiInBoston(克里斯·波提:波士顿音乐会)(2008-9-18)[WAV+CUE]
- 乡村民谣Paul O′Brien《Walk Back Home》[DFF][1.6G]
- 郑秀文《华纳超极品音色系列》[FLAC][927M]
- 群星《高度传真 DSD》[WAV][503M]
- 群星《飞歌》[WAV+CUE][680M]
- 群星《晶音年度发烧试听》非卖品[WAV+CUE][585M]
- 绝对发烧《男女合唱之那个季节里的歌》[WAV+CUE][619M]
- JacobCollier《DjesseVol.1》(2018)Hi-Res96kHz_24bit
- TIS发烧碟《西电王爵士活佛Blueeolution》[WAV+CUE]
- StockfischRecords老虎鱼卡帕与和谐二重唱《远航-遥距的小岛》SACD-ISO
- 古璇《粤听粤好听》柏菲音乐[WAV]
- 李祥庭-幽居(古琴独奏)[正版CD原抓WAV+CUE]
- 谭艳《再度重相逢HQ》头版限量编号[低速原抓WAV+CUE]
- 群星《人声典范-金嗓子 DSD》[WAV+CUE][524M]
- 群星《旅途欢歌》2CD[WAV+CUE][1.3G]