package rpc import ( "context" "egg-im/pkg/grpclib/picker" "egg-im/pkg/grpclib/resolver/k8s" "egg-im/pkg/pb" "fmt" "google.golang.org/grpc/balancer/roundrobin" "google.golang.org/grpc" ) var ( logicIntClient pb.LogicIntClient connectIntClient pb.ConnectIntClient businessIntClient pb.BusinessIntClient ) func GetLogicIntClient() pb.LogicIntClient { if logicIntClient == nil { initLogicIntClient() } return logicIntClient } func GetConnectIntClient() pb.ConnectIntClient { if connectIntClient == nil { initConnectIntClient() } return connectIntClient } func GetBusinessIntClient() pb.BusinessIntClient { if businessIntClient == nil { initBusinessIntClient() } return businessIntClient } func initLogicIntClient() { conn, err := grpc.DialContext(context.TODO(), k8s.GetK8STarget("egg-im", "logic", "8000"), grpc.WithInsecure(), grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy": "%s"}`, roundrobin.Name))) if err != nil { panic(err) } logicIntClient = pb.NewLogicIntClient(conn) } func initConnectIntClient() { conn, err := grpc.DialContext(context.TODO(), k8s.GetK8STarget("egg-im", "connect", "8000"), grpc.WithInsecure(), grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy": "%s"}`, picker.AddrPickerName))) if err != nil { panic(err) } connectIntClient = pb.NewConnectIntClient(conn) } func initBusinessIntClient() { conn, err := grpc.DialContext(context.TODO(), "127.0.0.1:8000", grpc.WithInsecure(), grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy": "%s"}`, roundrobin.Name))) //conn, err := grpc.DialContext(context.TODO(), k8s.GetK8STarget("egg-im", "business", "8000"), grpc.WithInsecure(), // grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy": "%s"}`, roundrobin.Name))) if err != nil { panic(err) } businessIntClient = pb.NewBusinessIntClient(conn) }