[Spring-Cloud][Maven][Docker]Should Feign interfaces be put in FeignClients or EurekaClients?

460 views Asked by At

EurekaServer: register/expose services

EurekaClients: provide services

FeignClients: consume services and provide APIs

I'm using Feign for service consumption. I wonder if feign interfaces (interfaces that annotated with @FeignClient) should be put in EurekaClients or FeignClients?

  1. If Feign interfaces are put in EurekaClients.

GOOD: I only need to write only one copy of Feign interfaces, and implement it in the EurekaClients. For any FeignClients that need to use this service, import these interfaces from EurekaClients and just use it.

BAD: Module dependencies could be easily set, but it is hard to do mvn package or using docker for production. As the problem I stated HERE.

  1. If Feign interfaces are put in FeignClients(almost every examples I can find on the internet do like this).

GOOD: Easy to build.

BAD: A lot of duplicated code. Because for every FeignClient I need to re-write @FeignClient annotated interfaces. If there are a lot of nested FeighClients and EurekaClients, it would be too difficult to maintain.

So any suggestions for a good practice of Where should I put Feigh interfaces ?

1

There are 1 answers

0
Indra Basak On

Here is the pattern we followed in our projects. Each service has two projects, e.g., :

Service A
  model
  service

All the controllers and other business related classes, e.g, DAOs, service, Repository classes are kept in the service project. While the models used by the controller and exposed to the outside world are kept in the model project. The controller also implements an interface which exposes the REST API. This interface is kept in the model project also.

public interface AuthorService {

    @RequestMapping(method = RequestMethod.GET, produces = {
        MediaType.APPLICATION_JSON_VALUE}, value = "/authors/{id}")
    @ResponseBody
    Author getAuthor(@PathVariable("id") Integer id);
}

The AuthorService interface and the model, Author are kept in the model project. The controller, AuthorController which implements the AuthorService is kept in the_service_ project.

Let's say ServiceB uses ServiceA, then the former import's the latter's model project. In ServiceA's service, we create a Feign interface, e.g.,

@FeignClient(name = "author", fallback = 
    AuthorServiceFallbackClient.class)
public interface AuthorServiceClient extends AuthorService {
}

This pattern helped us to reduce quite a bit of code duplication.