如何在基于Spring Data REST/Spring HATEOAS的(微)服务之间建立关系? [英] How to establish relationships between Spring Data REST / Spring HATEOAS based (micro) services?

查看:93
本文介绍了如何在基于Spring Data REST/Spring HATEOAS的(微)服务之间建立关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图弄清楚使用基于Spring Data Rest或HATEOAS的基于超媒体的微服务时如何处理关系的模式.

Trying to figure out a pattern for how to handle relationships when using a hypermedia based microservices based on Spring Data Rest or HATEOAS.

如果您拥有服务A(讲师)和服务B(课程),则它们都是独立的应用程序.

If you have service A (Instructor) and Service B (Course) each exist as an a stand alone app.

在两个服务之间建立关系的首选方法是什么.以不需要外部服务的ID列的方式.每个服务可能有许多其他服务需要在同一庄园内进行通信.

What is the preferred method for establishing a relationship between the two services. In a manner that does not require columns for IDs of the foreign service. It would be possible for each service to have many other services that need to communicate in the same manor.

可能的解决方案(不确定正确的路径)

Possible solution (Not sure a correct path)

每个服务都有一个带有OneToMany的第二个表,该表具有该服务内的主要实体.该表将包含以下字段:

Each service has a second table with a OneToMany with the primary entity within the service. The table would have the following fields:

ID,entityID,rel,relatedID

ID, entityID, rel, relatedID

然后在相反的服务中使用Spring Data Rest设置一个查询,查询联接表以查找匹配的记录.

Then in the opposite service using Spring Data Rest setup a find that queries the join table to find records that match.

我要实现的主要目标是,任何服务都可以与任意数量的其他服务建立关系,而不必了解其他服务.

The primary goal I want to accomplish would be any service can have relationships with any number of other services without having to have knowledge of the other service.

推荐答案

基本步骤如下:

  1. 该服务需要发现其他服务的资源.
  2. 然后该服务在必要时向其呈现的资源添加一个链接.

此存储库中,我有一个非常基本的示例步骤.该示例包含两个服务:用于为商店提供地理空间搜索的服务.第二项服务是一些基本的客户管理,可以选择将其与商店服务(如果当前可用)集成.

I have a very rudimentary example of these steps in this repository. The example consists of two services: a service to provide geo-spatial searches for stores. The second service is some rudimentary customer management that optionally integrates with store service if it is currently available.

以下是步骤的实现方式:

Here's how the steps are implemented:

在我的示例中,消费服务(即客户)使用Spring HATEOAS的Traverson API遍历一组链接关系,直到找到名为by-location的链接.这是在定期使用HEAD-请求来检查链接是否存在.

In my example the consuming service (i.e. the customer one) uses Spring HATEOAS' Traverson API to traverse a set of link relations until it finds a link named by-location. This is done in StoreIntegration. So all the client service needs to know is the root URI (taken from the environment in my case) and a set of link relations. It periodically checks the link for existence using a HEAD-request.

这当然可以通过更复杂的方式来完成:将基本URI硬连接到客户端服务可能被认为不是最佳选择,但是如果您仍然使用DNS,则实际上效果很好(以便可以交换实际主机硬编码的URI后面).尽管如此,这是一种不错的实用方法,如果更改URI,仍然可以重新发现其他服务,而无需其他库.

This of course can be done in a more sophisticated manner: hard-wiring the base URI into the client service might be considered suboptimal but actually works quite well if you're using DNS anyway (so that you can exchange the actual host behind the URI hard-coded). Nonetheless it's a decent pragmatic approach, still rediscovers the other service if it changes URIs, no additional libraries required.

有关更为复杂的方法,请查看 Netflix的Eureka库,该库基本上是服务注册中心.另外,您可能想检阅我们为此提供的 Spring Cloud集成.

For an even more sophisticated approach have a look at Netflix' Eureka library which is basically a service registry. Also, you might wanna checkout the Spring Cloud integration we have for that.

Spring HATEOAS提供了Spring Data REST利用的ResourceProcessor API.它允许您操纵即将渲染的Resource实例,例如添加链接.可以在

Spring HATEOAS provides a ResourceProcessor API that Spring Data REST leverages. It allows you to manipulate the Resource instance about to be rendered and e.g. add links to it. The implementation for the customers service can be found here.

它基本上采用了上述步骤中刚刚发现的链接,并使用众所周知的参数对其进行了扩展,从而使客户只需跟随该链接即可触发商店地理搜索.

It basically takes the link just discovered in the steps above and expands it using well-known parameters and thus allows clients to trigger a store geo-search by just following the link.

您可以在 Spring Cloud 的示例项目中找到该示例的更复杂的变体.它采用了相同的示例,但是切换到Spring Cloud组件,例如Eureka集成,收集指标,添加UI等.

You can find a more sophisticated variant of this example in the examples projects for Spring Cloud. It takes the very same example but switches to Spring Cloud components such as Eureka integration, gathering metrics, adding UI etc.

这篇关于如何在基于Spring Data REST/Spring HATEOAS的(微)服务之间建立关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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