consul

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

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

Go to latest
Published: Oct 11, 2023 License: MIT Imports: 20 Imported by: 0

README

Quick Start

Prerequisites:

Download the module:

go get -u github.com/zeromicro/zero-contrib/zrpc/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/zeromicro/zero-contrib/zrpc/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/zeromicro/zero-contrib/zrpc/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 RegisterService

func RegisterService(listenOn string, c Conf) error

RegisterService register service to consul

Types

type Conf

type Conf struct {
	Host  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) 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