Anno&Viper -分布式锁服务端怎么实现
1、Anno简介
Anno是一个微服务框架引擎。入门简单、安全、稳定、高可用、全平台可监控、依赖第三方框架少。底层通讯RPC(Remote Procedure Call)采用稳定可靠经过无数成功项目验证过的跨语言的thrift、
grpc
。 自带服务注册发现健康检查(不依赖于Etcd、Consul、Zookeeper)、调用链追踪、Cron 调度、限流、事件总线。插件化开发,业务模块以CQRS 、DDD作为指导思想。
一个不可监控
的微服务平台是可怕的
,出了问题 难以准确定位问题的根源, Anno则提供了一套完整的监控体系,包括链路追踪
、服务占用的系统资源
、系统自身 CPU、内存、硬盘使用率
实时可监控等等。
github Anno:https://github.com/duyanming/Anno.Core
gitee :https://gitee.com/duyanming/anno.core
体验地址:http://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:维护了一个分布式锁的列表。
释放超时锁:
分布式锁插件在加载的时候启动一个定时任务检测超时的分布式锁。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /// <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" ); } } |
关于分布式锁详细源码请查看:https://github.com/duyanming/Anno.Core/tree/master/samples/Packages/Anno.Plugs.DLockService
Anno核心源码:https://github.com/duyanming/Anno.Core
Viper示例项目:https://github.com/duyanming/Viper
体验地址:http://140.143.207.244/Home/Login
QQ交流群:478399354
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库