本文是 Spring Cloud 专栏的第七篇文章, 了解前六篇文章内容有助于更好的理解本文:
Spring Cloud 第一篇 | Spring Cloud 前言及其常用组件介绍概览
Spring Cloud 第二篇 | 使用并认识 Eureka 注册中心
Spring Cloud 第三篇 | 搭建高可用 Eureka 注册中心
Spring Cloud 第四篇 | 客户端负载均衡 Ribbon
Spring Cloud 第五篇 | 服务熔断 Hystrix
Spring Cloud 第六篇 | Hystrix 仪表盘监控 Hystrix Dashboard
一, Feign 是什么
Feign 是 Netflix 公司开发的一个声明式的 REST 调用客户端; Ribbon 负载均衡, Hystrⅸ 服务熔断是我们 Spring Cloud 中进行微服务开发非常基础的组件, 在使用的过程中我们也发现它们一般都是同时出现的, 而且配置也都非常相似, 每次开发都有很多相同的代码, 因此 Spring Cloud 基于 Netflix Feign 整合了 Ribbon 和 Hystrix 两个组件, 让我们的开发工作变得更加简单, 就像 Spring boot 是对 Spring+ SpringMVC 的简化, Spring Cloud Feign 对 Ribbon 负载均衡, Hystrⅸ 服务熔断进行简化, 在其基础上进行了进一步的封装, 不仅在配置上大大简化了开发工作, 同时还提供了一种声明式的 web 服务客户端定义方式. 使用方式类似 Dubbo 的使用方式.
二, 使用 Feign 实现消费者
1, 创建消费者服务命名为 (springcloud-service-feign)
2, 添加依赖
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-openfeign</artifactId>
- </dependency>
3, 在启动类上添加注解
@EnableFeignClients
4, 声明服务
定义一个 HelloService 接口, 通过 @FeignClient 注解来指定服务名称进而绑定服务, 然后在通过 Spring MVC 中提供的注解来绑定服务提供者的接口, 如下:
- // 使用 feign 的客户端注解绑定远程的名称, 名称可以是大写, 也可以小写
- @FeignClient(value = "springcloud-service-provider")
- public interface HelloService {
- // 声明一个方法, 这个方法就是远程的服务提供者提供的方法
- @RequestMapping("/provider/hello")
- public String hello();
- }
5, 使用 Controller 中调用服务, 代码如下
- @Autowired
- private HelloService helloService;
- @RequestMapping("/hello")
- public String hello(){
- // 调用声明式接口方法, 实现对远程服务的调用
- return helloService.hello();
- }
6,application.YAML 配置如下
- spring:
- application:
- name: springcloud-service-feign
- server:
- port: 9091
- eureka:
- client:
- service-url:
- defaultZone: http://localhost:8700/eureka
- #客户端每隔 30 秒从 Eureka 服务上更新一次服务信息
- registry-fetch-interval-seconds: 30
- #需要将我的服务注册到 eureka 上
- register-with-eureka: true
- #需要检索服务
- fetch-registry: true
- #心跳检测检测与续约时间
- instance:
- #告诉服务端, 如果我 10s 之内没有给你发心跳, 就代表我故障了, 将我剔除掉, 默认 90s
- #Eureka 服务端在收到最后一次心跳之后等待的时间上限, 单位为秒, 超过则剔除 (客户端告诉服务端按照此规则等待自己)
- lease-expiration-duration-in-seconds: 10
- #每隔 2s 向服务端发送一次心跳, 证明自已依然活着, 默认 30s
- #Eureka 客户端向服务端发送心跳的时间间隔, 单位为秒 (客户端告诉服务端自己会按照该规则)
- lease-renewal-interval-in-seconds: 2
7, 启动测试, 访问地址 http://localhost:9091/feign/hello
三, 使用 Feign 支持的特性
负载均衡:
Spring Cloud 提供了 Ribbon 来实现负载均衡, 使用 Ribbo 直接注入一个 RestTemplate 对象即可, RestTemplate 已经做好了负载均衡的配置在 Spring Cloud 下, 使用 Feign 也是直接可以实现负载均衡的, 定义一个有 @FeignClient 注解的接口, 然后使用 @RequestMappin 注解到方法上映射远程的 REST 服务, 此方法也是做好负责均衡配置的.
服务熔断:
1, 在 application.YAML 文件开启 hystrix 功能
- # 开启 hystrix 熔断机制
- feign:
- hystrix:
- enabled: true
2, 指定熔断回调逻辑
- @FeignClient(value = "springcloud-service-provider", fallback = MyFallback.class)
- @Component
- public class MyFallback implements HelloService {
- @Override
- public String hello() {
- return "远程服务不可用, 暂时采用本地逻辑代替.....";
- }
- }
3, 测试, 让服务提供者超时就行了
如果需要捕获提供者抛出的异常可以用:
- @FeignClient(value = "springcloud-service-provider", fallbackFactory = MyFallbackFactory.class)
- @Component
- public class MyFallbackFactory implements FallbackFactory<HelloService> {
- @Override
- public HelloService create(Throwable throwable) {
- return new HelloService() {
- @Override
- public String hello() {
- return throwable.getMessage();
- }
- };
- }
- }
详细参考案例源码: https://gitee.com/coding-farmer/spirngcloud-learn
?
来源: http://www.bubuko.com/infodetail-3331361.html