consul

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2023 License: Apache-2.0 Imports: 22 Imported by: 0

README

Quick Start

Prerequisites:

Download the module:

go get -u github.com/suyuan32/simple-admin-tools/plugins/registry/consul

For example:

修改RPC服务的代码

  • etc/*.yaml
Consul:
  Host: 127.0.0.1:8500 # consul endpoint
  Token: 'f0512db6-76d6-f25e-f344-a98cc3484d42' # consul ACL token (optional)
  Key: add.rpc # service name registered to Consul
  Meta:
    Protocol: grpc
  Tag:
    - tag
    - rpc
  • internal/config/config.go
type Config struct {
	zrpc.RpcServerConf
	Consul consul.Conf
}
  • main.go
import _ "github.com/suyuan32/simple-admin-tools/plugins/registry/consul"

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)

	server := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
	
	})
 	// register service to consul
	_ = consul.RegisterService(c.ListenOn, c.Consul)

	server.Start()
}

修改API服务里的代码

  • main.go
import _ "github.com/suyuan32/simple-admin-tools/plugins/registry/consul"
  • etc/*.yaml
# consul://[user:passwd]@host/service?param=value'
# format like this
Add:
  Target: consul://127.0.0.1:8500/add.rpc?wait=14s
Check:
  Target: consul://127.0.0.1:8500/check.rpc?wait=14s
  
# ACL token support
Add:
  Target: consul://127.0.0.1:8500/add.rpc?wait=14s&token=f0512db6-76d6-f25e-f344-a98cc3484d42
Check:
  Target: consul://127.0.0.1:8500/check.rpc?wait=14s&token=f0512db6-76d6-f25e-f344-a98cc3484d42

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadJSONConf

func LoadJSONConf(client *api.Client, key string, v any)

LoadJSONConf load config from consul kv

func LoadYAMLConf

func LoadYAMLConf(client *api.Client, key string, v any)

LoadYAMLConf load config from consul kv

func RegisterService

func RegisterService(c Conf) error

RegisterService register service to consul

Types

type Conf

type Conf struct {
	Host     string
	ListenOn string
	Key      string
	Token    string            `json:",optional"`
	Tag      []string          `json:",optional"`
	Meta     map[string]string `json:",optional"`
	TTL      int               `json:"ttl,optional"`
}

Conf is the config item with the given key on etcd.

func (Conf) NewClient

func (c Conf) NewClient() (*api.Client, error)

NewClient create new client

func (Conf) Validate

func (c Conf) Validate() error

Validate validates c.

Jump to

Keyboard shortcuts

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