点击下载教程项目代码: netflix_hystrix_demo.zip
netflix_hystrix_demo.zip
Feign 是一个声明式的 Web Service 客户端。它使得编写 Web 服务客户端变得更加容易。通过 Feign,你可以使用简单的接口定义和注解来调用 RESTful 服务,它会自动处理 HTTP 请求的构建和发送等操作。
如果要了解 Feign 更多知识,请点击学习“Spring Cloud OpenFeign” 或“Netflix Feign”
以下是基于 Spring Cloud 环境,使用 Hystrix 和 Feign 实现降级的示例,详细步骤如下:
在 pom.xml 中添加 OpenFeign 的依赖,如下:
<!-- Feign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
在 Spring Boot 应用程序启动类上添加 @EnableFeignClients 注解,开启 Feign 功能。如下:
package com.hxstrive.hystrix_demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * 入库类
 * @author hxstrive.com
 */
@RestController
@SpringBootApplication
// 启用断路器
@EnableCircuitBreaker
// 开启 Hystrix Dashboard 的功能
@EnableHystrixDashboard
// 开启 Feign
@EnableFeignClients
public class HystrixDemoApplication {
    //...
}创建一个 Feign 接口来定义对远程服务的调用方法,代码如下:
package com.hxstrive.hystrix_demo.feign;
import com.hxstrive.hystrix_demo.dto.CommonReturn;
import com.hxstrive.hystrix_demo.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Component
@FeignClient(name = "SERVICE-DEMO", fallback = UserServiceFallback.class)
public interface UserServiceFeign {
    @GetMapping("/user/getUserById")
    CommonReturn<User> getUserById(@RequestParam("id") Long id);
}在上述代码中,@FeignClient注解用于指定要调用的服务名称为SERVICE-DEMO,并通过fallback属性指定了降级处理类 UserServiceFallback。
创建降级处理类 UserServiceFallback,该类需要实现 UserServiceFeign 接口,并实现接口中的方法来提供降级逻辑,代码如下:
package com.hxstrive.hystrix_demo.feign;
import com.hxstrive.hystrix_demo.dto.CommonReturn;
import com.hxstrive.hystrix_demo.entity.User;
import org.springframework.stereotype.Component;
@Component
public class UserServiceFallback implements UserServiceFeign {
    @Override
    public CommonReturn<User> getUserById(Long id) {
        return CommonReturn.fail("调用失败,触发 fallback");
    }
}在这个降级处理类中,当远程服务调用失败时,getUserById 方法会返回一个预设的降级数据。
在 Spring Boot 的配置文件application.yml中,需要开启 Feign 对 Hystrix 的支持,配置如下:
feign: hystrix: # 启动熔断降级 enabled: true
在需要调用远程服务的地方,注入 UserServiceFeign 并使用它来调用远程服务的接口,代码如下:
package com.hxstrive.hystrix_demo.controller;
import com.hxstrive.hystrix_demo.dto.CommonReturn;
import com.hxstrive.hystrix_demo.entity.User;
import com.hxstrive.hystrix_demo.feign.UserServiceFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
 * Hystrix Feign 降级
 * @author hxstrive.com
 */
@RestController
@RequestMapping("/demo7")
public class Demo7Controller {
    @Autowired
    private UserServiceFeign userServiceFeign;
    @GetMapping("/getUserById")
    public CommonReturn<User> getUserById(@RequestParam Long id) {
        return userServiceFeign.getUserById(id);
    }
}当远程服务SERVICE-DEMO正常运行时,getUserById 方法会正常调用远程服务并返回结果,如下图:

当远程服务出现故障或不可用时,会自动触发降级逻辑,返回“调用失败,触发 fallback”降级数据,从而避免了服务调用失败对系统的影响,如下图:

