1.什么是Feign
1.1.为什么要使用Feign
使用Ribbon作为客户端负载均衡完成了订单服务和用户服务的通信,其实我们可以发现,当我们通过RestTemplate调用其它服务时,所需要的参数须在请求的URL中进行拼接,如果参数少的话或许我们还可以忍受,一旦有多个参数的话,这时拼接请求字符串就会效率低下,并且显得好傻
。而Feign的服务调用方式对于程序员来说更为友好,它基于Ribbon进行了封装,把一些负责的url和参数处理细节屏蔽起来,我们只需要简单编写Fiegn的客户端接口就可以像调用本地service去调用远程微服务。
1.2.什么是Feign
Feign是一个声明式的http客户端,使用Feign可以实现声明式REST调用,它的目的就是让Web Service调用更加简单。Feign整合了Ribbon和SpringMvc注解,这让Feign的客户端接口看起来就像一个Controller。Feign提供了HTTP请求的模板,通过编写简单的接口和插入注解,就可以定义好HTTP请求的参数、格式、地址等信息
。而Feign则会完全代理HTTP请求,我们只需要像调用方法一样调用它就可以完成服务请求及相关处理。同时Feign整合了Hystrix,可以很容易的实现服务熔断和降级(关于Hystrix我们后面再讲)。
2.OpenFeign使用
1. 创建工程- springcloud-netflix-service-pay
支付服务 通过浏览器调用支付服务,支付服务再调用用户服务
2. 导入依赖
导入OpenFeign,Eureka Client和web的依赖包,同时依赖user-common模块 ,具体的 pom.xml如下
<dependencies>
<!--eureka-client eureka客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- user -->
<dependency>
<groupId>com.xd</groupId>
<artifactId>springcloud-netflix-pojo-user</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
3. 配置
启动类 主配置类增加@EnableFeignClients注解
@EnableFeignClients // 开启feign支持
@SpringBootApplication
public class PayApplication {
public static void main(String[] args) {
SpringApplication.run(PayApplication.class);
}
}
yml基础配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:10010/eureka/
instance:
instance-id: pay-server
spring:
application:
name: pay-server
server:
port: 10040
logging: #配置日志等级 默认info
level:
com.xd: debug
feign:
hystrix:
enabled: true #开启熔断支持
4. 编写Feign的客户端接口
Feign的客户端接口是用来调用微服务的,我这里就编写了一个用来调用用户服务的客户端接口
// 配置服务名 - 通过服务名调用用户服务下面的 几个实例
@FeignClient(value = "user-server")
public interface UserFeignClient {
// 需要和用户服务controller方法一致
@GetMapping("/user/{id}")
User getUserById(@PathVariable("id") Long id);
}
5.PayController
controller注入UserFeignClient接口,通过该接口调用下面对应方法
@RestController
public class PayController {
@Autowired
private UserFeignClient userFeignClient;
@GetMapping("/pay/{id}")
public User getUserById(@PathVariable("id")Long id){
return userFeignClient.getUserById(id);
}
}
6. 测试
浏览器访问PayController http://localhost:10040/pay/1 支付服务会负载均衡调用用户服务
3.Feign的参数配置
负载均衡配置
Feign已经集成了Ribbon,所以它的负载均衡配置基于Ribbon配置即可,这里使用xml简单配置负载均衡策略如下
user-server:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
Feign的超时配置:
如果在服务调用时出现了 “feign.RetryableException : Read timed out...”错误日志,说明Ribbon处理超时 ,我们可以配置Ribbon的超时时间:
ribbon:
ConnectTimeout: 3000
ReadTimeout: 6000
如果服务调用出现“com.netflix.hystrix.exception.HystrixRuntimeException:.. timed - out and no fallback available” 错误日志,是因为Hystrix超时,默认Feign集成了Hystrix,但是高版本是关闭了Hystrix,我们可以配置Hystrix超时时间:
feign:
hystrix:
enabled: true #开启熔断支持
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 6000 #hystrix超时时间
配置Feign日志打印内容
配置类 + yml 配置日志级别
@Configuration
public class FeignConfiguration {
@Bean
Logger.Level feignLoggerLevel() {
/*
- NONE,不记录(DEFAULT)。
- BASIC,仅记录请求方法和URL以及响应状态代码和执行时间。
- HEADERS,记录基本信息以及请求和响应标头。
- FULL,记录请求和响应的标题,正文和元数据。
*/
return Logger.Level.FULL; //打印Feign的所有日志
}
}
logging: #配置日志等级 默认info
level:
com.xd: debug
Feign开启GZIP
feign:
compression:
request:
enabled: true
min-request-size: 1024 #最小阈值,小于这个不压缩
mime-types: text/xml,application/xml,application/json #压缩哪些类型的数据
response:
enabled: true