【问题标题】:spring cloud - get server name for feign client from application propertiesspring cloud - 从应用程序属性中获取 feign 客户端的服务器名称
【发布时间】:2018-08-10 14:53:55
【问题描述】:

我有两个微服务demo-cartservicedemo-feignclient,其中demo-feignclientdemo-cartservice 获取资源

在这两个项目中,我将server.servlet.context-path=/demo/api/ 设置为application.properties

Feign 客户端代理使用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);
}

这很好用。

有没有办法从application.properties 读取服务器别名,像这样:

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

demo-feignclient 项目的application.properties 中我想拥有

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

感谢您的帮助

【问题讨论】:

    标签: spring spring-boot spring-cloud netflix-feign spring-cloud-feign


    【解决方案1】:

    抱歉,应该先检查docs。设置后

    feign.name=demo-cartservice/demo/api
    

    demo-feignclient 中的application.properties 中有效:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-01
      • 1970-01-01
      • 2019-10-15
      • 2021-07-26
      • 2020-02-04
      • 2020-01-27
      • 2019-03-21
      • 2017-01-22
      相关资源
      最近更新 更多