❤️‍🔥 Solon Cloud Event 新的事务特性与应用

1、Solon Cloud Event?

复制代码
是 Solon 分布式事件总线的解决方案。也是 Solon "最终一致性"分布式事务的解决方案之一

2、事务特性

事务?就是要求 Event 有原子性,当多个 Event 发布时,要么全成功,要么全失败。

java 复制代码
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 事务整合到一起。

java 复制代码
@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个金币(作为奖励);如果没发推送,告知有抽奖

主服务程序,负责主业务:

java 复制代码
@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));
    }
}

次服务程序,负责辅助业务(也可以合到主服务程序):

java 复制代码
@CloudEvent("user.registered")
public class UserRegisteredEventHandler implements CloudEventHandler {
    @Inject
    UserService userService;
    @Inject
    MobileService mobileSerivce;
    
    @Override
    public boolean handler(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 handler(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;
    }
}
相关推荐
带刺的坐椅1 天前
SpringBoot3 使用 SolonMCP 开发 MCP
java·ai·springboot·solon·mcp
带刺的坐椅2 天前
SpringBoot2 可以使用 SolonMCP 开发 MCP(江湖救急)
java·spring·ai·solon·mcp
带刺的坐椅3 天前
Java Solon v3.3.0 发布(国产优秀应用开发基座)
java·spring·solon
带刺的坐椅4 天前
jFinal 使用 SolonMCP 开发 MCP(拥抱新潮流)
java·ai·solon·jfinal·mcp
带刺的坐椅5 天前
FastMCP(python)和 SolonMCP(java)的体验比较(不能说一样,但真的很像)
java·python·solon·mcp·fastmcp
带刺的坐椅6 天前
Java Solon-MCP 实现 MCP 实践全解析:SSE 与 STDIO 通信模式详解
java·solon·mcp·mcp-server·mcp-client
在未来等你10 天前
互联网大厂Java求职面试:AI与云原生下的系统设计挑战-3
分布式事务·可观测性·云原生架构·redis缓存·大模型集成·互联网大厂java面试·ai应用开发
在未来等你10 天前
互联网大厂Java求职面试:云原生与AI融合下的系统设计挑战-1
云原生·kubernetes·分布式事务·微服务架构·java面试·service mesh·ai集成
在未来等你11 天前
互联网大厂Java求职面试:高并发系统设计与架构实战
性能优化·消息队列·分布式事务·微服务架构·java面试·jvm内存模型·高并发系统设计