问题
I'm investigating spring-cloud and I've set up two microservices "offers" and "customers" as eureka clients.
The customers app has:
@Data
public class Customer extends ResourceSupport {
private Long customerId;
private String name;
}
@RestController
@RequestMapping("/customers")
@ExposesResourceFor(Customer.class)
public class CustomersController {
...
}
and the offers app has:
@Data
public class Offer extends ResourceSupport {
private final Long offerId;
private final Long priceI;
private final Customer customer;
}
@RestController
@RequestMapping("/offers")
@ExposesResourceFor(Offer.class)
public class OfferController {
...
}
How would I organize the code so that you can add a Customer
link to the Offer
instances? Autowiring an EntityLink
would of course not work since the two controllers live in separate apps.
Would it be reasonable to create interfaces for all the controllers with the @RequestMapping
on them and shared the inerfaces in all apps so that you could use e.g. Link link = linkTo(methodOn(OfferController.class).getOffer(2L)).withSelfRel();
?
回答1:
Depending on how much method needs to be linked I would construct the links manually or go with sharing api-s between apps and building links from those descriptors. I wouldn't introduce a dependency for a few links.
The more interesting question is what are you planning to set as host of the link? The actual host or the eureka id of the service? I recommend setting the id of the service and then setting up a zuul instance and deal with loadbalancing and proxying in that.
P.S: When linking services i always found myself in trouble when linking them too much. Deployment speed and resiliency can greatly suffer.
来源:https://stackoverflow.com/questions/32481469/linking-between-objects-on-different-apps-with-spring-hateoas