0

I want to use @FeignClient to take URL from property on the bases of environment on that it's run. like: i have test, dev and prod. All these enviroment have different URL for example : test : http://localhost:9000 dev : http://localhost:8080 prod : http://localhost:8181

@FeignClient(name = "my-test-servies", url = "${com.test.my.access.url}")
@RequestMapping(method = RequestMethod.GET, value = "/authors")
public interface MyFeignClient {
  public List<Author> getAuthors();
}

This works but I want the URL property to be changed based on enviroment. As i am using single property file My yml property file is as below : application.yml

com:
  prod:
    my:
      access:
        url: "http://localhost:8181"
  test:
    my:
      access:
        url: "http://localhost:9000"
  dev:
    my:
      access:
        url: "http://localhost:8080"

Can it be done and if yes; how?

1 Answers1

0

Yes, make an factory.

Edit : Class<T> clazz is an Feign interface.

public class FeignClientFactory {

    public static <T> T build(final String url, Class<T> clazz) {
        return Feign.builder().client(new OkHttpClient()).logger(new Slf4jLogger(clazz)).logLevel(Logger.Level.FULL)
                .encoder(new JacksonEncoder()).decoder(new JacksonDecoder()).target(clazz, url);
    }

    public static <T> T build(final String url, Class<T> clazz, ObjectMapper mapper) {
        Assert.notNull(mapper, "The mapper can't be null !");

        return Feign.builder().client(new OkHttpClient()).logger(new Slf4jLogger(clazz)).logLevel(Logger.Level.FULL)
                .encoder(new JacksonEncoder(mapper)).decoder(new JacksonDecoder(mapper)).target(clazz, url);
    }

    public static <T> T buildWithInterceptor(final String url, Class<T> clazz, RequestInterceptor interceptor) {
        return Feign.builder().client(new OkHttpClient()).logger(new Slf4jLogger(clazz)).logLevel(Logger.Level.FULL)
                .encoder(new JacksonEncoder()).requestInterceptor(interceptor).decoder(new JacksonDecoder())
                .target(clazz, url);
    }
}
Zorglube
  • 664
  • 1
  • 7
  • 15