Is it possible to proxy a POJO in microservices application?

我与影子孤独终老i 提交于 2019-12-10 21:03:15

问题


I would like to avoid duplicating my POJOs in a microservices application, so I am wondering if there is a way to do that (like proxying)?

I mean, is there a way for a Service A to access POJOs (or other classes/interfaces) defined inside a Service B without physically creating these POJOs classe files in Service A?

The big big challenge in a microservice architecture is that point and I didn't find a way to solve it.


回答1:


"Simple": when there are two services that should be using something common - then the answer is move this code into some form of library and have both services depend on it.

Anything else is most likely a bad idea. The whole idea of micro-services is that service A does not in any form depend on B. And you do not want to get into the reflection game and somehow access internals of another service through some sort of backdoor.

As some comment makes clear: using a library helps to avoid code duplication. The alternative is to willfully copy the "common" parts from service B into service A. This is an option, too.

In that sense: you either re-factor the common parts into a library - or you copy them. Both approaches have their pros and cons. You have to determine what matters most in your environment.




回答2:


Follow this rule of thumb:

For enterprise applications and complex objects, create a 3'rd project (an API project), and share it among your services as a dependency.

For simple and self-descriptive objects, use 'copies' of the same objects in each one of your services; note that this is powerful since the POJOs don't need to be identical;

For instance:

In one service (A), it can look like this:

@Entity
public class ExchangeValue {

    @Id
    private Long id;

    @Column(name = "currency_from")
    private String from;

    @Column(name = "currency_to")
    private String to;

    @Column(name = "conversion_multiple")
    private BigDecimal conversionMultiple;
    ...
}

wheres in another service (B) it can be much shorter, and with different types:

public class ExchangeValue {
    private int conversionMultiple;
    ...
}


来源:https://stackoverflow.com/questions/45351222/is-it-possible-to-proxy-a-pojo-in-microservices-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!