Spring 如何在运行时更改@FeignClient名称

Spring 如何在运行时更改@FeignClient名称,spring,spring-cloud,Spring,Spring Cloud,我使用SpringCloudNetflix构建我的微服务 @FeignClient(name = "ms-cloud",configuration = MsCloudClientConfig.class) public interface TestClient { /**

我使用SpringCloudNetflix构建我的微服务

@FeignClient(name = "ms-cloud",configuration = MsCloudClientConfig.class)      
public interface TestClient {                                                  

/**                                                                        
 * @return                                                                 
 */                                                                        
@RequestMapping(value = "/test", method = RequestMethod.GET)               
String test();                                                             

}  
当某个特殊用户出现时,我想将名称更改为ms cloud pre。
任何人都可以给出一些建议?

这实际上是可能的。在SpringCloudZooKeeper中,我们正在做一件类似的事情,因为外部客户端中的服务名称与In-Zookeeper中的名称不同。它可以是yaml文件中显示的别名。这里有代码示例,这里有依赖项功能的描述-

根据
名称
url
字段中的外部支持占位符

@FeignClient(name = "${store.name}")
public interface StoreClient {
    //..
}

因此,您可以使用正常的spring引导配置机制在运行时设置
store.name=storeProd

要在运行时创建spring cloud假客户端,在调用之前您不知道服务id的情况下:

import org.springframework.cloud.openfeign.FeignClientBuilder;

@Component
public class InfoFeignClient {

  interface InfoCallSpec {
    @RequestMapping(value = "/actuator/info", method = GET)
    String info();
  }

  FeignClientBuilder feignClientBuilder;

  public InfoFeignClient(@Autowired ApplicationContext appContext) {
    this.feignClientBuilder = new FeignClientBuilder(appContext);
  }

  public String getInfo(String serviceId) {

    InfoCallSpec spec =
        this.feignClientBuilder.forType(InfoCallSpec.class, serviceId).build();

    return spec.info();
  }
}

也许这是一种优雅的实现方式,这是一个疑问还是陈述P我的意思是它对您有帮助吗?现在我使用eureka作为注册服务器中心,这样您就可以使用与我向您展示的方法相同的方法。盒子里什么也没有。您必须自己构建它。注意:您不能基于参数或本地上下文动态更改它。作为普通用户,store.name=storeProd,但使用一些凭据,store.name=storeProd pre,。不,您不能这样做。