繁体   English   中英

我如何获得Feign客户的名字?

[英]How can I get name of the Feign client?

我有简单的界面:

public interface ServiceClient {
  String REFRESH_ENDPOINT = "/admin/refresh";
  ResponseEntity<String> refresh();
}

很少有FeignClient这样的客户:

@FeignClient(name = "${ServiceA.name}", configuration = 
FeignConfiguration.class, decode404 = true)
public interface ServiceA extends ServiceClient {

  @PostMapping(path = REFRESH_ENDPOINT)
  ResponseEntity<String> refresh();
}

然后我做:

private final List<ServiceClient> services;
...
services.parallelStream()
            .collect(Collectors.toMap(s-> s.getClass().getName(), s-> s.refresh().getStatusCode()));

我如何获得Feign客户的名字? getClass().getName()给我Proxy154 我希望不要在我拥有的每个FeignClient中创建静态字段。

您可以使用AopProxyUtils。

services.parallelStream()
            .collect(Collectors.toMap(s-> 
                  AopProxyUtils.proxiedUserInterfaces(greetingClient)[0].getName(), 
                  s-> s.refresh().getStatusCode()));  

不确定是否要使用类名或@FeignClient批注中的名称。 如果您想在注释中使用该名称,则可以像这样获得它

Class<?>[] classes = AopProxyUtils.proxiedUserInterfaces(greetingClient);
FeignClient annotation = classes[0].getAnnotation(FeignClient.class);
System.out.println(annotation.name());

您得到的是spring创建的代理类名称。 为了获得代理类名称的原始类名称,spring提供了实用程序类

春季文档

您可以使用此类的方法public static java.lang.Class<?> getUserClass(java.lang.Class<?> clazz)其描述为:

返回给定类的用户定义类:通常只是给定类,但对于CGLIB生成的子类,则返回原始类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM