我在 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 的工作案例中,服务在我不希望的网关“外部”可用。怎么了?