123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package pb
- import (
- fmt "fmt"
- "property-task/parser"
- "time"
- grpc "google.golang.org/grpc"
- "google.golang.org/grpc/keepalive"
- "go.etcd.io/etcd/client/v3/naming/resolver"
- )
- // 客户端集合
- var System SystemClient
- var Garden GardenClient
- var Thirdparty PropertyThirdpartyClient
- func setupSystemClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
- // 根据是否为k8s来组装targets
- var serviceName string
- serviceName = parser.Conf.Rpc.System.ServiceName
- builder, err := resolver.NewBuilder(parser.GetEtcdClient())
- if err != nil {
- panic(err)
- }
- // 发起一个连接并记录连接conn,后期释放
- if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
- grpc.WithResolvers(builder),
- grpc.WithBalancerName("round_robin"),
- grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
- System = NewSystemClient(conn)
- conns = append(conns, conn)
- } else {
- fmt.Println("[rpc] dial system conn err", err)
- }
- return
- }
- func setupGardenClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
- // 根据是否为k8s来组装targets
- var serviceName string
- serviceName = parser.Conf.Rpc.Garden.ServiceName
- builder, err := resolver.NewBuilder(parser.GetEtcdClient())
- if err != nil {
- panic(err)
- }
- // 发起一个连接并记录连接conn,后期释放
- if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
- grpc.WithResolvers(builder),
- grpc.WithBalancerName("round_robin"),
- grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
- Garden = NewGardenClient(conn)
- conns = append(conns, conn)
- } else {
- fmt.Println("[rpc] dial Garden conn err", err)
- }
- return
- }
- func setupThirdpartyClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
- // 根据是否为k8s来组装targets
- var serviceName string
- serviceName = parser.Conf.Rpc.Thirdparty.ServiceName
- builder, err := resolver.NewBuilder(parser.GetEtcdClient())
- if err != nil {
- panic(err)
- }
- // 发起一个连接并记录连接conn,后期释放
- if conn, err := grpc.Dial("etcd:///"+parser.Conf.Rpc.Prefix+"/"+serviceName,
- grpc.WithResolvers(builder),
- grpc.WithBalancerName("round_robin"),
- grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
- Thirdparty = NewPropertyThirdpartyClient(conn)
- conns = append(conns, conn)
- } else {
- fmt.Println("[rpc] dial Thirdparty conn err", err)
- }
- return
- }
- // SetupClients 创建客户端
- func SetupClients() (conns []*grpc.ClientConn) {
- // 客户端配置参数
- var kacp = keepalive.ClientParameters{
- // send pings every n seconds if there is no activity
- Time: time.Duration(parser.Conf.Rpc.Keepalive.ClientTime) * time.Second,
- // wait n second for ping ack before considering the connection dead
- Timeout: time.Duration(parser.Conf.Rpc.Keepalive.ClientTimeout) * time.Second,
- // send pings even without active streams
- PermitWithoutStream: true,
- }
- setupSystemClient2(kacp, conns)
- setupThirdpartyClient2(kacp, conns)
- setupGardenClient2(kacp, conns)
- return
- }
|