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微服务架构与实战》

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

  1. SpringCloud系列-利用Feign实现声明式服务调用

    上一篇文章<手把手带你利用Ribbon实现客户端的负载均衡>介绍了消费者通过Ribbon调用服务实现负载均衡的过程,里面所需要的参数需要在请求的URL中进行拼接,但是参数太多会导致拼接字符 ...

  2. springcloud系列五 feign远程调用服务

    一:Feign简介 Feign 是一种声明式.模板化的 HTTP 客户端,在 Spring Cloud 中使用 Feign,可以做到使用 HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完 ...

  3. 【SpringCloud构建微服务系列】Feign的使用详解

    一.简介 在微服务中,服务消费者需要请求服务生产者的接口进行消费,可以使用SpringBoot自带的RestTemplate或者HttpClient实现,但是都过于麻烦. 这时,就可以使用Feign了 ...

  4. SpringCloud系列十二:手动创建Feign

    1. 回顾 上文讲解了自定义Feign.但是在某些场景下,前文自定义Feign的方式满足不了需求,此时可使用Feign Builder API手动创建Feign. 本文围绕以下场景,为大家讲解如何手动 ...

  5. SpringCloud系列-整合Hystrix的两种方式

    Hystrix [hɪst'rɪks],中文含义是豪猪,因其背上长满棘刺,从而拥有了自我保护的能力.本文所说的Hystrix是Netflix开源的一款容错框架,同样具有自我保护能力. 本文目录 一.H ...

  6. SpringCloud系列——Feign 服务调用

    前言 前面我们已经实现了服务的注册与发现(请戳:SpringCloud系列——Eureka 服务注册与发现),并且在注册中心注册了一个服务myspringboot,本文记录多个服务之间使用Feign调 ...

  7. SpringCloud系列——Config 配置中心

    前言 Spring Cloud Config为分布式系统中的外部化配置提供了服务器端和客户端支持.有了配置服务器,您就有了一个中心位置来管理跨所有环境的应用程序的外部属性.本文记录实现一个配置中心.客 ...

  8. SpringCloud系列——Zuul 动态路由

    前言 Zuul 是在Spring Cloud Netflix平台上提供动态路由,监控,弹性,安全等边缘服务的框架,是Netflix基于jvm的路由器和服务器端负载均衡器,相当于是设备和 Netflix ...

  9. SpringCloud系列之Nacos+Dubbo+Seata应用篇

    目录 前言 项目版本 项目说明 Nacos服务 Seata服务 订单模块 支付模块 参考资料 系列文章 前言 本文接上篇文章<SpringCloud系列之Nacos+Dubbo应用篇>继续 ...

随机推荐

  1. LCIS最长公共上升子序列

    最长公共上升子序列LCIS,如字面意思,就是在对于两个数列A和B的最长的单调递增的公共子序列. 这道题目是LCS和LIS的综合. 在LIS中,我们通过两重循环枚举当序列以当前位置为结尾时,A序列中当前 ...

  2. 【dfs】bzoj3563 DZY Loves Chinese

    因为我们可以通过把某一行读到末尾来获取真正的K,所以把它和假K异或之后就是之前联通的次数(异或的逆运算为其本身).最后一次的暴力一下. #include<cstdio> #include& ...

  3. xshell与虚拟机无法连接

    遇到xshell无法连接到虚拟机的情况,我大概总结为以下几种情况: 1.宿主机或虚拟机中的防火墙阻止了xshell的访问. 关闭宿主机和虚拟机中的防火墙,linux虚拟机中的防火墙关闭为 :servi ...

  4. Android 架构 2.界面

    其中,最上层的界面,是变化最频繁的一个层面,也是最复杂最容易出问题的一个层面,如果规划不好,很容易做着做着,又乱成一团了.要规划好界面层,至少应该遵循几条基本的原则: 保持规范性:定义好开发规范,包括 ...

  5. 解决ThinkPHP3.2.3框架,PDO驱动查询出来的字段名全是小写的bug

    找到文件:ThinkPHP\Library\Think\Db\Driver.class.php 找到代码: // PDO连接参数 protected $options = array( PDO::AT ...

  6. Matlab与神经网络入门

    第一节.神经网络基本原理  1. 人工神经元( Artificial Neuron )模型  人工神经元是神经网络的基本元素,其原理可以用下图表示: 图1. 人工神经元模型 图中x1~xn是从其他神经 ...

  7. SQLite 使用技巧

    http://blog.csdn.net/beifengdelei/article/details/7166056 SQLite自增ID自段使用方法为 INTEGER PRIMARY KEY AUTO ...

  8. Nginx用作反向代理服务器

    Nginx作为反向代理服务器时转发请求的流程 客户端请求处理 当客户端请求来时,Nginx并不会立刻转发到上游服务器,而是想完整的接收到Nginx所在的服务器, 然后再把缓存的客户端的请求转发到上游服 ...

  9. RedisTemplate SerializationFailedException: Failed to deserialize payload 异常解决

    问题描述: 使用RedisTemplate(spring-data-redis )进行redis操作的封装 , 现有一个incr的key , 当调用incr后返回值一切正常, 当对此key进行get调 ...

  10. ios滚动UIScrollView的setContentOffset方法

    在UIScrollView,setContentOffset方法的功能是跳转到你指定内容的坐标,[self.scroview setContentOffset:CGPointMake(0, 50) a ...