Springboot Distributed State Machine

Springboot Distributed State Machine

#statemachine

What is a distributed state?

An application may exist in a finite number of states. when something happens it takes your application from one state to the next. A state machine is driven by triggers, which are based on either events or timers.

Distributed property for state machines comes with an abstraction layer on top of default state machine implementation, for the time being, only one implementation exists which is based on Apache Zookeeper, a coordination service for distributed applications. More details .

How it works ?

  1. A distributed state machine is implemented through a DistributedStateMachine class that wraps an actual instance of a StateMachine.
  2. DistributedStateMachine intercepts communication with a StateMachine instance and works with distributed state abstractions handled through the StateMachineEnsemble interface.
  3. A ZookeeperStateMachineEnsemble needs two mandatory settings, an instance of curatorClient and a basePath. The client is a curotorClient which is an API for simplifying usage of Zookeeper functionalities and basePath which indicates the base zookeeper path -which will be used for creating znodes.
  4. When the zookeeper up and the statemachines are connected, znode can be queried either with built-in Zookeeper commands or CuratorFramework built-in methods
  5. Use the StateMachinePersist interface to serialize a StateMachineContext, which contains enough information to reset a StateMachine.
@Configuration
@EnableStateMachine
public class Config
		extends StateMachineConfigurerAdapter<String, String> {

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

	@Override
	public void configure(StateMachineStateConfigurer<String, String> states)
			throws Exception {
		// config states
	}

	@Override
	public void configure(StateMachineTransitionConfigurer<String, String> transitions)
			throws Exception {
		// config transitions
	}

	@Bean
	public StateMachineEnsemble<String, String> stateMachineEnsemble()
			throws Exception {
		return new ZookeeperStateMachineEnsemble<String, String>(curatorClient(), "/zkpath");
	}

	@Bean
	public CuratorFramework curatorClient()
			throws Exception {
		CuratorFramework client = CuratorFrameworkFactory
				.builder()
				.defaultData(new byte[0])
				.connectString("localhost:2181").build();
		client.start();
		return client;
	}

}

An Example Explaining it All

  1. Create two different application instances.
  2. Start INSTANCE_ONE .When first state machine is started, it is in its INITIAL_STATE. Then it sends an EVENT to transition into NEXT_STATE state.
  3. Start INSTANCE_TWO then starts start the second state machine. You should see that the NEXT_STATE
  4. From either INSTANCE_ONE or INSTANCE_TWO (say INSTANCE_TWO) send an ANOTHER_EVENT to transit NEXT_STATE into the FINAL_STATE state , you should see the state be changed if you check it from the INSTANCE_ONE.

References:

[1] https://docs.spring.io/spring-statemachine/docs/1.1.x/reference/html/sm-distributed.html

[2] https://docs.spring.io/spring-statemachine/docs/current/reference/#statemachine-examples-zookeeper

[3] https://ozdinc-celikel.medium.com/distributed-state-machines-using-java-spring-state-machine-framework-e9ff9655c486

要查看或添加评论,请登录

Marwa Ali的更多文章

  • Spring Security 6 with Spring Boot 3 + KeyCloak

    Spring Security 6 with Spring Boot 3 + KeyCloak

    What is KeyCloak ? KeyCloak Open Source Identity and Access Management.It provides user federation, strong…

    1 条评论
  • Spring Security 6 with Spring Boot 3 + JWT

    Spring Security 6 with Spring Boot 3 + JWT

    In continuation to my article Spring security 6 and spring boot 3 , Next introducing JWT token. Learn Jwt token here .

  • Spring Security 6 with Spring Boot 3

    Spring Security 6 with Spring Boot 3

    Say goodbye to Old security , Say Hi to Spring Security 6 with Spring Boot 3 . it is easier and simpler.

  • SpringBoot batch framework

    SpringBoot batch framework

    Spring Batch is a lightweight, comprehensive batch framework designed to enable the development of robust batch…

  • Dockerizing Springboot Application

    Dockerizing Springboot Application

    Docker is a powerful tool that allows developers to package their applications into containers that can be easily…

  • Kafka Event sourcing in Event Driven Architecture

    Kafka Event sourcing in Event Driven Architecture

    What is Event Sourcing ? Event Sourcing is ensuring every change to the state of an application is captured in an event…

  • Istio addons

    Istio addons

    #devops #istio #grafana #promtheus #servicemesh Please see my previous artcile at Grafana An open source monitoring…

  • Istio service mesh

    Istio service mesh

    #devops #kubernets #istio #servicemesh What is a service mesh? Developers and operators face chanllenges with a…

  • Microservices Saga Pattern with Spring State machine

    Microservices Saga Pattern with Spring State machine

    What are sagas in microservices ? A database-per-microservice model provides many benefits for microservices…

  • SpringBoot State machine

    SpringBoot State machine

    The concept of a state machine is most likely older than any reader of this reference documentation and definitely…

社区洞察

其他会员也浏览了