You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Chaos Monkey为指定Spring Service配置异常攻击

配置Chaos Monkey指定Spring Service抛自定义异常操作步骤

1. 开启Service层监控能力

首先需要在项目配置文件中开启Chaos Monkey的Service组件监听开关,只有开启后才会识别并拦截Spring Service类的调用:

chaos:
  monkey:
    enabled: true
    watcher:
      service: true

2. 配置异常攻击基础规则

开启异常攻击开关,并指定需要拦截的目标Service全限定类名,支持配置多个目标:

chaos:
  monkey:
    assaults:
      exceptions-active: true # 启用异常攻击能力
    watched-custom-services:
      - com.example.service.ThirdPartyQueryService # 替换为你的目标Service全类名

3. 指定自定义异常类

配置你需要抛出的自定义异常的全限定类名,注意该异常必须存在public无参构造方法,否则Chaos Monkey无法正常实例化抛出:

chaos:
  monkey:
    assaults:
      custom-exception:
        type: com.example.exception.CustomRemoteTimeoutException # 替换为你的自定义异常全类名

可选调整配置

  • 如果你需要每次调用目标Service都必定抛出异常,可以把攻击触发概率调整为100%:
chaos:
  monkey:
    assaults:
      level: 100 # 取值范围1-100,数值越大触发概率越高
  • 如果你只需要针对目标Service下的特定方法抛出异常,可以增加方法级别的过滤规则:
chaos:
  monkey:
    assault-properties:
      - name: thirdPartyQueryService # 目标Service的bean名,默认是类名首字母小写
        methods:
          - name: getRemoteData # 指定要攻击的方法名
            exception:
              type: com.example.exception.CustomRemoteTimeoutException

注意事项

  • 若自定义异常没有无参构造方法,Chaos Monkey会默认抛出内置RuntimeException替代配置的自定义异常
  • 模拟超时场景时可以搭配延迟攻击使用,先增加对应时长的延迟再抛出异常,更贴合真实的超时表现
  • 开启Actuator端点的前提下,可以通过/actuator/chaosmonkey接口动态修改上述配置,无需重启服务

内容的提问来源于stack exchange,提问作者Raffaele Venafra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 06:39:01