❤️🔥 Solon Cloud Event 新的事务特性与应用
1、Solon Cloud Event?
是 Solon 分布式事件总线的解决方案。也是 Solon “最终一致性”分布式事务的解决方案之一
2、事务特性
事务?就是要求 Event 有原子性,当多个 Event 发布时,要么全成功,要么全失败。
public class EventDemo {
public void event_tran() {
//新建一个 Event 事务
EventTran eventTran = CloudClient.event().newTran();
try {
//发布,并使用事务
CloudClient.event().publish(new Event("user.event1", "test1").tran(eventTran));
CloudClient.event().publish(new Event("user.event2", "test2").tran(eventTran));
CloudClient.event().publish(new Event("user.event2", "test3").tran(eventTran));
//如果没问题,提交事务
eventTran.commit();
} catch (Throwable ex) {
//如果有问题,回滚事务
eventTran.rollback();
}
}
}
上面的体验与经典的 Jdbc 事务是很像的。加入 Solon 的事务注解管理后,体验可以再简洁些,也能与 Jdbc 事务整合到一起。
@Component
public class EventDemo {
//使用 @Tran 管理事务(将 jdbc, event 事务整合到一起)
@Tran
public void event_and_jdbc_tran() {
//新建一个 Event 事务,并加入 @Tran 的管理
EventTran eventTran = CloudClient.event().newTranAndJoin();
CloudClient.event().publish(new Event("user.event1", "test1").tran(eventTran));
CloudClient.event().publish(new Event("user.event2", "test2").tran(eventTran));
CloudClient.event().publish(new Event("user.event2", "test3").tran(eventTran));
}
}
3、拟模真实的场景应用:
我们设计一个用户注册的场景应用:
- 持久层添加用户记录
- 注册后发布一个已注册事件;再发布一个10天后触发的已唤醒事件
- 在已注册事件里,我们给用户送10个金币;再送手机100元冲值
- 在已唤醒事件里,我们检查用户的活动行为;如果有,再送100个金币(作为奖励);如果没发推送,告知有抽奖
主服务程序,负责主业务:
@Component
public class UserService {
@Inject
UserDao userDao;
//用户注册
@Tran
public void userRegister(long userId, String name){
userDao.addUser(userId, name);
this.onUserRegistered(userId);
}
//当用户完成注册时(发布事件)
private void onUserRegistered(long userId) {
String eventJson = String.format("{\"userId\":%d}", userId);
Date eventTime = DateTime.Now().addDay(10);
EventTran eventTran = CloudClient.event().newTranAndJoin();
//发布用户已注册事件
CloudClient.event().publish(new Event("user.registered", eventJson).tran(eventTran));
//发布用户已唤醒事件(用于检查用户在10内,有没有活动行为)
CloudClient.event().publish(new Event("user.reawakened", eventJson).scheduled(eventTime).tran(eventTran));
}
}
次服务程序,负责辅助业务(也可以合到主服务程序):
@CloudEvent("user.registered")
public class UserRegisteredEventHandler implements CloudEventHandler {
@Inject
UserService userService;
@Inject
MobileService mobileSerivce;
@Override
public boolean handle(Event event) throws Throwable {
long userId = ONode.load(event.context()).get("userId").getLong();
//送10个金币
userService.addGold(userId, 10);
//送手机充值100块
String mobie = userService.getMobile(userId);
mobileSerivce.recharge(mobile, 100);
return true;
}
}
@CloudEvent("user.reawakened")
public class UserReawakenedEventHandler implements CloudEventHandler {
@Inject
UserService userService;
@Inject
PushService pushService
@Override
public boolean handle(Event event) throws Throwable {
long userId = ONode.load(event.context()).get("userId").getLong();
if (userService.hasLive(userId, 10)) {
//再送100个金币
userService.addGold(userId, 100);
} else {
//获取设备id
String duid = userService.getDuid(userId);
//发布推送
pushService.push(duid, "有100个金币等你来拿哟...")
}
return true;
}
}
关注公众号
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
🔥🔥🔥一款兼具 “高性能、高颜值、高活跃” 的分布式任务调度与重试平台 Beta1 版本正式发布
🔥🔥🔥 灵活,可靠和快速的分布式任务重试和分布式任务调度平台 一文读懂 AizuDa/EasyRetry > ✅️ 可重放,可管控、为提高分布式业务系统一致性的分布式任务重试平台 > ✅️ 支持秒级、可中断、可编排的高性能分布式任务调度平台 📢特别声明 为了促进系统的更好发展,我们正式宣布自本版本起,EasyRetry将更名为SnailJob,并从版本号1.0.0开始,请大家尽快迁移至SnailJob。由于包路径和表结构的调整,新版本将不再兼容之前的EasyRetry版本, EasyRetry版本会继续做BUG修复。对此给大家带来的不便,我们深表歉意,并感谢大家的理解与支持。 易用性业务接入成本小。避免依赖研发人员的技术水平,保障稳定性 灵活性能够动态调整配置,启动 / 停止任务,以及终止运行中的任务 操作简单一分钟上手,支持 WEB 页面对任务数据 CRUD 操作。 数据大盘实时管控系统任务数据 分布式重试任务支持多样化退避策略、多样化重试类型、流量管控等 分布式调度任务提供丰富的任务触发策略、任务分片、停止恢复、失败重试等 工作流任务编排仿钉钉设计的...
-
下一篇
🎉 领域模型即服务 | Wow 3.5.5 发布
领域驱动|事件驱动|测试驱动|声明式设计|响应式编程|命令查询职责分离|事件溯源 官方文档:https://wow.ahoo.me/ 更新内容 特性(core): 支持从命令中自定义是否开启本地优先模式(LocalFirst) 特性(weblfux): 支持将原始请求写入查询上下文 特性(core): 支持从命令中定位特定的命令聚合。 特性(test): 支持快速验证命令合规性 特性(dashboard): 更新typescript到~ 5.4.0 特性(core): 使用第一个绑定错误作为默认错误消息。 特性(mongo): 为MongoConditionConverter.raw添加更多数据类型支持 特性(core): 添加CommandMessageFactory以增强命令创建 特性(core): 将验证消息的责任从CommandGateway转移到CommandMessageFactory 特性(webflux): 命令支持空请求体 修复(doc): 因 Gitee Pages 不可用,将文档站点从 Gitee Pages 变更为 GitHub。 修复(webflux): 修复...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS关闭SELinux安全模块
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Crontab安装和使用
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- Dcoker安装(在线仓库),最新的服务器搭配容器使用

微信收款码
支付宝收款码