https://github.com/ahoo-wang/simba
Distributed lock service | 分布式锁服务
https://github.com/ahoo-wang/simba
distributed java kotlin lock mutex mutex-lock redis scheduler spring spring-boot zookeeper
Last synced: about 2 months ago
JSON representation
Distributed lock service | 分布式锁服务
- Host: GitHub
- URL: https://github.com/ahoo-wang/simba
- Owner: Ahoo-Wang
- License: apache-2.0
- Created: 2021-09-03T13:08:22.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2025-04-09T07:03:06.000Z (about 2 months ago)
- Last Synced: 2025-04-09T08:22:18.088Z (about 2 months ago)
- Topics: distributed, java, kotlin, lock, mutex, mutex-lock, redis, scheduler, spring, spring-boot, zookeeper
- Language: Kotlin
- Homepage:
- Size: 938 KB
- Stars: 18
- Watchers: 3
- Forks: 5
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simba(Distributed Mutex)
[](https://www.apache.org/licenses/LICENSE-2.0.html)
[](https://github.com/Ahoo-Wang/Simba/releases)
[](https://maven-badges.herokuapp.com/maven-central/me.ahoo.simba/simba-core)
[](https://www.codacy.com/gh/Ahoo-Wang/Simba/dashboard?utm_source=github.com&utm_medium=referral&utm_content=Ahoo-Wang/Simba&utm_campaign=Badge_Grade)
[](https://codecov.io/gh/Ahoo-Wang/Simba)## Introduction
Simba aims to provide easy-to-use and flexible distributed lock services and supports multiple storage implementations: relational databases, Redis, and Zookeeper.
## Installation
### Gradle
> Kotlin DSL
``` kotlin
implementation("me.ahoo.simba:simba-spring-boot-starter:${simbaVersion}")
```### Maven
```xml
4.0.0
demo
simbaVersion
me.ahoo.simba
simba-spring-boot-starter
${simba.version}
```
### application.yaml```yaml
simba:
jdbc:
enabled: true
# redis:
# enabled: truespring:
datasource:
url: jdbc:mysql://localhost:3306/simba_db
username: root
password: root
```### Optional-1: JdbcMutexContendService

> Kotlin DSL
``` kotlin
implementation("me.ahoo.simba:simba-jdbc:${simbaVersion}")
``````sql
create table simba_mutex
(
mutex varchar(66) not null primary key comment 'mutex name',
acquired_at bigint unsigned not null,
ttl_at bigint unsigned not null,
transition_at bigint unsigned not null,
owner_id char(32) not null,
version int unsigned not null
);
```### Optional-2: RedisMutexContendService
> Kotlin DSL
``` kotlin
implementation("me.ahoo.simba:simba-redis:${simbaVersion}")
```### Optional-3: ZookeeperMutexContendService
> Kotlin DSL
``` kotlin
implementation("me.ahoo.simba:simba-zookeeper:${simbaVersion}")
```## Examples
[Simba-Examples](https://github.com/Ahoo-Wang/Simba/tree/main/simba-example)
## Usage
### MutexContender
```java
MutexContendService contendService = contendServiceFactory.createMutexContendService(new AbstractMutexContender(mutex) {
@Override
public void onAcquired(MutexState mutexState) {
log.info("onAcquired");
}
@Override
public void onReleased(MutexState mutexState) {
log.info("onReleased");
}
});
contendService.start();
```### SimbaLocker
```java
try (Locker locker = new SimbaLocker("mutex-locker", this.mutexContendServiceFactory)) {
locker.acquire(Duration.ofSeconds(1));
/**
* doSomething
*/
} catch (Exception e) {
log.error(e.getMessage(), e);
}
```### Scheduler
```kotlin
public class ExampleScheduler extends AbstractScheduler implements SmartLifecycle {public ExampleScheduler(MutexContendServiceFactory contendServiceFactory) {
super("example-scheduler", ScheduleConfig.ofDelay(Duration.ofSeconds(0), Duration.ofSeconds(10)), contendServiceFactory);
}@Override
protected String getWorker() {
return "ExampleScheduler";
}@Override
protected void work() {
if (log.isInfoEnabled()) {
log.info("do some work!");
}
}
}
```#### Use Cases
- [Govern-EventBus](https://github.com/Ahoo-Wang/govern-eventbus/tree/master/eventbus-core/src/main/java/me/ahoo/eventbus/core/compensate)
- [CoSky](https://github.com/Ahoo-Wang/CoSky/blob/main/cosky-rest-api/src/main/kotlin/me/ahoo/cosky/rest/stat/StatServiceScheduler.kt)