×

Simba-Distributed-Mutex分布式锁服务

消耗积分:2 | 格式:zip | 大小:0.26 MB | 2022-06-14

王桂兰

分享资料个

授权协议 Apache
开发语言 Java
操作系统 跨平台
软件类型 开源软件

软件简介

Simba 旨在提供易用、灵活的分布式锁服务,支持多种存储后端实现:关系型数据库、Redis、Zookeeper。

安装

Gradle

Kotlin DSL

    val simbaVersion = "0.3.2";
    implementation("me.ahoo.simba:simba-spring-boot-starter:${simbaVersion}")
 

Maven



 xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    4.0.0
    demo
    
        0.3.2
    

    
        
            me.ahoo.simba
            simba-spring-boot-starter
            ${simba.version}
        
    
    
 

application.yaml

simba:
  jdbc:
    enabled: true
#  redis:
#    enabled: true

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/simba_db
    username: root
    password: root
 

Optional-1: JdbcMutexContendService

poYBAGKhzayAGbTwAAEbqyNcLy0806.png

Kotlin DSL

    val simbaVersion = "0.3.2";
    implementation("me.ahoo.simba:simba-jdbc:${simbaVersion}")
 
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

    val simbaVersion = "0.3.2";
    implementation("me.ahoo.simba:simba-redis:${simbaVersion}")

Optional-3: ZookeeperMutexContendService

Kotlin DSL

    val simbaVersion = "0.3.2";
    implementation("me.ahoo.simba:simba-zookeeper:${simbaVersion}")
 

Examples

Simba-Examples

使用入门

MutexContender

        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

        try (Locker locker = new SimbaLocker("mutex-locker", this.mutexContendServiceFactory)) {
            locker.acquire(Duration.ofSeconds(1));
        /**
         * doSomething
         */
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
 

Scheduler

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

 

 

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

评论(0)
发评论

下载排行榜

全部0条评论

快来发表一下你的评论吧 !