成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Sentinel 實戰:如何實現分布式系統的高效流量管控?

開發 前端
我們將展示如何使用 Sentinel 配置流控規則、實現熔斷降級和跨服務流量管控,同時包含完整的代碼和配置。

實現分布式系統的高效流量管控是微服務架構中非常重要的一環。下面以 Spring Boot 和 Spring Cloud 框架為例,通過阿里巴巴的 Sentinel 組件實現多服務環境下的流量監控與管控。我們將展示如何使用 Sentinel 配置流控規則、實現熔斷降級和跨服務流量管控,同時包含完整的代碼和配置。

系統設計目標

  1. 模擬一個微服務架構:訂單服務(order-service)和庫存服務(inventory-service)。
  2. Sentinel 的核心功能:
  • 在每個服務上設置流控規則,限制流量請求。
  • 實現跨服務調用時的流量監控。
  • 配置熔斷降級規則,確保系統的穩定性。
  1. 提供完整的整合代碼,包括服務配置、Sentinel 規則代碼和流量控制邏輯。

系統架構圖

用戶請求 -> Gateway(網關) -> Order-Service -> Inventory-Service

流控目標:

  1. 限制 order-service 的并發訪問數為 10。
  2. 對 inventory-service 的 QPS 設置為 5。
  3. 配置熔斷規則,若 inventory-service 超時率超過 50%,熔斷 10 秒。
  4. 在 Sentinel Dashboard 上實時監控流量數據。

準備工作

依賴引入

在所有服務中需要添加以下依賴。

pom.xml 公共依賴

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Sentinel Dashboard 下載

下載并啟動 Sentinel 控制臺(建議使用最新版本):

java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -jar sentinel-dashboard.jar

核心服務實現

1、Order-Service(訂單服務)

訂單服務負責接收用戶訂單請求,并調用庫存服務扣減庫存。

application.yml 配置

server:
  port: 8081

spring:
  application:
    name: order-service
  cloud:
    sentinel:
      transport:
        dashboard: localhost:8080 # 指定 Sentinel Dashboard 地址
        port: 8719 # 服務與 Sentinel Dashboard 的通信端口

management:
  endpoints:
    web:
      exposure:
        include: '*'

主啟動類

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

控制層

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/create")
    @SentinelResource(value = "createOrder", blockHandler = "handleOrderBlock")
    public String createOrder(@RequestParam("productId") String productId) {
        // 調用庫存服務扣減庫存
        String result = restTemplate.getForObject("http://inventory-service/inventory/deduct?productId=" + productId, String.class);
        return "Order created successfully! Result from inventory: " + result;
    }

    // 流控處理邏輯
    public String handleOrderBlock(String productId, BlockException ex) {
        return "Order service is busy. Please try again later.";
    }
}

流控規則配置

@Configuration
public class SentinelConfig {

    @PostConstruct
    public void initFlowRules() {
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource("createOrder"); // 資源名稱
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS); // 基于 QPS 的流控
        rule.setCount(10); // 最大 QPS 為 10
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }
}

RestTemplate 配置

@Configuration
public class RestTemplateConfig {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

2、Inventory-Service(庫存服務)

庫存服務負責處理庫存扣減請求。

application.yml 配置

server:
  port: 8082

spring:
  application:
    name: inventory-service
  cloud:
    sentinel:
      transport:
        dashboard: localhost:8080
        port: 8720

management:
  endpoints:
    web:
      exposure:
        include: '*'

主啟動類

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

控制層

@RestController
@RequestMapping("/inventory")
public class InventoryController {

    @GetMapping("/deduct")
    @SentinelResource(value = "deductInventory", blockHandler = "handleDeductBlock", fallback = "fallbackForDeduct")
    public String deduct(@RequestParam("productId") String productId) {
        // 模擬業務處理
        if ("timeout".equals(productId)) {
            try {
                Thread.sleep(2000); // 模擬超時
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return "Inventory deducted successfully for productId: " + productId;
    }

    // 流控處理邏輯
    public String handleDeductBlock(String productId, BlockException ex) {
        return "Inventory service is busy. Please try again later.";
    }

    // Fallback 邏輯
    public String fallbackForDeduct(String productId, Throwable throwable) {
        return "Fallback: Unable to process inventory deduction.";
    }
}

熔斷規則配置

@Configuration
public class SentinelConfig {

    @PostConstruct
    public void initDegradeRules() {
        List<DegradeRule> rules = new ArrayList<>();
        DegradeRule rule = new DegradeRule();
        rule.setResource("deductInventory");
        rule.setGrade(RuleConstant.DEGRADE_GRADE_RT); // 基于響應時間熔斷
        rule.setCount(1000); // 超時閾值(ms)
        rule.setTimeWindow(10); // 熔斷持續時間(秒)
        rule.setMinRequestAmount(5); // 最小請求數
        rule.setStatIntervalMs(60000); // 統計時間窗口(ms)
        rules.add(rule);
        DegradeRuleManager.loadRules(rules);
    }
}

3、Gateway(網關服務)

application.yml 配置

server:
  port: 8080

spring:
  application:
    name: gateway
  cloud:
    sentinel:
      transport:
        dashboard: localhost:8080
        port: 8721

  gateway:
    routes:
      - id: order-service
        uri: http://localhost:8081
        predicates:
          - Path=/orders/**
      - id: inventory-service
        uri: http://localhost:8082
        predicates:
          - Path=/inventory/**

主啟動類

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

測試步驟

  1. 啟動 Sentinel Dashboard 和所有服務(gateway、order-service、inventory-service)。
  2. 在瀏覽器訪問 Sentinel 控制臺(http://localhost:8080),添加流控和熔斷規則。
  3. 測試訂單服務接口:
  • 正常請求:http://localhost:8080/orders/create?productId=1
  • 模擬超時:http://localhost:8080/orders/create?productId=timeout

總結

通過以上示例,我們實現了以下目標:

1、 基于 Sentinel 的流量控制(FlowRule)和熔斷降級(DegradeRule)。

2、 使用注解和手動規則配置,實現服務間調用的流量管控。 3、 利用 Gateway 統一流量入口,實現跨服務的高效監控。

你可以在此基礎上擴展更多規則,比如熱點參數限流、系統保護規則等,進一步增強系統的魯棒性和穩定性。

責任編輯:武曉燕 來源: 架構師秋天
相關推薦

2022-05-22 09:48:47

微服務Sentinel

2019-07-22 09:35:23

RedisSentinel

2024-05-06 08:45:25

Spring分布式日志

2019-08-05 07:58:01

分布式架構系統

2023-05-12 08:23:03

分布式系統網絡

2023-04-06 08:52:54

Sentinel分布式系統

2023-01-13 07:39:07

2024-01-26 13:17:00

rollbackMQ訂單系統

2013-09-11 16:02:00

Spark分布式計算系統

2023-08-21 19:10:34

Redis分布式

2023-02-11 00:04:17

分布式系統安全

2022-05-05 12:03:08

分布式系統設計

2023-05-29 14:07:00

Zuul網關系統

2022-02-22 10:29:24

分布式架構高可用

2023-05-05 06:13:51

分布式多級緩存系統

2022-11-06 19:28:02

分布式鎖etcd云原生

2022-05-11 13:55:18

高可用性分布式彈性

2023-11-08 10:25:00

模型技術

2017-10-27 08:40:44

分布式存儲剪枝系統

2024-10-07 10:07:31

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久综合九九 | 久久一区二区免费视频 | 一本色道精品久久一区二区三区 | 秋霞a级毛片在线看 | 亚洲伊人久久综合 | 免费视频一区二区 | 在线看亚洲 | 色av一区二区三区 | 看片国产| 亚州精品天堂中文字幕 | 日韩超碰在线 | 免费观看www | 一区二区三区欧美 | 伊人欧美视频 | 亚洲国产一区二区三区, | 欧美中文字幕在线观看 | 91精品国产综合久久久动漫日韩 | 日韩视频一区二区三区 | 精品伊人 | 日日夜夜天天 | 亚洲香蕉 | 在线观看视频亚洲 | 国产精品久久久久久久久免费樱桃 | 国产成人一区二区三区 | 久久www免费视频 | 久草青青草| 精品一区二区av | 国产免费一区二区 | 久久人爽 | 欧美在线视频一区二区 | 中文天堂网 | 国产精品日本一区二区不卡视频 | 午夜精品久久久久久久星辰影院 | 久久久久成人精品亚洲国产 | 欧美日韩国产一区二区三区 | 91精品一区 | 国产亚洲一区二区精品 | 在线免费看黄 | 中国一级特黄真人毛片免费观看 | 91久久国产精品 | 天天综合成人网 |