SpringCloud学习笔记(10)----Spring Cloud Netflix之声明式 REST客户端 -Feign的高级特性
1. Feign的默认配置
Feign 的默认配置 Spring Cloud Netflix 提供的默认实现类:FeignClientsConfiguration
解码器:Decoder feignDecoder: ResponseEntityDecoder (which wraps a SpringDecoder)
编码器:Encoder feignEncoder: SpringEncoder
日志框架:Logger feignLogger: Slf4jLogger
契约:Contract feignContract: SpringMvcContract
生成器:Feign.Builder feignBuilder: HystrixFeign.Builder
说明:
解码器的作用:将 HTTP 响应数据反序列化为 Java 对象
编码器的作用:将方法签名中方法参数对象序列化为请求参数放到 HTTP 请求中
2. 自定义配置
import org.springframework.context.annotation.Bean; import feign.Contract;
import feign.Logger; //@Configuration
public class RcFeignConfiguration {
@Bean
public Logger.Level feignLoggerLevel() {
return feign.Logger.Level.FULL;
} /*@Bean
public Contract feignContract() {
return new feign.Contract.Default();
}*/
}
配置@ FeignClient注解
@FeignClient(value = "spring-cloud-provider", configuration = RcFeignConfiguration.class)
说明:若配置文件被 SpringContext 扫描,则会被@ FeignClien 共用,会覆盖。
3. 日志配置
# 日志配置,默认是不打印任何的日志
logging.level.com.drunck.education.feign.IUserBiz=debug
需要在配置类里面添加以下代码:
@Bean
public Logger.Level feignLoggerLevel() {
return feign.Logger.Level.FULL;
}
说明:四种级别
NONE:默认,不打印任何日志
BASIC:打印请求方法和 URL,和请求返回状态码和执行时间。
HEADERS:打印请求和返回的头部信息。
FULL:打印以上的全部信息。
4. 契约配置
在配置类里面添加以下代码
@Bean
public Contract feignContract() {
return new feign.Contract.Default();
}
修改 IuserBiz 类
@RequestLine("GET /api/user/{id}")
String view1(@Param(value = "id") int id);
5. 支持压缩
# 开启压缩
feign.compression.request.enabled=true
feign.compression.response.enabled=true
# 更多配置
feign.compression.request.mime-types=text/xml,application/xml,application/json
feign.compression.request.min-request-size=2048
6. URL属性
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; /**
* @author
*/
@FeignClient(value = "baidu", url = "www.baidu.com")
public interface IdrunckBiz {
@RequestMapping(value = "/{url}", method = RequestMethod.GET)
String get(@PathVariable(name = "url") String url); }
说明:定义了 url 之后,vaule 为必选值,这时的 value 只是一个标识。
7. 支持继承
1、定义一个普通接口
public interface UserService {
@RequestMapping(method = RequestMethod.GET, value = "/api/user/find/{id}")
User find(@PathVariable(value = "id") int id);
}
2 、实现接口
import java.util.Date; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* @author*/
@RestController
public class FeignApiUserController implements UserService { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); @Override public User find(@PathVariable int id) { User user = new User(); user.setId(id); user.setName("张三"); user.setCreateTime(new Date()); logger.info("请求接口返回:{}", user); return user; }
}
说明:springmvc 里面不支持方法参数映射的继承
3、继承
public interface IUserBiz extends UserService{
// …
}
SpringCloud学习笔记(10)----Spring Cloud Netflix之声明式 REST客户端 -Feign的高级特性的更多相关文章
- SpringCloud学习笔记(9)----Spring Cloud Netflix之声明式 REST客户端 -Feign的使用
1. 什么是Feign? Feign是一种声明式.模板化的HTTP客户端,在SpringCloud中使用Feign.可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到 ...
- 3.【Spring Cloud Alibaba】声明式HTTP客户端-Feign
使用Feign实现远程HTTP调用 什么是Feign Feign是Netflix开源的声明式HTTP客户端 GitHub地址:https://github.com/openfeign/feign 实现 ...
- Spring 源码学习笔记10——Spring AOP
Spring 源码学习笔记10--Spring AOP 参考书籍<Spring技术内幕>Spring AOP的实现章节 书有点老,但是里面一些概念还是总结比较到位 源码基于Spring-a ...
- Spring Cloud 入门教程(六): 用声明式REST客户端Feign调用远端HTTP服务
首先简单解释一下什么是声明式实现? 要做一件事, 需要知道三个要素,where, what, how.即在哪里( where)用什么办法(how)做什么(what).什么时候做(when)我们纳入ho ...
- spring cloud 声明式rest客户端feign调用远程http服务
在Spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.Feign就是Spring Cloud提供的一种声明式R ...
- springCloud学习-消息总线(Spring Cloud Bus)
1.简介 Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现 ...
- Spring Cloud(三):声明式调用
声明式服务调用 前面在使用spring cloud时,通常都会利用它对RestTemplate的请求拦截来实现对依赖服务的接口调用,RestTemplate实现了对http的请求封装处理,形成了一套模 ...
- Spring Cloud探路(三)REST 客户端Feign
Declarative REST Client: Feign Feign is a declarative web service client. It makes writing web servi ...
- Spring4.0学习笔记(10) —— Spring AOP
个人理解: Spring AOP 与Struts 的 Interceptor 拦截器 有着一样的实现原理,即通过动态代理的方式,将目标对象与执行对象结合起来,降低代码之间的耦合度,主要运用了Proxy ...
随机推荐
- Ubuntu 16.04安装Caffe的记录及FCN官方代码的配置
相关内容搜集自官方文档与网络,既无创新性,也不求甚解,我也不了解Caffe,仅仅搭上之后做个记录,方便以后重装 安装依赖项sudo apt-get install libprotobuf-dev li ...
- bootstrap-导航条
默认样式的导航条 导航条是在您的应用或网站中作为导航页头的响应式基础组件.它们在移动设备上可以折叠(并且可开可关),且在视口(viewport)宽度增加时逐渐变为水平展开模式. 两端对齐的导航条导航链 ...
- MobilNnet
一.参数数量和理论计算量 1.定义 参数数量(params):关系到模型大小,单位通常为M,通常参数用 float32 表示,也就是每个参数占4个字节,所以模型大小是参数数量的 4 倍 理论计算量(F ...
- 进程线程之pid,tid
Linux中,每个进程有一个pid,类型pid_t,由getpid()取得.Linux下的POSIX线程也有一个id,类型pthread_t,由pthread_self()取得,该id由线程维护,其i ...
- Consider defining a bean of type 'XX.XX.XX.XX.mapper.XXMapper' in your configuration.
今天构建一个springboot 项目,采用mybatis+mysql 然后就出现了这种错误....浪费我半天时间 Description: Field loginLogMapper in com.g ...
- ansible 连通测试
[root@ftp:/root] > ansible ansible01 -m ping ansible01 | UNREACHABLE! => { "changed" ...
- VUE:过渡&动画
VUE:过渡&动画 vue动画的理解 1)操作css的 trasition 或 animation 2)vue会给目标元素添加/移除特定的class 3)过渡的相关类名 xxx-enter-a ...
- WampServer更改或重置数据库密码
WampServer安装后密码是空的, 修改一般有两种方式: 一是通过phpMyAdmin直接修改: 二是使用WAMP的MySql控制台修改. 第一种: ①在phpMyAdmin界面中点击[用户],将 ...
- js获得 json对象的个数(长度)
function getJsonObjLength(jsonObj) { var Length = 0; for (var item in jsonObj) { Length++; } return ...
- Linux下的进程环境
僵尸进程.孤儿进程.守护进程.进程组.会话.前台进程组.后台进程组 1,僵尸进程 子进程结束,父进程没有明确的答复操作系统内核:已收到子进程结束的消息.此时操作系统内核会一直保存该子进程的部分PCB信 ...