Spring Cloud --- feign
发布日期:2021-05-20 01:12:07 浏览次数:21 分类:精选文章

本文共 4101 字,大约阅读时间需要 13 分钟。

Spring Boot Feign 教程:声明式接口,负载均衡与容错处理

一、Feign 声明式客户端接口

1.1 新建项目

  • 创建项目:创建一个Spring Boot的微服务项目sp09-feign,选择Spring Boot Starter Parent作为父依赖。

  • 修改pom.xml,添加必要的依赖项:

    • spring-boot-starter-actuator
    • spring-boot-starter-web
    • sp01-commons(依赖项目自定义组件)
    • spring-cloud-starter-netflix-eureka-client
    • spring-cloud-starter-openfeign
  • 编写application.yml,配置服务发现和Feign:

    spring:
    application:
    name: feign
    server:
    port: 3001
    eureka:
    client:
    service-url:
    defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eura
  • 主程序Sp09FeignApplication

     
  • -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cloud.client.discovery.EnableDiscoveryClient; -import org.springframework.cloud.openfeign.EnableFeignClients;

    @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class Sp09FeignApplication { public static void main(String[] args) { SpringApplication.run(Sp09FeignApplication.class, args); } }

    ### 1.2 Feign 声明式客户端
    #### 定义接口
    通过定义接口,实现与远程服务的调用。例如:
    ```java
    @FeignClient(name = "item-service")
    public interface ItemFeignClient {
    @GetMapping("/{orderId}")
    JsonResult
    getItems(@PathVariable("orderId") String orderId);
    }

    接口配置说明

  • 服务标识:通过@FeignClient(name = "service-id")指定调用目标服务的标识符。
  • 请求路径:使用Spring的注解(如@GetMapping)定义调用路径。
  • 请求参数:通过@PathVariable@RequestParam等注解指定参数。
  • 1.3 Java源文件

    编写接口和实现类:

  • ItemFeignClient

    @FeignClient(name = "item-service")
    public interface ItemFeignClient {
    @GetMapping("/{orderId}")
    JsonResult
    getItems(@PathVariable(value = "orderId") String orderId);
    }
  • UserFeignClient

    @FeignClient(name = "user-service")
    public interface UserFeignClient {
    @GetMapping("/{userId}")
    JsonResult
    getUser(@PathVariable("userId") Integer userId);
    }
  • FeignController

    @RestController
    public class FeignController {
    @Autowired
    private ItemFeignClient itemFeignClient;
    @Autowired
    private UserFeignClient userFeignClient;
    @GetMapping("/item-service/{orderId}")
    public JsonResult
    getItems(@PathVariable("orderId") String orderId) {
    return itemFeignClient.getItems(orderId);
    }
    }
  • 开发总结

    通过以上配置,可以快速开发一套基于Feign的微服务客户端,实现与后台服务的高效对接。Feign的声明式定义极大降低了代码耦合度,使服务消费更加直观和高效。

    二、Feign + Ribbon 负载均衡与重试

    2.1 配置 Ribbon 超时和重试

    application.yml中配置全局和特定服务的Ribbon参数:

    spring:
    application:
    name: feign
    server:
    port: 3001
    eureka:
    client:
    service-url:
    defaultZone: http://eureka1:2001/eureka, http://eura
    ribbon:
    ConnectTimeout: 1000
    ReadTimeout: 1000
    item-service:
    ribbon:
    MaxAutoRetries: 1
    MaxAutoRetriesNextServer: 2
    ConnectTimeout: 1000
    ReadTimeout: 500

    启动与测试

  • 启动服务,使用Postman测试终端接口:
    • http://localhost:5001/item-service/decreaseNumber
    • 发送POST请求,参数格式为[{"id":1,"name":"abc","number":23},{"id":2,"name":"def","number":11}]
  • 三、Feign + Hystrix 降级

    3.1 启用 Hystrix

    application.yml中配置:

    feign:
    hystrix:
    enabled: true

    3.2 Hystrix 降级

  • 制定降级策略:

    • 创建降级实现类,实现远程接口:
      @Component
      public class ItemFeignClientFB implements ItemFeignClient {
      @Override
      public JsonResult
      getItems(String orderId) {
      return JsonResult.failure("获取订单商品失败");
      }
      }
  • 在接口中指定降级类:

    @FeignClient(name = "item-service", fallback = ItemFeignClientFB.class)
    public interface ItemFeignClient {
    // 定义接口方法...
    }
  • 四、Feign + Hystrix 监控与熔断测试

    项目检查

  • 添加Hystrix依赖:

    org.springframework.cloud
    spring-cloud-starter-netflix-hystrix
  • 配置主程序:

    @EnableCircuitBreaker
    @SpringBootApplication
    public class Sp09FeignApplication {
    public static void main(String[] args) {
    SpringApplication.run(Sp09FeignApplication.class, args);
    }
    }
  • 配置actuator暴露Hystrix流程:

    management:
    endpoints:
    web:
    exposure:
    include: hystrix.stream
  • 启动与监控

  • 启动服务,访问开发人员门户:

    • 访问http://localhost:5001/actuator/
  • 查看Hystrix监控端点:

    • URL: http://localhost:5001/actuator/hystrix
  • 熔断测试

    使用ab工具进行高并发测试:

    ab -n 20000 -c 50 http://localhost:5001/item-service/35

    这种情况下,断路器状态为Open,所有请求会直接使用fallback策略,跳过原接口进行容错处理。

    上一篇:RabbitMQ --- 安装及基本概念
    下一篇:Spring Cloud --- Hystrix(容错工具)

    发表评论

    最新留言

    逛到本站,mark一下
    [***.202.152.39]2025年04月17日 20时43分08秒