中文
Support Hertz to use Consul for service registration and discovery
Docs
Server
Basic Usage
package main
import (
"context"
"fmt"
"log"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/app/server/registry"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/cloudwego/hertz/pkg/protocol/consts"
consulapi "github.com/hashicorp/consul/api"
"github.com/hertz-contrib/registry/consul"
)
func main() {
// build a consul client
config := consulapi.DefaultConfig()
config.Address = "127.0.0.1:8500"
consulClient, err := consulapi.NewClient(config)
if err != nil {
log.Fatal(err)
return
}
// build a consul register with the consul client
r := consul.NewConsulRegister(consulClient)
// run Hertz with the consul register
localIP, err := consul.GetLocalIPv4Address()
if err != nil {
log.Fatal(err)
}
addr := fmt.Sprintf("%s:8888",localIP)
h := server.Default(
server.WithHostPorts(addr),
server.WithRegistry(r, ®istry.Info{
ServiceName: "hertz.test.demo",
Addr: utils.NewNetAddr("tcp", addr),
Weight: 10,
}),
)
h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
ctx.JSON(consts.StatusOK, utils.H{"ping": "pong1"})
})
h.Spin()
}
Customize
Consul extension provides custom configuration of service inspection and Tags
Meta
field.
Registry has a default config for service check as below
check.Timeout = "5s"
check.Interval = "5s"
check.DeregisterCriticalServiceAfter = "1m"
You can use WithCheck
to modify the configuration of the service check. At the same time, you can also use WithAdditionInfo
to modify the Meta
Tags
field of the service.
package main
import (
"log"
consulapi "github.com/hashicorp/consul/api"
"github.com/hertz-contrib/registry/consul"
)
func main() {
// build a consul client
config := consulapi.DefaultConfig()
config.Address = "127.0.0.1:8500"
consulClient, err := consulapi.NewClient(config)
if err != nil {
log.Fatal(err)
return
}
// build a consul register with the check option
check := new(consulapi.AgentServiceCheck)
check.Timeout = "10s"
check.Interval = "10s"
check.DeregisterCriticalServiceAfter = "1m"
// custom addition info
additionInfo := &consul.AdditionInfo{
Tags: []string{"tag1", "tag2"},
Meta: map[string]string{
"meta1": "val1",
"meta2": "val2",
},
}
r := consul.NewConsulRegister(consulClient,
consul.WithCheck(check), consul.WithAdditionInfo(additionInfo),
)
}
Client
package main
import (
"log"
"github.com/cloudwego/hertz/pkg/app/client"
"github.com/cloudwego/hertz/pkg/app/middlewares/client/sd"
consulapi "github.com/hashicorp/consul/api"
"github.com/hertz-contrib/registry/consul"
)
func main() {
// build a consul client
consulConfig := consulapi.DefaultConfig()
consulConfig.Address = "127.0.0.1:8500"
consulClient, err := consulapi.NewClient(consulConfig)
if err != nil {
log.Fatal(err)
return
}
// build a consul resolver with the consul client
r := consul.NewConsulResolver(consulClient)
// build a hertz client with the consul resolver
cli, err := client.NewClient()
if err != nil {
panic(err)
}
cli.Use(sd.Discovery(r))
}
Example
Server:example/server/main.go
Client:example/client/main.go
Compatibility
Compatible with consul from v1.11.x to v1.13.x.
consul version list
maintained by: Lemonfish / claude-zq