關(guān)于FeignClient的使用大全——使用篇

完整代碼參照:
ocean-sea(https://github.com/biticcf/ocean-sea-platform.git)

一個(gè)最簡(jiǎn)單的使用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,在main主入口類添加FeignClients啟用注解

@EnableFeignClients
......

3,編寫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);
}

4,直接使用FeignClient

@Autowired
MyFeignClient myFeignClient;

到此,F(xiàn)eignClient便可正常使用一般的Http接口了~
5,如果想使用文件上傳接口或者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);

6,如果想使用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 {
    ...
}

經(jīng)過(guò)這幾步操作后,便可啟用Apache的httpclient替換其內(nèi)嵌httpclient。
7,如果想啟用hystrix熔斷降級(jí),則可作如下配置
添加依賴

<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

添加降級(jí)策略

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})

8,如果想處理熔斷的具體原因,可以做如下更新
更新熔斷策略代碼實(shí)現(xiàn)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-

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容