spring cloud - 从应用程序属性获取feign客户端的服务器名称

问题描述 投票:0回答:1

我有两个微服务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);
}

我想在application.properties项目的demo-feignclient

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

谢谢您的帮助

spring spring-boot spring-cloud netflix-feign spring-cloud-feign
1个回答
1
投票

对不起应该先检查一下docs。设定后

feign.name=demo-cartservice/demo/api

application.propertiesdemo-feignclient这个作品:

@FeignClient(name="${feign.name}")
@RibbonClient(name="${feign.name}")
public interface DemoCartServiceProxy 
{
    @GetMapping("/carts/{cartId}")
    public Cart getCart(@PathVariable("cartId") long id);
}
© www.soinside.com 2019 - 2024. All rights reserved.