consul

package module
v0.0.0-...-d07355f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 17, 2024 License: Apache-2.0 Imports: 10 Imported by: 7

README

registry-consul (This is a community driven project)

中文

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, &registry.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

Serverexample/server/main.go

Clientexample/client/main.go

Compatibility

Compatible with consul from v1.11.x to v1.13.x.

consul version list

maintained by: Lemonfish / claude-zq

Documentation

Index

Constants

View Source
const (
	DefaultCheckInterval                       = "5s"
	DefaultCheckTimeout                        = "5s"
	DefaultCheckDeregisterCriticalServiceAfter = "1m"
)

Variables

View Source
var (
	ErrNilInfo            = errors.New("info is nil")
	ErrMissingServiceName = errors.New("missing service name in consul register")
	ErrMissingAddr        = errors.New("missing addr in consul register")
)

Functions

func NewConsulRegister

func NewConsulRegister(consulClient *api.Client, opts ...Option) registry.Registry

NewConsulRegister create a new registry using consul.

func NewConsulResolver

func NewConsulResolver(consulClient *api.Client) discovery.Resolver

NewConsulResolver create a service resolver using consul.

Types

type Option

type Option func(o *options)

Option is the option of Consul.

func WithCheck

func WithCheck(check *api.AgentServiceCheck) Option

WithCheck is consul registry option to set AgentServiceCheck.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL