Search code examples
springspring-bootspring-cloudnetflix-feignspring-cloud-feign

spring cloud - get server name for feign client from application properties


I have two microservices demo-cartservice and demo-feignclient where the demo-feignclient fetches resources from demo-cartservice

In both projects I set server.servlet.context-path=/demo/api/ in application.properties

The Feign client proxy uses the hardcoded server name for demo-cartservice

@FeignClient("demo-cartservice/demo/api")
@RibbonClient("demo-cartservice/demo/api")
public interface DemoCartServiceProxy 
{
    @GetMapping("/carts/{cartId}")
    public Cart getCart(@PathVariable("cartId") long id);
}

This works fine.

Is there a way to read the server alias from application.properties as well, like so:

@FeignClient("${cartservice-alias}/${servlet-context}")
@RibbonClient("${cartservice-alias}/${servlet-context}")
public interface DemoCartServiceProxy 
{
    @GetMapping("/carts/{cartId}")
    public Cart getCart(@PathVariable("cartId") long id);
}

In application.properties of the demo-feignclient project I would like to have

server.servlet.context-path=/demo/api/
cartservice-alias=demo-cartservice

Thanks for the help


Solution

  • Sorry should have checked the docs first. After setting

    feign.name=demo-cartservice/demo/api
    

    in application.properties of demo-feignclient this works:

    @FeignClient(name="${feign.name}")
    @RibbonClient(name="${feign.name}")
    public interface DemoCartServiceProxy 
    {
        @GetMapping("/carts/{cartId}")
        public Cart getCart(@PathVariable("cartId") long id);
    }