golang-im聊天
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

69 lines
1.6 KiB

  1. package rpc
  2. import (
  3. "context"
  4. "fmt"
  5. "gim/pkg/grpclib/picker"
  6. "gim/pkg/grpclib/resolver/k8s"
  7. "gim/pkg/pb"
  8. "google.golang.org/grpc/balancer/roundrobin"
  9. "google.golang.org/grpc"
  10. )
  11. var (
  12. logicIntClient pb.LogicIntClient
  13. connectIntClient pb.ConnectIntClient
  14. businessIntClient pb.BusinessIntClient
  15. )
  16. func GetLogicIntClient() pb.LogicIntClient {
  17. if logicIntClient == nil {
  18. initLogicIntClient()
  19. }
  20. return logicIntClient
  21. }
  22. func GetConnectIntClient() pb.ConnectIntClient {
  23. if connectIntClient == nil {
  24. initConnectIntClient()
  25. }
  26. return connectIntClient
  27. }
  28. func GetBusinessIntClient() pb.BusinessIntClient {
  29. if businessIntClient == nil {
  30. initBusinessIntClient()
  31. }
  32. return businessIntClient
  33. }
  34. func initLogicIntClient() {
  35. conn, err := grpc.DialContext(context.TODO(), k8s.GetK8STarget("gim", "logic", "8000"), grpc.WithInsecure(),
  36. grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy": "%s"}`, roundrobin.Name)))
  37. if err != nil {
  38. panic(err)
  39. }
  40. logicIntClient = pb.NewLogicIntClient(conn)
  41. }
  42. func initConnectIntClient() {
  43. conn, err := grpc.DialContext(context.TODO(), k8s.GetK8STarget("gim", "connect", "8000"), grpc.WithInsecure(),
  44. grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy": "%s"}`, picker.AddrPickerName)))
  45. if err != nil {
  46. panic(err)
  47. }
  48. connectIntClient = pb.NewConnectIntClient(conn)
  49. }
  50. func initBusinessIntClient() {
  51. conn, err := grpc.DialContext(context.TODO(), k8s.GetK8STarget("gim", "business", "8000"), grpc.WithInsecure(),
  52. grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy": "%s"}`, roundrobin.Name)))
  53. if err != nil {
  54. panic(err)
  55. }
  56. businessIntClient = pb.NewBusinessIntClient(conn)
  57. }