url-rewriting - Mapping microservices on frontend
问题描述
This is probably a bit opinion-based question, but I will try to be technical to still be relevant.
Consider having several microservices: a
, b
, c
.
To make this available on frontend, these could be made available as:
https://host/services/a
https://host/services/b
https://host/services/c
However, the fact that the endpoints are split between differents services are kind of irrelevant for frontend and basically if we can guarantee the endpoints don't clash, it would be great to have these available directly:
a/endpoint1
->https://host/services/endpoint1
a/endpoint2
->https://host/services/endpoint2
b/endpoint3
->https://host/services/endpoint3
c/endpoint4
->https://host/services/endpoint4
To implement such mapping, one needs to list all endpoint or at least write some matching pattern within the proxy service. This is very nice for the Frontend team to work with, however it is unfortunately very easy to brake.
What are the best practices for mapping the urls of microservices? Only thing which comes to my mind are some exports of OpenApi, which could be handled by FE to get the right path. However, every service generates its own OpenApi json, so we are basically back to the original problem.
解决方案
你确定前端团队需要所有暴露的端点吗?通常,前端与 API 网关对话,或者,就像现在酷孩子们所说的那样,“前端的后端”。
简而言之,它是一项特殊服务,只负责公开前端所需的功能/端点。它将调用转发到相关服务,或者在必要时调用多个服务并聚合结果。
在大多数情况下,这些 API 网关没有数据库,因为它们正在从其他服务中检索所有数据。然而,他们可能会使用缓存层来加速事情。
您甚至可以拥有多个 API 网关,每个前端(例如桌面、移动)一个。
推荐阅读
- python-3.x - 如何在特定子列表之前插入元素?
- javascript - 如何让网页认为它在屏幕上?
- discord.js - 尝试获取 SQLite 值时,discord.js 机器人返回未定义
- r - 在Rstudio中省略大于常数值的值时查找向量的平均值
- java - 在执行 .jar 文件时,`scala` 是否不需要 -jar,而 `java` 需要?
- python - Django 3.0.5 到 sql server 2012 的连接问题
- linux - 两个文件之间的 AWK 部分字符串搜索
- java - 在 Java 中执行 /usr/bin/env bash -c "command"
- android - 启动特定的 Flutter 屏幕
- node.js - Docker - 撰写我无法创建 postgress 服务器