The Dubbo framework supports preloading the contents of the configuration file ‘dubbogo.yaml’ into the configuration center, and then merging it with local configurations through remote loading, thus achieving some dynamic and centralized management of configurations.
You can view the full example source code here. This article demonstrates using Zookeeper, and the usage of Nacos is similar, with specific source code examples available at the above address.
In the dubbo-go application, enable the configuration center with dubbo.WithConfigCenter()
:
ins, err := dubbo.NewInstance(
dubbo.WithConfigCenter(
config_center.WithZookeeper(),
config_center.WithDataID("dubbo-go-samples-configcenter-zookeeper-server"),
config_center.WithAddress("127.0.0.1:2181"),
config_center.WithGroup("dubbogo"),
),
)
if err != nil {
panic(err)
}
Before running the application, pre-write the following configuration into the Zookeeper cluster at the path /dubbo/config/dubbogo/dubbo-go-samples-configcenter-zookeeper-server
:
dubbo:
registries:
demoZK:
protocol: zookeeper
timeout: 3s
address: '127.0.0.1:2181'
protocols:
triple:
name: tri
port: 20000
srv, err := ins.NewServer()
if err != nil {
panic(err)
}
if err := greet.RegisterGreetServiceHandler(srv, &GreetTripleServer{}); err != nil {
panic(err)
}
if err := srv.Serve(); err != nil {
logger.Error(err)
}
You will find that the application has read the remote dubbogo.yml file and connected to the registered center address, protocol, and port configured in the file.
$ go run ./go-client/cmd/main.go
Greet response: greeting:"hello world"