Coder Social home page Coder Social logo

woshihoujinxin / distributelockgenerator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bingyufight/distributelockgenerator

0.0 2.0 0.0 43 KB

跨机器或JVM的分布式锁管理器,用于在分布式环境中协调共享资源,如:不同机器或JVM的定时离线程序可以部署多台机器

Java 100.00%

distributelockgenerator's Introduction

这是一个基于zookeeper和redis上的分布式锁,用于在跨机器或是跨JVM上进行分布式资源的锁管理,确保部署在多台机器或是JVM上的应用程序在同一时刻只能有一台机器或JVM获取分布式锁。 应用场景:1.某些离线定时任务做多机部署。如果不用分布式锁按照原来的方式进行部署,就会遇到在一定的间隔时间内,可能出现多次重复调用的问题。分布式锁保证只有一个机器上离线程序执行. 2.分布式补偿框架和分布式事务中保证全局事务的有序性 3.其他场景:部署在不同机器上的程序需要对共享资源的互斥访问 。。。。。

分布式锁的基本组成模块

zookeeper分布式锁
redis分布式锁	
配置管理
JDK 动态代理 + 注解(实现一个类似于Spring的aop 实现将各个机器分别操作分布式锁的log信息保存进mysql或是mongo)(这个log信息未来可以用来实现优先级抢占式分布式锁)
自定义线程池
mybatis操作mysql	
mongo db操作
Unit test模块

分布式锁的核心接口

public interface IDistributeLock {
	
boolean acquireLock() throws DistributeLockException;

boolean releaseLock() throws DistributeLockException;

boolean isLocked() throws DistributeLockException;

boolean tryAcquireLock(long duration) throws DistributeLockException;

}

JDK 动态代理 + 自定义注解 实现aop记录各台机器获取分布式锁的log信息

1 自定义注解

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD)
public @interface DistributeLockAopTag {
	public Class defaultAopHandleClass();
}

2 jdk 动态代理类

public class DistributeLockAopInvocationHandler implements InvocationHandler {

private static Logger logger = LoggerFactory.getLogger(DistributeLockAopInvocationHandler.class);

private Object proxyTarget;

private AbstractDistributeLockAopEntity proxyDistributeLockAopEntity;

public DistributeLockAopInvocationHandler(Object target,AbstractDistributeLockAopEntity proxyDistributeLockAopEntity){
	proxyTarget = target;
	this.proxyDistributeLockAopEntity = proxyDistributeLockAopEntity;
}

public Object invoke(Object proxy, Method method, Object[] args)
		throws Throwable {
	  Method originalMethod = proxyTarget.getClass().getMethod(method.getName(), method.getParameterTypes());
	  if (!originalMethod.isAnnotationPresent(DistributeLockAopTag.class)) {
	      return method.invoke(proxyTarget, args);
	  }
	  Object result = method.invoke(proxyTarget, args);
	  Map<String,Object> params = generateParamsFromTargetObject(originalMethod.getName(),result);
	  proxyDistributeLockAopEntity.logDistributeLockInfo(params);
	  return result;			
}

distributelockgenerator's People

Contributors

bingyufight avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.