How to establish relationships between Spring Data REST / Spring HATEOAS based (micro) services?

后端 未结 2 860
梦谈多话
梦谈多话 2021-01-02 18:02

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

If you have service

相关标签:
2条回答
  • 2021-01-02 18:47

    The basic steps are the following ones:

    1. The service needs to discover the resources of the other service.
    2. The service then adds a link to the resources it renders where necessary.

    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:

    Resource discovery

    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.

    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.

    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.

    Augmenting resources with links

    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.

    Beyond that

    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.

    0 讨论(0)
  • 2021-01-02 18:52

    In my case I can only derive related items from the service itself. My goal is to abstract the related items to the point that any number of services can be related to a service and only need to lookup ID's or links. One thought was an @ElementCollection named related with a join of the entity ID of the service. Then in the @Embedded have a relLink field and a relatedID field. Then in the repository do a findby to find the relLink and relatedID.

    The hope is to keep it abstracted enough to essentially mimic a Many to Many setup.

    0 讨论(0)
提交回复
热议问题