一 . OpenFeign介绍
OpenFeign是一种声明式、模板化的HTTP客户端 (仅在Application Client中使用)。 声明式调用是指,就像调用本地方法一样调用远程方法,无需感知操作远程http请求。 使用 OpenFeign 后可以不使用 RestTemplate 来进行调用。
二 . 代码分享
1.在springboot项目中导入相关依赖
导入Feign,Eureka Client和web的依赖包,同时依赖user-common模块
<dependencies>
<!--1.导入EurekaClient的包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--2.导入Feign的包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--web包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--user-common-->
<dependency>
<groupId>cn.itsource.springboot</groupId>
<artifactId>springcloud-user-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
2.配置yml
server:
port: 1040
eureka:
client:
serviceUrl:
defaultZone: http://peer1:1010/eureka/,http://peer2:1011/eureka/,http://peer3:1012/eureka/
instance:
instance-id: pay-server:1040 #实例ID
prefer-ip-address: true #使用ip注册到注册中心
spring:
application:
name: pay-server
3.在启动类加上@EnableFeignClients(value="接口路径")
/`
* 支付的启动类
* @EnableFeignClients :开启Feign支持
*/
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(value="cn.zs.feignclient")
public class PayServerApplication1040
{
public static void main( String[] args )
{
SpringApplication.run(PayServerApplication1040.class);
}
}
4.编写接口
接口中的方法需要跟调用的服务中方法一致
@FeignClient中是被调用的服务的名字
@FeignClient("user-server")
public interface UserFeignClient {
//订单服务来调用这个方法 http://localhost:1020/user/10
// @GetMapping(value = "/user/{id}" )
@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
User getById(@PathVariable("id")Long id);
}
5.控制层
//支付服务的controller
@RestController
public class PayController{
@Autowired
private UserFeignClient userFeignClient;
//浏览器来掉
@RequestMapping("/pay/{id}")
public User getById(@PathVariable("id")Long id){
//使用Feign调用用户服务获取User
return userFeignClient.getById(id);
}
}
6.启动服务后测试--大功告成!
三.Feign的参数配置
3.1.负载均衡配置
Feign已经集成了Ribbon,所以它的负载均衡配置基于Ribbon配置即可,这里使用xml简单配置负载均衡策略如下:
user-server: ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
3.2.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超时时间
3.3.Feign开启日志调试
3.4.配置Feign日志打印内容
有的时候我们需要看到Feign的调用过程中的参数及相应,我们可以对Feign的日志进行配置,Feign支持如下几种日志模式来决定日志记录内容多少:
-
NONE,不记录(DEFAULT)。
-
BASIC,仅记录请求方法和URL以及响应状态代码和执行时间。
-
HEADERS,记录基本信息以及请求和响应标头。
-
FULL,记录请求和响应的标题,正文和元数据。
创建Feign配置类
@Configuration public class FeignConfiguration { @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; //打印Feign的所有日志 } }
3.5.配置日志打印级别(扩展)
配置UserFeignClient的日志打印级别,上面的配置打印Feign的那些内容,下面这个是配置日志框架打印日志的级别,不修改可能打印不出来日志,DEBUG打印日志调试信息。
logging: level: cn.itsource.springboot.feignclient.UserFeignClient: debug
3.6.Feign开启GZIP(扩展)
可以通过开启Feign的数据压缩传输来节省网络开销,但是压缩数据会增加CPU的开销,所以太小的数据没必要压缩,可以通过压缩大小阈值来控制,配置如下:
feign: compression: request: enabled: true min-request-size: 1024 #最小阈值,小于这个不压缩 mime-types: text/xml,application/xml,application/json #压缩哪些类型的数据 response: enabled: true
在Feign中该配置对应的配置类是FeignClientEncodingProperties ,最终效果如下 :