11

基于consul实现服务注册与发现

 1 year ago
source link: https://www.liwenzhou.com/posts/Go/consul/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Consul是一个分布式、高可用性和数据中心感知的解决方案,用于跨动态、分布式基础设施连接和配置应用程序。

Consul介绍

Consul是一个分布式、高可用性和数据中心感知的解决方案,用于跨动态、分布式基础设施连接和配置应用程序。

Consul提供了几个关键特性:

  • 多数据中心——Consul被构建为数据中心感知的,可以支持任意数量的区域,而不需要复杂的配置。
  • 服务网格/服务细分——使用自动 TLS 加密和基于身份的授权实现安全的服务对服务通信。应用程序可以在服务网格配置中使用 sidecar 代理为入站和出站连接建立 TLS 连接,而完全不知道连接方。
  • 服务发现——Consul使服务注册自己和通过 DNS 或 HTTP 接口发现其他服务变得简单。也可以注册外部服务,如 SaaS 提供者。
  • 健康检查——健康检查使得领事能够就集群中的任何问题快速向操作员发出警报。与服务发现的集成可以防止将流量路由到不健康的主机,并启用服务级断路器。
  • Key/Value存储——一个灵活的键/值存储允许存储动态配置、特性标记、协调、领导选举等。简单的 HTTP API 使得在任何地方都可以轻松使用。

Consul安装

推荐按官方docker-compose-datacenter教程使用docker-compose一键搭建consul环境。

推荐使用 Docker Compose 快速搭建学习使用的 Consul 环境。

执行下面的命令,从官方 github 仓库中获取的配置和资源。

git clone https://github.com/hashicorp/learn-consul-docker.git

这个仓库中有很多定义好的各式 consul 环境 docker-compose 配置,我们这里使用的是服务发现的环境,进入到 datacenter-deploy-service-discovery 目录。

cd datacenter-deploy-service-discovery

执行以下命令,启动容器。

❯ docker-compose up -d
Creating network "datacenter-deploy-service-discovery_consul" with driver "bridge"
Creating consul-server ... done
Creating consul-client ... done

这样一个用于服务发现的Consul环境就搭建好了。

使用浏览器打开 http:127.0.0.1:8500 就可以看到Consul的管理界面了。

consul ui

服务注册与服务发现

详细用法和参数请参照 Agent HTTP API文档

Method Path Produces
GET /agent/services application/json
Method Path Produces
PUT /agent/service/register application/json
Method Path Produces
PUT /agent/service/deregister/:service_id application/json

注意: 实际Path路径前需要加v1版本前缀。

Go SDK

在代码中导入官方 api 包

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

连接consul

type consul struct {
	client *api.Client
}

// NewConsul 连接至consul服务返回一个consul对象
func NewConsul(addr string) (*consul, error) {
	cfg := api.DefaultConfig()
	cfg.Address = addr
	c, err := api.NewClient(cfg)
	if err != nil {
		return nil, err
	}
	return &consul{c}, nil
}

将我们 grpc 服务注册到 consul。

// RegisterService 将服务注册到consul
func (c *consul) RegisterService() error {
	check := &api.AgentServiceCheck{
		GRPC:                           "192.168.1.11:8972", // 这里一定是外部可以访问的地址
		Timeout:                        "10s",
		Interval:                       "10s",
		DeregisterCriticalServiceAfter: "20s",
	}
	srv := &api.AgentServiceRegistration{
		ID:      "hello-127.0.0.1-8500",    // 服务唯一ID
		Name:    "hello",                   // 服务名称
		Tags:    []string{"q1mi", "hello"}, // 为服务打标签
		Address: "127.0.0.1",
		Port:    8972,
		Check:   check,
	}
	return c.client.Agent().ServiceRegister(srv)
}

通常我们会选择使用filter查询我们想要的服务,具体支持的filter策略可查看文档

// ListService 服务发现
func (c *consul) ListService() (map[string]*api.AgentService, error) {
	// c.client.Agent().Service("hello-127.0.0.1-8972")
	return c.client.Agent().ServicesWithFilter("Service==`hello`")
}

扫码关注微信公众号

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK