關於FeignClient使用大全

一個最簡單的使用FeignClient的例子如下:
1,添加maven依賴

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>2.0.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-core</artifactId>
    <version>9.7.0</version>
</dependency>
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-slf4j</artifactId>
    <version>9.7.0</version>
</dependency>

2,編寫FeignClient代碼

@FeignClient(name = "myFeignClient", url = "http://127.0.0.1:8001")
public interface MyFeignClient {
    @RequestMapping(method = RequestMethod.GET, value = "/participate")
    String getCategorys(@RequestParam Map<String, Object> params);
}

3,直接使用FeignClient

@Autowired
MyFeignClient myFeignClient;

到此,FeignClient便可正常使用一般的Http接口了~
4,如果想使用文件上傳接口或者post的x-www-form-urlencoded接口,那需要做如下配置
添加依賴包

<dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form</artifactId>
    <version>3.4.1</version>
</dependency>
<dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form-spring</artifactId>
    <version>3.4.1</version>
</dependency>
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.3</version>
</dependency>

添加bean註解配置

@Bean
@Primary
@Scope("prototype")
public Encoder multipartFormEncoder(ObjectFactory<HttpMessageConverters> messageConverters) {
    return new SpringFormEncoder(new SpringEncoder(messageConverters));
}

定義文件上傳接口

@RequestMapping(value = {"/demo/v1/upload"}, 
    method = {RequestMethod.POST}, 
    consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
ReturnResult<ImageVO> uploadFile(
    @RequestPart(value = "file") MultipartFile file, 
    @RequestParam(value = "bucketName", required = false) String bucketName);

5,如果想使用Apache的httpclient的連接池,可以做如下配置
添加依賴

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.6</version>
</dependency>
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-httpclient</artifactId>
    <version>9.7.0</version>
</dependency>

添加屬性配置

feign:
  okhttp: 
    enabled: false
  httpclient:
    enabled: true
    maxConnections: 20480
    maxConnectionsPerRoute: 512
    timeToLive: 60
    connectionTimeout: 10000
    userAgent: 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0'

引入FeignAutoConfiguration配置

@Import(FeignAutoConfiguration.class)
@Configuration
public class FeignConfig {
    ...
}

經過這幾步操作後,便可啓用Apache的httpclient替換其內嵌httpclient。
6,如果想啓用hystrix熔斷降級,則可作如下配置
添加依賴

<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-hystrix</artifactId>
    <version>9.7.0</version>
</dependency>

添加屬性配置

feign:
  hystrix: 
    enabled: true

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 15000
  threadpool:
    default:
      coreSize: 40
      maximumSize: 100
      maxQueueSize: 100

添加降級策略

public class MyFeignClientFallback implements MyFeignClient {
    @Override
    public ReturnResult<ImageVO> uploadFile(MultipartFile file, String bucketName) {
        return new ReturnResult<>(5001);
    }
}

添加bean配置

@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
    return HystrixFeign.builder();
}

@Bean
public MyFeignClientFallback fb() {
    return new MyFeignClientFallback();
}

更新@FeignClient代碼

@FeignClient(
    name = "myFeignClient", 
    url = "http://127.0.0.1:8001",
    fallback = MyFeignClientFallback.class,
    configuration = {FeignConfig.class})

7,如果想處理熔斷的具體原因,可以做如下更新
更新熔斷策略代碼實現FallbackFactory接口

public class MyFeignClientFallback implements FallbackFactory<MyFeignClient> {
    @Override
    public MyFeignClient create(final Throwable cause) {
        return new MyFeignClient() {
            @Override
            public ReturnResult<ImageVO> uploadFile(MultipartFile file, String bucketName) {
                // 處理cause
                
                return new ReturnResult<>(5001);
            }
        };
    }
}

更新bean配置

@Bean
public MyFeignClientFallback fbf() {
    return new MyFeignClientFallback();
}

更新@FeignClient代碼

@FeignClient(
    name = "myFeignClient", 
    url = "http://127.0.0.1:8001",
    fallbackFactory = MyFeignClientFallback.class,
    configuration = {FeignConfig.class})

-End-

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章