golang-im聊天
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

71 řádky
1.8 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(), "127.0.0.1:8000", grpc.WithInsecure(),
  52. // grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy": "%s"}`, roundrobin.Name)))
  53. conn, err := grpc.DialContext(context.TODO(), k8s.GetK8STarget("gim", "business", "8000"), grpc.WithInsecure(),
  54. grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy": "%s"}`, roundrobin.Name)))
  55. if err != nil {
  56. panic(err)
  57. }
  58. businessIntClient = pb.NewBusinessIntClient(conn)
  59. }