zoukankan      html  css  js  c++  java
  • sprincloud-Feign配置二

    一 前言

    前文Feign配置一中讲述了feign的工作流程,日志设置,基本的HTTP远程过程调用,以及相关的注解说明;这篇文章主要说明的是feign的相关支持配置,以及替换原生的FeignClient;

    二 OkHttp

    目前主流的是使用OkHttp替换原生的FeignClient,Apache的HttpClient感觉没有OkHttp好,所以就不提了。

    OkHttp 详细的信息可以参照文档 https://square.github.io/okhttp/; 主要特色如下:

    1. HTTP2支持多请求一台主机共享socket;
    2. 连接池减少请求延迟
    3. GZIP压缩,减少传输体积
    4. 支持响应缓存,减少重复响应;

    2.1 引入依赖

    父工程中已经引入springboot start依赖,这边不会重复引入;在原来工程的基础上新增 feign-okhttp 依赖;

    <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
            <dependency>
                <groupId>io.github.openfeign</groupId>
                <artifactId>feign-okhttp</artifactId>
            </dependency>
        </dependencies>
    

    2.2 application.yml

    跟配置一文章中不同之处是不用默认的feign配置,替换为开启okhttp配置;

    server:
      port: 8093
    
    spring:
      application:
        name: feign-client # 应用名称
    
    eureka:
      client:
        service-url:
          # 服务注册地址
          defaultZone: http://peer1:10081/eureka/,http://peer2:10082/eureka/,http://peer3:10083/eureka/
    
    
    logging:
      level:
        com.zszxz.feign.service: Debug
    # 开启okhttp    
    feign:
      okhttp:
        enabled: true
    

    2.3 FeignOkHttpConfig

    配置中都是基本配置,设置读取时间,超时时间,连接池,额外配置(例如拦截器)读者可以参照上面文档研究;

    /**
     * @Author lsc
     * <p> okhttp基本配置 </p>
     */
    @Configuration
    @ConditionalOnClass(Feign.class)
    @AutoConfigureBefore(FeignAutoConfiguration.class)
    public class FeignOkHttpConfig {
    
    
        @Bean
        public okhttp3.OkHttpClient okHttpClient(){
            return new okhttp3.OkHttpClient.Builder()
                    .readTimeout(60, TimeUnit.SECONDS)
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(120, TimeUnit.SECONDS)
                    .connectionPool(new ConnectionPool())
                    .build();
        }
    }
    

    2.5 service

    这边调用的之前eureka-client提供的一个表现层API,跟平常的API没有什么区别;如果不懂可可以看看之前的文章或者拉取下源码

    /**
     * @Author lsc
     * <p> </p>
     */
    @FeignClient( name = "eureka-client", value = "eureka-client")
    public interface FeignService {
    
        @GetMapping("zszxz/feign")
        public String getFeign();
    
    }
    

    2.6 controller

    controller层不变,直接调用service层API

    /**
     * @Author lsc
     * <p> feign 表现层 </p>
     */
    @RestController
    public class FeignController {
    
        @Autowired
        FeignService feignService;
    
        @GetMapping("zszxz/feign")
        public String getFeign(){
            // 调用 getFeign方法
            return feignService.getFeign();
        }
    }
    

    2.7 访问结果

    在这里插入图片描述

    三 Feign集成配置说明

    3.1 默认集成ribbon

    feign默认集成了ribbon,不想像上面的配置文件中全局配置超时时间,而是单独配置ribbon可以参照如下:

    #ribbon的超时时间
    ribbon:
      # 读超时时间
      ReadTimeout: 60000    
       # 连接的超时时间       
      ConnectTimeout: 30000       
    

    3.2 开启 hystrix

    默认情况下feign是没有开启hystrix,需要手动开启;

    feign:
      # 开启okhttp
      okhttp:
        enabled: true
      # 开启 hystrix
      hystrix:
        enabled: true
    # 熔断机制
    hystrix:
      shareSecurityContext: true
      command:
        default:
          execution:
            isolation:
              thread:
                timeoutInMillisecond: 10000
          circuitBreaker:
            sleepWindowInMilliseconds: 50000
            forceClosed: true
    

    四 其他配置说明

    经过这两篇文章学习,基本的feign操作已经很全面,如官网上说的feign的继承,知识追寻者觉得是没必要学习,耦合度太高了;GZIP压缩,okhttp是支持的,跟平常调用没什么区别,如果要手动配置也就是返回值是byte而已,所以读者了解以下即可;还有一个回调处理,官网上的示例也很明了,读者很容易上手,如果时间充裕有可能会在hystrix讲解;

    在这里插入图片描述

  • 相关阅读:
    delphi RTTI 四 获取类属性列表
    delphi 控件编辑器
    delphi 属性编辑器
    des加密delphi与c#
    delphi c#语法转换
    电脑组装DIY
    .net DLL 注册 regasm delphi调用
    自助机调试过程
    delphi面向对象 继承窗体
    E2040 Declaration terminated incorrectly
  • 原文地址:https://www.cnblogs.com/zszxz/p/12114837.html
Copyright © 2011-2022 走看看