How to Integrate Go-Micro v2 with Nacos for Service Registration and Discovery
This tutorial walks through using the Go-Micro microservice framework (v2/Nitro) together with Nacos to register services, discover them, and build a simple Go-based client‑server demo, including all required code snippets and configuration steps.
Go-Micro Overview
Go-Micro (v2, now Nitro) is a pluggable microservice framework for Go. It provides core components such as Registry, Selector, Broker, Transport, Codec, Server and Client. By default it uses Consul for service discovery, but any implementation that satisfies the Registry interface (e.g., Nacos, etcd, Zookeeper) can be swapped without changing business logic.
Core components
Registry – service discovery (Consul, etcd, Zookeeper, Nacos …).
Selector – load‑balancing (round‑robin, random, hash, blacklist).
Broker – pub/sub interface (HTTP, NATS, Kafka, RabbitMQ).
Transport – point‑to‑point transport (HTTP, NATS, RabbitMQ).
Codec – message encoding/decoding.
Server – builds a running microservice using the above plugins.
Client – RPC client that reuses Registry, Selector, Broker and Transport.
Nacos
Nacos is an open‑source dynamic service discovery and configuration platform. It offers high availability and implements the Go-Micro Registry interface.
Quick start
1. Install protoc plugin
go get github.com/golang/protobuf/protoc-gen-go2. Install Go‑Micro v2
go install github.com/micro/micro/v23. Create project structure
Create a proto directory and add greeter.proto defining a Greeter service.
syntax = "proto3";
package helloworld;
service Greeter {
rpc Hello(HelloRequest) returns (HelloResponse) {}
}
message HelloRequest {
string name = 1;
}
message HelloResponse {
string greeting = 2;
}4. Generate Go code
protoc --micro_out=. --go_out=. proto/greeter.proto5. Server implementation (server.go)
package main
import (
"context"
helloworld "go-micro-nacos-demo1/proto"
"github.com/micro/go-micro/v2"
"github.com/micro/go-micro/v2/logger"
"github.com/micro/go-micro/v2/registry"
nacos "github.com/micro/go-plugins/registry/nacos/v2"
)
type Helloworld struct{}
func (e *Helloworld) Hello(ctx context.Context, req *helloworld.HelloRequest, rsp *helloworld.HelloResponse) error {
logger.Info("Received Helloworld.Call request")
rsp.Greeting = "Hello " + req.Name
return nil
}
func main() {
addrs := []string{"console.nacos.io:80"}
reg := nacos.NewRegistry(func(o *registry.Options) { o.Addrs = addrs })
service := micro.NewService(
micro.Name("my.micro.service"),
micro.Registry(reg),
)
helloworld.RegisterGreeterHandler(service.Server(), new(Helloworld))
service.Run()
}Running the binary registers my.micro.service in the Nacos console.
6. Client implementation (client.go)
package main
import (
"context"
"fmt"
helloworld "go-micro-nacos-demo1/proto"
"github.com/micro/go-micro/v2"
"github.com/micro/go-micro/v2/registry"
nacos "github.com/micro/go-plugins/registry/nacos/v2"
)
const serverName = "my.micro.service"
func main() {
addrs := []string{"console.nacos.io:80"}
reg := nacos.NewRegistry(func(o *registry.Options) { o.Addrs = addrs })
service := micro.NewService(
micro.Name("my.micro.service.client"),
micro.Registry(reg),
)
greeter := helloworld.NewGreeterService(serverName, service.Client())
rsp, err := greeter.Hello(context.TODO(), &helloworld.HelloRequest{Name: "John"})
if err != nil {
fmt.Println(err)
}
fmt.Println(rsp.Greeting)
// List all services
fmt.Println(registry.ListServices())
// Get a specific service
svc, _ := registry.GetService(serverName)
fmt.Println(svc)
// Watch for changes
watch, _ := registry.Watch()
for {
if result, err := watch.Next(); len(result.Action) > 0 {
fmt.Println(result, err)
}
}
}The client also appears in the Nacos console; the server logs the request.
Key integration points
Registry is created with nacos.NewRegistry and passed to micro.NewService via micro.Registry.
Service name is set with micro.Name. The same name is used by the client to locate the server.
Service discovery, load‑balancing and health checks are handled automatically by the Nacos plugin.
Additional registry operations demonstrated: registry.ListServices(), registry.GetService() and registry.Watch().
References
Demo repository: https://github.com/sanxun0325/go-micro-nacos-demo
Go‑Micro (Nitro) repository: https://github.com/asim/nitro
Nacos official site: https://nacos.io/zh-cn/index.html
Nacos SDK for Go: https://github.com/nacos-group/nacos-sdk-go
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Native
We publish cloud-native tech news, curate in-depth content, host regular events and live streams, and share Alibaba product and user case studies. Join us to explore and share the cloud-native insights you need.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
