简单示例

增加feign maven依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

修改主类

//激活erueka中的DiscoveryClient实现
//自动化配置,创建DiscoveryClient接口针对Eureka客户端的EurekaDiscoveryClient实例
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaServerApplication { public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}

通过@FeignClient注解绑定服务

//不区分大小写
@FeignClient("eureka-client")
public interface HelloService
{
@RequestMapping(value = "/hello", method = RequestMethod.GET)
String index();
}

调用服务

RestController
public class ConsumerController
{
@Autowired
HelloService helloService; @RequestMapping(value = "/feign-consumer", method = RequestMethod.GET)
public String helloController()
{
return helloService.index();
}
}

参数绑定

增加User类

public class User
{
private String name;
private Integer age; public User()
{ } public User(String name, Integer age)
{
this.name = name;
this.age = age;
} public String getName()
{
return name;
} public void setName(String name)
{
this.name = name;
} public Integer getAge()
{
return age;
} public void setAge(Integer age)
{
this.age = age;
} @Override
public String toString()
{
return "User{" + "name='" + name + '\'' + ", age=" + age + '}';
}
}

在提供的服务中增加FeignController类和下面的User类

@RestController
public class FeignController
{
@RequestMapping(value = "/hello1", method = RequestMethod.GET)
public String hello(@RequestParam String name)
{
return "Hello " + name;
} @RequestMapping(value = "/hello2", method = RequestMethod.GET)
public User hello(@RequestHeader String name, @RequestHeader Integer age)
{
return new User(name, age);
} @RequestMapping(value = "/hello3", method = RequestMethod.POST)
public String hello(@RequestBody User user)
{
return "Hello " + user.getName() + ", " + user.getAge();
}
}

增加HelloService类

//不区分大小写
@FeignClient("eureka-client")
public interface HelloService
{
@RequestMapping(value = "/hello", method = RequestMethod.GET)
String hello(); @RequestMapping(value = "/hello1", method = RequestMethod.GET)
String hello(@RequestParam("name") String name); @RequestMapping(value = "/hello2", method = RequestMethod.GET)
User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age); @RequestMapping(value = "/hello3", method = RequestMethod.POST)
String hello(@RequestBody User user);

增加ConsumerController类

@RestController
public class ConsumerController
{
@Autowired
HelloService helloService; @RequestMapping(value = "/feign-consumer", method = RequestMethod.GET)
public String helloConsumer()
{
return helloService.hello();
} @RequestMapping(value = "/feign-consumer2", method = RequestMethod.GET)
public String helloConsumer2()
{
StringBuilder sb = new StringBuilder();
sb.append(helloService.hello()).append("\n");
sb.append(helloService.hello("zhangsan")).append("\n");
sb.append(helloService.hello("zhangsan", 20)).append("\n");
sb.append(helloService.hello(new User("zhangsan", 20))).append("\n");
return sb.toString();
}
}

调用http://localhost:9001/feign-consumer2返回

hello world! Hello zhangsan User{name='zhangsan', age=20} Hello zhangsan, 20

feign中ribbon配置

feign中包含ribbon,其客户端负载均衡是通过ribbon实现的

ribbon全局配置

直接使用ribbon.=方式配置参数

ribbon.ConnectTimeout=500
ribbon.ReadTimeout=5000

指定服务配置

<服务名>.ribbon.=

HELLO-SERVICE.ribbon.ConnectTimeout=500
HELLO-SERVICE.ribbon.ReadTime=2000
HELO-SERVICE.ribbon.OkToRetryOnAllOperations=true
HELLO-SERVICE.ribbon.MaxAutoRetriesMextServer=2
HELLO-SERVICE.ribbon.MaxAutoRetries=1

feign中Hystrix配置

feign中包含了Hystrix断路器

全局配置

直接使用前缀hystrix.command.default进行配置,eg:

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000

开启和关闭feign中hystrix断路器功能

feign.hystrix.enabled=true/false

开启和关闭熔断功能

hystrix.command.default.execution.timeout.enabled=true/false

指定客户端配置

  • 构建一个关闭Hystrix的配置
@Configuration
public class DisableHystrixConfiguration
{
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder()
{
return Feign.builder();
}
}
  • 在@FeignClient注解的类中,通过configuration参数引入实现的配置
@FeignClient(name="HELLO-SERVICE", configuration=DisableHystrixConfiguration.class)
public interface HelloService
{
...
}

服务降级

与单独使用Hystrix服务降级有很大区别

  • 创建服务接口的实现类
@Component
public class HelloServiceFallback implements HelloService
{ @Override
public String hello()
{
return "error";
} @Override
public String hello(String name)
{
return "error";
} @Override
public User hello(String name, Integer age)
{
return new User("none", 0);
} @Override
public String hello(User user)
{
return "error";
}
}
  • 在服务接口中通过@FeignClient注解的fallback属性来指定对应服务降级的实现类
//不区分大小写
@FeignClient(value = "eureka-client", configuration = DisableHystrixConfiguration.class, fallback =
HelloServiceFallback.class)
public interface HelloService
{
@RequestMapping(value = "/hello", method = RequestMethod.GET)
String hello(); @RequestMapping(value = "/hello1", method = RequestMethod.GET)
String hello(@RequestParam("name") String name); @RequestMapping(value = "/hello2", method = RequestMethod.GET)
User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age); @RequestMapping(value = "/hello3", method = RequestMethod.POST)
String hello(@RequestBody User user);
}

请求压缩

Feign支持对请求和响应进行GZIP压缩

feign.compression.request.enabled=true
feign.compression.response.enabled=true

自定义请求压缩规则

#开启请求压缩
feign.compression.request.enable=true
#限制压缩请求类型
feign.compression.request.mime-types=text/xml,application/xml,application/json
#限制大小,只有超过这个大小,才会执行压缩,默认值为2048
feign.compression.request.min-request-size=2048

日志配置

Feign在构建背@FeignClient注解修饰的服务客户端是,会为每一个客户端都创建一个feign.Logger实例

配置feign.Logger

logging.level.<被注解接口的完整路径>,eg:

logging.levle.com.example.web.HelloService=DEBUG

创建全局日志级别的Bean

Feign客户端默认的Logger.level对象定义为NONE级别,该级别不会记录任何Feign调用过程中的信息,故添加上面的配置无法实现对DEBUG日志的输出

//激活erueka中的DiscoveryClient实现
//自动化配置,创建DiscoveryClient接口针对Eureka客户端的EurekaDiscoveryClient实例
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaServerApplication { @Bean
Logger.Level feignLoggerLevel()
{
return Logger.Level.FULL;
} public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}

指定客户端的配置类实现

@Configuration
public class FullLogConfiguration
{
@Bean
Logger.Level feignLoggerLevel()
{
return Logger.Level.FULL;
}
}
  • 在@FeignClient注解中的属性configuration中使用
@FeignClient(name="HELLO-SERVICE", configuration=FullLogConfiguration.class)
public interface HelloService
{
...
}

日志级别分裂

  • NONE : 不记录任何信息
  • BASIC :仅记录请求方法、URL以及响应状态码和执行时间
  • HEADERS: 除了记录BASIC级别的信息之外,还会记录请求和响应的头信息
  • FULL:记录所有请求与响应的明细,包括头信息、请求体、元数据等

spring cloud学习--feign的更多相关文章

  1. spring cloud学习笔记三 Feign与Ribbon负载均衡的区别

    一.Feign的介绍 Feign一般比较书面的解释是:Feign是一个声明式的WebService客户端,使用Feign编写的WebService客户端更加简单,他的使用方法是定义一个接口,然后在上线 ...

  2. spring cloud 使用feign 遇到问题

    spring cloud 使用feign 项目的搭建 在这里就不写了,本文主要讲解在使用过程中遇到的问题以及解决办法 1:示例 @RequestMapping(value = "/gener ...

  3. Spring Cloud 整合 Feign 的原理

    前言 在 上篇 介绍了 Feign 的核心实现原理,在文末也提到了会再介绍其和 Spring Cloud 的整合原理,Spring 具有很强的扩展性,会把一些常用的解决方案通过 starter 的方式 ...

  4. spring cloud 学习(9) - turbine stream无法在eureka注册的解决办法

    turbine是啥就不多解释了,初次接触的可以移步spring cloud 学习(4) - hystrix 服务熔断处理 拉到最后看一下,turbine stream默认情况下启动成功后,eureka ...

  5. spring cloud(四) feign

    spring cloud 使用feign进行服务间调用 1. 新建boot工程 pom引入依赖 <dependency> <groupId>org.springframewor ...

  6. Spring Cloud学习(一):Eureka服务注册与发现

    1.Eureka是什么 Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的. Eureka ...

  7. spring cloud 学习资料

    spring cloud 学习资料 网址 拜托!面试请不要再问我Spring Cloud底层原理 https://mp.weixin.qq.com/s/ZH-3JK90mhnJPfdsYH2yDA

  8. Spring Boot和Spring Cloud学习资源推荐

    Spring Boot和Spring Cloud学习资源推荐   比较好的学习资源,分享一下. 1.Spring Boot官方文档:http://projects.spring.io/spring-b ...

  9. spring cloud关于feign client的调用对象列表参数、设置header参数、多环境动态参数试配

    spring cloud关于feign client的调用 1.有些场景接口参数需要传对象列表参数 2.有些场景接口设置设置权限等约定header参数 3.有些场景虽然用的是feign调用,但并不会走 ...

随机推荐

  1. 2019牛客暑期多校训练营(第一场) - E - ABBA - 贪心 - dp - 组合

    https://ac.nowcoder.com/acm/contest/881/E 从dp的角度来看是比较正常的.无后效性来源于前面只要的合法的方案分配,那么对后面造成的影响就只有A,B的数目. 从贪 ...

  2. Windows程序设计--(四)文本输出

    4.1 绘制和重绘 4.1.2 有效矩阵和无效矩阵 在擦除对话框之后,需要重画的被对话框遮住的矩形区域,这个区域称为「无效区域」或「更新区域」.正是显示区域内无效区域的存在,才会让Windows将一个 ...

  3. javascript:变量声明&&赋值的提升和函数声明&&定义的提升在不同情况下的表现

    console.log(a); //undefined console.log(show); //函数的定义 show();         //aaa123 var a = 1; function ...

  4. Codeforces 1208F Bits And Pieces 位运算 + 贪心 + dp

    题意:给你一个序列a, 问a[i] ^ (a[j] & a[k])的最大值,其中i < j < k. 思路:我们考虑对于每个a[i]求出它的最优解.因为是异或运算,所以我们从高位向 ...

  5. BZOJ3227 [sdoi2008]红黑树

    贪心什么的太神仙了( 老老实实dp于是就是沙茶题了 f[i][d][0/1]表示i个节点bh为d当前节点颜色白/黑[好好读题是真.. 转移一下然后就可以打表了( 由于我们发现这玩意很好卡有很好的性质( ...

  6. hdu 5964:平行四边形 【计算几何】

    打重现赛时,一点思路也没有,然后又看到这题AC数那么少,就直接放弃了.今天重新看了看,借鉴了下别人的,发现此题应该算是一道可解题. 看上去,这题的ans是同时有两个点作为自变量的函数(然而n^2复杂度 ...

  7. 【leetcode】1022. Smallest Integer Divisible by K

    题目如下: Given a positive integer K, you need find the smallest positive integer N such that N is divis ...

  8. 【leetcode】472. Concatenated Words

    题目如下: Given a list of words (without duplicates), please write a program that returns all concatenat ...

  9. wnmp的配置

    第一部分:准备工作.(系统:Windows 8.1) 1.首先是下载软件. NGINX-1.3.8官网下载:http://nginx.org/en/download.html PHP5.4.8版本下载 ...

  10. Yii2 $app总结

    //验证登陆 Yii::$app->user->isGuest; //当前user的id Yii::$app->user->id; //当前controller的名称 Yii: ...