Hystrixのタイムアウト調整と回退戦略

商品サービスのインターフェースが安全認証などの複雑な処理を行い、2秒程度のタイムアウトを必要とする場合があります。

以下に示すのは、商品サービスの制御層です。

package com.inventorysystem.service.controllers;

import com.inventorysystem.service.models.inventory.InventoryItem;
import com.inventorysystem.service.services.InventoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/v1/inventory")
public class InventoryController {
    @Value("${server.port}")
    private String serverPort;

    @Autowired
    private InventoryService inventoryService;

    /**
     * 全ての商品を取得する
     * @return 商品リスト
     */
    @RequestMapping("retrieveAll")
    public Object getAllItems() {
        return inventoryService.getProductList();
    }

    /**
     * 商品IDに基づき商品の詳細を取得する
     * @param id 商品ID
     * @return 商品の詳細情報
     */
    @RequestMapping("retrieveItem")
    public Object getItemDetails(@RequestParam("id") int itemId) {
        //2秒間の待ち時間を設定し、タイムアウトをシミュレート
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        InventoryItem item = inventoryService.getItemById(itemId);
        InventoryItem result = new InventoryItem();
        BeanUtils.copyProperties(item, result);

        result.setName(item.getName() + " from port " + serverPort);
        return result;
    }
}

注文サービが商品サービスのインターフェースを呼び出す際、タイムアウト例外が発生し警告処理がトリガーされます。この問題を解決するには、注文サービの設定ファイルでタイムアウト時間を4秒に調整します。

Hystrixのタイムアウト設定を以下のようにします。

server:
  port: 8781

# サービスディスカバリの設定
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

# このサービスの名前
spring:
  application:
    name: order-service
  redis:
    database: 0
    host: 127.0.0.1
    port: 6379
    timeout: 2000

# ロードバランシング設定
inventory-service:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

# Feignクライアントのタイムアウト設定
feign:
  hystrix:
    enabled: true
  client:
    config:
      default:
        connectTimeout: 4000
        readTimeout: 4000

# Hystrixのタイムアウトを無効化しないように注意
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 4000
  1. ポリシーの調整:

HystrixCommandPropertiesクラスの設定を確認します。

execution.isolation.strategy - 隔離戦略

  • THREAD: テーブループール隔離(デフォルト)
  • SEMAPHORE: セマフォ

セマフォは、インターフェースの同時アクセス数が非常に高い場合(毎秒数千回の呼び出しなど)に適しています。しかし、非ネットワーク呼び出しにのみ使用し、実行時間が短い場合に限ります。

execution.isolation.thread.timeoutInMilliseconds - タイムアウト時間

  • デフォルト値: 1000ミリ秒

execution.timeout.enabled - タイムアウト制限を有効にする

  • 絶対に無効にしないでください

execution.isolation.semaphore.maxConcurrentRequests - セマフォ隔離を使用する場合の最大同時リクエスト数

  • デフォルト値: 10

公式ドキュメントを参照してください: Execution Isolation Strategy Configuration

タグ: Hystrix フィーン タイムアウト管理 微サービス

5月18日 18:24 投稿