@@ -2,6 +2,7 @@ package service
22
33import (
44 "context"
5+ "strconv"
56 "testing"
67 "time"
78
@@ -30,7 +31,7 @@ func TestRegisterAllService(t *testing.T) {
3031 })
3132}
3233
33- // The default is to connect to the local grpc service , if you want to connect to a remote grpc service ,
34+ // The default is to connect to the local grpc server , if you want to connect to a remote grpc server ,
3435// pass in the parameter grpcClient.
3536func getRPCClientConnForTest (grpcClient ... config.GrpcClient ) * grpc.ClientConn {
3637 err := config .Init (configs .Path ("serverNameExample.yml" ))
@@ -40,21 +41,22 @@ func getRPCClientConnForTest(grpcClient ...config.GrpcClient) *grpc.ClientConn {
4041 var grpcClientCfg config.GrpcClient
4142
4243 if len (grpcClient ) == 0 {
44+ // default config from configuration file serverNameExample.yml
4345 grpcClientCfg = config.GrpcClient {
44- Host : "127.0.0.1" ,
46+ Host : config . Get (). App . Host ,
4547 Port : config .Get ().Grpc .Port ,
46-
47- Name : "" ,
48- EnableLoadBalance : false ,
49- RegistryDiscoveryType : "" ,
50- ClientSecure : config. ClientSecure {},
51- ClientToken : config. ClientToken {},
48+ // If RegistryDiscoveryType is not empty, service discovery is used, and Host and Port values are invalid
49+ RegistryDiscoveryType : config . Get (). App . RegistryDiscoveryType , // supports consul, etcd and nacos
50+ Name : config . Get (). App . Name ,
51+ }
52+ if grpcClientCfg . RegistryDiscoveryType != "" {
53+ grpcClientCfg . EnableLoadBalance = true
5254 }
5355 } else {
56+ // custom config
5457 grpcClientCfg = grpcClient [0 ]
5558 }
5659
57- endpoint := grpcClientCfg .Host + ":" + utils .IntToStr (grpcClientCfg .Port )
5860 var cliOptions []grpccli.Option
5961
6062 // load balance
@@ -83,66 +85,58 @@ func getRPCClientConnForTest(grpcClient ...config.GrpcClient) *grpc.ClientConn {
8385 grpccli .WithEnableLog (logger .Get ()),
8486 )
8587
86- isUseDiscover := false
87- if config .Get ().App .RegistryDiscoveryType != "" {
88- var iDiscovery registry.Discovery
89- endpoint = "discovery:///" + config .Get ().App .Name // Connecting to grpc services by service name
90-
91- // Use consul service discovery, note that the host field in the configuration file serverNameExample.yml
92- // needs to be filled with the local ip, not 127.0.0.1, to do the health check
93- if config .Get ().App .RegistryDiscoveryType == "consul" {
94- cli , err := consulcli .Init (config .Get ().Consul .Addr , consulcli .WithWaitTime (time .Second * 2 ))
95- if err != nil {
96- panic (err )
97- }
98- iDiscovery = consul .New (cli )
99- isUseDiscover = true
100- }
88+ var (
89+ endpoint string
90+ isUseDiscover bool
91+ iDiscovery registry.Discovery
92+ )
10193
102- // Use etcd service discovery, use the command etcdctl get / --prefix to see if the service is registered before testing,
103- // note: the IDE using a proxy may cause the connection to the etcd service to fail
104- if config .Get ().App .RegistryDiscoveryType == "etcd" {
105- cli , err := etcdcli .Init (config .Get ().Etcd .Addrs , etcdcli .WithDialTimeout (time .Second * 2 ))
106- if err != nil {
107- panic (err )
108- }
109- iDiscovery = etcd .New (cli )
110- isUseDiscover = true
94+ switch grpcClientCfg .RegistryDiscoveryType {
95+ case "consul" :
96+ endpoint = "discovery:///" + grpcClientCfg .Name // Connecting to grpc services by service name
97+ cli , err := consulcli .Init (config .Get ().Consul .Addr , consulcli .WithWaitTime (time .Second * 2 ))
98+ if err != nil {
99+ panic (err )
111100 }
112-
113- // Use nacos service discovery
114- if config .Get ().App .RegistryDiscoveryType == "nacos" {
115- // example: endpoint = "discovery:///serverName.scheme"
116- endpoint = "discovery:///" + config .Get ().App .Name + ".grpc"
117- cli , err := nacoscli .NewNamingClient (
118- config .Get ().NacosRd .IPAddr ,
119- config .Get ().NacosRd .Port ,
120- config .Get ().NacosRd .NamespaceID )
121- if err != nil {
122- panic (err )
123- }
124- iDiscovery = nacos .New (cli )
125- isUseDiscover = true
101+ iDiscovery = consul .New (cli )
102+ isUseDiscover = true
103+
104+ case "etcd" :
105+ endpoint = "discovery:///" + grpcClientCfg .Name // Connecting to grpc services by service name
106+ cli , err := etcdcli .Init (config .Get ().Etcd .Addrs , etcdcli .WithDialTimeout (time .Second * 2 ))
107+ if err != nil {
108+ panic (err )
126109 }
110+ iDiscovery = etcd .New (cli )
111+ isUseDiscover = true
112+ case "nacos" :
113+ // example: endpoint = "discovery:///serverName.scheme"
114+ endpoint = "discovery:///" + grpcClientCfg .Name + ".grpc"
115+ cli , err := nacoscli .NewNamingClient (
116+ config .Get ().NacosRd .IPAddr ,
117+ config .Get ().NacosRd .Port ,
118+ config .Get ().NacosRd .NamespaceID )
119+ if err != nil {
120+ panic (err )
121+ }
122+ iDiscovery = nacos .New (cli )
123+ isUseDiscover = true
127124
128- cliOptions = append (cliOptions , grpccli .WithDiscovery (iDiscovery ))
125+ default :
126+ endpoint = grpcClientCfg .Host + ":" + strconv .Itoa (grpcClientCfg .Port )
127+ iDiscovery = nil
128+ isUseDiscover = false
129129 }
130130
131- if config .Get ().App .EnableTrace {
132- cliOptions = append (cliOptions , grpccli .WithEnableTrace ())
133- }
134- if config .Get ().App .EnableCircuitBreaker {
135- cliOptions = append (cliOptions , grpccli .WithEnableCircuitBreaker ())
136- }
137- if config .Get ().App .EnableMetrics {
138- cliOptions = append (cliOptions , grpccli .WithEnableMetrics ())
131+ if iDiscovery != nil {
132+ cliOptions = append (cliOptions , grpccli .WithDiscovery (iDiscovery ))
139133 }
140134
141135 msg := "dialing grpc server"
142136 if isUseDiscover {
143- msg += " with discovery from " + config . Get (). App .RegistryDiscoveryType
137+ msg += " with discovery from " + grpcClientCfg .RegistryDiscoveryType
144138 }
145- logger .Info (msg , logger .String ("name" , config . Get (). App .Name ), logger .String ("endpoint" , endpoint ))
139+ logger .Info (msg , logger .String ("name" , grpcClientCfg .Name ), logger .String ("endpoint" , endpoint ))
146140
147141 conn , err := grpccli .Dial (context .Background (), endpoint , cliOptions ... )
148142 if err != nil {
0 commit comments