Distributed system, declare interface in a common module OR declare 2 same interfaces in both provider and consumer?

时间:2019-05-31 11:31:24

标签: java distributed-system apache-dubbo

In distributed system, declare interface in a common module OR declare 2 same interfaces in both provider and consumer sub-system, which way is more recommended?

I'm working on a distributed system with Apache Dubbo, and I'm confused about the problem above. A 'consumer sub-system'(running in its own JVM) could invoke methods in the 'provider sub-system' as long as the Interface of these methods is registered in Dubbo. So Dubbo service is based on Interface, I have 2 options:

  1. Declare the interface, let's say MyDubboService, in the common module, let's say my-comm, and add my-comm as a dependency lib in 'consumer sub-system' and 'provider sub-system'. Then implement and register MyDubboService (to Dubbo) in 'provider sub-system'.
  2. Declare MyDubboService in 'consumer sub-system' and 'provider sub-system' two times, Then implement and register MyDubboService (to Dubbo) in 'provider sub-system'.

My team adopted option#2 as the strategy, but in my view, option#1 is better because these advantages:

  1. Guarantee the interface is exactly same at both side
  2. Avoid the duplicated code.
  3. Looks more elegant

I understand option#1 also has defect, like "opened some useless methods to consumer which consumer may not interested"

So what I want to know is which option is better, or say, how do we decide which way to go, option #1 or #2?

Any comments is welcomed, thanks!

0 个答案:

没有答案