Skip to content

如何自定义订阅框架事件

HaojunRen edited this page May 31, 2024 · 3 revisions

通过EventBus机制可以订阅框架内部发生的事件,通过这些事件,在使用侧做扩展异步订阅,例如,进行蓝绿灰度发布监控,网关动态路由监控,并通过邮件、钉钉等工具通知给当事人

① 内部事件,如下

事件类 作用 备注
RuleUpdatedEvent 蓝绿灰度规则策略更新成功事件 配置中心Xml内容改变才会触发
RuleFailureEvent 蓝绿灰度规则策略更新失败事件 配置中心Xml内容改变才会触发
RuleClearedEvent 蓝绿灰度规则策略清除失败事件 配置中心Xml内容改变才会触发
ParameterChangedEvent 动态参数改变事件 配置中心xml内容改变才会触发
VersionUpdatedEvent 动态版本更新事件 通过Rest Endpoint推送才会触发,不常用
VersionClearedEvent 动态版本清除事件 通过Rest Endpoint推送才会触发,不常用
RegisterFailureEvent 注册失败事件 通过Rest Endpoint推送才会触发,不常用
StrategyAlarmEvent 蓝绿灰度告警监控告警事件 蓝绿灰度告警监控告警事件需要区分告警类型
GatewayStrategyRouteAddedEvent Spring Cloud Gateway动态路由增加事件 配置中心Json内容改变才会触发
GatewayStrategyRouteModifiedEvent Spring Cloud Gateway动态路由修改事件 配置中心Json内容改变才会触发
GatewayStrategyRouteDeletedEvent Spring Cloud Gateway动态路由删除事件 配置中心Json内容改变才会触发
GatewayStrategyRouteUpdatedAllEvent Spring Cloud Gateway动态路由批量更新事件 配置中心Json内容改变才会触发
ZuulStrategyRouteAddedEvent Zuul动态路由增加事件 配置中心Json内容改变才会触发
ZuulStrategyRouteModifiedEvent Zuul动态路由修改事件 配置中心Json内容改变才会触发
ZuulStrategyRouteDeletedEvent Zuul动态路由删除事件 配置中心Json内容改变才会触发
ZuulStrategyRouteUpdatedAllEvent Zuul动态路由批量更新事件 配置中心Json内容改变才会触发

② 订阅示例,如下

@EventBus
public class MySubscriber {
    private static final Logger LOG = LoggerFactory.getLogger(MySubscriber.class);

    @Subscribe
    public void onRuleUpdated(RuleUpdatedEvent ruleUpdatedEvent) {
        LOG.info("规则执行更新, rule=" + ruleUpdatedEvent.getRule());
    }

    @Subscribe
    public void onRuleCleared(RuleClearedEvent ruleClearedEvent) {
        LOG.info("规则执行清空");
    }

    @Subscribe
    public void onRuleRuleFailure(RuleFailureEvent ruleFailureEvent) {
        LOG.info("规则更新失败, rule=" + ruleFailureEvent.getRule() + ", exception=" + ruleFailureEvent.getException());
    }

    @Subscribe
    public void onAlarm(StrategyAlarmEvent strategyAlarmEvent) {
        LOG.info("告警类型=" + strategyAlarmEvent.getAlarmType());
        LOG.info("告警内容=" + strategyAlarmEvent.getAlarmMap());
    }

    @Subscribe
    public void onZuulStrategyRouteAdded(ZuulStrategyRouteAddedEvent zuulStrategyRouteAddedEvent) {
        LOG.info("增加网关路由=" + zuulStrategyRouteAddedEvent.getZuulStrategyRouteEntity());
    }

    @Subscribe
    public void onZuulStrategyRouteModified(ZuulStrategyRouteModifiedEvent zuulStrategyRouteModifiedEvent) {
        LOG.info("修改网关路由=" + zuulStrategyRouteModifiedEvent.getZuulStrategyRouteEntity());
    }

    @Subscribe
    public void onZuulStrategyRouteDeleted(ZuulStrategyRouteDeletedEvent zuulStrategyRouteDeletedEvent) {
        LOG.info("删除网关路由=" + zuulStrategyRouteDeletedEvent.getRouteId());
    }

    @Subscribe
    public void onZuulStrategyRouteUpdatedAll(ZuulStrategyRouteUpdatedAllEvent zuulStrategyRouteUpdatedAllEvent) {
        LOG.info("更新全部网关路由=" + zuulStrategyRouteUpdatedAllEvent.getZuulStrategyRouteEntityList());
    }
}

在配置类里@Bean方式进行订阅类创建

@Bean
public MySubscriber mySubscriber() {
    return new MySubscriber();
}




2017-2050 ©Nepxion Studio Apache License

           

Total visits

讲义篇

集成篇

概念篇

实践篇

功能篇

配置篇

扩展篇

测试篇

升级篇

贡献篇

Clone this wiki locally