GovernEventBus 是一个历经四年生产环境验证的事件驱动架构框架,通过事件总线机制来治理微服务间的远程过程调用。使用本地事务来支持微服务内强一致性,事件总线来实现微服务间的最终一致性,另外还提供了事件发布/订阅失败的自动补偿机制。
执行流安装初始化dbcreatetablecompensate_leader(namevarchar(16)notnullprimarykey,term_startbigintunsignednotnull,term_endbigintunsignednotnull,transition_periodbigintunsignednotnull,leader_idvarchar(100)notnull,versionintunsignednotnull);createtablepublish_event(idbigintunsignedauto_incrementprimarykey,event_namevarchar(100)notnull,event_datamediumtextnotnull,statussmallintunsignednotnull,published_timebigintunsigneddefault0notnull,versionsmallintunsignednotnull,create_timebigintunsignednotnull);createindexidx_statusonpublish_event(status);createtablepublish_event_compensate(idbigintunsignedauto_incrementprimarykey,publish_event_idbigintunsignednotnull,start_timebigintunsignednotnull,takenbigintunsignednotnull,failed_msgtextnull);createtablepublish_event_failed(idbigintunsignedauto_incrementprimarykey,publish_event_idbigintunsignednotnull,failed_msgtextnotnull,create_timebigintunsignednotnull);createtablesubscribe_event(idbigintunsignedauto_incrementprimarykey,subscribe_namevarchar(100)notnull,statussmallintunsignednotnull,subscribe_timebigintunsignednotnull,event_idbigintunsignednotnull,event_namevarchar(100)notnull,event_datamediumtextnotnull,event_create_timebigintunsignednotnull,versionsmallintunsignednotnull,create_timebigintunsignednotnull,constraintuk_subscribe_name_even_id_event_nameunique(subscribe_name,event_id,event_name));createindexidx_statusonsubscribe_event(status);createtablesubscribe_event_compensate(idbigintunsignedauto_incrementprimarykey,subscribe_event_idbigintunsignednotnull,start_timebigintunsignednotnull,takenintunsignednotnull,failed_msgtextnull);createtablesubscribe_event_failed(idbigintunsignedauto_incrementprimarykey,subscribe_event_idbigintunsignednotnull,failed_msgtextnotnull,create_timebigintunsignednotnull);insertintocompensate_leader(name,term_start,term_end,transition_period,leader_id,version)values('publish_leader',0,0,0,'',0);insertintocompensate_leader(name,term_start,term_end,transition_period,leader_id,version)values('subscribe_leader',0,0,0,'',0);GradlevaleventbusVersion="0.9.2";implementation("me.ahoo.eventbus:eventbus-spring-boot-starter:${eventbusVersion}")implementation("me.ahoo.eventbus:eventbus-spring-boot-autoconfigure:${eventbusVersion}"){capabilities{requireCapability("me.ahoo.eventbus:rabbit-bus-support")//requireCapability("me.ahoo.eventbus:kafka-bus-support")}}Maven<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="https://maven.apache.org/POM/4.0.0"xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="https://maven.apache.org/POM/4.0.0https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><artifactId>demo</artifactId><properties><eventbus.version>0.9.2</eventbus.version></properties><dependencies><dependency><groupId>me.ahoo.eventbus</groupId><artifactId>eventbus-spring-boot-starter</artifactId><version>${eventbus.version}</version></dependency><dependency><groupId>me.ahoo.eventbus</groupId><artifactId>eventbus-rabbit</artifactId><version>${eventbus.version}</version></dependency><!--<dependency>--><!--<groupId>me.ahoo.eventbus</groupId>--><!--<artifactId>eventbus-kafka</artifactId>--><!--<version>${eventbus.version}</version>--><!--</dependency>--></dependencies></project>SpringBootApplicationConfigspring:application:name:eventbus-demodatasource:url:jdbc:mysql://localhost:3306/eventbus_db?serverTimezone=GMT%2B8&characterEncoding=utf-8username:rootpassword:rootrabbitmq:host:localhostusername:eventbuspassword:eventbusgovern:eventbus:rabbit:exchange:eventbuscompensate:db:publish:schedule:initial-delay:30period:10subscribe:schedule:initial-delay:30period:10enabled:truesubscriber:prefix:${spring.application.name}.快速上手一般情况下 Publisher 与 Subscriber 不在同一个应用服务内。这里只是作为演示用途。
Demo
Publisher/***定义发布事件*/publicclassOrderCreatedEvent{privatelongorderId;publiclonggetOrderId(){returnorderId;}publicvoidsetOrderId(longorderId){this.orderId=orderId;}@OverridepublicStringtoString(){return"OrderCreatedEvent{"+"orderId="+orderId+'}';}}packageme.ahoo.eventbus.demo.service;importme.ahoo.eventbus.core.annotation.Publish;importme.ahoo.eventbus.demo.event.OrderCreatedEvent;importorg.springframework.stereotype.Service;/***@authorahoowang*/@ServicepublicclassOrderService{@PublishpublicOrderCreatedEventcreateOrder(){OrderCreatedEventorderCreatedEvent=newOrderCreatedEvent();orderCreatedEvent.setOrderId(1L);returnorderCreatedEvent;}}Subscriberpackageme.ahoo.eventbus.demo.service;importlombok.extern.slf4j.Slf4j;importme.ahoo.eventbus.core.annotation.Subscribe;importme.ahoo.eventbus.demo.event.OrderCreatedEvent;importorg.springframework.stereotype.Service;@Slf4j@ServicepublicclassNoticeService{@SubscribepublicvoidhandleOrderCreated(OrderCreatedEventorderCreatedEvent){log.info("handleOrderCreated-event:[{}].",orderCreatedEvent);/***执行相应的业务代码*sendsms/email?*/}}
评论