[英]How do you generate HATEOAS links in distributed REST service architectures?
I'm new to REST and doing a lot of research right now to consider migrating an internal monolithic service architecture to something leaner and more distributed. 我是REST的新手,现在正在做大量的研究,考虑将内部单片服务架构迁移到更精简和更分散的东西。 I'm having a bit of trouble wrapping my head around how to generate HATEOAS links for resources in a distributed microservice system.
我在如何为分布式微服务系统中的资源生成HATEOAS链接时遇到了一些麻烦。 I understand generally why you shouldn't store the relations themselves in the database, but the alternative is to generate them in code.
我一般都理解为什么不应该将关系本身存储在数据库中,但另一种方法是在代码中生成它们。
If one of the primary benefits of microservices is that they allow distinct teams to work independently to improve their service and APIs, how can one team reliably generate links to another team's service resources? 如果微服务的主要好处之一是它们允许不同的团队独立工作以改进他们的服务和API,那么一个团队如何可靠地生成到另一个团队服务资源的链接? Is this just a matter of keeping an eye on breaking changes in your APIs and then versioning them appropriately so that other teams can update their resource links whenever they choose?
这只是关注您的API中的更改,然后对它们进行适当的版本化,以便其他团队可以随时更新其资源链接吗?
If so, is it really best to just hardcode links? 如果是这样,那么硬编码链接真的最好吗? It seems to me that there must be some sort of best practice for how to do this, I'm just new enough to the scene that I must not be finding the right search terms.
在我看来,必须有一些最佳实践,如何做到这一点,我只是新的足够的场景,我不能找到正确的搜索条件。
Thanks for any help! 谢谢你的帮助!
I haven't had the opportunity to implement a REST API using HATEOAS, but I had some time to think of how I would implement it. 我没有机会使用HATEOAS实现REST API,但我有时间思考如何实现它。
The most interesting way I thought of would be to implement a sort of "DNS server "for REST APIs, which would basically construct the urls to the different REST APIs available on your system. 我想到的最有趣的方法是为REST API实现一种“DNS服务器”,它基本上构建了系统上可用的不同REST API的URL。
This "DNS" type service would expose a operation like: 此“DNS”类型服务将公开如下操作:
GET /apis/{resourceTypeIdentifier}/{resourceIdentifier} GET / apis / {resourceTypeIdentifier} / {resourceIdentifier}
Which would in turn return the url where the resource could be consumed. 这将反过来返回可以消耗资源的URL。
Example: 例:
Your API (let's say an offer API) needs to return a reference to a resource that is outside of its domain (let's say a customer with id 001). 您的API(比如商品API)需要返回对其域外资源的引用(假设客户ID为001)。 To obtain the link to the external resource it will call the DNS api like so:
要获得外部资源的链接,它将调用DNS api,如下所示:
GET /apis/offer/001 GET / apis / offer / 001
Which would return an url from where someone could fetch the rest of the information about that resource (eg, https://www.example.org/myofferapi/v3/offers/001 or https://www.example.org/myofferapi/v3/offers?offerId=001 ). 哪个会从有人可以获取有关该资源的其他信息的地址返回一个网址(例如, https : //www.example.org/myofferapi/v3/offers/001或https://www.example.org/myofferapi / v3 / offers?offerId = 001 )。 The service could return an url as complex as is needed as long as the api of this DNS is implemented in generic way.
只要此DNS的api以通用方式实现,该服务就可以返回所需的复杂URL。
It would then be the responsibility of the API owners to update the "DNS" database with the information that the "DNS" API needs in order to construct the urls. 然后,API所有者有责任使用“DNS”API所需的信息更新“DNS”数据库以构建URL。 This would take out the responsibility of keeping track and updating the url on each of the consumers and instead put it solely on the service provider.
这将负责跟踪和更新每个消费者的URL,而不是仅将其放在服务提供者上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.