Service Registration and Discovery
Service Registration and Discovery Code Example
- Click to view Service Registration Usage Example.
- Click to view Service Discovery Usage Example.
Consul, Etcd, Nacos Client Code Examples
- Consul client, click to view Usage Example.
- Etcd client, click to view Usage Example.
- Nacos client, click to view Usage Example.
Example of Using Service Registration and Discovery in Services
Services created by sponge support Consul
, Etcd
, Nacos
as service registration and discovery centers. Below, using Consul
as an example, we will introduce how to enable the service registration and discovery functions, helping developers quickly complete configuration and implementation. The process is the same for Etcd
or Nacos
.
Enabling Service Registration
The service registration function is used to register services with service registration centers (such as Consul, Etcd, Nacos, etc.), making it easy for other services to discover and call them. The following steps demonstrate how to enable service registration on Consul:
Modify Configuration File
Open the configuration file
configs/xxx.yml
.In the
app
configuration block, set the value ofhost
to the local IP address or domain name. If the service and Consul are not on the same machine, you cannot use 127.0.0.1.In the
app
configuration block, set the value ofregistryDiscoveryType
toconsul
, and remove the comment symbol (#
) before this field.Find the
consul
configuration block at the end of the configuration file, uncomment it, and fill in the Consul address, for example:consul: address: "192.168.3.37:8500"
Modify Service Initialization Code
- Open the file
cmd/xxx/initial/createService.go
and adjust the code according to the following steps: - Comment out the code block under
// case 1, create a grpc/http service without registry
. - Uncomment the code block under
// case 2, create a grpc service and register it with consul or etcd or nacos
. - Uncomment the
// register service with consul or etcd or nacos, select one of them to use
section, and delete the registration code for Etcd and Nacos that you do not need, keeping only the Consul code.
- Open the file
Enabling Service Discovery
The service discovery function is used to dynamically obtain the addresses of target services from the service registration center, enabling load balancing and high availability. The following steps demonstrate how to enable service discovery on Consul:
Modify Configuration File
Open the configuration file
configs/xxx.yml
.In the
grpcClient
configuration block, set the value ofregistryDiscoveryType
toconsul
, and remove the comment symbol (#
) before this field.Find the
consul
configuration block at the end of the configuration file, uncomment it, and fill in the Consul address, for example:consul: address: "192.168.3.37:8500"
Modify Client Code
- Open the file
internal/rpcclient/xxx.go
. If the file does not exist, generate it through the [Public] → [Generate gRPC Service Connection Code] function on the management page. - Adjust the code according to the following steps:
- Uncomment the code block under
// using service discovery
. - Uncomment the code block under
// discovery service with consul or etcd or nacos, select one of them to use
, and delete the irrelevant Etcd and Nacos related code, keeping only the Consul code.
- Open the file