【发布时间】:2020-01-13 05:50:36
【问题描述】:
我对弹簧/弹簧靴比较陌生。
目前我正在使用 Spring Boot Rest 应用程序,它提供 FeignClient 以包含在其他项目中。现在,我希望这些 FeignClient 被 CircuitBreaker 包裹起来。
我想出的最佳解决方案是我动态创建一个代理,其中包括 CircuitBreaker 实现,它本身调用创建的 FeignClient。
假设我有以下描述 RestController 的接口:
@RequestMapping("/")
public interface MyWebService {
@GetMapping("name")
public String getName();
}
现在,我有了 FeignClient 的接口:
@FeignClient("app")
public interface WebServiceClient extends WebService {
}
所以.. 我的目标是实现类似我有另一个注释 e。 G。 @WithCircuitBreaker 然后我将被扫描并动态创建一个代理 bean,它将被注入而不是 FeignClient bean。
目前我的代码如下所示:
@FeignClient("app")
@WithCircuitBreaker
public interface WebServiceClient extends WebService {
}
据我所知,我现在可以创建一个如下所示的@Configuration 类:
@Configuration
public class WithCircuitBreakerConfiguration implements ImportAware {
private AnnotationMetadata annotationMetadata;
private AnnotationAttributes annotationAttributes;
@Override
public void setImportMetadata(AnnotationMetadata importMetadata) {
this.annotationMetadata = importMetadata;
Map<String, Object> annotatedClasses = importMetadata.getAnnotationAttributes(WithCircuitBreaker.class.getName());
this.annotationAttributes = AnnotationAttributes.fromMap(annotatedClasses);
}
What else to import to create the proxy and inject it?
}
现在我到了重点,我不知道如何继续。如何动态创建一个代理类来做这样的事情:
public class PorxyedWebService {
private WebService feignClientProxy;
@Autowired
public ProxyedWebService(WebService feignClientProxy) {
this.feignClientProxy = feignClientProxy;
}
public String getName() {
...
<some circuitbreaker stuff>
....
return this.feignClientProxy.getName();
}
}
然后一旦有人自动连接WebService 接口,就返回这个代理而不是从 Feign 生成的代理。
【问题讨论】:
标签: java proxy spring-cloud feign resilience4j