選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

71 行
1.8 KiB

  1. package rpc
  2. import (
  3. "context"
  4. "egg-im/pkg/grpclib/picker"
  5. "egg-im/pkg/grpclib/resolver/k8s"
  6. "egg-im/pkg/pb"
  7. "fmt"
  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("egg-im", "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("egg-im", "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("egg-im", "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. }