42

My goal is to create a strategy of different steps to get from a point-to-point communication between 2 components to a "full blown netflix" style of communication using eureka, ribbon, hystrix. With each iteration I want to add more while I try to limit the amount of changes to the actual code. Feign is my preferred client side framework to make this happen. First step is to create a FeignClient to communicate to the server:

@FeignClient(url = "http://localhost:9000")
interface Client {
    @RequestMapping(method = RequestMethod.GET, value = "/author/{author}/addedValue/{addedValue}")
    Result addToTotal(@RequestParam(value="author") String author, @RequestParam(value="addedValue") long addedValue);
}

This works but I don't want the URL to be hardcoded in the annotation. I would like to have this: @FeignClient() and have a properties construct like: client.url: http://localhost:9000

So far I couldn't find any clues on how to configure that and I couldn't find a solution in the spring-cloud sources.

Can it be done and if yes; how?

Freek van Gool
  • 423
  • 1
  • 4
  • 6
  • hope this answer can help you. [https://stackoverflow.com/questions/43733569/how-can-i-change-the-feign-url-during-the-runtime/54455735#54455735](https://stackoverflow.com/questions/43733569/how-can-i-change-the-feign-url-during-the-runtime/54455735#54455735) – Forest10 Jan 31 '19 at 08:03

3 Answers3

48

It can be done with a "serviceId" instead of a "url". E.g.

@FeignClient("foo")
interface Client { ... }

and

foo.ribbon.listOfServers: localhost:9000

e.g. see http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-ribbon-without-eureka for docs.

Dave Syer
  • 56,583
  • 10
  • 155
  • 143
  • Generally it's not if it's used to bind to Spring Boot `@ConfigurationPoperties` but this is still Netflix native. – Dave Syer Mar 26 '15 at 13:34
  • 4
    I tried it but I have the following error: `com.netflix.client.ClientException: Load balancer does not have available server for client: foo`. Note: I use an `application.yml` and not `application.properties` – Pleymor Dec 15 '15 at 16:00
  • You probably got the YAML format wrong then. Or I didn't understand the question. – Dave Syer Dec 15 '15 at 16:19
  • 4
    @Pleymor, I had the same issue and had to set `ribbon.eureka.enabled=false` to get Ribbon to use the configuration-based server list. I am using rather old versions of the Spring Cloud libraries (1.0.0.RELEASE) and Netflix stuff (feign-ribbon 7.1.0 and ribbon 2.0-RC13), though. Maybe this has been changed in newer versions – moxn Jun 02 '16 at 12:47
  • 1
    What if you have a base URL for all feign clients like "/api"? – dukethrash Mar 03 '19 at 14:30
45

This can be done like this:

@FeignClient(name="fd-mobileapi-service",url="${fdmobile.ribbon.listOfServers}")

Where fdmobile.ribbon.listOfServers : value is a property in application.properties.

I have tested it and it is working.

vegemite4me
  • 6,621
  • 5
  • 53
  • 79
cody123
  • 2,040
  • 24
  • 29
  • 1
    Not true if you have multiple servers in your list of servers. This hardcodes only one url, and takes listOfServers as a single value. – alrodi Oct 11 '18 at 20:39
  • 2
    This worked just fine for us as we needed this only for local environment, for other envs we are using Eureka , so the name suffices – Tatha Dec 17 '19 at 16:00
  • Can this be accomplished within the RequestLine annotation? – frlzjosh Jul 22 '21 at 20:54
0

I got a way to pass the environment variables in a very simple way interface FeignClient,

    @FeignClient(url = "https://"+"\${url}")
    interface Client {
    
  @RequestMapping(method = RequestMethod.GET, value = "/author/{author}/addedValue/{addedValue}")
    Result addToTotal(@RequestParam(value="author") String author, @RequestParam(value="addedValue") long addedValue);

properties

#URL
url.client=${URL}

.env

URL=https:localhost:9000