1

我在 Express 中实现了一些简单的 REST api 服务。这些服务以 swarm 模式在 docker 容器中运行。此外,我正在尝试将 express-api 网关与这些服务一起使用。express api 网关也作为容器的一部分在容器中运行docker swarm.Following 是 Dockercompose 文件

 version: "3"
services:
  firstService:
    image: chaitanyaw/firstrepository:first
    deploy:
      replicas: 3
      restart_policy:
        condition: on-failure
    ports:
        - '3000:3000'
    networks:
      - webnet
  apiGateway:
    image: firstgateway:latest
    deploy:
      restart_policy:
        condition: on-failure
    ports:
      - '80:80'
    networks: 
      - webnet
  visualizer:
    image: dockersamples/visualizer:latest
    ports:
     - 8080:8080
    volumes: 
      - /var/run/docker.sock:/var/run/docker.sock
    deploy:
      placement:
         constraints: [node.role == manager]
    networks:
      - webnet
networks:
  webnet:

此外,以下是 gateway.config 文件

http:
  port: 80
admin:
  port: 9876
  hostname: localhost
apiEndpoints:
  api:
    host: 192.168.99.100
    paths: '/ip'
  localApi:
    host: 192.168.99.100
    paths: '/'
serviceEndpoints:
  httpbin:
    url: 'https://httpbin.org'
  services:
    urls:
      - 'http://192.168.99.100:3000/serviceonerequest'
      - 'http://192.168.99.100:3000/servicetwo'
policies:
  - basic-auth
  - cors
  - expression
  - key-auth
  - log
  - oauth2
  - proxy
  - rate-limit
pipelines:
  default:
    apiEndpoints:
      - api
    policies:
    # Uncomment `key-auth:` when instructed to in the Getting Started guide.
    # - key-auth:
      - proxy:
          - action:
              serviceEndpoint: httpbin 
              changeOrigin: true
  customPipeline:
    apiEndpoints:
      - localApi
    policies:
      - proxy:
        - action:
            serviceEndpoint: services
            changeOrigin: true

ip '192.168.99.100' 是 docker-machine ip。如果我使用上面的 gateway.config 文件运行堆栈,一切正常。但是,如果我更换

services:
    urls:
      - 'http://192.168.99.100:3000/serviceonerequest'
      - 'http://192.168.99.100:3000/servicetwo'

services:
    urls:
      - 'http://services_firstService:3000/serviceonerequest'
      - 'http://services_firstService:3000/servicetwo'

我的网关不好!这个 docker swarm 运行一个覆盖网络“webnet”。所以,我必须能够根据这个链接在 gateway.config 文件中使用服务名称作为主机名。在上述使用 IP 的工作案例中,服务在我不希望的网关“外部”可用。怎么了?

4

2 回答 2

2

所以我找到了哪里出错了!看看上面的 compose 文件!服务名称是 firstService(大写的 S!)。现在,如果我使用

urls:
      - 'http://services_firstService:3000/serviceonerequest'
      - 'http://services_firstService:3000/servicetwo'

API 网关将继续寻找services_firstservice(注意 firstService 中的小“s”),这显然不存在于覆盖网络中!现在,我将名称更改为小写字母,它按预期工作!这是新的 Docker-compose 文件

version: "3"
services:
  firstservice:
    image: chaitanyaw/firstrepository:first
    deploy:
      replicas: 3
      restart_policy:
        condition: on-failure
    networks:
      - webnet
  apigateway:
    image: firstgateway:latest
    deploy:
      restart_policy:
        condition: on-failure
    ports:
      - '80:80'
    networks: 
      - webnet
  visualizer:
    image: dockersamples/visualizer:latest
    ports:
     - 8080:8080
    volumes: 
      - /var/run/docker.sock:/var/run/docker.sock
    deploy:
      placement:
         constraints: [node.role == manager]
    networks:
      - webnet
networks:
  webnet:

另外,这里是 gateway.config 文件

http:
  port: 80
admin:
  port: 9876
  hostname: localhost
apiEndpoints:
  api:
    host: 192.168.99.100
    paths: '/ip'
  localApi:
    host: 192.168.99.100
    paths: '/'
serviceEndpoints:
  httpbin:
    url: 'https://httpbin.org'
  services:
    urls:
      - 'http://services_firstservice:3000/serviceonerequest'
      - 'http://services_firstservice:3000/servicetwo'
policies:
  - basic-auth
  - cors
  - expression
  - key-auth
  - log
  - oauth2
  - proxy
  - rate-limit
pipelines:
  default:
    apiEndpoints:
      - api
    policies:
    # Uncomment `key-auth:` when instructed to in the Getting Started guide.
    # - key-auth:
      - proxy:
          - action:
              serviceEndpoint: httpbin 
              changeOrigin: true
  customPipeline:
    apiEndpoints:
      - localApi
    policies:
      - proxy:
        - action:
            serviceEndpoint: services
            changeOrigin: true
于 2018-04-29T15:35:12.937 回答
0

从网关的角度来看,配置没有任何问题。只要可以正确解析 DNS 名称,请求应该没有问题。

我会尝试做的是登录网关容器并尝试使用ping这两个服务。如果出现问题,那么文件很可能docker-compose.yml有问题。

例如,我会尝试删除network密钥。网络是隐式创建的,因此您实际上并不需要该部分。

另外——我不认为该服务的 DNS 名称是services_firstService; 我会firstService直接尝试。

干杯,

五。

于 2018-04-29T12:36:19.053 回答