Go 使用 Consul

注册服务

package main

import "github.com/hashicorp/consul/api"

func Register(address string, port int, name string, tags []string, id string) error {
	// 获取consul连接对象配置信息
	cfg := api.DefaultConfig()
	cfg.Address = "192.168.1.6:8500"

	// 获取consul的客户端对象
	client, err := api.NewClient(cfg)
	if err != nil {
		panic(err)
	}

	// 生成注册对象
	registration := new(api.AgentServiceRegistration)
	registration.Port = port
	registration.Name = name
	registration.Address = address
	registration.Tags = tags

	// 真正实现注册逻辑
	err = client.Agent().ServiceRegister(registration)
	if err != nil {
		return err
	}
	return nil
}


func main() {
	err := Register("192.168.1.4", 8021, "user-web", []string{"tag1", "tag2"}, "user-web")
	if err != nil {
		panic(err)
	}
}

注销服务

package main

import "github.com/hashicorp/consul/api"

func DeRegister(id string) error {
	// 获取consul连接对象配置信息
	cfg := api.DefaultConfig()
	cfg.Address = "192.168.1.6:8500"

	// 获取consul的客户端对象
	client, err := api.NewClient(cfg)
	if err != nil {
		panic(err)
	}
	
    // 真正实现删除服务
	err = client.Agent().ServiceDeregister(id)
	if err != nil {
		return err
	}
	return nil
}

func main() {
    // user-web是服务的id
	err := DeRegister("user-web")
	if err != nil {
		panic(err)
	}
}

注册服务带健康检查

package main

import "github.com/hashicorp/consul/api"

func Register(address string, port int, name string, tags []string, id string) error {
	// 获取consul连接对象配置信息
	cfg := api.DefaultConfig()
	cfg.Address = "192.168.1.6:8500"

	// 获取consul的客户端对象
	client, err := api.NewClient(cfg)
	if err != nil {
		panic(err)
	}
	
	// 服务健康检查配置
	check := &api.AgentServiceCheck{
		HTTP: "http://192.168.1.6:8500/v1/agent/checks",
		Timeout: "5s",
		Interval: "5s",
		DeregisterCriticalServiceAfter: "10s",
	}
	
	// 生成注册对象
	registration := new(api.AgentServiceRegistration)
	registration.Port = port
	registration.Name = name
	registration.Address = address
	registration.Tags = tags
	registration.Check = check
	
	// 真正实现注册逻辑
	err = client.Agent().ServiceRegister(registration)
	if err != nil {
		return err
	}
	return nil
}


func main() {
	err := Register("192.168.1.4", 8021, "user-web", []string{"tag1", "tag2"}, "user-web")
	if err != nil {
		panic(err)
	}
}

查询服务

package main

import (
	"fmt"
	"github.com/hashicorp/consul/api"
)

func AllService() {
	// 获取consul连接对象配置信息
	cfg := api.DefaultConfig()
	cfg.Address = "192.168.1.6:8500"

	// 获取consul的客户端对象
	client, err := api.NewClient(cfg)
	if err != nil {
		panic(err)
	}

	// 真正实现查询所有服务操作
	services, err := client.Agent().Services()
	if err != nil {
		panic(err)
	}else {
		for key, value := range services{
			fmt.Println(key, value)
		}
	}
}


func main() {
    AllService()
}

过滤服务

package main

import (
	"fmt"
	"github.com/hashicorp/consul/api"
)

func filterService(service_name) {
	// 获取consul连接对象配置信息
	cfg := api.DefaultConfig()
	cfg.Address = "192.168.1.6:8500"

	// 获取consul的客户端对象
	client, err := api.NewClient(cfg)
	if err != nil {
		panic(err)
	}

	// 真正实现过滤服务操作
    services, err := client.Agent().ServicesWithFilter(fmt.Sprintf(`Service == "%s"`, service_name)) // 注意:过滤条件非常严格
	if err != nil {
		panic(err)
	}else {
		for key, value := range services {
			fmt.Println(key, value)
		}	
	}
}

func main() {
    filterService()	
}
Licensed under CC BY-NC-SA 4.0
使用 Hugo 构建
主题 StackJimmy 设计