Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 A (Instructor) and Service B (Course) each exist as an a stand alone app.

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)

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

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.

like image 621
code Avatar asked Jan 06 '15 01:01

code


People also ask

What is the use of spring HATEOAS?

The Spring HATEOAS project is a library of APIs that we can use to easily create REST representations that follow the principle of HATEOAS (Hypertext as the Engine of Application State).

Should I use spring HATEOAS?

Spring HATEOAS provides common abstractions (representational models, a Link class, API to build links pointing to Spring MVC controllers, etc.) to ease building hypermedia driven REST APIs with Spring MVC in general. Thus, you can use it alongside Spring MVC to manually build those services.


1 Answers

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.

like image 105
Oliver Drotbohm Avatar answered Nov 13 '22 20:11

Oliver Drotbohm