user7440148
user7440148

Reputation: 53

Can I dynamic create a Feign Client or create an instance with a different name

I've defined an REST interface that has a different Spring Boot Application implementation using different spring.application.name (spring.application.name can not be the same in my business).

How can I only define a Feign Client, and can access all the SpringBootApplication REST Services?

SpringBootApplication A(spring.application.name=A) and B(spring.application.name=) has this RestService:

@RestController
@RequestMapping(value = "/${spring.application.name}")
public class FeignRestService {

    @Autowired
    Environment env;

    @RequestMapping(path = "/feign")
    public String feign() {
        return env.getProperty("server.port");
    }
}

another SpringBootApplication C:

@FeignClient(name="SpringApplication A or B")
public interface FeignClientService {

    @RequestMapping(path = "/feign")
    public String feign();
}

In SpringBootApplication C, I want to use a FeignClientService to access A and B. Do you have any idea?

Upvotes: 5

Views: 7812

Answers (2)

Andy Brown
Andy Brown

Reputation: 12999

Yes you can create a Feign client and re-use it for as many calls to different named services in the Eureka directory (you tagged the question with spring-cloud-netflix) as you want. Here's an example of how you do it:

@Component
public class DynamicFeignClient {

  interface MyCall {
    @RequestMapping(value = "/rest-service", method = GET)
    void callService();
  }

  FeignClientBuilder feignClientBuilder;

  public DynamicFeignClient(@Autowired ApplicationContext appContext) {
    this.feignClientBuilder = new FeignClientBuilder(appContext);
  }

  /*
   * Dynamically call a service registered in the directory.
   */

  public void doCall(String serviceId) {

    // create a feign client

    MyCall fc =
        this.feignClientBuilder.forType(MyCall.class, serviceId).build();

    // make the call

    fc.callService();
  }
}

Adjust the call interface to suit your requirements and then you can inject and use a DynamicFeignClient instance into the bean where you need to use it.

We've been using this approach in production for months now to interrogate dozens of different services for stuff like version information and other useful runtime actuator data.

Upvotes: 5

cosmos
cosmos

Reputation: 2263

You might have figured this out already but this might help anyone who is looking answers for same question. You'll need to configure Feign Clients for each service clients that consume the services.

It's not possible to use same Feign client to call different services as Feign Clients are tied to services that you define.

Upvotes: 0

Related Questions