通过实例学习SpringStateMachine之基于Zookeeper的分布式TURNSTILE

本文介绍了一个基于Zookeeper的分布式状态机实例,通过SpringStateMachine在不同机器间同步状态变化,实现状态一致性。文章详细展示了如何配置依赖、实现状态机并启动多实例进行状态同步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景介绍

本系列通过学习SpringStateMachine中附带的10余个Sample来学习SpringStateMachine中的各个概念和用法。项目是使用的分支为2.2.0.RELEASE。项目参考文档也是2.2.0.RELEASE。

基于Zookeeper的分布式TURNSTILE简介

这个例子是对TURNSTILE[1]的改进,在TURNSTILE demo基础上基于zookeeper增加分布式能力,来实现分布式状态状态机。当我们部署状态机应用在不同机器上时,如果在一台机器改变了状态机状态,那么其它机器上的状态机也会发生变化。而不是像TURNSTILE单机版一样,每次都从初始状态开始转换。

依赖

由于这个demo在TURNSTILE上基于ZOOKEEPER改造,因此除了TURNSTILE的依赖外还需要依赖spring-statemachine-zookeeper与curator。

        <dependency>
            <groupId>org.springframework.statemachine</groupId>
            <artifactId>spring-statemachine-zookeeper</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.2.0</version>
        </dependency>

实现

我们需要依赖Zookeeper,首先准备好Zookeeper环境,启动Zookeeper。如果启动Zookeeper失败,查看zookeeper.XXX.out文件根据具体原因解决即可。随后在状态机的配置中增加Zookeeper客户端的配置。这样一个分布式版本的TURNSTILE就改造完毕。

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.ensemble.StateMachineEnsemble;
import org.springframework.statemachine.zookeeper.ZookeeperStateMachineEnsemble;

@Configuration
@EnableStateMachine
public class StateMachineConfig
        extends StateMachineConfigurerAdapter<String, String> {

    @Override
    public void configure(StateMachineConfigurationConfigurer<String, String> config) throws Exception {
        config.withDistributed().ensemble(stateMachineEnsemble());
    }

    @Override
    public void configure(StateMachineStateConfigurer<String, String> states)
            throws Exception {
        states
                .withStates()
                .initial("LOCKED")
                .state("UNLOCKED");
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<String, String> transitions)
            throws Exception {
                transitions
                .withExternal()
                .source("LOCKED")
                .target("UNLOCKED")
                .event("COIN")
                .and()
                .withExternal()
                .source("UNLOCKED")
                .target("LOCKED")
                .event("PUSH");
    }


    @Bean
    public StateMachineEnsemble<String,String> stateMachineEnsemble() throws Exception{
        return new ZookeeperStateMachineEnsemble<String, String>(curatorClient(),"/foo");
    }
    @Bean
    public CuratorFramework curatorClient() {
        CuratorFramework client = CuratorFrameworkFactory.builder().defaultData(new byte[0])
                .retryPolicy(new ExponentialBackoffRetry(1000,3))
                .connectString("localhost:2181").build();
        client.start();
        return client;
    }
}

最后我们启动两个shell模拟不多个状态机。在第一个shell中触发修改状态机状态。随后在第二个shell中查看当前状态,最后会发现二者状态是一致的。这样一个简单的分布式版状态机demo就完成了。

shell1:

sm>sm start
Entry state LOCKED
State machine started

sm>sm event COIN
Exit state LOCKED
Entry state UNLOCKED
Event COIN send

sm>sm state
UNLOCKED

shell2:

sm>sm start
State machine started
sm>sm state
UNLOCKED

总结

分布式版的状态机基于Zookeeper实现。状态机部署在不同的机器上,但维持一致的状态。

参考

[1].TURNSTILE demo https://blog.youkuaiyun.com/Revivedsun/article/details/104232341
[2].statemachine zookeeper doc,https://docs.spring.io/spring-statemachine/docs/2.2.0.RELEASE/reference/#statemachine-examples-zookeeper

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值