SpringCloud系列十三:Feign对继承、压缩、日志的支持以及构造多参数请求

1. 回顾

  上文讲解了手动创建Feign,比默认的使用更加灵活。

  本文将讲解Feign对继承、压缩的支持以及日志和多参数请求的构造等。

2. Feign对继承的支持

  Feign支持继承。使用继承,可将一些公共操作分组到一些父接口中,从而简化Feign的开发。

  尽管Feign的继承可帮助我们进一步简化开发,但是Spring Cloud指出——通常情况下,

  不建议服务器端和客户端之间共享接口,因为这种方式会造成服务器端和客户端代码的紧耦合。

  并且,Feign本身并不使用Spring MVC的工作机制(方法参数映射不被继承)。

3. Feign对压缩的支持

  在一些场景下,可能需要对请求或响应进行压缩,此时可使用启用Feign的压缩功能。

  其中,feign.compression.request.mime-types 用于支持的媒体类型列表,默认是 text/xml,application/xml,application/json

  feign.compression.request.min-request-size用于设置请求的最小阈值,默认是2048

4. Feign的日志

  很多场景下,需要了解Feign处理请求的具体细节。  

  Feign对日志的处理非常灵活,可为每个Feign客户端指定日志记录策略,每个Feign客户端都会创建一个logger。

  默认情况下,logger的名称是Feign接口的完整类名。需要注意的是,Feign的日志打印只会对DEBUG级别作出响应。

  我们可为每个Feign客户端配置各自的Logger.Level对象,告诉Feign记录那些日志。Logger.Level的值有以下选择。

  • NONE:不记录任何日志(默认值)
  • BASIC:仅记录请求方法、URL、响应状态代码以及执行时间
  • HEADERS:记录BASIC级别的基础上,记录请求和响应的header
  • FULL:记录请求和响应的header,body和元数据

  > 复制项目 microservice-consumer-movie-feign,将 ArtifactId 修改为 microservice-consumer-movie-feign-logging

  > 创建Feign配置类

package com.itmuch.cloud.microserviceconsumermoviefeignlogging.config;

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignLogConfiguration {

    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

}

  > 修改Feign的接口,指定其配置类

package com.itmuch.cloud.microserviceconsumermoviefeignlogging.feign;

import com.itmuch.cloud.microserviceconsumermoviefeignlogging.config.FeignLogConfiguration;
import com.itmuch.cloud.microserviceconsumermoviefeignlogging.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(name = "microservice-provider-user", configuration = FeignLogConfiguration.class)
public interface UserFeignClient {

    @GetMapping(value = "/{id}")
    User findById(@PathVariable("id") Long id);

}

  > 在 application.yml 中添加以下内容,指定Feign接口的日志级别为DEBUG(因为Feign的Logger.Level只对DEBUG级别作出响应)

logging:
  level:
    com.itmuch.cloud.microserviceconsumermoviefeignlogging.feign.UserFeignClient: DEBUG # 将Feign接口的日志级别设置为DEBUG,因为Feign的Logger.Level只对DEBUG作出响应

  > 启动 microservice-discovery-eureka

  > 启动 microservice-provider-user

  > 启动 microservice-consumer-movie-feign-logging

  > 访问 http://localhost:8010/user/1,可在电影微服务的控制台看见如下内容

5. 使用Feign构造多参数请求

  5.1 GET请求多参数的URL

    http://localhost:8001/get?id=1&username=张三

    > 最直观的方法,URL有几个参数,Feign接口就有几个参数

package com.itmuch.cloud.microserviceconsumermoviefeignlogging.feign;

import com.itmuch.cloud.microserviceconsumermoviefeignlogging.config.FeignLogConfiguration;
import com.itmuch.cloud.microserviceconsumermoviefeignlogging.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name = "microservice-provider-user", configuration = FeignLogConfiguration.class)
public interface UserFeignClient {

    @GetMapping(value = "/get")
    User findUserByCondi(@RequestParam("id") Long id, @RequestParam("username") String username);

}

    > 使用 Map 构建。当目标URL参数非常多时,使用Map构建可简化Feign接口的编写

package com.itmuch.cloud.microserviceconsumermoviefeignlogging.feign;

import com.itmuch.cloud.microserviceconsumermoviefeignlogging.config.FeignLogConfiguration;
import com.itmuch.cloud.microserviceconsumermoviefeignlogging.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

@FeignClient(name = "microservice-provider-user", configuration = FeignLogConfiguration.class)
public interface UserFeignClient {

    @GetMapping(value = "/get")
    User findUserByCondi(@RequestParam Map<String, Object> map);

}

  5.2 POST请求包含多个参数

package com.itmuch.cloud.microserviceconsumermoviefeignlogging.feign;

import com.itmuch.cloud.microserviceconsumermoviefeignlogging.config.FeignLogConfiguration;
import com.itmuch.cloud.microserviceconsumermoviefeignlogging.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

@FeignClient(name = "microservice-provider-user", configuration = FeignLogConfiguration.class)
public interface UserFeignClient {

    @PostMapping(value = "/post")
    User findUserByCondi(@RequestBody User user);

}

6. 总结

  这几章讲解了Feign的相关知识。

  下文将讲解使用Hystrix实现微服务的容错处理。敬请期待~~~

7. 参考

  周立 --- 《Spring Cloud与Docker微服务架构与实战》

原文地址:https://www.cnblogs.com/jinjiyese153/p/8664968.html

时间: 2024-08-30 09:13:06

SpringCloud系列十三:Feign对继承、压缩、日志的支持以及构造多参数请求的相关文章

springCloud(10):使用Feign实现声明式REST调用-构造多参数请求

请求多参数的URL 假设请求的URL包含多个参数,如:http://localhost:8086/user1?id=1&username=nihao 1.1.Feign接口 @FeignClient(name = "spring-ribbon-eureka-client2") public interface UserFeignClient {     @RequestMapping(value = "/{id}", method = RequestMeth

springcloud系列四 feign远程调用服务

一:Feign简介 Feign 是一种声明式.模板化的 HTTP 客户端,在 Spring Cloud 中使用 Feign,可以做到使用 HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到这是远程方法,更感知不到这是个 HTTP 请求. Feign 的灵感来源于 Retrofit.JAXRS-2.0 和 WebSocket,它使得 Java HTTP 客户端编写更方便,旨在通过最少的资源和代码来实现和 HTTP API 的连接. 二:Feign使用步骤 在客户端集成Feig

SpringCloud系列——Feign 服务调用

前言 前面我们已经实现了服务的注册与发现(请戳:SpringCloud系列——Eureka 服务注册与发现),并且在注册中心注册了一个服务myspringboot,本文记录多个服务之间使用Feign调用. Feign是一个声明性web服务客户端.它使编写web服务客户机变得更容易,本质上就是一个http,内部进行了封装而已. GitHub地址:https://github.com/OpenFeign/feign 官方文档:https://cloud.spring.io/spring-cloud-

openlayers入门开发系列之批量叠加zip压缩SHP图层篇

本篇的重点内容是直接加载压缩文件zip形式shp,可以批量加载点.线.面shp图层,效果图如下: 实现的思路跟之前实现arcgis api for js版本是一致的:arcgis api for js入门开发系列二十三批量叠加zip压缩SHP图层 压缩的shp截图如下: 实现的核心代码: //叠加压缩SHP图层,批量加载显示 $("#zipshp").click(function () { shp("js/main/shpJS/gishome.zip").then(

解剖SQLSERVER 第十三篇 Integers在行压缩和页压缩里的存储格式揭秘(译)

原文:解剖SQLSERVER 第十三篇 Integers在行压缩和页压缩里的存储格式揭秘(译) 解剖SQLSERVER 第十三篇    Integers在行压缩和页压缩里的存储格式揭秘(译) http://improve.dk/the-anatomy-of-row-amp-page-compressed-integers/ 当解决OrcaMDF对行压缩的支持的时候,视图解析整数的时候遇到了一些挑战. 和正常的未压缩整数存储不同的是这些都是可变长度--这意味着1个整数的值50只占用1个字节,而不是

【开源】OSharp框架解说系列(6.1):日志系统设计

〇.前言 日志记录对于一个系统而言,重要性不言而喻.日志记录功能在系统开发阶段,往往容易被忽略.因为开发阶段,程序可以调试,可以反复的运行以查找问题.但在系统进入正常的运行维护阶段,特别是在进行审计统计的时候,追踪问题的时候,在追溯责任的时候,在系统出错的时候等等场景中,日志记录才会显示出它不可替代的作用.记录的日志,平时看似累赘,只有在需要的时候,才会拍大腿后悔当初为什么不把日志记录得详细些. 日志系统,是一个非常基础的系统,但由于需求的复杂性,各个场景需要的日志分类,来源,输出方式各有不同,

C++语言笔记系列之十四——继承后的访问权限

1.析构函数不继承:派生类对象在析构时,基类析构函数的调用顺序与构造函数相反. 注:派生类对象建立时要调用基类构造函数,派生类对象删除时要调用基类析构,顺序与构造函数严格相反. 2.例子 example 1 #include <iostream.h> #include <math.h> class Point { public: Point(double a, double b, doule c) { x = a; y = b; z = c; } double Getx() {re

hbase源码系列(十)HLog与日志恢复

HLog概述 hbase在写入数据之前会先写入MemStore,成功了再写入HLog,当MemStore的数据丢失的时候,还可以用HLog的数据来进行恢复,下面先看看HLog的图. 旧版的HLog是实际上是一个SequceneFile,0.96的已经使用Protobuf来进行序列化了.从Writer和Reader上来看HLog的都是Entry的,换句话说就是,它的每一条记录就是一个Entry. class Entry implements Writable { private WALEdit e

springCloud系列教程01:Eureka 注册中心集群搭建

springCloud系列教程包含如下内容: springCloud系列教程02:ConfigServer 配置中心server搭建 springCloud系列教程03:ConfigClient 配置中心client搭建 springCloud系列教程04:配置信息动态刷新 /bus/refresh springCloud系列教程05:@FeignClient微服务间接口调用及权限验证 springCloud系列教程06:zuul统一网关配置及权限验证 springCloud系列教程07:综合演