how to change the @FeignClient name in runtime

你离开我真会死。 提交于 2019-12-23 22:25:03

问题


I use Spring Cloud Netflix to build my micro service .

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

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

}  

I want to change the name to ms-cloud-pre when some special user. Anyone can give some advice?


回答1:


According to the documentation feign supports placeholders in the name and url fields.

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

So you could set store.name=storeProd at runtime using normal spring boot configuration mechanisms.




回答2:


That actually is possible. In Spring Cloud Zookeeper we're doing a similar thing since the name of the service in the Feign client is not the one that is there in the in Zookeeper. It can be an alias presented in the yaml file. Here you have the code example https://github.com/spring-cloud/spring-cloud-zookeeper/blob/master/spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/dependency/DependencyRibbonAutoConfiguration.java#L54 and here you have the description of the dependencies feature - https://github.com/spring-cloud/spring-cloud-zookeeper/blob/master/docs/src/main/asciidoc/spring-cloud-zookeeper.adoc#using-the-zookeeper-dependencies




回答3:


To create a spring-cloud Feign client at runtime in situations where you don't know the service-id until the point of call:

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();
  }
}


来源:https://stackoverflow.com/questions/35168148/how-to-change-the-feignclient-name-in-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!