NewServerNameExampleRPCConn instantiate rpc client connection
()
| 20 | |
| 21 | // NewServerNameExampleRPCConn instantiate rpc client connection |
| 22 | func NewServerNameExampleRPCConn() { |
| 23 | cfg := config.Get() |
| 24 | |
| 25 | serverName := "serverNameExample" |
| 26 | var grpcClientCfg config.GrpcClient |
| 27 | for _, cli := range cfg.GrpcClient { |
| 28 | if strings.EqualFold(cli.Name, serverName) { |
| 29 | grpcClientCfg = cli |
| 30 | break |
| 31 | } |
| 32 | } |
| 33 | if grpcClientCfg.Name == "" { |
| 34 | panic(fmt.Sprintf("not found grpc service name '%v' in configuration file(yaml), "+ |
| 35 | "please add gprc service configuration in the configuration file(yaml) under the field grpcClient.", serverName)) |
| 36 | } |
| 37 | |
| 38 | var cliOptions = []grpccli.Option{ |
| 39 | grpccli.WithEnableRequestID(), |
| 40 | grpccli.WithEnableLog(logger.Get()), |
| 41 | } |
| 42 | |
| 43 | // if service discovery is not used, connect directly to the rpc service using the ip and port |
| 44 | endpoint := fmt.Sprintf("%s:%d", grpcClientCfg.Host, grpcClientCfg.Port) |
| 45 | isUseDiscover := false |
| 46 | |
| 47 | // using service discovery |
| 48 | //discoverOption, discoveryEndpoint := discoverService(cfg, grpcClientCfg) |
| 49 | //if discoverOption != nil { |
| 50 | // isUseDiscover = true |
| 51 | // endpoint = discoveryEndpoint |
| 52 | // cliOptions = append(cliOptions, discoverOption) |
| 53 | // cliOptions = append(cliOptions, grpccli.WithEnableLoadBalance()) // load balance |
| 54 | //} |
| 55 | |
| 56 | // secure |
| 57 | cliOptions = append(cliOptions, grpccli.WithSecure( |
| 58 | grpcClientCfg.ClientSecure.Type, |
| 59 | grpcClientCfg.ClientSecure.ServerName, |
| 60 | grpcClientCfg.ClientSecure.CaFile, |
| 61 | grpcClientCfg.ClientSecure.CertFile, |
| 62 | grpcClientCfg.ClientSecure.KeyFile, |
| 63 | )) |
| 64 | |
| 65 | // token |
| 66 | cliOptions = append(cliOptions, grpccli.WithToken( |
| 67 | grpcClientCfg.ClientToken.Enable, |
| 68 | grpcClientCfg.ClientToken.AppID, |
| 69 | grpcClientCfg.ClientToken.AppKey, |
| 70 | )) |
| 71 | |
| 72 | if cfg.App.EnableTrace { |
| 73 | cliOptions = append(cliOptions, grpccli.WithEnableTrace()) |
| 74 | } |
| 75 | if cfg.App.EnableCircuitBreaker { |
| 76 | cliOptions = append(cliOptions, grpccli.WithEnableCircuitBreaker()) |
| 77 | } |
| 78 | if cfg.App.EnableMetrics { |
| 79 | cliOptions = append(cliOptions, grpccli.WithEnableMetrics()) |