123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package pb
- import (
- fmt "fmt"
- "property-household-gateway/parser"
- "time"
- "go.etcd.io/etcd/client/v3/naming/resolver"
- grpc "google.golang.org/grpc"
- "google.golang.org/grpc/keepalive"
- )
- // 客户端集合
- var Household HouseholdClient
- var Garden GardenClient
- var Common CommonClient
- var Thirdparty PropertyThirdpartyClient
- var System SystemClient
- var Device DeviceClient
- func setupDeviceClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
- // 根据是否为k8s来组装targets
- var serviceName string
- if parser.Conf.K8s {
- serviceName = parser.Conf.Rpc.Device.ServiceName
- } else {
- serviceName = parser.Conf.Rpc.Device.ServiceIp
- }
- // 发起一个连接并记录连接conn,后期释放
- if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
- parser.Conf.Rpc.Device.ServicePort),
- grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
- Device = NewDeviceClient(conn)
- conns = append(conns, conn)
- } else {
- fmt.Println("[rpc] dial Device conn err", err)
- }
- return
- }
- func setupSystemClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
- // 根据是否为k8s来组装targets
- var serviceName string
- if parser.Conf.K8s {
- serviceName = parser.Conf.Rpc.System.ServiceName
- } else {
- serviceName = parser.Conf.Rpc.System.ServiceIp
- }
- // 发起一个连接并记录连接conn,后期释放
- if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
- parser.Conf.Rpc.System.ServicePort),
- 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 setupHouseholdClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
- // 根据是否为k8s来组装targets
- var serviceName string
- if parser.Conf.K8s {
- serviceName = parser.Conf.Rpc.Household.ServiceName
- } else {
- serviceName = parser.Conf.Rpc.Household.ServiceIp
- }
- // 发起一个连接并记录连接conn,后期释放
- if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
- parser.Conf.Rpc.Household.ServicePort),
- grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
- Household = NewHouseholdClient(conn)
- conns = append(conns, conn)
- } else {
- fmt.Println("[rpc] dial household conn err", err)
- }
- return
- }
- func setupGardenClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
- // 根据是否为k8s来组装targets
- var serviceName string
- if parser.Conf.K8s {
- serviceName = parser.Conf.Rpc.Garden.ServiceName
- } else {
- serviceName = parser.Conf.Rpc.Garden.ServiceIp
- }
- // 发起一个连接并记录连接conn,后期释放
- if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
- parser.Conf.Rpc.Garden.ServicePort),
- 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 setupCommonClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
- // 根据是否为k8s来组装targets
- var serviceName string
- if parser.Conf.K8s {
- serviceName = parser.Conf.Rpc.Common.ServiceName
- } else {
- serviceName = parser.Conf.Rpc.Common.ServiceIp
- }
- // 发起一个连接并记录连接conn,后期释放
- if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
- parser.Conf.Rpc.Common.ServicePort),
- grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
- Common = NewCommonClient(conn)
- conns = append(conns, conn)
- } else {
- fmt.Println("[rpc] dial common conn err", err)
- }
- return
- }
- func setupThirdpartyClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
- // 根据是否为k8s来组装targets
- var serviceName string
- if parser.Conf.K8s {
- serviceName = parser.Conf.Rpc.Thirdparty.ServiceName
- } else {
- serviceName = parser.Conf.Rpc.Thirdparty.ServiceIp
- }
- // 发起一个连接并记录连接conn,后期释放
- if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
- parser.Conf.Rpc.Thirdparty.ServicePort),
- grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
- Thirdparty = NewPropertyThirdpartyClient(conn)
- conns = append(conns, conn)
- } else {
- fmt.Println("[rpc] dial thirdparty conn err", err)
- }
- return
- }
- func setupDeviceClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
- // 根据是否为k8s来组装targets
- var serviceName string
- serviceName = parser.Conf.Rpc.Device.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 {
- Device = NewDeviceClient(conn)
- conns = append(conns, conn)
- } else {
- fmt.Println("[rpc] dial Device conn err", err)
- }
- return
- }
- 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 setupHouseholdClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
- // 根据是否为k8s来组装targets
- var serviceName string
- serviceName = parser.Conf.Rpc.Household.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 {
- Household = NewHouseholdClient(conn)
- conns = append(conns, conn)
- } else {
- fmt.Println("[rpc] dial Household 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 setupCommonClient2(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
- // 根据是否为k8s来组装targets
- var serviceName string
- serviceName = parser.Conf.Rpc.Common.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 {
- Common = NewCommonClient(conn)
- conns = append(conns, conn)
- } else {
- fmt.Println("[rpc] dial Common 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,
- }
- if len(parser.Conf.EtcdAddrs) == 0 {
- setupHouseholdClient(kacp, conns)
- setupGardenClient(kacp, conns)
- setupCommonClient(kacp, conns)
- setupThirdpartyClient(kacp, conns)
- setupSystemClient(kacp, conns)
- setupDeviceClient(kacp, conns)
- return
- }
- setupHouseholdClient2(kacp, conns)
- setupGardenClient2(kacp, conns)
- setupCommonClient2(kacp, conns)
- setupThirdpartyClient2(kacp, conns)
- setupSystemClient2(kacp, conns)
- setupDeviceClient2(kacp, conns)
- return
- }
|