SpringCloud系列文章列表
0. SpringCloud实战专栏介绍准备
1. SpringCloud父工程搭建
2. 服务注册中心之Eureka(单机+集群+Ribbon调用)
3. 服务注册中心之Zookeeper
4. 服务注册中心之Consul
5. eureka、zookeeper和consul三种注册中心之间的区别
6. 负载均衡服务调用之Ribbon
7. 服务调用之OpenFeign
8. Hystrix断路器全面实战总结
9. SpringCloud Gateway网关
10. SpringCloud Config配置中心
11. SpringCloud Bus消息总线
12. SpringCloud Stream消息驱动
13. SpringCloud Sleuth分布式请求链路追踪
目录
1 OpenFeign介绍
官方文档:
https://spring.io/projects/spring-cloud-openfeign
https://docs.spring.io/spring-cloud-openfeign/docs/2.2.4.RELEASE/reference/html/
1.1 简介
Feign是一个声明式WebService客户端。使用Feign能让编写Web Service客户端更加简单。
它的使用方法是定义一个服务接口然后在上面添加注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。
1.2 功能
前面在使用Ribbon+RestTemplate时,利用RestTemplate对http请求的封装处理,形成了一套模版化的调用方法。
但是在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。
所以,Feign在此基础上做了进一步封装,由他来帮助我们定义和实现依赖服务接口的定义。在Feign的实现,我们只需创建一个接口并使用注解的方式来配置它(以前是Dao接口上面标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解即可),即可完成对服务提供方的接口绑定,简化了使用Spring cloud Ribbon时,自动封装服务调用客户端的开发量。
1.3 Feign集成了Ribbon
利用Ribbon维护了Payment的服务列表信息,并且通过轮询实现了客户端的负载均衡。而与Ribbon不同的是,通过feign只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用。
1.4 Feign和OpenFeign的区别
2 OpenFeign实战
接口+注解,微服务调用接口+@FeignClient
2.1 基本用法
注册中心 cloud-eureka-server7001
服务提供者 cloud-provider-payment8001 和 cloud-provider-payment8002
以上项目还用之前的;可以参考第二章节 2.服务注册中心之Eureka
新增消费者项目 cloud-consumeropenfeign-order80
- pom.xml
新增依赖 spring-cloud-starter-openfeign
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- application.yml
server:
port: 80
spring:
application:
name: openfeign-order80
eureka:
client:
register-with-eureka: false
fetch-registry: true
service-url:
#单机
defaultZone: http://localhost:7001/eureka
#defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
- 启动类 新增注解 @EnableFeignClients
@SpringBootApplication
@EnableFeignClients
public class OpenFeignOrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OpenFeignOrderMain80.class, args);
}
}
- Feign的业务类(接口+@FeignClient)
@FeignClient("PROVIDER-PAYMENT") //服务名称
public interface PaymentFeignClient {
@GetMapping("/payment/hello")
String getPaymentById();
}
注意:1、方法名和参数与服务提供者里面的controller层一样
2、@FeignClient后面的服务名称要正确,可以去eureka里面找
- 控制层controller,便于测试
注入feign的业务类,调用其中的方法。
@RestController
public class PaymentController {
@Autowired
PaymentFeignClient paymentFeignClient;
@GetMapping("/consumer/hello")
public String getPaymentById() {
return paymentFeignClient.getPaymentById();
}
}
分别启动cloud-eureka-server7001、cloud-provider-payment8001、cloud-provider-payment8002、cloud-consumeropenfeign-order80
浏览器访问:http://localhost/consumer/hello
服务调用成功;
2.2 超时控制
OpenFeign默认等待一秒钟,超时会报错;
2.2.1 演示超时案例
修改cloud-provider-payment8001
controller层新增方法:
@GetMapping("/feign/timeout")
public String paymentFeignTimeOut(){
try {
TimeUnit.SECONDS.sleep(3); //休眠三秒
} catch (InterruptedException e) {
e.printStackTrace();
}
return "feign timeout:" + port;
}
修改消费者cloud-consumeropenfeign-order80
PaymentFeignClient.java中新增方法
@GetMapping("/payment/feign/timeout")
String paymentFeignTimeOut();
controller层新增测试方法
@GetMapping("/consumer/feign/timeout")
public String paymentFeignTimeOut(){
return paymentFeignClient.paymentFeignTimeOut();
}
分别启动cloud-eureka-server7001、cloud-provider-payment8001、cloud-consumeropenfeign-order80
浏览器访问 http://localhost/consumer/feign/timeout
如下图,调用超时了
2.2.2 开启超时配置
默认Feign客户端只等待一秒钟,但是服务端处理需要超过1秒钟,导致Feign客户端不想等待了,直接返回报错。为了避免这种情况,我们需要设置Feign客户端的超时控制;
修改cloud-consumeropenfeign-order80 的 application.yml文件
ribbon:
ReadTimeout : 5000 #响应时间,单位秒
ConnectTimeout : 5000 #连接时间,单位秒
重启cloud-consumeropenfeign-order80,
浏览器再次访问http://localhost/consumer/feign/timeout
不报错,调用成功了;
明明是openFeign,为什么配置的是ribbon呢???
查看maven依赖可以发现,openFeign依赖ribbon;
2.3 日志打印功能
Feign提供了日志打印功能,我们可以通过配置来调整日志级别,从而了解Feign中Http请求的细节。
说白了就是对Feign接口的调用情况进行监控和输出
2.3.1 日志级别
2.3.2 配置日志
- 配置类
新增日志级别配置类 FeignLogConfig.java
@Configuration
public class FeignLogConfig {
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
- YML中开启
logging:
level:
com.chouxiaozi.feignclient.PaymentFeignClient: debug
重启cloud-consumeropenfeign-order80,再次访问接口,查看控制台如下:
点赞+评论+关注
本文源码地址: https://gitee.com/shuaidawang/SpringCloudDemo.git
有错误的地方欢迎指正!可以加入qq交流群: 700637673