consul 中的服务通信

Services communication in consul

我正在开发几个服务,并使用 consul 作为服务注册中心。我可以向领事注册我所有的服务。

接下来要做的是,我需要能够从服务 A 到服务 B 进行通信。

没有服务注册表,通常我所做的只是将客户端 HTTP 请求从服务 A 发送到服务 B。

但是现在我已经有了服务发现,我是否应该通过 consul 获取服务 B 主机地址,然后将客户端 HTTP 请求发送到服务 B 主机地址?或者consul是否也提供了一个API网关,所以我只需要将我的客户端HTTP请求从服务A派发给consul,然后consul会自动转发到目的地?

另外,如果有关于我的案例的相关文档,我会很乐意看一下? (找不到相关文档,可能是我的google搜索关键字不对)

我最终从领事处获取了服务信息列表,然后对其进行名称匹配,然后获取服务地址。

我使用此端点获取服务列表及其数据:

http://localhost:8500/v1/agent/services

我猜这是 client-side 的发现。

Consul 支持两种服务发现方法,DNS 和 HTTP。

应用程序可以针对其本地 Consul 代理执行 DNS 查找,该代理在端口 8600 上公开 DNS 服务器(您还可以配置 DNS forwarding). For example, an application can issue an A record query for web.service.consul and Consul will return a list of healthy instance endpoints for the web service. SRV lookups are also supported in order to retrieve the IP and port for a given service. The DNS interface also supports querying endpoints by service tag and data center. Details can be found at Consul.io: DNS - Service Lookups

HTTP-based 可以通过针对本地代理查询 /v1/health/service/:name 端点来执行服务发现。以下将 return 服务的健康和不健康端点的完整列表 nginx

$ curl http://127.0.0.1:8500/v1/health/service/nginx

您可以使用 passing 查询参数将输出限制为仅运行状况良好的服务。

$ curl "http://127.0.0.1:8500/v1/health/service/nginx?passing"

我建议查看指南 Register a Service with Consul Service Discovery 以了解有关从目录注册和查询服务的更多信息。

最后,API 网关,如 Traefik 和 Solo 的 Gloo 支持使用 Consul 进行服务发现(参见 Traefik 的 Consul Catalog Provider and Gloo's Consul Services)。您可以将您的服务配置为将请求路由到这些网关,并允许网关转发到后端目的地。