• 关于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替换其内嵌client。

    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-

    https://zhuanlan.zhihu.com/p/58515273?from_voters_page=true
  • 相关阅读:
    Set the Welcome Page in SharePoint through Code
    调试工具
    Sublime Text 3编译Sass
    js储存参数的数组arguments
    js 判断客户端浏览器
    手机设计尺寸
    required
    前端工具HBuilder安装Sass插件
    apache极简配置虚拟机
    跳转输入框按键操作
  • 原文地址:https://www.cnblogs.com/tiancai/p/16458908.html
Copyright © 2020-2023  润新知