使用ThreadPoolExecutor调用feign
时间: 2024-01-11 09:22:34 浏览: 146
使用ThreadPoolExecutor调用feign的示例代码如下:
```java
import feign.Feign;
import feign.Request;
import feign.Retryer;
import feign.Target;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.codec.ErrorDecoder;
import feign.hystrix.HystrixFeign;
import feign.hystrix.SetterFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
import java.util.concurrent.*;
@Component
@Import(FeignClientsConfiguration.class)
public class MyFeignClient {
private final ThreadPoolExecutor threadPoolExecutor;
@Autowired
public MyFeignClient(ThreadPoolExecutor threadPoolExecutor) {
this.threadPoolExecutor = threadPoolExecutor;
}
public <T> T createClient(Class<T> type, String url) {
return HystrixFeign.builder()
.setterFactory((Target<?> target, SetterFactory setterFactory) -> {
String name = target.name();
return HystrixFeign.Setter.withGroupKey(HystrixFeign.DefaultGroupKey
.from(target.type()))
.andCommandKey(HystrixFeign.DefaultCommandKey
.from(target.type()))
.andThreadPoolKey(HystrixFeign.DefaultThreadPoolKey
.from(target.type()))
.andCommandPropertiesDefaults(HystrixFeign.SetterFactory
.create(target, setterFactory).getCommandProperties())
.andThreadPoolPropertiesDefaults(HystrixFeign.SetterFactory
.create(target, setterFactory).getThreadPoolProperties());
})
.retryer(Retryer.NEVER_RETRY)
.encoder(new Encoder.Default())
.decoder(new Decoder.Default())
.errorDecoder(new ErrorDecoder.Default())
.options(new Request.Options())
.target(type, url);
}
public <T> Future<T> executeAsync(Callable<T> callable) {
return threadPoolExecutor.submit(callable);
}
}
```
相关问题--:
1. 如何使用ThreadPoolExecutor创建线程池?
2. 如何使用Feign调用远程接口?
3. 如何使用Hystrix实现服务容错?
阅读全文