simple_client.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package main
  2. import (
  3. "../clientImpl"
  4. "crypto/tls"
  5. "git.wanpinghui.com/WPH/go_common/wph/logger"
  6. "google.golang.org/grpc"
  7. "google.golang.org/grpc/credentials"
  8. "google.golang.org/grpc/grpclog"
  9. "log"
  10. "os"
  11. "time"
  12. )
  13. // 简单请求客户端,用于测试k8s环境本身的请求分发能力
  14. func main() {
  15. //以k8s service名来建立连接
  16. //serviceConn, err := grpc.Dial("grpc-example:80", grpc.WithInsecure())
  17. //if err != nil {
  18. // log.Fatalf("did not connect: %v", err)
  19. //}
  20. //defer func() {
  21. // _ = serviceConn.Close()
  22. //}()
  23. //
  24. //log.Println("==== Calling grpc-example by k8s service ====")
  25. //clientImpl.MakeCalls(serviceConn, 10)
  26. // 以k8s ingress来建立连接
  27. // 用qcloud ingress访问,不论是在service中网络type设为负载均衡所添加的,或手工添加指向NodePort的4层负载均衡,对grpc均不能真正起到负载均衡的效果
  28. // qcloud类型7层http/https ingress,直接无法访问通grpc服务
  29. //ingressConn, err := grpc.Dial("10.7.255.37:80", grpc.WithInsecure())
  30. // 用nginx ingress访问,通过配置DNS服务器做域名解析(推荐指向nginx controller内网service ip),在客户端的yaml文件中配置本地hosts的方式无效
  31. // 详细访问路径:解析域名 -> nginx controller的service的ip -> nginx controller pod(TKE中额外手工部署ingress-nginx后添加的)->
  32. // 转发到具体nginx ingres的yaml配置文件中指向的后端service(创建或更新nginx ingress时,其中的backend配置都会自动同步到nginx controller pod里的nginx.conf中,可进入该pod查看)
  33. // 读文件证书
  34. //certFile := `./ex_tls.crt` // 服务器上的证书路径,可以在构件docker镜像时复制,但k8s环境更建议从Secret中挂载
  35. //isExisted, _ := pathExists(certFile)
  36. //if !isExisted {
  37. // certFile = `/Users/marion/Documents/project/test/gotest/grpc-demo/tls/ex_tls.crt` // 本地测试时的证书文件绝对路径
  38. //}
  39. //crt, _ := credentials.NewClientTLSFromFile(certFile, "example.wanpinghui.com")
  40. // 跳过证书验证,但直接grpc.WithInsecure()不通
  41. var tlsConf tls.Config
  42. tlsConf.InsecureSkipVerify = true
  43. crt := credentials.NewTLS(&tlsConf)
  44. ingressConn, err := grpc.Dial("example.wanpinghui.com:443", grpc.WithTransportCredentials(crt)) // 推荐把域名解析到nginx controller service内网ip
  45. //ingressConn, err := grpc.Dial("example.wanpinghui.com:443", grpc.WithInsecure())
  46. if err != nil {
  47. log.Fatalf("did not connect: %v", err)
  48. }
  49. defer func() {
  50. _ = ingressConn.Close()
  51. }()
  52. // 打印更详细的日志
  53. l := logger.New()
  54. grpclog.SetLoggerV2(l)
  55. log.Println("==== Calling grpc-example by k8s ingress ====")
  56. clientImpl.MakeCalls(ingressConn, 10)
  57. // 停顿10秒再请求,此时可手工增减pod,以测试pod掉线或新节点上线的情况
  58. println(`sleeping ------------------------------------------------------->`)
  59. time.Sleep(10 * time.Second)
  60. clientImpl.MakeCalls(ingressConn, 10)
  61. }
  62. func pathExists(path string) (bool, error) {
  63. _, err := os.Stat(path)
  64. if err == nil {
  65. return true, nil
  66. }
  67. if os.IsNotExist(err) {
  68. return false, nil
  69. }
  70. return false, err
  71. }