我怎样才能得到假装客户的名字?
How can I get name of the Feign client?
我有简单的界面:
public interface ServiceClient {
String REFRESH_ENDPOINT = "/admin/refresh";
ResponseEntity<String> refresh();
}
很少有像这样的 FeignClients:
@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()));
不确定您是想要 class 名称还是 @FeignClient
注释中的名称。如果你想要Annotation中的名字,那么你可以这样获取
Class<?>[] classes = AopProxyUtils.proxiedUserInterfaces(greetingClient);
FeignClient annotation = classes[0].getAnnotation(FeignClient.class);
System.out.println(annotation.name());
我有简单的界面:
public interface ServiceClient {
String REFRESH_ENDPOINT = "/admin/refresh";
ResponseEntity<String> refresh();
}
很少有像这样的 FeignClients:
@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()));
不确定您是想要 class 名称还是 @FeignClient
注释中的名称。如果你想要Annotation中的名字,那么你可以这样获取
Class<?>[] classes = AopProxyUtils.proxiedUserInterfaces(greetingClient);
FeignClient annotation = classes[0].getAnnotation(FeignClient.class);
System.out.println(annotation.name());