zoukankan      html  css  js  c++  java
  • Fign入门学习

    Feign的目标

    feign是声明式的web service客户端,它让微服务之间的调用变得更简单了,类似controller调用service。Spring Cloud集成了Ribbon和Eureka,可在使用Feign时提供负载均衡的http客户端

    引入Feign

    项目中使用了gradle作为依赖管理,maven类似。

    dependencies {
        //feign
        implementation('org.springframework.cloud:spring-cloud-starter-openfeign:2.0.2.RELEASE')
        //web
        implementation('org.springframework.boot:spring-boot-starter-web')
        //eureka client
        implementation('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:2.1.0.M1')
        //test
        testImplementation('org.springframework.boot:spring-boot-starter-test')
    }

    因为feign底层是使用了ribbon作为负载均衡的客户端,而ribbon的负载均衡也是依赖于eureka 获得各个服务的地址,所以要引入eureka-client。

    SpringbootApplication启动类加上@FeignClient注解,以及@EnableDiscoveryClient

    @EnableFeignClients
    @EnableDiscoveryClient
    @SpringBootApplication
    public class ProductApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ProductApplication.class, args);
        }
    }

    yml配置:

    server:
      port: 8082
    
    #配置eureka
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka
      instance:
        status-page-url-path: /info
        health-check-url-path: /health
    
    #服务名称
    spring:
      application:
        name: product
      profiles:
        active: ${boot.profile:dev}
    #feign的配置,连接超时及读取超时配置
    feign:
      client:
        config:
          default:
            connectTimeout: 5000
            readTimeout: 5000
            loggerLevel: basic

    Feign的使用

    @FeignClient(value = "CART")
    public interface CartFeignClient {
    
        @PostMapping("/cart/{productId}")
        Long addCart(@PathVariable("productId")Long productId);
    }

    上面是最简单的feign client的使用,声明完为feign client后,其他spring管理的类,如service就可以直接注入使用了,例如:

    //这里直接注入feign client
    @Autowired
    private CartFeignClient cartFeignClient;
    
    @PostMapping("/toCart/{productId}")
    public ResponseEntity addCart(@PathVariable("productId") Long productId){
        Long result = cartFeignClient.addCart(productId);
        return ResponseEntity.ok(result);
    }

    可以看到,使用feign之后,我们调用eureka 注册的其他服务,在代码中就像各个service之间相互调用那么简单。

    FeignClient注解的一些属性

    属性名默认值作用备注
    value 空字符串 调用服务名称,和name属性相同  
    serviceId 空字符串 服务id,作用和name属性相同 已过期
    name 空字符串 调用服务名称,和value属性相同  
    url 空字符串 全路径地址或hostname,http或https可选  
    decode404 false 配置响应状态码为404时是否应该抛出FeignExceptions  
    configuration {} 自定义当前feign client的一些配置 参考FeignClientsConfiguration
    fallback void.class 熔断机制,调用失败时,走的一些回退方法,可以用来抛出异常或给出默认返回数据。 底层依赖hystrix,启动类要加上@EnableHystrix
    path 空字符串 自动给所有方法的requestMapping前加上前缀,类似与controller类上的requestMapping  
    primary true    

    此外,还有qualifier及fallbackFactory,这里就不再赘述。

    Feign自定义处理返回的异常

    这里贴上GitHub上openFeign的wiki给出的自定义errorDecoder例子。

    public class StashErrorDecoder implements ErrorDecoder {
    
        @Override
        public Exception decode(String methodKey, Response response) {
            if (response.status() >= 400 && response.status() <= 499) {
                //这里是给出的自定义异常
                return new StashClientException(
                        response.status(),
                        response.reason()
                );
            }
            if (response.status() >= 500 && response.status() <= 599) {
                //这里是给出的自定义异常
                return new StashServerException(
                        response.status(),
                        response.reason()
                );
            }
            //这里是其他状态码处理方法
            return errorStatus(methodKey, response);
        }
    }

    自定义好异常处理类后,要在@Configuration修饰的配置类中声明此类。

    Feign使用OKhttp发送request

    Feign底层默认是使用jdk中的HttpURLConnection发送HTTP请求,feign也提供了OKhttp来发送请求,具体配置如下:

    feign:
      client:
        config:
          default:
            connectTimeout: 5000
            readTimeout: 5000
            loggerLevel: basic
      okhttp:
        enabled: true
      hystrix:
        enabled: true

    Feign原理简述

    • 启动时,程序会进行包扫描,扫描所有包下所有@FeignClient注解的类,并将这些类注入到spring的IOC容器中。当定义的Feign中的接口被调用时,通过JDK的动态代理来生成RequestTemplate。
    • RequestTemplate中包含请求的所有信息,如请求参数,请求URL等。
    • RequestTemplate声场Request,然后将Request交给client处理,这个client默认是JDK的HTTPUrlConnection,也可以是OKhttp、Apache的HTTPClient等。
    • 最后client封装成LoadBaLanceClient,结合ribbon负载均衡地发起调用。

    详细原理请参考源码解析。

    Feign、hystrix与retry的关系请参考https://xli1224.github.io/2017/09/22/configure-feign/

    Feign开启GZIP压缩

    Spring Cloud Feign支持对请求和响应进行GZIP压缩,以提高通信效率。

    application.yml配置信息如下:

    feign:
      compression:
        request: #请求
          enabled: true #开启
          mime-types: text/xml,application/xml,application/json #开启支持压缩的MIME TYPE
          min-request-size: 2048 #配置压缩数据大小的下限
        response: #响应
          enabled: true #开启响应GZIP压缩

    注意:

    由于开启GZIP压缩之后,Feign之间的调用数据通过二进制协议进行传输,返回值需要修改为ResponseEntity<byte[]>才可以正常显示,否则会导致服务之间的调用乱码。

    示例如下

    @PostMapping("/order/{productId}")
    ResponseEntity<byte[]> addCart(@PathVariable("productId") Long productId);

    作用在所有Feign Client上的配置方式

    方式一:通过java bean 的方式指定。

    @EnableFeignClients注解上有个defaultConfiguration属性,可以指定默认Feign Client的一些配置

    @EnableFeignClients(defaultConfiguration = DefaultFeignConfiguration.class)
    @EnableDiscoveryClient
    @SpringBootApplication
    @EnableCircuitBreaker
    public class ProductApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ProductApplication.class, args);
        }
    }

    DefaultFeignConfiguration内容:

    @Configuration
    public class DefaultFeignConfiguration {
    
        @Bean
        public Retryer feignRetryer() {
            return new Retryer.Default(1000,3000,3);
        }
    }

    方式二:通过配置文件方式指定。

    feign:
      client:
        config:
          default:
            connectTimeout: 5000 #连接超时
            readTimeout: 5000 #读取超时
            loggerLevel: basic #日志等级

    Feign Client开启日志

    日志配置和上述配置相同,也有两种方式。

    方式一:通过java bean的方式指定

    @Configuration
    public class DefaultFeignConfiguration {
        @Bean
        public Logger.Level feignLoggerLevel(){
            return Logger.Level.BASIC;
        }
    }

    通过源码可以看到日志等级有 4 种,分别是:

    • NONE:不输出日志。
    • BASIC:只输出请求方法的 URL 和响应的状态码以及接口执行的时间。
    • HEADERS:将 BASIC 信息和请求头信息输出。
    • FULL:输出完整的请求信息。

    Feign 日志等级源码如下图所示:

    public enum Level {
        NONE,
        BASIC,
        HEADERS,
        FULL
    }

    更多配置可参见:Spring Cloud Feign的自定义配置及使用 (biancheng.net)

    方式二:通过配置文件指定

    logging:
      level:
        com.xt.open.jmall.product.remote.feignclients.CartFeignClient: debug

    Feign 的GET的多参数传递

    目前,feign不支持GET请求直接传递POJO对象的,目前解决方法如下:

    1. 把POJO拆散城一个一个单独的属性放在方法参数中
    2. 把方法参数编程Map传递
    3. 使用GET传递@RequestBody,但此方式违反restful风格

    介绍一个最佳实践,通过feign的拦截器来实现。

    @Component
    @Slf4j
    public class FeignCustomRequestInteceptor implements RequestInterceptor {
    
        @Autowired
        private ObjectMapper objectMapper;
    
        @Override
        public void apply(RequestTemplate template) {
            if (HttpMethod.GET.toString() == template.method() && template.body() != null) {
                //feign 不支持GET方法传输POJO 转换成json,再换成query
                try {
                    Map<String, Collection<String>> map = objectMapper.readValue(template.bodyTemplate(), new TypeReference<Map<String, Collection<String>>>() {
    
                    });
                    template.body(null);
                    template.queries(map);
                } catch (IOException e) {
                    log.error("cause exception", e);
                }
            }
        }

    怎么选择使用Ribbon还是Feign:
    通过对比Feign和Ribbon实现负载均衡,我可以发现:

    二者一个是通过接口的思想实现了消费者在客户端对于服务提供者提供的服务的消费和负载均衡,一个是按照RESTFUL风格实现了消费者在客户端对于服务提供者提供的服务的消费和负载均衡,在实际的开发中我们可以根据需要选择二者中的一个实现负载均衡;

    (1)如果不需要使用负载均衡我们可以直接像前面的做法,直接在消费者端将某一个服务提供者的URL写死,通过URL+服务提供者在controller中暴露的API,实现一直只调用这一个服务提供者提供的服务
    (2)如果对于效率要求高一些,对于代码可读性要求低一些可以选择使用Feign,因为它在中间加了一层,所以效率会变低,但是它通过接口的思想,简化了消费者调用服务者服务的流程,使得调用流程更像controller层调用service层
      相反可以选择Ribbon

    源码分析请见文章微服务实战SpringCloud之Feign源码分析

  • 相关阅读:
    JavaScript函数
    JavaScript数组知识点
    面向对象之继承及属性查找顺序
    面向对象二
    面向对象
    正则表达式补充
    垃圾回收机制、标记删除及分代回收
    hashlib、hmac、subprocess、configparser模块
    模块、起别名、from导入
    递归、匿名函数、内置函数
  • 原文地址:https://www.cnblogs.com/cy0628/p/15183766.html
Copyright © 2011-2022 走看看