首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Spring Boot 整合 Hystrix 實現熔斷降級:核心配置、命令模式與執行流程詳解

     編輯:Java 綜合教學

Spring Boot 整合 Hystrix 實現熔斷降級

Hystrix 是 Netflix 開源的延遲與容錯庫,提供**熔斷**、**降級**、**隔離**等機制,防止單一服務失敗導致雪崩。本文記錄 Spring Boot 1.5.x 整合 Hystrix 核心步驟。

1. 依賴引入

<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-core</artifactId>
    <version>1.5.2</version>
</dependency>

2. 配置屬性

# application.properties
hystrix.timeoutInMillions = 3000

對應配置類:

@Component
@ConfigurationProperties(prefix = "hystrix")
@Getter @Setter
public class HyStrixProperties {
    private int timeoutInMillions;
}

3. 核心命令類

繼承 HystrixCommand<Response>,實作 run()(正常邏輯)與 getFallback()(降級邏輯)。

public class MyHystrixCommand extends HystrixCommand<Response> {
    private static final Logger LOGGER = LoggerFactory.getLogger(MyHystrixCommand.class);
    private String url;

    public MyHystrixCommand(Setter setter, String url) {
        super(setter);
        this.url = url;
    }

    @Override
    protected Response run() throws Exception {
        LOGGER.info("服務正被調用,線程:{}", Thread.currentThread().getName());
        Request request = new Request.Builder().url(url).build();
        return new OkHttpClient().newCall(request).execute();
    }

    @Override
    public Response getFallback() {
        LOGGER.error("服務調用失敗");
        return null; // 或返回預設回應
    }
}

4. 工具類封裝

組裝 Setter(群組鍵、命令鍵、超時屬性),提供同步/異步執行入口。

@Component
public class HystrixUtil {
    @Autowired private HyStrixProperties hp;

    public Response execute(String serviceName, String methodName, String url)
            throws InterruptedException, ExecutionException {
        Setter setter = Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(serviceName))
                .andCommandKey(HystrixCommandKey.Factory.asKey(methodName))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withExecutionTimeoutInMilliseconds(hp.getTimeoutInMillions()));
        return new MyHystrixCommand(setter, url).execute(); // 同步
        // 異步:Future<Response> f = new MyHystrixCommand(setter, url).queue(); return f.get();
    }
}

5. 隔離層級說明

  • HystrixCommandGroupKey:服務級,同群組共用線程池(未配置 ThreadPoolKey 時)。
  • HystrixCommandKey:方法級,區分同服務不同介面。
  • ThreadPoolKey:線程池級,極少用,需為特定方法獨立分配線程池時使用。

6. Controller 整合

@RestController
@RequestMapping("/hystrix")
public class HystrixController {
    @Value("${service.hotel.url}") private String HOTEL_URL;
    @Value("${service.hotel.name}") private String hotelServiceName;
    @Value("${service.hotel.method.getHotelInfo}") private String methodName;
    @Autowired private HystrixUtil hystrixUtil;

    @GetMapping("/firstHystrix")
    public String getHotelInfo(@RequestParam int id, @RequestParam String name) {
        String url = String.format(HOTEL_URL, id, name);
        try {
            Response resp = hystrixUtil.execute(hotelServiceName, methodName, url);
            if (resp != null) return resp.body().string();
        } catch (Exception e) { e.printStackTrace(); }
        finally { /* 關閉 response.body() */ }
        return "獲取酒店信息失敗";
    }
}

7. 被調用服務範例

@RestController
@RequestMapping("/hotel")
public class HotelController {
    @GetMapping("/getHotelInfo")
    public Hotel getHotelInfo(@RequestParam int id, @RequestParam String name) {
        // 模擬延遲測試熔斷:TimeUnit.MILLISECONDS.sleep(2000);
        return new Hotel(id, name);
    }
}

參考資源

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved