使用Spring HATEOAS在不同应用程序上的对象之间进行链接 [英] Linking between objects on different apps with Spring HATEOAS

查看:138
本文介绍了使用Spring HATEOAS在不同应用程序上的对象之间进行链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调查Spring-cloud,并且已经建立了两个服务"和客户"微服务作为eureka客户.

I'm investigating spring-cloud and I've set up two microservices "offers" and "customers" as eureka clients.

客户应用具有:

@Data
public class Customer extends ResourceSupport {
    private Long customerId;
    private String name;
}

@RestController
@RequestMapping("/customers")
@ExposesResourceFor(Customer.class)
public class CustomersController {
    ...
}

并且优惠"应用具有:

@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 {
    ...
}

我将如何组织代码,以便您可以将Customer链接添加到Offer实例?自动布线EntityLink当然是行不通的,因为这两个控制器位于单独的应用程序中.

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.

合理的做法是为所有带有控制器上的@RequestMapping的控制器创建接口,并在所有应用程序中共享界面,以便您可以使用例如Link link = linkTo(methodOn(OfferController.class).getOffer(2L)).withSelfRel();?

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();?

推荐答案

根据需要链接的方法的多少,我将手动构建链接,或者在应用程序之间共享api-并根据这些描述符构建链接.我不会为几个链接引入依赖关系.

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.

更有趣的问题是,您打算将什么设置为链接的主机?实际的主机或服务的eureka ID?我建议先设置服务ID,然后再设置zuul实例,并在其中处理负载平衡和代理.

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:在链接服务时,如果链接太多,我总是会遇到麻烦.部署速度和弹性可能会受到很大影响.

P.S: When linking services i always found myself in trouble when linking them too much. Deployment speed and resiliency can greatly suffer.

这篇关于使用Spring HATEOAS在不同应用程序上的对象之间进行链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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