我只有很少的微服务,我们用React App称它为food-ms
,receipt-ms
,ingredients-ms
和frontend
会消耗这些微服务。
它们每个都在/api/[methods]
公开API。
我想使用Docker和docker-compose创建具有下一个属性的生产和开发环境:
http://food-app.test
,对于开发而言,主机(理想情况下)应为localhost food-ms
API应该位于localhost/food/api
,receipt-ms
API应该位于localhost/receipt/api
等。前端应该位于localhost
根/
路径。 localhost/{service}/api
反向代理和使用。 我找到了traefik反向代理,并对其进行了一些尝试,但遇到了问题:
localhost
。目前,我可以通过在Host
header 中指定一个奇怪的主机(例如<container-name>.<network-name>.docker.localhost
)来将请求代理到特定后端traefik.toml
中描述的前端似乎没有作用。 host
网络吗? 提前致谢。
这是我的
traefik.toml
defaultEntryPoints = ["http"]
[entryPoints]
[entryPoints.http]
address = ":80"
[file]
[frontends]
[frontends.food]
entrypoints = ["http"]
backend="food"
[frontends.receipts]
entrypoints = ["http"]
backend="receipts"
这些前端似乎没有得到应用,因为如果我赞扬仪表板,它们也不会改变。
最佳答案
花了一段时间后,我的问题取得了一些成功。
首先,以traefik作为本地应用程序而不是docker容器运行的实验要容易得多。
所以我在本地安装了traefik(brew install traefik
)并在下一个命令行中运行它:
traefik --web --configfile=./docker/traefik-local.toml --logLevel=INFO
有一个已弃用但有效的参数
--web
,同时可以省略。然后我用配置创建了一个TOML文件
defaultEntryPoints = ["http"]
[entryPoints]
[entryPoints.http]
address = ":80"
[file]
[frontends]
[frontends.fin]
entrypoints = ["http"]
backend="fin"
[frontends.fin.routes.matchUrl]
rule="PathPrefixStrip:/api/fin"
[frontends.fin.routes.rewriteUrl]
rule = "AddPrefix: /api"
[frontends.proj]
entrypoints = ["http"]
backend="proj"
[frontends.proj.routes.matchUrl]
rule="PathPrefixStrip: /api/proj"
[frontends.proj.routes.rewriteUrl]
rule = "AddPrefix: /api"
[backends]
[backends.fin]
#
[backends.fin.servers.main]
url = "http://localhost:81"
[backends.proj]
#
[backends.proj.servers.main]
url = "http://localhost:82"
服务名称与初始答案不同,但思路应明确。
首先,在描述前端和后端之前必须执行
[file]
指令。没有它是行不通的,arghh :(服务在docker容器中运行,并为
fin
公开端口81和proj
公开端口82。由于现在traefik在docker隔离网络之外工作,因此它支持本地运行的应用程序和容器中的应用程序。然后描述了两个前端。最初,我对规则也有疑问:
PathPrefixStrip
是Matcher,但它也通过删除路径前缀来修改路径。因此,现在它可以在本地运行时按我希望的方式工作,并且使其在Docker中工作应该容易得多。
关于docker - Docker和Traefik的反向代理微服务,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50874694/