如何获取api网关地址以便从docker compose中的角度容器调用它 [英] how to get api gateway address in order to call it from angular container in docker compose

查看:210
本文介绍了如何获取api网关地址以便从docker compose中的角度容器调用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个docker compose文件,其中有多个容器,其中有一个api网关,另一个包含有angularjs应用程序(此堆栈的网站). api网关用于调用撰写文件中存在的多个容器的正确api.但是我需要从网站(角度容器)调用api网关,并且 我想知道什么是获取api网关容器地址以便从有角度的容器(或无论如何从浏览器中调用)的最佳实践. 目前我在当地工作,所以我指定 本地主机:PortOfApiGateway/apis/...

i have a docker compose file in which there are several containers among which there are an api gateway and another one with an angularjs application (the website of this stack). The api gateway is concerned to call correct apis of several containers presents in the compose file. But i need to call the api gateway from the website (angular container) and i would like to know what is the best practices for get the address of the api gateway container in order to call it from angular container (or from browser anyway..). For the moment i working in local, so i specify localhost:PortOfApiGateway/apis/...

一切正常,但是很显然,如果尝试从另一台主机进行连接,则无法正常工作...

and everything work fine, but obviously if try to connect from another host it not works...

有什么建议吗?

推荐答案

根据您的环境,我看到了几种可能的解决方案.

Depending on your environment, I see several possible solutions.

1.您可以在Angular中拥有一个配置文件

例如,Angular Cli提供了2个文件:environment.ts和environment.prod.ts

For example, Angular Cli provides 2 files: environment.ts and environment.prod.ts

文件的内容可以是:

export const environment = {
  production: false,
  apiBaseUrl: 'http://localhost:3000'
};

和用于生产的等效项可以是真实的DNS网址

and the equivalent for production can be the real DNS url

这是Angular CLI提供的,但是您可以在没有CLI的情况下执行相同的操作.这里的想法是在编译时选择文件.

This is provided by Angular CLI but you can do the same without the CLI. The idea here is that the files are chosen at compile time.

2.始终使用同一服务器

您可以假定主机名将与从Angular应用程序获得的主机名相同:

You can assume that the hostname will be the same as the one you got your Angular app from:

import { Location } from '@angular/common';
// Get the hostname
this.hostname = location.host;
if (this.hostname.indexOf(':') > 0) {
 this.hostname = this.hostname.substr(0, this.hostname.indexOf(':'));
}
// Add a port or a subdomain to get the API url:
this.apiUrl = 'http://' + this.hostname + ':8080';

撰写中的变量?

正如您在前面的评论中所说,调用是从浏览器进行的,因此是在Docker之外进行的.我看不到将这些信息放入撰写文件的简便方法.话虽如此,我很想找出答案.

As you said in an earlier comment, the calls are made from the browser, so outside Docker. I don't see an easy way to put this information in the compose file. That said I would be interested to find out.

希望对您有帮助.

这篇关于如何获取api网关地址以便从docker compose中的角度容器调用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆