0

我正在使用一组使用 Java + Spring Boot 开发的微服务,并希望在 Docker 组合中使用 Consul 和 KrakenD。

我的 docker-compose 文件如下所示:

version: '3.8'

services:

  # Consul
  # Service discovery similar to Eureka but could keep configuration
  consul:
    image: consul
    container_name: consul
    command: agent -server -ui -node=server1 -bootstrap-expect=1 -client=0.0.0.0
    environment:
      - CONSUL_BIND_INTERFACE=eth0
    ports:
      - "8500:8500"
      - "8600:8600/udp"
      - "8600:8600/tcp"
    hostname: "consul"
    networks:
      - micros

  krakend:
    image: devopsfaith/krakend
    container_name: krakend
    volumes:
      - ./krakend:/etc/krakend
    ports:
      - "1234:1234"
      - "8080:8080"
      - "8090:8090"
    networks:
      - micros

  google-search-service:
    environment:
      - JAVA_OPTIONS=-Dlogging.level.net.leyba.googlesearch=INFO
    image: google-search-service
    container_name: google-search-service
    #ports:
    #  - "7000-7200"
    networks:
      - micros
    depends_on:
      consul:
        condition: service_started
#      krakend:
#        condition: service_started

networks:
  micros:

KrakenD 配置文件如下所示:

{
  "version": 2,
  "extra_config": {
    "github_com/devopsfaith/krakend-gologging": {
      "level": "ERROR",
      "prefix": "[KRAKEND]",
      "syslog": false,
      "stdout": true,
      "format": "default"
    }
  },
  "timeout": "3000ms",
  "cache_ttl": "300s",
  "output_encoding": "json",
  "name": "googlesearch",
  "port": 8080,
  "endpoints": [
    {
      "endpoint": "/v1/googlesearch/{searchParam}",
      "method": "GET",
      "output_encoding": "json",
      "extra_config": {},
      "backend": [
        {
          "sd": "dns",
          "url_pattern": "/external-dbs/google/{searchParam}",
          "host": [
            "google-search-service.service.consul"
          ],
          "disable_host_sanitize": true
        }
      ]
    },
  ]
}

在没有 docker 的情况下,主机google-search-service.service.consul是通过我的 macOS 中的解析器解析的,但是似乎无法在 docker-compose 网络中解析,因此 Krakend 无法将 API 调用重定向到微服务因为找不到领事。

我想知道是否有办法在 docker-compose 中配置解析器或允许 KrakendD 以任何其他方式找到 Consul。

提前谢谢

4

2 回答 2

0

您可以将 Consul 配置为在端口 53 而不是 8600 上侦听 DNS,然后将其他容器配置为使用 Consul 进行 DNS 解析。

---
version: "3.8"
networks:
  micros:
    driver: bridge
    ipam:
      config:
        - subnet: 10.70.90.0/24
services:
  consul:
    image: consul:1.11.1
    container_name: consul
    environment:
      CONSUL_BIND_INTERFACE: eth0
      CONSUL_LOCAL_CONFIG: |
        {
          "client_addr": "0.0.0.0",
          "node": "server1",
          "ports": {
            "dns": 53
          },
          "recursors": [
            "8.8.8.8",
            "8.8.4.4"
          ],
          "server": true,
          "bootstrap_expect": 1,
          "ui_config": {
            "enabled": true
          }
        }
    hostname: consul
    networks:
      micros:
        ipv4_address: 10.70.90.5
    ports:
      - 8500:8500
      - 8600:53/tcp
      - 8600:53/udp

  krakend:
    image: devopsfaith/krakend
    container_name: krakend
    volumes:
      - ./krakend:/etc/krakend
    ports:
      - "1234:1234"
      - "8080:8080"
      - "8090:8090"
    dns: 10.70.90.5
    networks:
      - micros

  google-search-service:
    environment:
      - JAVA_OPTIONS=-Dlogging.level.net.leyba.googlesearch=INFO
    image: google-search-service
    container_name: google-search-service
    #ports:
    #  - "7000-7200"
    dns: 10.70.90.5
    networks:
      - micros
    depends_on:
      consul:
        condition: service_started
#      krakend:
#        condition: service_started
于 2022-01-01T19:03:01.823 回答
0

docker compose 中的 extra-hosts 可以解决您的问题,您可以让 docker 将某些主机名解析为所需的 IP 地址。

检查此线程以获取更多信息:使用 --add-host 或 extra_hosts 与 docker-compose

于 2021-11-09T09:10:31.900 回答