gRPC服務發現與服務治理,目前常看法決方案有如下兩種node
本文粗略講解一下兩種方案的優缺點nginx
nginx-consul.conf大體以下golang
# nginx須要安裝grpc模塊
upstream User {
server 127.0.0.1:17000;
server 127.0.0.1:18000;
}
server {
listen 19000 http2;
server_name _;
location /protobuf.User {
default_type application/grpc;
grpc_pass grpc://User;
}
}
複製代碼
Envoy 是專爲大型現代 SOA(面向服務架構)架構設計的 L7 代理和通訊總線。是istio重要組件之一
bash
envoy.yaml架構
node:
id: id_1
cluster: test_cluster
watchdog:
miss_timeout: 0.2s
megamiss_timeout: 1s
kill_timeout: 0s
multikill_timeout: 0s
admin:
access_log_path: /var/log/admin_access.log
address:
socket_address: { address: 127.0.0.1, port_value: 10000 }
static_resources:
listeners:
- name: listener_0
address:
socket_address: { address: 127.0.0.1, port_value: 19000 }
filter_chains:
- filters:
- name: envoy.http_connection_manager
config:
stat_prefix: ingress_http
codec_type: HTTP2
route_config:
name: local_route
virtual_hosts:
- name: local_service
domains: ["*"]
routes:
- match: { prefix: "/protobuf.User" }
route: { cluster: xds_cluster }
http_filters:
- name: envoy.router
clusters:
- name: xds_cluster
connect_timeout: { seconds: 1 }
type: STATIC
lb_policy: ROUND_ROBIN
http2_protocol_options: {}
hosts:
- socket_address: { address: 127.0.0.1, port_value: 18000 }
- socket_address: { address: 127.0.0.1, port_value: 17000 }
health_checks:
- grpc_health_check: {service_name: YourServiceName}
unhealthy_threshold : 1
healthy_threshold: 1
timeout: 0.5s
interval: 0.5s
interval_jitter: 0.5s
複製代碼
這是一個靜態的路由配置,帶grpc健康檢查, 有四個關鍵點app
# 引入protobuf包google.golang.org/grpc/health/grpc_health_v1並實現裏面的Check和Watch方法
package health
import(
"log"
"context"
pb "google.golang.org/grpc/health/grpc_health_v1"
)
type Health struct{}
func New() *Health {
return &Health{}
}
func (h *Health) Check(ctx context.Context, in *pb.HealthCheckRequest) (*pb.HealthCheckResponse, error){
log.Printf("checking............%s", in.Service)
var s pb.HealthCheckResponse_ServingStatus = 1
return &pb.HealthCheckResponse{
Status : s,
}, nil
}
func (h *Health) Watch(in *pb.HealthCheckRequest, w pb.Health_WatchServer) (error){
log.Printf("watching............%s", in.Service)
var s pb.HealthCheckResponse_ServingStatus = 1
r := &pb.HealthCheckResponse{
Status : s,
}
for {
w.Send(r)
}
return nil
}
複製代碼
# 服務註冊
pb.RegisterHealthServer(grpcServer, health.New())
複製代碼
啓動envoy和grpc服務後, grpc就會收心跳(默認的心跳是60秒,yaml的心跳設置是0.5秒)
YourServiceName就是yaml中grpc_health_check的service_name參數,這裏你能夠自定義你想監聽的方法dom
相對於nginx, 我更傾向於envoy,首先envoy就是爲微服務而生的負載勻衡工具. grpc健康檢查是微服務中重要的一環. 可是nginx擁有活躍的社區,說不定不久未來也會有支持grpc健康檢查的插件.socket