为什么使用openfeigin而不是用feign
时间: 2023-05-25 13:01:49 浏览: 116
A:作为一个人工智能语言模型,我没有实际的使用需求,也没有具体的技术环境和应用场景,因此我无法确定为什么使用OpenFeign而不是Feign。不过,一般来说,OpenFeign是Feign的一个分支项目,它基于Feign改进了一些不足之处,提供了更多的功能和扩展性,同时也更加适合与Spring Cloud等微服务框架进行集成。因此,在使用Java微服务架构时,使用OpenFeign可能会是更好的选择。
相关问题
为什么不用feign而是用dubbo
Feign和Dubbo都是用于微服务架构中服务之间的通信的工具,但它们有不同的使用场景和优劣势。
Feign是一个基于HTTP协议的轻量级RESTful框架,它的主要作用是让开发者更容易地编写基于HTTP的API的客户端。如果你的应用程序使用的是RESTful架构,并且需要与其他的HTTP API进行通信,那么使用Feign是一个不错的选择。
Dubbo是一款高性能、轻量级的RPC框架,它主要用于构建分布式服务端和客户端。如果你的应用程序需要处理大量的服务调用,并且需要满足高性能、高可靠性和可扩展性的要求,那么使用Dubbo是一个不错的选择。
综上所述,Feign适用于基于HTTP协议的RESTful架构,而Dubbo适用于构建分布式服务端和客户端。选择哪种工具取决于你的具体需求和应用场景。
使用ThreadPoolExecutor调用feign
使用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实现服务容错?
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)