Anno&Viper -分布式锁服务端怎么实现

 1、Anno简介

  Anno是一个微服务框架引擎。入门简单安全稳定高可用全平台可监控、依赖第三方框架少。底层通讯RPC(Remote Procedure Call)采用稳定可靠经过无数成功项目验证过的跨语言的thrift grpc。 自带服务注册发现健康检查(不依赖于Etcd、Consul、Zookeeper)、调用链追踪、Cron 调度、限流、事件总线。插件化开发,业务模块以CQRS 、DDD作为指导思想。

  一个不可监控的微服务平台是可怕的,出了问题 难以准确定位问题的根源, Anno则提供了一套完整的监控体系,包括链路追踪服务占用的系统资源、系统自身 CPU、内存、硬盘使用率实时可监控等等。

github Anno://github.com/duyanming/Anno.Core  

gitee      ://gitee.com/duyanming/anno.core

体验地址://140.143.207.244/Home/Login

2、Anno分布式锁服务端怎么实现

  上一章节我们了解了分布式锁是为了解决什么问题以及客户端怎么使用,今天我们来看一下分布式锁协调中心是怎么实现的。不足之处望大佬们多多指正。如果还不了解怎么使用可以查看上一章节《.netcore 微服务快速开发框架 Anno&Viper -分布式锁是个什么鬼

首先我们需要一个分布式锁服务端的入口类 DLockCenter

伪代码:

 public static class DLockCenter
    {
        private static List<LockerQueue> _lockerQueues = new List<LockerQueue>();
        private static readonly object Lock = new object();
     //进入分布式锁
     public static EngineData.ActionResult Enter(LockInfo info)
        {
            var locker = _lockerQueues.Find(l => l.MLoker.Key == info.Key);  
        ............
return locker.Enter(info); }
    //释放分布式锁
public static void Free(LockInfo info) { _lockerQueues.Find(l => l.MLoker.Owner == info.Owner)?.Free(); } /// <summary> /// 定时任务检测分布式锁是否超时, 超时直接抛弃 /// </summary> public static void Detection() { if (_lockerQueues.Count > 0) { _lockerQueues.Where(l=>l.MLoker.IsTimeOut&&l.MLoker.Type!=ProcessType.Free).ToList().ForEach(l=>l.Detection()); } } }

 _lockerQueues:维护了一个分布式锁的列表。

 释放超时锁:

 分布式锁插件在加载的时候启动一个定时任务检测超时的分布式锁。

public class DLockBootstrap: IPlugsConfigurationBootstrap
    {
        private static readonly CronDaemon CronDaemon = new CronDaemon();
        public void ConfigurationBootstrap()
        {
            //分布式锁启动配置
            /*
             * 每个一段时间检测是否有锁超时,超时则释放锁
             */
            CronDaemon.AddJob("* * * * * ? *", DLockCenter.Detection);
            if (CronDaemon.Status == DaemonStatus.Stop)
            {
                CronDaemon.Start();
            }
        }

        public void PreConfigurationBootstrap()
        {
            //throw new NotImplementedException();
        }
    }

分布式锁服务Module:

 /// <summary>
    /// 分布式锁服务
    /// </summary>
    public class DLockModule : BaseModule
    {
        [AnnoInfo(Desc = "分布式锁服务 获取锁[DLKey][TimeOut:5000][Owner]")]
        public ActionResult EnterLock()
        {
            var dlKey = RequestString("DLKey");
            var timeOut = RequestInt32("TimeOut")??5000;
            var owner = RequestString("Owner");
            var locker=new  LockInfo()
            {
                Key = dlKey,
                Time = timeOut,
                Owner=owner,
                EnterTime=DateTime.Now,
                Type=ProcessType.Enter
            };
            var rlt = DLockCenter.Enter(locker);
            return rlt;
        }
        [AnnoInfo(Desc = "分布式锁服务 释放锁[DLKey][Owner]")]
        public ActionResult DisposeLock()
        {
            var dlKey = RequestString("DLKey");
            var owner = RequestString("Owner");
            var locker = new LockInfo();
            locker.Key = dlKey;
            locker.Owner = owner;
            DLockCenter.Free(locker);
            return new ActionResult(true, null, null, "DisposeLock Message");
        }
    }  

 

关于分布式锁详细源码请查看://github.com/duyanming/Anno.Core/tree/master/samples/Packages/Anno.Plugs.DLockService

 

Anno核心源码://github.com/duyanming/Anno.Core  

Viper示例项目://github.com/duyanming/Viper  

体验地址://140.143.207.244/Home/Login

QQ交流群:478399354