简体   繁体   English

通过HTTP for API Gateway在链接的Docker容器之间进行通信

[英]Communication between linked docker containers over http for api gateway

I'm currently working on a golang web app which is currently one application consisting of numerous packages and is deployed in an individual docker container. 我目前正在使用golang网络应用程序,该应用程序目前是一个由许多软件包组成的应用程序,并已部署在单独的docker容器中。 I have a redis instance and a mysql instance deployed and linked as separate containers. 我有一个redis实例和一个mysql实例作为单独的容器进行部署和链接。 In order to get their addresses, I pull them from the environment variables set by docker. 为了获取它们的地址,我将它们从docker设置的环境变量中拉出。 I would like to implement an api gateway pattern wherein I have one service which exposes the HTTP port (either 80 for http or 443 for https) called 'api' which proxies requests to other services. 我想实现一种api网关模式,其中有一个服务公开了称为“ api”的HTTP端口(对于HTTP为80或对于HTTPs为443),该服务将请求转发给其他服务。 The other services ideally do not expose any ports publicly but rather are linked directly with the services they depend on. 理想情况下,其他服务不会公开公开任何端口,而是直接与其所依赖的服务链接。

So, api will be linked with all the services except for mysql and redis. 因此,api将与除mysql和redis之外的所有服务链接。 Any service that need to validate a user's session information will be linked with the user service, etc. My question is, how can I make my http servers listen to http requests on the ports that docker links between my containers. 任何需要验证用户会话信息的服务都将与用户服务等链接。我的问题是,如何使我的http服务器侦听docker链接到我的容器之间的端口上的http请求。

Simplest way to do this is Docker Compose . 最简单的方法是Docker Compose You can simply define which services you want and Docker Compose automatically link them in a dedicated network. 您可以轻松定义所需的服务,然后Docker Compose会在专用网络中自动链接它们。 Suppose you have your goapp , redis , and mysql instance and want to use nginx as your reverse proxy. 假设您有goappredismysql实例,并想使用nginx作为反向代理。 Your docker-compose.yml file looks as follows: docker-compose.yml文件如下所示:

services:
  redis:
    image: redis
  mysql:
    image: mysql
  goapp:
    image: myrepo/goapp
  nginx:
    image: nginx
    volumes:
      - /PATH/TO/MY/CONF/api.conf:/etc/nginx/conf.d/api.conf
    ports:
      - "443:443"
      - "80:80"

The advantage is that you can reference any service from other services by its name. 好处是您可以按名称引用其他服务中的任何服务。 So from your goapp you can reach your MySQL server under hostname mysql and so on. 因此,您可以从goapp以主机名mysql等访问MySQL服务器。 The only exposed ports (ie reachable from the host machine) are 443 and 80 of nginx container. 唯一公开的端口(即从主机可访问的端口)是nginx容器的44380

You can start the whole system with docker-compose up ! 您可以使用docker-compose up启动整个系统!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Redis作为Docker容器之间的链接 - Using Redis as a Link Between Docker Containers 如何在Docker容器之间建立连接 - How to make connection between Docker Containers 在共享同一网络的两个 docker 容器之间共享数据 - Share data between two docker containers sharing same network AWS API 网关 HTTP API 如何传递字符串查询参数? - AWS API Gateway HTTP API how to pass string query params? Amazon API Gateway HTTP API: Custom types in lambda functions in go - Amazon API Gateway HTTP API: Custom types in lambda functions in go Docker构成Go应用程序和Postgres之间的通信问题 - Docker-compose communication issues between a Go app and Postgres 使用 Go 进行 IAM 身份验证的 API Gateway HTTP 客户端请求 - API Gateway HTTP client request with IAM auth with Go Docker 容器之间的请求失败拨打 tcp 172.18.0.6:3050:连接:连接被拒绝 - Request between Docker containers failing dial tcp 172.18.0.6:3050: connect: connection refused Golang:`dial tcp 172.20.0.7:8081: connect: connection refused`,在 docker 容器/服务之间随机出错 - Golang: `dial tcp 172.20.0.7:8081: connect: connection refused`, error at random times between docker containers / services 是否有 docker API 或 exec.Command 方法可以从给定的容器名称(不是容器列表)中检索容器 ID? - Is there a docker API or exec.Command way to retrieve container ID from a given container name (not list of containers)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM