├── .gitattributes ├── .github ├── stale.yml └── workflows │ └── maven.yml ├── .gitignore ├── LICENSE ├── README.md ├── aquarius-cache-aop ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── nepxion │ └── aquarius │ └── cache │ ├── CacheDelegate.java │ ├── annotation │ ├── CacheEvict.java │ ├── CachePut.java │ └── Cacheable.java │ ├── aop │ ├── CacheAutoScanProxy.java │ └── CacheInterceptor.java │ ├── configuration │ └── CacheAopConfiguration.java │ └── constant │ └── CacheConstant.java ├── aquarius-cache-redis ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── nepxion │ └── aquarius │ └── cache │ └── redis │ ├── condition │ └── RedisCacheCondition.java │ ├── configuration │ ├── RedisCacheConfiguration.java │ └── RedissonCacheConfiguration.java │ └── impl │ ├── RedisCacheDelegateImpl.java │ └── RedissonCacheDelegateImpl.java ├── aquarius-cache-starter ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── nepxion │ │ └── aquarius │ │ └── cache │ │ ├── annotation │ │ └── EnableCache.java │ │ ├── aop │ │ └── CacheImportSelector.java │ │ └── configuration │ │ └── CacheConfiguration.java │ └── resources │ └── META-INF │ └── spring.factories ├── aquarius-common-redis ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── nepxion │ └── aquarius │ └── common │ ├── redis │ ├── configuration │ │ └── RedisConfiguration.java │ ├── constant │ │ └── RedisConstant.java │ ├── exception │ │ └── RedisException.java │ └── handler │ │ ├── RedisHandler.java │ │ └── RedisHandlerImpl.java │ └── redisson │ ├── adapter │ └── RedissonAdapter.java │ ├── constant │ └── RedissonConstant.java │ ├── exception │ └── RedissonException.java │ ├── handler │ ├── RedissonHandler.java │ └── RedissonHandlerImpl.java │ └── util │ └── RedissonUtil.java ├── aquarius-common-zookeeper ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── nepxion │ └── aquarius │ └── common │ └── curator │ ├── constant │ └── CuratorConstant.java │ ├── entity │ └── RetryTypeEnum.java │ ├── exception │ └── CuratorException.java │ └── handler │ ├── CuratorHandler.java │ └── CuratorHandlerImpl.java ├── aquarius-common ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── nepxion │ │ └── aquarius │ │ └── common │ │ ├── condition │ │ └── AquariusCondition.java │ │ ├── configuration │ │ └── AquariusConfiguration.java │ │ ├── constant │ │ └── AquariusConstant.java │ │ ├── context │ │ └── AquariusContextAware.java │ │ ├── exception │ │ └── AquariusException.java │ │ ├── property │ │ ├── AquariusContent.java │ │ └── AquariusProperties.java │ │ └── util │ │ ├── DateUtil.java │ │ ├── IOUtil.java │ │ ├── KeyUtil.java │ │ ├── MathsUtil.java │ │ └── StringUtil.java │ └── resources │ └── com │ └── nepxion │ └── aquarius │ └── resource │ └── logo.txt ├── aquarius-id-generator-local ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── nepxion │ └── aquarius │ └── idgenerator │ └── local │ ├── LocalIdGenerator.java │ ├── configuration │ └── LocalIdGeneratorConfiguration.java │ └── impl │ ├── LocalIdGeneratorImpl.java │ └── SnowflakeIdGenerator.java ├── aquarius-id-generator-redis ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── nepxion │ └── aquarius │ └── idgenerator │ └── redis │ ├── RedisIdGenerator.java │ ├── configuration │ └── RedisIdGeneratorConfiguration.java │ └── impl │ └── RedisIdGeneratorImpl.java ├── aquarius-id-generator-starter ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── nepxion │ │ └── aquarius │ │ └── idgenerator │ │ ├── annotation │ │ ├── EnableLocalIdGenerator.java │ │ ├── EnableRedisIdGenerator.java │ │ └── EnableZookeeperIdGenerator.java │ │ ├── aop │ │ ├── LocalIdGeneratorImportSelector.java │ │ ├── RedisIdGeneratorImportSelector.java │ │ └── ZookeeperIdGeneratorImportSelector.java │ │ ├── configuration │ │ ├── LocalIdGeneratorConfig.java │ │ ├── RedisIdGeneratorConfig.java │ │ └── ZookeeperIdGeneratorConfig.java │ │ └── constant │ │ └── IdGeneratorConstant.java │ └── resources │ └── META-INF │ └── spring.factories ├── aquarius-id-generator-zookeeper ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── nepxion │ └── aquarius │ └── idgenerator │ └── zookeeper │ ├── ZookeeperIdGenerator.java │ ├── configuration │ └── ZookeeperIdGeneratorConfiguration.java │ └── impl │ └── ZookeeperIdGeneratorImpl.java ├── aquarius-limit-aop ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── nepxion │ └── aquarius │ └── limit │ ├── LimitDelegate.java │ ├── LimitExecutor.java │ ├── annotation │ └── Limit.java │ ├── aop │ ├── LimitAutoScanProxy.java │ └── LimitInterceptor.java │ ├── configuration │ └── LimitAopConfiguration.java │ └── constant │ └── LimitConstant.java ├── aquarius-limit-local ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── nepxion │ └── aquarius │ └── limit │ └── local │ ├── condition │ └── LocalLimitCondition.java │ ├── configuration │ └── LocalLimitConfiguration.java │ └── impl │ ├── GuavaLocalLimitExecutorImpl.java │ ├── JdkLimitExecutorImpl.java │ └── LocalLimitDelegateImpl.java ├── aquarius-limit-redis ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── nepxion │ └── aquarius │ └── limit │ └── redis │ ├── condition │ └── RedisLimitCondition.java │ ├── configuration │ └── RedisLimitConfiguration.java │ └── impl │ ├── RedisLimitDelegateImpl.java │ └── RedisLimitExecutorImpl.java ├── aquarius-limit-starter ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── nepxion │ │ └── aquarius │ │ └── limit │ │ ├── annotation │ │ └── EnableLimit.java │ │ ├── aop │ │ └── LimitImportSelector.java │ │ └── configuration │ │ └── LimitConfiguration.java │ └── resources │ └── META-INF │ └── spring.factories ├── aquarius-lock-aop ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── nepxion │ └── aquarius │ └── lock │ ├── LockDelegate.java │ ├── LockExecutor.java │ ├── annotation │ ├── Lock.java │ ├── ReadLock.java │ └── WriteLock.java │ ├── aop │ ├── LockAutoScanProxy.java │ └── LockInterceptor.java │ ├── configuration │ └── LockAopConfiguration.java │ ├── constant │ └── LockConstant.java │ └── entity │ └── LockType.java ├── aquarius-lock-local ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── nepxion │ └── aquarius │ └── lock │ └── local │ ├── condition │ └── LocalLockCondition.java │ ├── configuration │ └── LocalLockConfiguration.java │ └── impl │ ├── LocalLockDelegateImpl.java │ └── LocalLockExecutorImpl.java ├── aquarius-lock-redis ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── nepxion │ └── aquarius │ └── lock │ └── redis │ ├── condition │ └── RedisLockCondition.java │ ├── configuration │ └── RedisLockConfiguration.java │ └── impl │ ├── RedisLockDelegateImpl.java │ └── RedisLockExecutorImpl.java ├── aquarius-lock-starter ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── nepxion │ │ └── aquarius │ │ └── lock │ │ ├── annotation │ │ └── EnableLock.java │ │ ├── aop │ │ └── LockImportSelector.java │ │ └── configuration │ │ └── LockConfiguration.java │ └── resources │ └── META-INF │ └── spring.factories ├── aquarius-lock-zookeeper ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── nepxion │ └── aquarius │ └── lock │ └── zookeeper │ ├── condition │ └── ZookeeperLockCondition.java │ ├── configuration │ └── ZookeeperLockConfiguration.java │ └── impl │ ├── ZookeeperLockDelegateImpl.java │ └── ZookeeperLockExecutorImpl.java ├── aquarius-spring-boot-example ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── nepxion │ │ └── aquarius │ │ └── example │ │ ├── adapter │ │ └── RedissonAdapterImpl.java │ │ ├── cache │ │ ├── app1 │ │ │ └── CacheAopApplication.java │ │ └── service │ │ │ ├── MyService5.java │ │ │ ├── MyService5Impl.java │ │ │ └── MyService6Impl.java │ │ ├── idgenerator │ │ ├── app1 │ │ │ └── LocalIdGeneratorApplication.java │ │ ├── app2 │ │ │ └── RedisIdGeneratorApplication.java │ │ └── app3 │ │ │ └── ZookeeperIdGeneratorApplication.java │ │ ├── limit │ │ ├── app1 │ │ │ └── LimitAopApplication.java │ │ ├── app2 │ │ │ └── LimitApplication.java │ │ └── service │ │ │ ├── MyService7.java │ │ │ ├── MyService7Impl.java │ │ │ └── MyService8Impl.java │ │ └── lock │ │ ├── app1 │ │ └── LockAopApplication.java │ │ ├── app2 │ │ └── LockApplication.java │ │ ├── app3 │ │ └── ReadWriteLockAopApplication.java │ │ ├── app4 │ │ └── ReadWriteLockApplication.java │ │ └── service │ │ ├── MyService1.java │ │ ├── MyService1Impl.java │ │ ├── MyService2Impl.java │ │ ├── MyService3.java │ │ ├── MyService3Impl.java │ │ └── MyService4Impl.java │ └── resources │ ├── application.properties │ ├── logback.xml │ └── redisson.yaml ├── aquarius-spring-cloud-example ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── nepxion │ │ └── aquarius │ │ └── example │ │ ├── AquariusApplication.java │ │ ├── cache │ │ └── CacheAopController.java │ │ ├── configuration │ │ └── WebMvcConfiguration.java │ │ ├── idgenerator │ │ └── IdGeneratorController.java │ │ ├── limit │ │ ├── LimitAopController.java │ │ └── LimitController.java │ │ └── lock │ │ ├── LockAopController.java │ │ └── LockController.java │ └── resources │ ├── application.properties │ ├── logback.xml │ └── redisson.yaml ├── deploy.bat ├── pmd.xml ├── pom.xml └── version.bat /.gitattributes: -------------------------------------------------------------------------------- 1 | # Declare files that will always have UNIX line endings on checkout. 2 | *.sh text eol=lf -------------------------------------------------------------------------------- /.github/stale.yml: -------------------------------------------------------------------------------- 1 | # General configuration 2 | # Number of days of inactivity before an issue becomes stale 3 | daysUntilStale: 60 4 | # Issues with these labels will never be considered stale 5 | exemptLabels: 6 | - good first issue 7 | - contribution welcome 8 | - bug 9 | - discussion 10 | - enhancement 11 | - feature 12 | - feature request 13 | - help wanted 14 | - info 15 | - need investigation 16 | - tips 17 | 18 | # Set to true to ignore issues in a project (defaults to false) 19 | exemptProjects: true 20 | # Set to true to ignore issues in a milestone (defaults to false) 21 | exemptMilestones: true 22 | # Set to true to ignore issues with an assignee (defaults to false) 23 | exemptAssignees: true 24 | # Label to use when marking an issue as stale 25 | staleLabel: stale 26 | 27 | # Pull request specific configuration 28 | pulls: 29 | # Number of days of inactivity before a stale Issue or Pull Request is closed. 30 | # Set to false to disable. If disabled, issues still need to be closed manually, but will remain marked as stale. 31 | daysUntilClose: 30 32 | # Comment to post when marking as stale. Set to `false` to disable 33 | markComment: > 34 | This pull request has been automatically marked as stale because it has not had activity 35 | in the last 90 days. It will be closed in 30 days if no further activity occurs. Please 36 | feel free to give a status update now, ping for review, or re-open when it's ready. 37 | Thank you for your contributions! 38 | # Comment to post when closing a stale Issue or Pull Request. 39 | closeComment: > 40 | This pull request has been automatically closed because it has not had 41 | activity in the last 30 days. Please feel free to give a status update now, ping for review, or re-open when it's ready. 42 | Thank you for your contributions! 43 | # Limit the number of actions per hour, from 1-30. Default is 30 44 | limitPerRun: 1 45 | 46 | # Issue specific configuration 47 | issues: 48 | # Number of days of inactivity before a stale Issue or Pull Request is closed. 49 | daysUntilClose: 14 50 | # Comment to post when marking as stale. Set to `false` to disable 51 | markComment: > 52 | This issue has been automatically marked as stale because it has not had activity in the 53 | last 90 days. It will be closed in 14 days unless it is tagged "help wanted" or other activity 54 | occurs. Thank you for your contributions. 55 | # Comment to post when closing a stale Issue or Pull Request. 56 | closeComment: > 57 | This issue has been automatically closed because it has not had activity in the 58 | last 14 days. If this issue is still valid, please ping a maintainer and ask them to label it as "help wanted". 59 | Thank you for your contributions. 60 | # Limit the number of actions per hour, from 1-30. Default is 30 61 | limitPerRun: 1 -------------------------------------------------------------------------------- /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Maven 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven 3 | 4 | name: build 5 | 6 | on: 7 | push: 8 | branches: [ master ] 9 | pull_request: 10 | branches: [ master ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v2 19 | - name: Set up JDK 8 20 | uses: actions/setup-java@v2 21 | with: 22 | java-version: '8' 23 | distribution: 'adopt' 24 | - name: Build with Maven 25 | run: mvn -B package --file pom.xml -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | .classpath 4 | .springBeans 5 | .factorypath 6 | # Mobile Tools for Java (J2ME) 7 | .mtj.tmp/ 8 | 9 | *.class 10 | *.classpath 11 | *.project 12 | *.springBeans 13 | bin/ 14 | log/ 15 | test-output/ 16 | 17 | # Package Files # 18 | *.jar 19 | *.war 20 | *.ear 21 | *.zip 22 | *.tar.gz 23 | *.rar 24 | *.swp 25 | *.log 26 | *.ctxt 27 | # nodejs local modules 28 | .tags* 29 | .idea/ 30 | *.iml 31 | .gradle/ 32 | .settings/ 33 | target/ 34 | hs_err_pid* -------------------------------------------------------------------------------- /aquarius-cache-aop/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | aquarius-cache-aop 5 | Nepxion Aquarius Cache For Aop 6 | jar 7 | 4.0.0 8 | Nepxion Aquarius is a list of distribution components based on Redis + Zookeeper with Nepxion Matrix AOP framework 9 | http://www.nepxion.com 10 | 11 | 12 | com.nepxion 13 | aquarius 14 | 2.0.13 15 | 16 | 17 | 18 | 19 | ${project.groupId} 20 | aquarius-common 21 | 22 | 23 | 24 | ${project.groupId} 25 | matrix-aop-starter 26 | 27 | 28 | -------------------------------------------------------------------------------- /aquarius-cache-aop/src/main/java/com/nepxion/aquarius/cache/CacheDelegate.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.cache; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.util.List; 13 | 14 | import org.aopalliance.intercept.MethodInvocation; 15 | 16 | public interface CacheDelegate { 17 | // 新增缓存 18 | Object invokeCacheable(MethodInvocation invocation, List keys, long expire) throws Throwable; 19 | 20 | // 更新缓存 21 | Object invokeCachePut(MethodInvocation invocation, List keys, long expire) throws Throwable; 22 | 23 | // 清除缓存 24 | Object invokeCacheEvict(MethodInvocation invocation, List keys, String name, boolean allEntries, boolean beforeInvocation) throws Throwable; 25 | } -------------------------------------------------------------------------------- /aquarius-cache-aop/src/main/java/com/nepxion/aquarius/cache/annotation/CacheEvict.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.cache.annotation; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.lang.annotation.Documented; 13 | import java.lang.annotation.ElementType; 14 | import java.lang.annotation.Inherited; 15 | import java.lang.annotation.Retention; 16 | import java.lang.annotation.RetentionPolicy; 17 | import java.lang.annotation.Target; 18 | 19 | @Target({ ElementType.METHOD, ElementType.TYPE }) 20 | @Retention(RetentionPolicy.RUNTIME) 21 | @Inherited 22 | @Documented 23 | public @interface CacheEvict { 24 | /** 25 | * 缓存名字 26 | * @return String 27 | */ 28 | String name() default ""; 29 | 30 | /** 31 | * 缓存Key 32 | * @return String[] 33 | */ 34 | String[] key() default {}; 35 | 36 | /** 37 | * 是否全部清除缓存内容 38 | * @return boolean 39 | */ 40 | boolean allEntries() default false; 41 | 42 | /** 43 | * 缓存清理是在方法调用前还是调用后 44 | * @return boolean 45 | */ 46 | boolean beforeInvocation() default false; 47 | } -------------------------------------------------------------------------------- /aquarius-cache-aop/src/main/java/com/nepxion/aquarius/cache/annotation/CachePut.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.cache.annotation; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.lang.annotation.Documented; 13 | import java.lang.annotation.ElementType; 14 | import java.lang.annotation.Inherited; 15 | import java.lang.annotation.Retention; 16 | import java.lang.annotation.RetentionPolicy; 17 | import java.lang.annotation.Target; 18 | 19 | @Target({ ElementType.METHOD, ElementType.TYPE }) 20 | @Retention(RetentionPolicy.RUNTIME) 21 | @Inherited 22 | @Documented 23 | public @interface CachePut { 24 | /** 25 | * 缓存名字 26 | * @return String 27 | */ 28 | String name() default ""; 29 | 30 | /** 31 | * 缓存Key 32 | * @return String[] 33 | */ 34 | String[] key() default {}; 35 | 36 | /** 37 | * 过期时间 38 | * 单位毫秒 39 | * @return long 40 | */ 41 | long expire() default -1234567890L; 42 | } -------------------------------------------------------------------------------- /aquarius-cache-aop/src/main/java/com/nepxion/aquarius/cache/annotation/Cacheable.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.cache.annotation; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.lang.annotation.Documented; 13 | import java.lang.annotation.ElementType; 14 | import java.lang.annotation.Inherited; 15 | import java.lang.annotation.Retention; 16 | import java.lang.annotation.RetentionPolicy; 17 | import java.lang.annotation.Target; 18 | 19 | @Target({ ElementType.METHOD, ElementType.TYPE }) 20 | @Retention(RetentionPolicy.RUNTIME) 21 | @Inherited 22 | @Documented 23 | public @interface Cacheable { 24 | /** 25 | * 缓存名字 26 | * @return String 27 | */ 28 | String name() default ""; 29 | 30 | /** 31 | * 缓存Key 32 | * @return String[] 33 | */ 34 | String[] key() default {}; 35 | 36 | /** 37 | * 过期时间 38 | * 单位毫秒 39 | * @return long 40 | */ 41 | long expire() default -1234567890L; 42 | } -------------------------------------------------------------------------------- /aquarius-cache-aop/src/main/java/com/nepxion/aquarius/cache/aop/CacheAutoScanProxy.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.cache.aop; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.lang.annotation.Annotation; 13 | 14 | import com.nepxion.aquarius.cache.annotation.CacheEvict; 15 | import com.nepxion.aquarius.cache.annotation.CachePut; 16 | import com.nepxion.aquarius.cache.annotation.Cacheable; 17 | import com.nepxion.matrix.proxy.aop.DefaultAutoScanProxy; 18 | import com.nepxion.matrix.proxy.mode.ProxyMode; 19 | import com.nepxion.matrix.proxy.mode.ScanMode; 20 | 21 | public class CacheAutoScanProxy extends DefaultAutoScanProxy { 22 | private static final long serialVersionUID = 5099476398968133135L; 23 | 24 | private String[] commonInterceptorNames; 25 | 26 | @SuppressWarnings("rawtypes") 27 | private Class[] methodAnnotations; 28 | 29 | public CacheAutoScanProxy(String scanPackages) { 30 | super(scanPackages, ProxyMode.BY_METHOD_ANNOTATION_ONLY, ScanMode.FOR_METHOD_ANNOTATION_ONLY); 31 | } 32 | 33 | @Override 34 | protected String[] getCommonInterceptorNames() { 35 | if (commonInterceptorNames == null) { 36 | commonInterceptorNames = new String[] { "cacheInterceptor" }; 37 | } 38 | 39 | return commonInterceptorNames; 40 | } 41 | 42 | @SuppressWarnings("unchecked") 43 | @Override 44 | protected Class[] getMethodAnnotations() { 45 | if (methodAnnotations == null) { 46 | methodAnnotations = new Class[] { Cacheable.class, CachePut.class, CacheEvict.class }; 47 | } 48 | 49 | return methodAnnotations; 50 | } 51 | } -------------------------------------------------------------------------------- /aquarius-cache-aop/src/main/java/com/nepxion/aquarius/cache/configuration/CacheAopConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.cache.configuration; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.beans.factory.annotation.Value; 13 | import org.springframework.context.annotation.Bean; 14 | import org.springframework.context.annotation.Configuration; 15 | 16 | import com.nepxion.aquarius.cache.aop.CacheAutoScanProxy; 17 | import com.nepxion.aquarius.cache.aop.CacheInterceptor; 18 | import com.nepxion.aquarius.cache.constant.CacheConstant; 19 | 20 | @Configuration 21 | public class CacheAopConfiguration { 22 | @Value("${" + CacheConstant.CACHE_SCAN_PACKAGES + ":}") 23 | private String scanPackages; 24 | 25 | @Bean 26 | public CacheAutoScanProxy cacheAutoScanProxy() { 27 | return new CacheAutoScanProxy(scanPackages); 28 | } 29 | 30 | @Bean 31 | public CacheInterceptor cacheInterceptor() { 32 | return new CacheInterceptor(); 33 | } 34 | } -------------------------------------------------------------------------------- /aquarius-cache-aop/src/main/java/com/nepxion/aquarius/cache/constant/CacheConstant.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.cache.constant; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | public class CacheConstant { 13 | public static final String CACHE_ENABLED = "cache.enabled"; 14 | 15 | public static final String CACHE_TYPE = "cache.type"; 16 | 17 | public static final String CACHE_TYPE_REDIS = "redisCache"; 18 | 19 | public static final String CACHE_AOP_EXCEPTION_IGNORE = "cache.aop.exception.ignore"; 20 | 21 | public static final String CACHE_EXPIRE = "cache.expire"; 22 | 23 | public static final String CACHE_SCAN_PACKAGES = "cache.scan.packages"; 24 | } -------------------------------------------------------------------------------- /aquarius-cache-redis/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | aquarius-cache-redis 5 | Nepxion Aquarius Cache For Redis 6 | jar 7 | 4.0.0 8 | Nepxion Aquarius is a list of distribution components based on Redis + Zookeeper with Nepxion Matrix AOP framework 9 | http://www.nepxion.com 10 | 11 | 12 | com.nepxion 13 | aquarius 14 | 2.0.13 15 | 16 | 17 | 18 | 19 | ${project.groupId} 20 | aquarius-common-redis 21 | 22 | 23 | 24 | ${project.groupId} 25 | aquarius-cache-aop 26 | 27 | 28 | 29 | org.redisson 30 | redisson 31 | provided 32 | 33 | 34 | -------------------------------------------------------------------------------- /aquarius-cache-redis/src/main/java/com/nepxion/aquarius/cache/redis/condition/RedisCacheCondition.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.cache.redis.condition; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import com.nepxion.aquarius.cache.constant.CacheConstant; 13 | import com.nepxion.aquarius.common.condition.AquariusCondition; 14 | 15 | public class RedisCacheCondition extends AquariusCondition { 16 | public RedisCacheCondition() { 17 | super(CacheConstant.CACHE_TYPE, CacheConstant.CACHE_TYPE_REDIS); 18 | } 19 | } -------------------------------------------------------------------------------- /aquarius-cache-redis/src/main/java/com/nepxion/aquarius/cache/redis/configuration/RedisCacheConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.cache.redis.configuration; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 13 | import org.springframework.context.annotation.Bean; 14 | import org.springframework.context.annotation.Conditional; 15 | import org.springframework.context.annotation.Configuration; 16 | import org.springframework.context.annotation.Import; 17 | 18 | import com.nepxion.aquarius.cache.CacheDelegate; 19 | import com.nepxion.aquarius.cache.redis.condition.RedisCacheCondition; 20 | import com.nepxion.aquarius.cache.redis.impl.RedisCacheDelegateImpl; 21 | import com.nepxion.aquarius.common.redis.configuration.RedisConfiguration; 22 | import com.nepxion.aquarius.common.redis.handler.RedisHandler; 23 | import com.nepxion.aquarius.common.redis.handler.RedisHandlerImpl; 24 | 25 | @Configuration 26 | @Import({ RedisConfiguration.class }) 27 | public class RedisCacheConfiguration { 28 | @Bean 29 | @Conditional(RedisCacheCondition.class) 30 | public CacheDelegate redisCacheDelegate() { 31 | return new RedisCacheDelegateImpl(); 32 | } 33 | 34 | @Bean 35 | @Conditional(RedisCacheCondition.class) 36 | @ConditionalOnMissingBean 37 | public RedisHandler redisHandler() { 38 | return new RedisHandlerImpl(); 39 | } 40 | } -------------------------------------------------------------------------------- /aquarius-cache-redis/src/main/java/com/nepxion/aquarius/cache/redis/configuration/RedissonCacheConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.cache.redis.configuration; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.beans.factory.annotation.Value; 14 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 15 | import org.springframework.context.annotation.Bean; 16 | import org.springframework.context.annotation.Conditional; 17 | import org.springframework.context.annotation.Configuration; 18 | 19 | import com.nepxion.aquarius.cache.CacheDelegate; 20 | import com.nepxion.aquarius.cache.redis.condition.RedisCacheCondition; 21 | import com.nepxion.aquarius.cache.redis.impl.RedissonCacheDelegateImpl; 22 | import com.nepxion.aquarius.common.redisson.adapter.RedissonAdapter; 23 | import com.nepxion.aquarius.common.redisson.constant.RedissonConstant; 24 | import com.nepxion.aquarius.common.redisson.handler.RedissonHandler; 25 | import com.nepxion.aquarius.common.redisson.handler.RedissonHandlerImpl; 26 | 27 | @Configuration 28 | public class RedissonCacheConfiguration { 29 | @Value("${" + RedissonConstant.PATH + ":" + RedissonConstant.DEFAULT_PATH + "}") 30 | private String redissonPath; 31 | 32 | @Autowired(required = false) 33 | private RedissonAdapter redissonAdapter; 34 | 35 | @Bean 36 | @Conditional(RedisCacheCondition.class) 37 | public CacheDelegate redisCacheDelegate() { 38 | return new RedissonCacheDelegateImpl(); 39 | } 40 | 41 | @Bean 42 | @Conditional(RedisCacheCondition.class) 43 | @ConditionalOnMissingBean 44 | public RedissonHandler redissonHandler() { 45 | if (redissonAdapter != null) { 46 | return redissonAdapter.getRedissonHandler(); 47 | } 48 | 49 | return new RedissonHandlerImpl(redissonPath); 50 | } 51 | } -------------------------------------------------------------------------------- /aquarius-cache-starter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | aquarius-cache-starter 5 | Nepxion Aquarius Starter For Cache 6 | jar 7 | 4.0.0 8 | Nepxion Aquarius is a list of distribution components based on Redis + Zookeeper with Nepxion Matrix AOP framework 9 | http://www.nepxion.com 10 | 11 | 12 | com.nepxion 13 | aquarius 14 | 2.0.13 15 | 16 | 17 | 18 | 19 | ${project.groupId} 20 | aquarius-common 21 | 22 | 23 | 24 | ${project.groupId} 25 | aquarius-common-redis 26 | 27 | 28 | 29 | ${project.groupId} 30 | aquarius-cache-aop 31 | 32 | 33 | 34 | ${project.groupId} 35 | aquarius-cache-redis 36 | 37 | 38 | -------------------------------------------------------------------------------- /aquarius-cache-starter/src/main/java/com/nepxion/aquarius/cache/annotation/EnableCache.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.cache.annotation; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.lang.annotation.Documented; 13 | import java.lang.annotation.ElementType; 14 | import java.lang.annotation.Inherited; 15 | import java.lang.annotation.Retention; 16 | import java.lang.annotation.RetentionPolicy; 17 | import java.lang.annotation.Target; 18 | 19 | import org.springframework.context.annotation.Import; 20 | 21 | import com.nepxion.aquarius.cache.aop.CacheImportSelector; 22 | 23 | @Target(ElementType.TYPE) 24 | @Retention(RetentionPolicy.RUNTIME) 25 | @Documented 26 | @Inherited 27 | @Import(CacheImportSelector.class) 28 | public @interface EnableCache { 29 | 30 | } -------------------------------------------------------------------------------- /aquarius-cache-starter/src/main/java/com/nepxion/aquarius/cache/aop/CacheImportSelector.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.cache.aop; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.core.Ordered; 13 | import org.springframework.core.annotation.Order; 14 | 15 | import com.nepxion.aquarius.cache.annotation.EnableCache; 16 | import com.nepxion.aquarius.cache.constant.CacheConstant; 17 | import com.nepxion.matrix.selector.AbstractImportSelector; 18 | import com.nepxion.matrix.selector.RelaxedPropertyResolver; 19 | 20 | @Order(Ordered.LOWEST_PRECEDENCE - 100) 21 | public class CacheImportSelector extends AbstractImportSelector { 22 | @Override 23 | protected boolean isEnabled() { 24 | return new RelaxedPropertyResolver(getEnvironment()).getProperty(CacheConstant.CACHE_ENABLED, Boolean.class, Boolean.TRUE); 25 | } 26 | } -------------------------------------------------------------------------------- /aquarius-cache-starter/src/main/java/com/nepxion/aquarius/cache/configuration/CacheConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.cache.configuration; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.context.annotation.Configuration; 13 | import org.springframework.context.annotation.Import; 14 | 15 | import com.nepxion.aquarius.cache.configuration.CacheAopConfiguration; 16 | import com.nepxion.aquarius.cache.redis.configuration.RedisCacheConfiguration; 17 | import com.nepxion.aquarius.common.configuration.AquariusConfiguration; 18 | 19 | @Configuration 20 | @Import({ AquariusConfiguration.class, CacheAopConfiguration.class, RedisCacheConfiguration.class }) 21 | public class CacheConfiguration { 22 | 23 | } -------------------------------------------------------------------------------- /aquarius-cache-starter/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | com.nepxion.aquarius.cache.annotation.EnableCache=\ 2 | com.nepxion.aquarius.cache.configuration.CacheConfiguration -------------------------------------------------------------------------------- /aquarius-common-redis/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | aquarius-common-redis 5 | Nepxion Aquarius Common For Redis 6 | jar 7 | 4.0.0 8 | Nepxion Aquarius is a list of distribution components based on Redis + Zookeeper with Nepxion Matrix AOP framework 9 | http://www.nepxion.com 10 | 11 | 12 | com.nepxion 13 | aquarius 14 | 2.0.13 15 | 16 | 17 | 18 | 19 | ${project.groupId} 20 | aquarius-common 21 | 22 | 23 | 24 | org.redisson 25 | redisson 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-data-redis 31 | 32 | 33 | -------------------------------------------------------------------------------- /aquarius-common-redis/src/main/java/com/nepxion/aquarius/common/redis/configuration/RedisConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.redis.configuration; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.slf4j.Logger; 13 | import org.slf4j.LoggerFactory; 14 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 15 | import org.springframework.context.annotation.Bean; 16 | import org.springframework.context.annotation.Configuration; 17 | import org.springframework.data.redis.connection.RedisConnectionFactory; 18 | import org.springframework.data.redis.core.RedisTemplate; 19 | import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; 20 | import org.springframework.data.redis.serializer.StringRedisSerializer; 21 | 22 | import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; 23 | import com.fasterxml.jackson.annotation.PropertyAccessor; 24 | import com.fasterxml.jackson.databind.ObjectMapper; 25 | 26 | @Configuration 27 | public class RedisConfiguration { 28 | private static final Logger LOG = LoggerFactory.getLogger(RedisConfiguration.class); 29 | 30 | @Bean 31 | @ConditionalOnMissingBean 32 | public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) { 33 | LOG.info("Start to initialize Redis..."); 34 | 35 | ObjectMapper objectMapper = new ObjectMapper(); 36 | objectMapper.setVisibility(PropertyAccessor.ALL, Visibility.ANY); 37 | objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 38 | 39 | Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); 40 | jackson2JsonRedisSerializer.setObjectMapper(objectMapper); 41 | 42 | RedisTemplate redisTemplate = new RedisTemplate(); 43 | redisTemplate.setConnectionFactory(redisConnectionFactory); 44 | redisTemplate.setKeySerializer(new StringRedisSerializer()); 45 | redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); 46 | // redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); 47 | 48 | return redisTemplate; 49 | } 50 | } -------------------------------------------------------------------------------- /aquarius-common-redis/src/main/java/com/nepxion/aquarius/common/redis/constant/RedisConstant.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.redis.constant; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | public class RedisConstant { 13 | 14 | } -------------------------------------------------------------------------------- /aquarius-common-redis/src/main/java/com/nepxion/aquarius/common/redis/exception/RedisException.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.redis.exception; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | public class RedisException extends RuntimeException { 13 | private static final long serialVersionUID = -1137413793056992202L; 14 | 15 | public RedisException() { 16 | super(); 17 | } 18 | 19 | public RedisException(String message) { 20 | super(message); 21 | } 22 | 23 | public RedisException(String message, Throwable cause) { 24 | super(message, cause); 25 | } 26 | 27 | public RedisException(Throwable cause) { 28 | super(cause); 29 | } 30 | } -------------------------------------------------------------------------------- /aquarius-common-redis/src/main/java/com/nepxion/aquarius/common/redis/handler/RedisHandler.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.redis.handler; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.data.redis.core.RedisTemplate; 13 | 14 | public interface RedisHandler { 15 | // 获取RedisTemplate 16 | RedisTemplate getRedisTemplate(); 17 | } -------------------------------------------------------------------------------- /aquarius-common-redis/src/main/java/com/nepxion/aquarius/common/redis/handler/RedisHandlerImpl.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.redis.handler; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.data.redis.core.RedisTemplate; 14 | 15 | public class RedisHandlerImpl implements RedisHandler { 16 | @Autowired 17 | private RedisTemplate redisTemplate; 18 | 19 | // 获取RedisTemplate 20 | @Override 21 | public RedisTemplate getRedisTemplate() { 22 | return redisTemplate; 23 | } 24 | } -------------------------------------------------------------------------------- /aquarius-common-redis/src/main/java/com/nepxion/aquarius/common/redisson/adapter/RedissonAdapter.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.redisson.adapter; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import com.nepxion.aquarius.common.redisson.handler.RedissonHandler; 13 | 14 | public interface RedissonAdapter { 15 | RedissonHandler getRedissonHandler(); 16 | } -------------------------------------------------------------------------------- /aquarius-common-redis/src/main/java/com/nepxion/aquarius/common/redisson/constant/RedissonConstant.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.redisson.constant; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | public class RedissonConstant { 13 | public static final String DEFAULT_PATH = "redisson.yaml"; 14 | public static final String PATH = "redisson.path"; 15 | } -------------------------------------------------------------------------------- /aquarius-common-redis/src/main/java/com/nepxion/aquarius/common/redisson/exception/RedissonException.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.redisson.exception; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | public class RedissonException extends RuntimeException { 13 | private static final long serialVersionUID = 4550515832057492430L; 14 | 15 | public RedissonException() { 16 | super(); 17 | } 18 | 19 | public RedissonException(String message) { 20 | super(message); 21 | } 22 | 23 | public RedissonException(String message, Throwable cause) { 24 | super(message, cause); 25 | } 26 | 27 | public RedissonException(Throwable cause) { 28 | super(cause); 29 | } 30 | } -------------------------------------------------------------------------------- /aquarius-common-redis/src/main/java/com/nepxion/aquarius/common/redisson/handler/RedissonHandler.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.redisson.handler; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.redisson.api.RedissonClient; 13 | 14 | public interface RedissonHandler { 15 | // 关闭Redisson客户端连接 16 | void close(); 17 | 18 | // 获取Redisson客户端是否初始化 19 | boolean isInitialized(); 20 | 21 | // 获取Redisson客户端连接是否正常 22 | boolean isStarted(); 23 | 24 | // 检查Redisson是否是启动状态 25 | void validateStartedStatus(); 26 | 27 | // 检查Redisson是否是关闭状态 28 | void validateClosedStatus(); 29 | 30 | // 获取Redisson客户端 31 | RedissonClient getRedisson(); 32 | } -------------------------------------------------------------------------------- /aquarius-common-redis/src/main/java/com/nepxion/aquarius/common/redisson/handler/RedissonHandlerImpl.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.redisson.handler; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.redisson.Redisson; 13 | import org.redisson.api.RedissonClient; 14 | import org.redisson.config.Config; 15 | import org.slf4j.Logger; 16 | import org.slf4j.LoggerFactory; 17 | 18 | import com.nepxion.aquarius.common.redisson.exception.RedissonException; 19 | import com.nepxion.aquarius.common.redisson.util.RedissonUtil; 20 | 21 | public class RedissonHandlerImpl implements RedissonHandler { 22 | private static final Logger LOG = LoggerFactory.getLogger(RedissonHandlerImpl.class); 23 | 24 | private RedissonClient redisson; 25 | 26 | public RedissonHandlerImpl(String yamlConfigPath) { 27 | try { 28 | Config config = RedissonUtil.createYamlFileConfig(yamlConfigPath); 29 | 30 | initialize(config); 31 | } catch (Exception e) { 32 | LOG.error("Initialize Redisson failed", e); 33 | } 34 | } 35 | 36 | public RedissonHandlerImpl(Config config) { 37 | try { 38 | initialize(config); 39 | } catch (Exception e) { 40 | LOG.error("Initialize Redisson failed", e); 41 | } 42 | } 43 | 44 | // 创建Redisson 45 | private void initialize(Config config) { 46 | create(config); 47 | } 48 | 49 | // 使用config创建Redisson 50 | private void create(Config config) { 51 | LOG.info("Start to initialize Redisson..."); 52 | 53 | if (redisson != null) { 54 | throw new RedissonException("Redisson isn't null, it has been initialized already"); 55 | } 56 | 57 | redisson = Redisson.create(config); 58 | } 59 | 60 | // 关闭Redisson客户端连接 61 | @Override 62 | public void close() { 63 | LOG.info("Start to close Redisson..."); 64 | 65 | validateStartedStatus(); 66 | 67 | redisson.shutdown(); 68 | } 69 | 70 | // 获取Redisson客户端是否初始化 71 | @Override 72 | public boolean isInitialized() { 73 | return redisson != null; 74 | } 75 | 76 | // 获取Redisson客户端连接是否正常 77 | @Override 78 | public boolean isStarted() { 79 | if (redisson == null) { 80 | throw new RedissonException("Redisson isn't initialized"); 81 | } 82 | 83 | return !redisson.isShutdown() && !redisson.isShuttingDown(); 84 | } 85 | 86 | // 检查Redisson是否是启动状态 87 | @Override 88 | public void validateStartedStatus() { 89 | if (redisson == null) { 90 | throw new RedissonException("Redisson isn't initialized"); 91 | } 92 | 93 | if (!isStarted()) { 94 | throw new RedissonException("Redisson is closed"); 95 | } 96 | } 97 | 98 | // 检查Redisson是否是关闭状态 99 | @Override 100 | public void validateClosedStatus() { 101 | if (redisson == null) { 102 | throw new RedissonException("Redisson isn't initialized"); 103 | } 104 | 105 | if (isStarted()) { 106 | throw new RedissonException("Redisson is started"); 107 | } 108 | } 109 | 110 | // 获取Redisson客户端 111 | @Override 112 | public RedissonClient getRedisson() { 113 | return redisson; 114 | } 115 | } -------------------------------------------------------------------------------- /aquarius-common-redis/src/main/java/com/nepxion/aquarius/common/redisson/util/RedissonUtil.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.redisson.util; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.io.IOException; 13 | 14 | import org.redisson.config.Config; 15 | import org.slf4j.Logger; 16 | import org.slf4j.LoggerFactory; 17 | 18 | import com.nepxion.aquarius.common.constant.AquariusConstant; 19 | import com.nepxion.aquarius.common.property.AquariusContent; 20 | 21 | public class RedissonUtil { 22 | private static final Logger LOG = LoggerFactory.getLogger(RedissonUtil.class); 23 | 24 | // 创建Yaml格式的配置文件 25 | public static Config createYamlFileConfig(String yamlConfigPath) throws IOException { 26 | LOG.info("Start to read {}...", yamlConfigPath); 27 | 28 | AquariusContent content = new AquariusContent(yamlConfigPath, AquariusConstant.ENCODING_UTF_8); 29 | 30 | return createYamlConfig(content.getContent()); 31 | } 32 | 33 | // 创建Json格式的配置文件 34 | public static Config createJsonFileConfig(String jsonConfigPath) throws IOException { 35 | LOG.info("Start to read {}...", jsonConfigPath); 36 | 37 | AquariusContent content = new AquariusContent(jsonConfigPath, AquariusConstant.ENCODING_UTF_8); 38 | 39 | return createJsonConfig(content.getContent()); 40 | } 41 | 42 | // 创建Yaml格式的配置文件 43 | public static Config createYamlConfig(String yamlConfigContent) throws IOException { 44 | return Config.fromYAML(yamlConfigContent); 45 | } 46 | 47 | // 创建Json格式的配置文件 48 | public static Config createJsonConfig(String jsonConfigContent) throws IOException { 49 | return Config.fromJSON(jsonConfigContent); 50 | } 51 | } -------------------------------------------------------------------------------- /aquarius-common-zookeeper/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | aquarius-common-zookeeper 5 | Nepxion Aquarius Common For Zookeeper 6 | jar 7 | 4.0.0 8 | Nepxion Aquarius is a list of distribution components based on Redis + Zookeeper with Nepxion Matrix AOP framework 9 | http://www.nepxion.com 10 | 11 | 12 | com.nepxion 13 | aquarius 14 | 2.0.13 15 | 16 | 17 | 18 | 19 | ${project.groupId} 20 | aquarius-common 21 | 22 | 23 | 24 | org.apache.curator 25 | curator-framework 26 | 27 | 28 | 29 | org.apache.curator 30 | curator-recipes 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter 36 | 37 | 38 | -------------------------------------------------------------------------------- /aquarius-common-zookeeper/src/main/java/com/nepxion/aquarius/common/curator/constant/CuratorConstant.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.curator.constant; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | public class CuratorConstant { 13 | public static final String CONNECT_STRING = "curator.connectString"; 14 | public static final String SESSION_TIMEOUT_MS = "curator.sessionTimeoutMs"; 15 | public static final String CONNECTION_TIMEOUT_MS = "curator.connectionTimeoutMs"; 16 | 17 | public static final String RETRY_TYPE = "curator.retryType"; 18 | 19 | public static final String RETRY_TYPE_EXPONENTIAL_BACKOFF_RETRY_BASE_SLEEP_TIME_MS = "curator.exponentialBackoffRetry.baseSleepTimeMs"; 20 | public static final String RETRY_TYPE_EXPONENTIAL_BACKOFF_RETRY_MAX_RETRIES = "curator.exponentialBackoffRetry.maxRetries"; 21 | public static final String RETRY_TYPE_BOUNDED_EXPONENTIAL_BACKOFF_RETRY_BASE_SLEEP_TIME_MS = "curator.boundedExponentialBackoffRetry.baseSleepTimeMs"; 22 | public static final String RETRY_TYPE_BOUNDED_EXPONENTIAL_BACKOFF_RETRY_MAX_SLEEP_TIME_MS = "curator.boundedExponentialBackoffRetry.maxSleepTimeMs"; 23 | public static final String RETRY_TYPE_BOUNDED_EXPONENTIAL_BACKOFF_RETRY_MAX_RETRIES = "curator.boundedExponentialBackoffRetry.maxRetries"; 24 | public static final String RETRY_TYPE_RETRY_NTIMES_COUNT = "curator.retryNTimes.count"; 25 | public static final String RETRY_TYPE_RETRY_NTIMES_SLEEP_MS_BETWEEN_RETRIES = "curator.retryNTimes.sleepMsBetweenRetries"; 26 | public static final String RETRY_TYPE_RETRY_FOREVER_RETRY_INTERVAL_MS = "curator.retryForever.retryIntervalMs"; 27 | public static final String RETRY_TYPE_RETRY_UNTIL_ELAPSED_MAX_ELAPSED_TIME_MS = "curator.retryUntilElapsed.maxElapsedTimeMs"; 28 | public static final String RETRY_TYPE_RETRY_UNTIL_ELAPSED_SLEEP_MS_BETWEEN_RETRIES = "curator.retryUntilElapsed.sleepMsBetweenRetries"; 29 | } -------------------------------------------------------------------------------- /aquarius-common-zookeeper/src/main/java/com/nepxion/aquarius/common/curator/entity/RetryTypeEnum.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.curator.entity; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | public enum RetryTypeEnum { 13 | EXPONENTIAL_BACKOFF_RETRY("exponentialBackoffRetry"), 14 | BOUNDED_EXPONENTIAL_BACKOFF_RETRY("boundedExponentialBackoffRetry"), 15 | RETRY_NTIMES("retryNTimes"), 16 | RETRY_FOREVER("retryForever"), 17 | RETRY_UNTIL_ELAPSED("retryUntilElapsed"); 18 | 19 | private String value; 20 | 21 | private RetryTypeEnum(String value) { 22 | this.value = value; 23 | } 24 | 25 | public String getValue() { 26 | return value; 27 | } 28 | 29 | public static RetryTypeEnum fromString(String value) { 30 | for (RetryTypeEnum type : RetryTypeEnum.values()) { 31 | if (type.getValue().equalsIgnoreCase(value.trim())) { 32 | return type; 33 | } 34 | } 35 | 36 | throw new IllegalArgumentException("Mismatched type with value=" + value); 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return value; 42 | } 43 | } -------------------------------------------------------------------------------- /aquarius-common-zookeeper/src/main/java/com/nepxion/aquarius/common/curator/exception/CuratorException.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.curator.exception; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | public class CuratorException extends RuntimeException { 13 | private static final long serialVersionUID = 851864048447611118L; 14 | 15 | public CuratorException() { 16 | super(); 17 | } 18 | 19 | public CuratorException(String message) { 20 | super(message); 21 | } 22 | 23 | public CuratorException(String message, Throwable cause) { 24 | super(message, cause); 25 | } 26 | 27 | public CuratorException(Throwable cause) { 28 | super(cause); 29 | } 30 | } -------------------------------------------------------------------------------- /aquarius-common-zookeeper/src/main/java/com/nepxion/aquarius/common/curator/handler/CuratorHandler.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.curator.handler; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.util.List; 13 | import java.util.concurrent.TimeUnit; 14 | 15 | import org.apache.curator.framework.CuratorFramework; 16 | import org.apache.zookeeper.CreateMode; 17 | import org.apache.zookeeper.data.Stat; 18 | 19 | public interface CuratorHandler { 20 | // 启动ZooKeeper客户端 21 | public void start(); 22 | 23 | // 启动ZooKeeper客户端,直到第一次连接成功 24 | public void startAndBlock() throws Exception; 25 | 26 | // 启动ZooKeeper客户端,直到第一次连接成功,为每一次连接配置超时 27 | public void startAndBlock(int maxWaitTime, TimeUnit units) throws Exception; 28 | 29 | // 关闭ZooKeeper客户端连接 30 | public void close(); 31 | 32 | // 获取ZooKeeper客户端是否初始化 33 | public boolean isInitialized(); 34 | 35 | // 获取ZooKeeper客户端连接是否正常 36 | public boolean isStarted(); 37 | 38 | // 检查ZooKeeper是否是启动状态 39 | public void validateStartedStatus(); 40 | 41 | // 检查ZooKeeper是否是关闭状态 42 | public void validateClosedStatus(); 43 | 44 | // 获取ZooKeeper客户端 45 | public CuratorFramework getCurator(); 46 | 47 | // 判断路径是否存在 48 | public boolean pathExist(String path) throws Exception; 49 | 50 | // 判断stat是否存在 51 | public Stat getPathStat(String path) throws Exception; 52 | 53 | // 创建路径 54 | public void createPath(String path) throws Exception; 55 | 56 | // 创建路径,并写入数据 57 | public void createPath(String path, byte[] data) throws Exception; 58 | 59 | // 创建路径 60 | public void createPath(String path, CreateMode mode) throws Exception; 61 | 62 | // 创建路径,并写入数据 63 | public void createPath(String path, byte[] data, CreateMode mode) throws Exception; 64 | 65 | // 删除路径 66 | public void deletePath(String path) throws Exception; 67 | 68 | // 获取子节点名称列表 69 | public List getChildNameList(String path) throws Exception; 70 | 71 | // 获取子节点路径列表 72 | public List getChildPathList(String path) throws Exception; 73 | 74 | // 组装根节点路径 75 | public String getRootPath(String prefix); 76 | 77 | // 组装节点路径 78 | public String getPath(String prefix, String key); 79 | } -------------------------------------------------------------------------------- /aquarius-common/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | aquarius-common 5 | Nepxion Aquarius Common 6 | jar 7 | 4.0.0 8 | Nepxion Aquarius is a list of distribution components based on Redis + Zookeeper with Nepxion Matrix AOP framework 9 | http://www.nepxion.com 10 | 11 | 12 | com.nepxion 13 | aquarius 14 | 2.0.13 15 | 16 | 17 | 18 | 19 | ${project.groupId} 20 | banner 21 | 22 | 23 | 24 | org.apache.commons 25 | commons-lang3 26 | 27 | 28 | 29 | org.apache.commons 30 | commons-collections4 31 | 32 | 33 | 34 | commons-io 35 | commons-io 36 | 37 | 38 | 39 | org.springframework 40 | spring-context 41 | 42 | 43 | -------------------------------------------------------------------------------- /aquarius-common/src/main/java/com/nepxion/aquarius/common/condition/AquariusCondition.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.condition; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.apache.commons.lang3.StringUtils; 13 | import org.springframework.context.annotation.Condition; 14 | import org.springframework.context.annotation.ConditionContext; 15 | import org.springframework.core.type.AnnotatedTypeMetadata; 16 | 17 | public class AquariusCondition implements Condition { 18 | private String key; 19 | private String value; 20 | 21 | public AquariusCondition(String key, String value) { 22 | this.key = key; 23 | this.value = value; 24 | } 25 | 26 | @Override 27 | public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { 28 | String beanName = context.getEnvironment().getProperty(key); 29 | 30 | return StringUtils.equals(beanName, value); 31 | } 32 | } -------------------------------------------------------------------------------- /aquarius-common/src/main/java/com/nepxion/aquarius/common/configuration/AquariusConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.configuration; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.context.annotation.ComponentScan; 13 | import org.springframework.context.annotation.Configuration; 14 | 15 | import com.nepxion.aquarius.common.constant.AquariusConstant; 16 | import com.nepxion.banner.BannerConstant; 17 | import com.nepxion.banner.Description; 18 | import com.nepxion.banner.LogoBanner; 19 | import com.nepxion.banner.NepxionBanner; 20 | import com.taobao.text.Color; 21 | 22 | @Configuration 23 | @ComponentScan(basePackages = { "com.nepxion.aquarius.common.context" }) 24 | public class AquariusConfiguration { 25 | static { 26 | /*String bannerShown = System.getProperty(BannerConstant.BANNER_SHOWN, "true"); 27 | if (Boolean.valueOf(bannerShown)) { 28 | System.out.println(""); 29 | System.out.println("╔═══╗"); 30 | System.out.println("║╔═╗║"); 31 | System.out.println("║║ ║╠══╦╗╔╦══╦═╦╦╗╔╦══╗"); 32 | System.out.println("║╚═╝║╔╗║║║║╔╗║╔╬╣║║║══╣"); 33 | System.out.println("║╔═╗║╚╝║╚╝║╔╗║║║║╚╝╠══║"); 34 | System.out.println("╚╝ ╚╩═╗╠══╩╝╚╩╝╚╩══╩══╝"); 35 | System.out.println(" ║║"); 36 | System.out.println(" ╚╝"); 37 | System.out.println("Nepxion Aquarius v" + AquariusConstant.AQUARIUS_VERSION); 38 | System.out.println(""); 39 | }*/ 40 | 41 | LogoBanner logoBanner = new LogoBanner(AquariusConfiguration.class, "/com/nepxion/aquarius/resource/logo.txt", "Welcome to Nepxion", 8, 5, new Color[] { Color.red, Color.green, Color.cyan, Color.blue, Color.yellow, Color.magenta, Color.red, Color.green }, true); 42 | 43 | NepxionBanner.show(logoBanner, new Description(BannerConstant.VERSION + ":", AquariusConstant.AQUARIUS_VERSION, 0, 1), new Description(BannerConstant.GITHUB + ":", BannerConstant.NEPXION_GITHUB + "/Aquarius", 0, 1)); 44 | } 45 | } -------------------------------------------------------------------------------- /aquarius-common/src/main/java/com/nepxion/aquarius/common/constant/AquariusConstant.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.constant; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | public class AquariusConstant { 13 | public static final String AQUARIUS_VERSION = "2.0.13"; 14 | 15 | public static final String ENCODING_UTF_8 = "UTF-8"; 16 | public static final String ENCODING_GBK = "GBK"; 17 | public static final String ENCODING_ISO_8859_1 = "ISO-8859-1"; 18 | 19 | public static final String PREFIX = "prefix"; 20 | 21 | public static final String FREQUENT_LOG_PRINT = "frequent.log.print"; 22 | } -------------------------------------------------------------------------------- /aquarius-common/src/main/java/com/nepxion/aquarius/common/context/AquariusContextAware.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.context; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.beans.BeansException; 13 | import org.springframework.beans.factory.NoSuchBeanDefinitionException; 14 | import org.springframework.context.ApplicationContext; 15 | import org.springframework.context.ApplicationContextAware; 16 | import org.springframework.core.ResolvableType; 17 | import org.springframework.core.env.Environment; 18 | import org.springframework.stereotype.Component; 19 | 20 | @Component 21 | public class AquariusContextAware implements ApplicationContextAware { 22 | private static ApplicationContext applicationContext; 23 | 24 | private AquariusContextAware() { 25 | } 26 | 27 | private static void setContext(ApplicationContext applicationContext) { 28 | AquariusContextAware.applicationContext = applicationContext; 29 | } 30 | 31 | @Override 32 | public void setApplicationContext(ApplicationContext applicationContext) { 33 | AquariusContextAware.setContext(applicationContext); 34 | } 35 | 36 | public static Object getBean(String name) throws BeansException { 37 | return applicationContext.getBean(name); 38 | } 39 | 40 | public static T getBean(String name, Class requiredType) throws BeansException { 41 | return applicationContext.getBean(name, requiredType); 42 | } 43 | 44 | public static Object getBean(String name, Object... args) throws BeansException { 45 | return applicationContext.getBean(name, args); 46 | } 47 | 48 | public static T getBean(Class requiredType) throws BeansException { 49 | return applicationContext.getBean(requiredType); 50 | } 51 | 52 | public static T getBean(Class requiredType, Object... args) throws BeansException { 53 | return applicationContext.getBean(requiredType, args); 54 | } 55 | 56 | public static boolean containsBean(String name) { 57 | return applicationContext.containsBean(name); 58 | } 59 | 60 | public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { 61 | return applicationContext.isSingleton(name); 62 | } 63 | 64 | public static boolean isPrototype(String name) throws NoSuchBeanDefinitionException { 65 | return applicationContext.isPrototype(name); 66 | } 67 | 68 | public static boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException { 69 | return applicationContext.isTypeMatch(name, typeToMatch); 70 | } 71 | 72 | public static boolean isTypeMatch(String name, Class typeToMatch) throws NoSuchBeanDefinitionException { 73 | return applicationContext.isTypeMatch(name, typeToMatch); 74 | } 75 | 76 | public static Class getType(String name) throws NoSuchBeanDefinitionException { 77 | return applicationContext.getType(name); 78 | } 79 | 80 | public static String[] getAliases(String name) { 81 | return applicationContext.getAliases(name); 82 | } 83 | 84 | public static Environment getEnvironment() { 85 | return applicationContext.getEnvironment(); 86 | } 87 | } -------------------------------------------------------------------------------- /aquarius-common/src/main/java/com/nepxion/aquarius/common/exception/AquariusException.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.exception; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | public class AquariusException extends RuntimeException { 13 | private static final long serialVersionUID = 7895884193269203187L; 14 | 15 | public AquariusException() { 16 | super(); 17 | } 18 | 19 | public AquariusException(String message) { 20 | super(message); 21 | } 22 | 23 | public AquariusException(String message, Throwable cause) { 24 | super(message, cause); 25 | } 26 | 27 | public AquariusException(Throwable cause) { 28 | super(cause); 29 | } 30 | } -------------------------------------------------------------------------------- /aquarius-common/src/main/java/com/nepxion/aquarius/common/property/AquariusContent.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.property; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.io.File; 13 | import java.io.IOException; 14 | import java.io.InputStream; 15 | 16 | import org.apache.commons.io.FileUtils; 17 | import org.apache.commons.io.IOUtils; 18 | 19 | import com.nepxion.aquarius.common.util.IOUtil; 20 | 21 | public class AquariusContent { 22 | private String content; 23 | 24 | public AquariusContent(String path, String encoding) throws IOException { 25 | InputStream inputStream = null; 26 | try { 27 | inputStream = IOUtil.getInputStream(path); 28 | this.content = IOUtils.toString(inputStream, encoding); 29 | } finally { 30 | if (inputStream != null) { 31 | IOUtils.closeQuietly(inputStream); 32 | } 33 | } 34 | } 35 | 36 | public AquariusContent(File file, String encoding) throws IOException { 37 | this.content = FileUtils.readFileToString(file, encoding); 38 | } 39 | 40 | public AquariusContent(StringBuilder stringBuilder) throws IOException { 41 | this.content = stringBuilder.toString(); 42 | } 43 | 44 | public String getContent() { 45 | return content; 46 | } 47 | } -------------------------------------------------------------------------------- /aquarius-common/src/main/java/com/nepxion/aquarius/common/util/DateUtil.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.util; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.time.Instant; 13 | import java.time.LocalDateTime; 14 | import java.time.ZoneId; 15 | import java.time.format.DateTimeFormatter; 16 | import java.util.Date; 17 | import java.util.Map; 18 | import java.util.concurrent.ConcurrentHashMap; 19 | 20 | public class DateUtil { 21 | private static volatile Map dateFormatMap = new ConcurrentHashMap(); 22 | 23 | public static String formatDate(Date date, String pattern) { 24 | DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(pattern); 25 | 26 | ZoneId zoneId = ZoneId.systemDefault(); 27 | Instant instant = date.toInstant(); 28 | LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId); 29 | 30 | return localDateTime.format(dateTimeFormatter); 31 | } 32 | 33 | public static Date parseDate(String date, String pattern) { 34 | DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(pattern); 35 | 36 | LocalDateTime localDateTime = LocalDateTime.parse(date, dateTimeFormatter); 37 | 38 | ZoneId zoneId = ZoneId.systemDefault(); 39 | Instant instant = localDateTime.atZone(zoneId).toInstant(); 40 | 41 | return Date.from(instant); 42 | } 43 | 44 | private static DateTimeFormatter getDateTimeFormatter(String pattern) { 45 | DateTimeFormatter dateTimeFormatter = dateFormatMap.get(pattern); 46 | if (dateTimeFormatter == null) { 47 | DateTimeFormatter newDateTimeFormatter = DateTimeFormatter.ofPattern(pattern); 48 | dateTimeFormatter = dateFormatMap.putIfAbsent(pattern, newDateTimeFormatter); 49 | if (dateTimeFormatter == null) { 50 | dateTimeFormatter = newDateTimeFormatter; 51 | } 52 | } 53 | 54 | return dateTimeFormatter; 55 | } 56 | } -------------------------------------------------------------------------------- /aquarius-common/src/main/java/com/nepxion/aquarius/common/util/IOUtil.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.util; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.io.FileInputStream; 13 | import java.io.IOException; 14 | import java.io.InputStream; 15 | 16 | public class IOUtil { 17 | public static InputStream getInputStream(String path) throws IOException { 18 | // 从Resource路径获取 19 | InputStream inputStream = IOUtil.class.getClassLoader().getResourceAsStream(path); 20 | if (inputStream == null) { 21 | // 从文件路径获取 22 | inputStream = new FileInputStream(path); 23 | } 24 | 25 | return inputStream; 26 | } 27 | } -------------------------------------------------------------------------------- /aquarius-common/src/main/java/com/nepxion/aquarius/common/util/KeyUtil.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.util; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | public class KeyUtil { 13 | public static String getCompositeKey(String prefix, String name, String key) { 14 | return prefix + "_" + name + "_" + key; 15 | } 16 | 17 | public static String getCompositeWildcardKey(String prefix, String name) { 18 | return prefix + "_" + name + "*"; 19 | } 20 | 21 | public static String getCompositeWildcardKey(String key) { 22 | return key + "*"; 23 | } 24 | } -------------------------------------------------------------------------------- /aquarius-common/src/main/java/com/nepxion/aquarius/common/util/MathsUtil.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.util; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.apache.commons.lang3.StringUtils; 13 | 14 | public class MathsUtil { 15 | private static final char ASTERISK = '*'; 16 | 17 | public static Long calculate(String value) { 18 | if (StringUtils.isEmpty(value)) { 19 | return null; 20 | } 21 | 22 | long result = 1; 23 | try { 24 | String[] array = StringUtils.split(value, ASTERISK); 25 | for (String data : array) { 26 | result *= Long.parseLong(data.trim()); 27 | } 28 | } catch (Exception e) { 29 | return null; 30 | } 31 | 32 | return result; 33 | } 34 | } -------------------------------------------------------------------------------- /aquarius-common/src/main/java/com/nepxion/aquarius/common/util/StringUtil.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.common.util; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.text.DecimalFormat; 13 | import java.util.Map; 14 | import java.util.concurrent.ConcurrentHashMap; 15 | 16 | import org.apache.commons.lang3.ArrayUtils; 17 | 18 | public class StringUtil { 19 | private static volatile Map decimalFormatMap = new ConcurrentHashMap(); 20 | 21 | public static String formatString(long key, int length, String pattern) { 22 | String value = String.valueOf(key); 23 | if (value.length() < length) { 24 | DecimalFormat format = getDecimalFormat(pattern); 25 | return format.format(key); 26 | } else { 27 | return value.substring(value.length() - length, value.length()); 28 | } 29 | } 30 | 31 | private static DecimalFormat getDecimalFormat(String pattern) { 32 | DecimalFormat decimalFormat = decimalFormatMap.get(pattern); 33 | if (decimalFormat == null) { 34 | DecimalFormat newDecimalFormat = new DecimalFormat(); 35 | newDecimalFormat.applyPattern(pattern); 36 | decimalFormat = decimalFormatMap.putIfAbsent(pattern, newDecimalFormat); 37 | if (decimalFormat == null) { 38 | decimalFormat = newDecimalFormat; 39 | } 40 | } 41 | 42 | return decimalFormat; 43 | } 44 | 45 | public static String convert(String[] arrays) { 46 | if (ArrayUtils.isEmpty(arrays)) { 47 | return null; 48 | } 49 | 50 | StringBuilder builder = new StringBuilder(); 51 | for (String array : arrays) { 52 | builder.append(array).append(","); 53 | } 54 | 55 | String result = builder.toString(); 56 | result = result.substring(0, result.length() - 1); 57 | 58 | return result; 59 | } 60 | } -------------------------------------------------------------------------------- /aquarius-common/src/main/resources/com/nepxion/aquarius/resource/logo.txt: -------------------------------------------------------------------------------- 1 | ,---. 2 | / O \ 3 | | .-. | 4 | | | | | 5 | `--' `--' 6 | ,-----. 7 | ' .-. ' 8 | | | | | 9 | ' '-' '-. 10 | `-----'--' 11 | ,--. ,--. 12 | | | | | 13 | | | | | 14 | ' '-' ' 15 | `-----' 16 | ,---. 17 | / O \ 18 | | .-. | 19 | | | | | 20 | `--' `--' 21 | ,------. 22 | | .--. ' 23 | | '--'.' 24 | | |\ \ 25 | `--' '--' 26 | ,--. 27 | | | 28 | | | 29 | | | 30 | `--' 31 | ,--. ,--. 32 | | | | | 33 | | | | | 34 | ' '-' ' 35 | `-----' 36 | ,---. 37 | ' .-' 38 | `. `-. 39 | .-' | 40 | `-----' -------------------------------------------------------------------------------- /aquarius-id-generator-local/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | aquarius-id-generator-local 5 | Nepxion Aquarius Id Generator For Local 6 | jar 7 | 4.0.0 8 | Nepxion Aquarius is a list of distribution components based on Redis + Zookeeper with Nepxion Matrix AOP framework 9 | http://www.nepxion.com 10 | 11 | 12 | com.nepxion 13 | aquarius 14 | 2.0.13 15 | 16 | 17 | 18 | 19 | ${project.groupId} 20 | aquarius-common 21 | 22 | 23 | -------------------------------------------------------------------------------- /aquarius-id-generator-local/src/main/java/com/nepxion/aquarius/idgenerator/local/LocalIdGenerator.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.idgenerator.local; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | public interface LocalIdGenerator { 13 | String nextUniqueId(long dataCenterId, long machineId) throws Exception; 14 | 15 | String nextUniqueId(String startTimestamp, long dataCenterId, long machineId) throws Exception; 16 | 17 | /** 18 | * 获取全局唯一ID,根据Twitter雪花ID算法。为兼顾到前端失精问题,把返回值long改成String 19 | * SnowFlake算法用来生成64位的ID,刚好可以用long整型存储,能够用于分布式系统中生产唯一的ID, 并且生成的ID有大致的顺序。 在这次实现中,生成的64位ID可以分成5个部分: 20 | * 0 - 41位时间戳 - 5位数据中心标识 - 5位机器标识 - 12位序列号 21 | * @param startTimestamp 起始计算时间戳(默认2017-01-01) 22 | * @param dataCenterId 数据中心标识ID 23 | * @param machineId 机器标识ID 24 | * @return String 25 | */ 26 | String nextUniqueId(long startTimestamp, long dataCenterId, long machineId) throws Exception; 27 | 28 | String[] nextUniqueIds(long dataCenterId, long machineId, int count) throws Exception; 29 | 30 | String[] nextUniqueIds(String startTimestamp, long dataCenterId, long machineId, int count) throws Exception; 31 | 32 | String[] nextUniqueIds(long startTimestamp, long dataCenterId, long machineId, int count) throws Exception; 33 | } -------------------------------------------------------------------------------- /aquarius-id-generator-local/src/main/java/com/nepxion/aquarius/idgenerator/local/configuration/LocalIdGeneratorConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.idgenerator.local.configuration; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.context.annotation.Bean; 13 | import org.springframework.context.annotation.Configuration; 14 | 15 | import com.nepxion.aquarius.idgenerator.local.LocalIdGenerator; 16 | import com.nepxion.aquarius.idgenerator.local.impl.LocalIdGeneratorImpl; 17 | 18 | @Configuration 19 | public class LocalIdGeneratorConfiguration { 20 | @Bean 21 | public LocalIdGenerator localIdGenerator() { 22 | return new LocalIdGeneratorImpl(); 23 | } 24 | } -------------------------------------------------------------------------------- /aquarius-id-generator-redis/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | aquarius-id-generator-redis 5 | Nepxion Aquarius Id Generator For Redis 6 | jar 7 | 4.0.0 8 | Nepxion Aquarius is a list of distribution components based on Redis + Zookeeper with Nepxion Matrix AOP framework 9 | http://www.nepxion.com 10 | 11 | 12 | com.nepxion 13 | aquarius 14 | 2.0.13 15 | 16 | 17 | 18 | 19 | ${project.groupId} 20 | aquarius-common-redis 21 | 22 | 23 | -------------------------------------------------------------------------------- /aquarius-id-generator-redis/src/main/java/com/nepxion/aquarius/idgenerator/redis/RedisIdGenerator.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.idgenerator.redis; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | public interface RedisIdGenerator { 13 | /** 14 | * 获取全局唯一ID 15 | * ID规则: 16 | * 1. ID的前半部分为yyyyMMddHHmmssSSS格式的17位数字 17 | * 2. ID的后半部分为由length(最大为8位,如果length大于8,则取8)决定,取值Redis对应Value,如果小于length所对应的数位,如果不足该数位,前面补足0 18 | * 例如Redis对应Value为1234,length为8,那么ID的后半部分为00001234;length为2,那么ID的后半部分为34 19 | * @param name 资源名字 20 | * @param key 资源Key 21 | * @param step 递增值 22 | * @param length 长度 23 | * @return String 24 | */ 25 | String nextUniqueId(String name, String key, int step, int length) throws Exception; 26 | 27 | String nextUniqueId(String compositeKey, int step, int length) throws Exception; 28 | 29 | String[] nextUniqueIds(String name, String key, int step, int length, int count) throws Exception; 30 | 31 | String[] nextUniqueIds(String compositeKey, int step, int length, int count) throws Exception; 32 | } -------------------------------------------------------------------------------- /aquarius-id-generator-redis/src/main/java/com/nepxion/aquarius/idgenerator/redis/configuration/RedisIdGeneratorConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.idgenerator.redis.configuration; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 13 | import org.springframework.context.annotation.Bean; 14 | import org.springframework.context.annotation.Configuration; 15 | import org.springframework.context.annotation.Import; 16 | 17 | import com.nepxion.aquarius.common.redis.configuration.RedisConfiguration; 18 | import com.nepxion.aquarius.common.redis.handler.RedisHandler; 19 | import com.nepxion.aquarius.common.redis.handler.RedisHandlerImpl; 20 | import com.nepxion.aquarius.idgenerator.redis.RedisIdGenerator; 21 | import com.nepxion.aquarius.idgenerator.redis.impl.RedisIdGeneratorImpl; 22 | 23 | @Configuration 24 | @Import({ RedisConfiguration.class }) 25 | public class RedisIdGeneratorConfiguration { 26 | @Bean 27 | public RedisIdGenerator redisIdGenerator() { 28 | return new RedisIdGeneratorImpl(); 29 | } 30 | 31 | @Bean 32 | @ConditionalOnMissingBean 33 | public RedisHandler redisHandler() { 34 | return new RedisHandlerImpl(); 35 | } 36 | } -------------------------------------------------------------------------------- /aquarius-id-generator-starter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | aquarius-id-generator-starter 5 | Nepxion Aquarius Starter For Id Generator 6 | jar 7 | 4.0.0 8 | Nepxion Aquarius is a list of distribution components based on Redis + Zookeeper with Nepxion Matrix AOP framework 9 | http://www.nepxion.com 10 | 11 | 12 | com.nepxion 13 | aquarius 14 | 2.0.13 15 | 16 | 17 | 18 | 19 | ${project.groupId} 20 | aquarius-common 21 | 22 | 23 | 24 | ${project.groupId} 25 | aquarius-common-redis 26 | 27 | 28 | 29 | ${project.groupId} 30 | aquarius-common-zookeeper 31 | 32 | 33 | 34 | ${project.groupId} 35 | aquarius-id-generator-redis 36 | 37 | 38 | 39 | ${project.groupId} 40 | aquarius-id-generator-zookeeper 41 | 42 | 43 | 44 | ${project.groupId} 45 | aquarius-id-generator-local 46 | 47 | 48 | 49 | ${project.groupId} 50 | matrix-aop-starter 51 | 52 | 53 | -------------------------------------------------------------------------------- /aquarius-id-generator-starter/src/main/java/com/nepxion/aquarius/idgenerator/annotation/EnableLocalIdGenerator.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.idgenerator.annotation; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.lang.annotation.Documented; 13 | import java.lang.annotation.ElementType; 14 | import java.lang.annotation.Inherited; 15 | import java.lang.annotation.Retention; 16 | import java.lang.annotation.RetentionPolicy; 17 | import java.lang.annotation.Target; 18 | 19 | import org.springframework.context.annotation.Import; 20 | 21 | import com.nepxion.aquarius.idgenerator.aop.LocalIdGeneratorImportSelector; 22 | 23 | @Target(ElementType.TYPE) 24 | @Retention(RetentionPolicy.RUNTIME) 25 | @Documented 26 | @Inherited 27 | @Import(LocalIdGeneratorImportSelector.class) 28 | public @interface EnableLocalIdGenerator { 29 | 30 | } -------------------------------------------------------------------------------- /aquarius-id-generator-starter/src/main/java/com/nepxion/aquarius/idgenerator/annotation/EnableRedisIdGenerator.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.idgenerator.annotation; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.lang.annotation.Documented; 13 | import java.lang.annotation.ElementType; 14 | import java.lang.annotation.Inherited; 15 | import java.lang.annotation.Retention; 16 | import java.lang.annotation.RetentionPolicy; 17 | import java.lang.annotation.Target; 18 | 19 | import org.springframework.context.annotation.Import; 20 | 21 | import com.nepxion.aquarius.idgenerator.aop.RedisIdGeneratorImportSelector; 22 | 23 | @Target(ElementType.TYPE) 24 | @Retention(RetentionPolicy.RUNTIME) 25 | @Documented 26 | @Inherited 27 | @Import(RedisIdGeneratorImportSelector.class) 28 | public @interface EnableRedisIdGenerator { 29 | 30 | } -------------------------------------------------------------------------------- /aquarius-id-generator-starter/src/main/java/com/nepxion/aquarius/idgenerator/annotation/EnableZookeeperIdGenerator.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.idgenerator.annotation; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.lang.annotation.Documented; 13 | import java.lang.annotation.ElementType; 14 | import java.lang.annotation.Inherited; 15 | import java.lang.annotation.Retention; 16 | import java.lang.annotation.RetentionPolicy; 17 | import java.lang.annotation.Target; 18 | 19 | import org.springframework.context.annotation.Import; 20 | 21 | import com.nepxion.aquarius.idgenerator.aop.ZookeeperIdGeneratorImportSelector; 22 | 23 | @Target(ElementType.TYPE) 24 | @Retention(RetentionPolicy.RUNTIME) 25 | @Documented 26 | @Inherited 27 | @Import(ZookeeperIdGeneratorImportSelector.class) 28 | public @interface EnableZookeeperIdGenerator { 29 | 30 | } -------------------------------------------------------------------------------- /aquarius-id-generator-starter/src/main/java/com/nepxion/aquarius/idgenerator/aop/LocalIdGeneratorImportSelector.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.idgenerator.aop; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.core.Ordered; 13 | import org.springframework.core.annotation.Order; 14 | 15 | import com.nepxion.aquarius.idgenerator.annotation.EnableLocalIdGenerator; 16 | import com.nepxion.aquarius.idgenerator.constant.IdGeneratorConstant; 17 | import com.nepxion.matrix.selector.AbstractImportSelector; 18 | import com.nepxion.matrix.selector.RelaxedPropertyResolver; 19 | 20 | @Order(Ordered.LOWEST_PRECEDENCE - 100) 21 | public class LocalIdGeneratorImportSelector extends AbstractImportSelector { 22 | @Override 23 | protected boolean isEnabled() { 24 | return new RelaxedPropertyResolver(getEnvironment()).getProperty(IdGeneratorConstant.IDGENERATOR_ENABLED, Boolean.class, Boolean.TRUE); 25 | } 26 | } -------------------------------------------------------------------------------- /aquarius-id-generator-starter/src/main/java/com/nepxion/aquarius/idgenerator/aop/RedisIdGeneratorImportSelector.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.idgenerator.aop; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.core.Ordered; 13 | import org.springframework.core.annotation.Order; 14 | 15 | import com.nepxion.aquarius.idgenerator.annotation.EnableRedisIdGenerator; 16 | import com.nepxion.aquarius.idgenerator.constant.IdGeneratorConstant; 17 | import com.nepxion.matrix.selector.AbstractImportSelector; 18 | import com.nepxion.matrix.selector.RelaxedPropertyResolver; 19 | 20 | @Order(Ordered.LOWEST_PRECEDENCE - 100) 21 | public class RedisIdGeneratorImportSelector extends AbstractImportSelector { 22 | @Override 23 | protected boolean isEnabled() { 24 | return new RelaxedPropertyResolver(getEnvironment()).getProperty(IdGeneratorConstant.IDGENERATOR_ENABLED, Boolean.class, Boolean.TRUE); 25 | } 26 | } -------------------------------------------------------------------------------- /aquarius-id-generator-starter/src/main/java/com/nepxion/aquarius/idgenerator/aop/ZookeeperIdGeneratorImportSelector.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.idgenerator.aop; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.core.Ordered; 13 | import org.springframework.core.annotation.Order; 14 | 15 | import com.nepxion.aquarius.idgenerator.annotation.EnableZookeeperIdGenerator; 16 | import com.nepxion.aquarius.idgenerator.constant.IdGeneratorConstant; 17 | import com.nepxion.matrix.selector.AbstractImportSelector; 18 | import com.nepxion.matrix.selector.RelaxedPropertyResolver; 19 | 20 | @Order(Ordered.LOWEST_PRECEDENCE - 100) 21 | public class ZookeeperIdGeneratorImportSelector extends AbstractImportSelector { 22 | @Override 23 | protected boolean isEnabled() { 24 | return new RelaxedPropertyResolver(getEnvironment()).getProperty(IdGeneratorConstant.IDGENERATOR_ENABLED, Boolean.class, Boolean.TRUE); 25 | } 26 | } -------------------------------------------------------------------------------- /aquarius-id-generator-starter/src/main/java/com/nepxion/aquarius/idgenerator/configuration/LocalIdGeneratorConfig.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.idgenerator.configuration; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.context.annotation.Configuration; 13 | import org.springframework.context.annotation.Import; 14 | 15 | import com.nepxion.aquarius.common.configuration.AquariusConfiguration; 16 | import com.nepxion.aquarius.idgenerator.local.configuration.LocalIdGeneratorConfiguration; 17 | 18 | @Configuration 19 | @Import({ AquariusConfiguration.class, LocalIdGeneratorConfiguration.class }) 20 | public class LocalIdGeneratorConfig { 21 | 22 | } -------------------------------------------------------------------------------- /aquarius-id-generator-starter/src/main/java/com/nepxion/aquarius/idgenerator/configuration/RedisIdGeneratorConfig.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.idgenerator.configuration; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.context.annotation.Configuration; 13 | import org.springframework.context.annotation.Import; 14 | 15 | import com.nepxion.aquarius.common.configuration.AquariusConfiguration; 16 | import com.nepxion.aquarius.idgenerator.redis.configuration.RedisIdGeneratorConfiguration; 17 | 18 | @Configuration 19 | @Import({ AquariusConfiguration.class, RedisIdGeneratorConfiguration.class }) 20 | public class RedisIdGeneratorConfig { 21 | 22 | } -------------------------------------------------------------------------------- /aquarius-id-generator-starter/src/main/java/com/nepxion/aquarius/idgenerator/configuration/ZookeeperIdGeneratorConfig.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.idgenerator.configuration; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.context.annotation.Configuration; 13 | import org.springframework.context.annotation.Import; 14 | 15 | import com.nepxion.aquarius.common.configuration.AquariusConfiguration; 16 | import com.nepxion.aquarius.idgenerator.zookeeper.configuration.ZookeeperIdGeneratorConfiguration; 17 | 18 | @Configuration 19 | @Import({ AquariusConfiguration.class, ZookeeperIdGeneratorConfiguration.class }) 20 | public class ZookeeperIdGeneratorConfig { 21 | 22 | } -------------------------------------------------------------------------------- /aquarius-id-generator-starter/src/main/java/com/nepxion/aquarius/idgenerator/constant/IdGeneratorConstant.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.idgenerator.constant; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | public class IdGeneratorConstant { 13 | public static final String IDGENERATOR_ENABLED = "idgenerator.enabled"; 14 | } -------------------------------------------------------------------------------- /aquarius-id-generator-starter/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | com.nepxion.aquarius.idgenerator.annotation.EnableLocalIdGenerator=\ 2 | com.nepxion.aquarius.idgenerator.configuration.LocalIdGeneratorConfig 3 | 4 | com.nepxion.aquarius.idgenerator.annotation.EnableRedisIdGenerator=\ 5 | com.nepxion.aquarius.idgenerator.configuration.RedisIdGeneratorConfig 6 | 7 | com.nepxion.aquarius.idgenerator.annotation.EnableZookeeperIdGenerator=\ 8 | com.nepxion.aquarius.idgenerator.configuration.ZookeeperIdGeneratorConfig -------------------------------------------------------------------------------- /aquarius-id-generator-zookeeper/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | aquarius-id-generator-zookeeper 5 | Nepxion Aquarius Id Generator For Zookeeper 6 | jar 7 | 4.0.0 8 | Nepxion Aquarius is a list of distribution components based on Redis + Zookeeper with Nepxion Matrix AOP framework 9 | http://www.nepxion.com 10 | 11 | 12 | com.nepxion 13 | aquarius 14 | 2.0.13 15 | 16 | 17 | 18 | 19 | ${project.groupId} 20 | aquarius-common-zookeeper 21 | 22 | 23 | 24 | org.apache.curator 25 | curator-framework 26 | provided 27 | 28 | 29 | 30 | org.apache.curator 31 | curator-recipes 32 | provided 33 | 34 | 35 | -------------------------------------------------------------------------------- /aquarius-id-generator-zookeeper/src/main/java/com/nepxion/aquarius/idgenerator/zookeeper/ZookeeperIdGenerator.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.idgenerator.zookeeper; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | public interface ZookeeperIdGenerator { 13 | /** 14 | * 获取全局唯一序号 15 | * @param name 资源名字 16 | * @param key 资源Key 17 | * @return String 18 | * @throws Exception 异常 19 | */ 20 | String nextSequenceId(String name, String key) throws Exception; 21 | 22 | String nextSequenceId(String compositeKey) throws Exception; 23 | 24 | String[] nextSequenceIds(String name, String key, int count) throws Exception; 25 | 26 | String[] nextSequenceIds(String compositeKey, int count) throws Exception; 27 | } -------------------------------------------------------------------------------- /aquarius-id-generator-zookeeper/src/main/java/com/nepxion/aquarius/idgenerator/zookeeper/configuration/ZookeeperIdGeneratorConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.idgenerator.zookeeper.configuration; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 13 | import org.springframework.context.annotation.Bean; 14 | import org.springframework.context.annotation.Configuration; 15 | 16 | import com.nepxion.aquarius.common.curator.handler.CuratorHandler; 17 | import com.nepxion.aquarius.common.curator.handler.CuratorHandlerImpl; 18 | import com.nepxion.aquarius.idgenerator.zookeeper.ZookeeperIdGenerator; 19 | import com.nepxion.aquarius.idgenerator.zookeeper.impl.ZookeeperIdGeneratorImpl; 20 | 21 | @Configuration 22 | public class ZookeeperIdGeneratorConfiguration { 23 | @Bean 24 | public ZookeeperIdGenerator zookeeperIdGenerator() { 25 | return new ZookeeperIdGeneratorImpl(); 26 | } 27 | 28 | @Bean 29 | @ConditionalOnMissingBean 30 | public CuratorHandler curatorHandler() { 31 | return new CuratorHandlerImpl(); 32 | } 33 | } -------------------------------------------------------------------------------- /aquarius-limit-aop/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | aquarius-limit-aop 5 | Nepxion Aquarius Limit For Aop 6 | jar 7 | 4.0.0 8 | Nepxion Aquarius is a list of distribution components based on Redis + Zookeeper with Nepxion Matrix AOP framework 9 | http://www.nepxion.com 10 | 11 | 12 | com.nepxion 13 | aquarius 14 | 2.0.13 15 | 16 | 17 | 18 | 19 | ${project.groupId} 20 | aquarius-common 21 | 22 | 23 | 24 | ${project.groupId} 25 | matrix-aop-starter 26 | 27 | 28 | -------------------------------------------------------------------------------- /aquarius-limit-aop/src/main/java/com/nepxion/aquarius/limit/LimitDelegate.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.limit; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.aopalliance.intercept.MethodInvocation; 13 | 14 | public interface LimitDelegate { 15 | Object invoke(MethodInvocation invocation, String key, int limitPeriod, int limitCount) throws Throwable; 16 | } -------------------------------------------------------------------------------- /aquarius-limit-aop/src/main/java/com/nepxion/aquarius/limit/LimitExecutor.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.limit; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | public interface LimitExecutor { 13 | /** 14 | * 在给定的时间段里最多的访问限制次数(超出次数返回false);等下个时间段开始,才允许再次被访问(返回true),周而复始 15 | * @param name 资源名字 16 | * @param key 资源Key 17 | * @param limitPeriod 给定的时间段(单位秒) 18 | * @param limitCount 最多的访问限制次数 19 | * @return boolean 20 | */ 21 | boolean tryAccess(String name, String key, int limitPeriod, int limitCount) throws Exception; 22 | 23 | boolean tryAccess(String compositeKey, int limitPeriod, int limitCount) throws Exception; 24 | } -------------------------------------------------------------------------------- /aquarius-limit-aop/src/main/java/com/nepxion/aquarius/limit/annotation/Limit.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.limit.annotation; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.lang.annotation.Documented; 13 | import java.lang.annotation.ElementType; 14 | import java.lang.annotation.Inherited; 15 | import java.lang.annotation.Retention; 16 | import java.lang.annotation.RetentionPolicy; 17 | import java.lang.annotation.Target; 18 | 19 | @Target({ ElementType.METHOD, ElementType.TYPE }) 20 | @Retention(RetentionPolicy.RUNTIME) 21 | @Inherited 22 | @Documented 23 | public @interface Limit { 24 | /** 25 | * 资源的名字 26 | * @return String 27 | */ 28 | String name() default ""; 29 | 30 | /** 31 | * 资源的key 32 | * @return String 33 | */ 34 | String key() default ""; 35 | 36 | /** 37 | * 给定的时间段 38 | * 单位秒 39 | * @return int 40 | */ 41 | int limitPeriod(); 42 | 43 | /** 44 | * 最多的访问限制次数 45 | * @return int 46 | */ 47 | int limitCount(); 48 | } -------------------------------------------------------------------------------- /aquarius-limit-aop/src/main/java/com/nepxion/aquarius/limit/aop/LimitAutoScanProxy.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.limit.aop; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.lang.annotation.Annotation; 13 | 14 | import com.nepxion.aquarius.limit.annotation.Limit; 15 | import com.nepxion.matrix.proxy.aop.DefaultAutoScanProxy; 16 | import com.nepxion.matrix.proxy.mode.ProxyMode; 17 | import com.nepxion.matrix.proxy.mode.ScanMode; 18 | 19 | public class LimitAutoScanProxy extends DefaultAutoScanProxy { 20 | private static final long serialVersionUID = -6456216398492047529L; 21 | 22 | private String[] commonInterceptorNames; 23 | 24 | @SuppressWarnings("rawtypes") 25 | private Class[] methodAnnotations; 26 | 27 | public LimitAutoScanProxy(String scanPackages) { 28 | super(scanPackages, ProxyMode.BY_METHOD_ANNOTATION_ONLY, ScanMode.FOR_METHOD_ANNOTATION_ONLY); 29 | } 30 | 31 | @Override 32 | protected String[] getCommonInterceptorNames() { 33 | if (commonInterceptorNames == null) { 34 | commonInterceptorNames = new String[] { "limitInterceptor" }; 35 | } 36 | 37 | return commonInterceptorNames; 38 | } 39 | 40 | @SuppressWarnings("unchecked") 41 | @Override 42 | protected Class[] getMethodAnnotations() { 43 | if (methodAnnotations == null) { 44 | methodAnnotations = new Class[] { Limit.class }; 45 | } 46 | 47 | return methodAnnotations; 48 | } 49 | } -------------------------------------------------------------------------------- /aquarius-limit-aop/src/main/java/com/nepxion/aquarius/limit/configuration/LimitAopConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.limit.configuration; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.beans.factory.annotation.Value; 13 | import org.springframework.context.annotation.Bean; 14 | import org.springframework.context.annotation.Configuration; 15 | 16 | import com.nepxion.aquarius.limit.aop.LimitAutoScanProxy; 17 | import com.nepxion.aquarius.limit.aop.LimitInterceptor; 18 | import com.nepxion.aquarius.limit.constant.LimitConstant; 19 | 20 | @Configuration 21 | public class LimitAopConfiguration { 22 | @Value("${" + LimitConstant.LIMIT_SCAN_PACKAGES + ":}") 23 | private String scanPackages; 24 | 25 | @Bean 26 | public LimitAutoScanProxy limitAutoScanProxy() { 27 | return new LimitAutoScanProxy(scanPackages); 28 | } 29 | 30 | @Bean 31 | public LimitInterceptor limitInterceptor() { 32 | return new LimitInterceptor(); 33 | } 34 | } -------------------------------------------------------------------------------- /aquarius-limit-aop/src/main/java/com/nepxion/aquarius/limit/constant/LimitConstant.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.limit.constant; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | public class LimitConstant { 13 | public static final String LIMIT_ENABLED = "limit.enabled"; 14 | 15 | public static final String LIMIT_TYPE = "limit.type"; 16 | 17 | public static final String LIMIT_TYPE_REDIS = "redisLimit"; 18 | public static final String LIMIT_TYPE_LOCAL = "localLimit"; 19 | 20 | public static final String LIMIT_AOP_EXCEPTION_IGNORE = "limit.aop.exception.ignore"; 21 | 22 | public static final String LIMIT_SCAN_PACKAGES = "limit.scan.packages"; 23 | } -------------------------------------------------------------------------------- /aquarius-limit-local/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | aquarius-limit-local 5 | Nepxion Aquarius Limit For Local 6 | jar 7 | 4.0.0 8 | Nepxion Aquarius is a list of distribution components based on Redis + Zookeeper with Nepxion Matrix AOP framework 9 | http://www.nepxion.com 10 | 11 | 12 | com.nepxion 13 | aquarius 14 | 2.0.13 15 | 16 | 17 | 18 | 19 | ${project.groupId} 20 | aquarius-limit-aop 21 | 22 | 23 | 24 | com.google.guava 25 | guava 26 | 27 | 28 | -------------------------------------------------------------------------------- /aquarius-limit-local/src/main/java/com/nepxion/aquarius/limit/local/condition/LocalLimitCondition.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.limit.local.condition; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import com.nepxion.aquarius.common.condition.AquariusCondition; 13 | import com.nepxion.aquarius.limit.constant.LimitConstant; 14 | 15 | public class LocalLimitCondition extends AquariusCondition { 16 | public LocalLimitCondition() { 17 | super(LimitConstant.LIMIT_TYPE, LimitConstant.LIMIT_TYPE_LOCAL); 18 | } 19 | } -------------------------------------------------------------------------------- /aquarius-limit-local/src/main/java/com/nepxion/aquarius/limit/local/configuration/LocalLimitConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.limit.local.configuration; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.context.annotation.Bean; 13 | import org.springframework.context.annotation.Conditional; 14 | import org.springframework.context.annotation.Configuration; 15 | 16 | import com.nepxion.aquarius.limit.LimitDelegate; 17 | import com.nepxion.aquarius.limit.LimitExecutor; 18 | import com.nepxion.aquarius.limit.local.condition.LocalLimitCondition; 19 | import com.nepxion.aquarius.limit.local.impl.GuavaLocalLimitExecutorImpl; 20 | import com.nepxion.aquarius.limit.local.impl.LocalLimitDelegateImpl; 21 | 22 | @Configuration 23 | public class LocalLimitConfiguration { 24 | @Bean 25 | @Conditional(LocalLimitCondition.class) 26 | public LimitDelegate localLimitDelegate() { 27 | return new LocalLimitDelegateImpl(); 28 | } 29 | 30 | @Bean 31 | @Conditional(LocalLimitCondition.class) 32 | public LimitExecutor localLimitExecutor() { 33 | return new GuavaLocalLimitExecutorImpl(); 34 | 35 | // return new JdkLimitExecutorImpl(); 36 | } 37 | } -------------------------------------------------------------------------------- /aquarius-limit-local/src/main/java/com/nepxion/aquarius/limit/local/impl/LocalLimitDelegateImpl.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.limit.local.impl; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.aopalliance.intercept.MethodInvocation; 13 | import org.slf4j.Logger; 14 | import org.slf4j.LoggerFactory; 15 | import org.springframework.beans.factory.annotation.Autowired; 16 | import org.springframework.beans.factory.annotation.Value; 17 | 18 | import com.nepxion.aquarius.common.exception.AquariusException; 19 | import com.nepxion.aquarius.limit.LimitDelegate; 20 | import com.nepxion.aquarius.limit.LimitExecutor; 21 | import com.nepxion.aquarius.limit.constant.LimitConstant; 22 | 23 | public class LocalLimitDelegateImpl implements LimitDelegate { 24 | private static final Logger LOG = LoggerFactory.getLogger(LocalLimitDelegateImpl.class); 25 | 26 | @Autowired 27 | private LimitExecutor limitExecutor; 28 | 29 | @Value("${" + LimitConstant.LIMIT_AOP_EXCEPTION_IGNORE + ":true}") 30 | private Boolean limitAopExceptionIgnore; 31 | 32 | @Override 33 | public Object invoke(MethodInvocation invocation, String key, int limitPeriod, int limitCount) throws Throwable { 34 | boolean status = true; 35 | try { 36 | status = limitExecutor.tryAccess(key, limitPeriod, limitCount); 37 | } catch (Exception e) { 38 | if (limitAopExceptionIgnore) { 39 | LOG.error("Exception occurs while Limit", e); 40 | 41 | return invocation.proceed(); 42 | } else { 43 | throw e; 44 | } 45 | } 46 | 47 | if (status) { 48 | return invocation.proceed(); 49 | } else { 50 | throw new AquariusException("Reach max limited access count=" + limitCount + " within period=" + limitPeriod + " seconds"); 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /aquarius-limit-redis/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | aquarius-limit-redis 5 | Nepxion Aquarius Limit For Redis 6 | jar 7 | 4.0.0 8 | Nepxion Aquarius is a list of distribution components based on Redis + Zookeeper with Nepxion Matrix AOP framework 9 | http://www.nepxion.com 10 | 11 | 12 | com.nepxion 13 | aquarius 14 | 2.0.13 15 | 16 | 17 | 18 | 19 | ${project.groupId} 20 | aquarius-common-redis 21 | 22 | 23 | 24 | ${project.groupId} 25 | aquarius-limit-aop 26 | 27 | 28 | -------------------------------------------------------------------------------- /aquarius-limit-redis/src/main/java/com/nepxion/aquarius/limit/redis/condition/RedisLimitCondition.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.limit.redis.condition; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import com.nepxion.aquarius.common.condition.AquariusCondition; 13 | import com.nepxion.aquarius.limit.constant.LimitConstant; 14 | 15 | public class RedisLimitCondition extends AquariusCondition { 16 | public RedisLimitCondition() { 17 | super(LimitConstant.LIMIT_TYPE, LimitConstant.LIMIT_TYPE_REDIS); 18 | } 19 | } -------------------------------------------------------------------------------- /aquarius-limit-redis/src/main/java/com/nepxion/aquarius/limit/redis/configuration/RedisLimitConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.limit.redis.configuration; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 13 | import org.springframework.context.annotation.Bean; 14 | import org.springframework.context.annotation.Conditional; 15 | import org.springframework.context.annotation.Configuration; 16 | import org.springframework.context.annotation.Import; 17 | 18 | import com.nepxion.aquarius.common.redis.configuration.RedisConfiguration; 19 | import com.nepxion.aquarius.common.redis.handler.RedisHandler; 20 | import com.nepxion.aquarius.common.redis.handler.RedisHandlerImpl; 21 | import com.nepxion.aquarius.limit.LimitDelegate; 22 | import com.nepxion.aquarius.limit.LimitExecutor; 23 | import com.nepxion.aquarius.limit.redis.condition.RedisLimitCondition; 24 | import com.nepxion.aquarius.limit.redis.impl.RedisLimitDelegateImpl; 25 | import com.nepxion.aquarius.limit.redis.impl.RedisLimitExecutorImpl; 26 | 27 | @Configuration 28 | @Import({ RedisConfiguration.class }) 29 | public class RedisLimitConfiguration { 30 | @Bean 31 | @Conditional(RedisLimitCondition.class) 32 | public LimitDelegate redisLimitDelegate() { 33 | return new RedisLimitDelegateImpl(); 34 | } 35 | 36 | @Bean 37 | @Conditional(RedisLimitCondition.class) 38 | public LimitExecutor redisLimitExecutor() { 39 | return new RedisLimitExecutorImpl(); 40 | } 41 | 42 | @Bean 43 | @Conditional(RedisLimitCondition.class) 44 | @ConditionalOnMissingBean 45 | public RedisHandler redisHandler() { 46 | return new RedisHandlerImpl(); 47 | } 48 | } -------------------------------------------------------------------------------- /aquarius-limit-redis/src/main/java/com/nepxion/aquarius/limit/redis/impl/RedisLimitDelegateImpl.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.limit.redis.impl; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.aopalliance.intercept.MethodInvocation; 13 | import org.slf4j.Logger; 14 | import org.slf4j.LoggerFactory; 15 | import org.springframework.beans.factory.annotation.Autowired; 16 | import org.springframework.beans.factory.annotation.Value; 17 | 18 | import com.nepxion.aquarius.common.exception.AquariusException; 19 | import com.nepxion.aquarius.limit.LimitDelegate; 20 | import com.nepxion.aquarius.limit.LimitExecutor; 21 | import com.nepxion.aquarius.limit.constant.LimitConstant; 22 | 23 | public class RedisLimitDelegateImpl implements LimitDelegate { 24 | private static final Logger LOG = LoggerFactory.getLogger(RedisLimitDelegateImpl.class); 25 | 26 | @Autowired 27 | private LimitExecutor limitExecutor; 28 | 29 | @Value("${" + LimitConstant.LIMIT_AOP_EXCEPTION_IGNORE + ":true}") 30 | private Boolean limitAopExceptionIgnore; 31 | 32 | @Override 33 | public Object invoke(MethodInvocation invocation, String key, int limitPeriod, int limitCount) throws Throwable { 34 | boolean status = true; 35 | try { 36 | status = limitExecutor.tryAccess(key, limitPeriod, limitCount); 37 | } catch (Exception e) { 38 | if (limitAopExceptionIgnore) { 39 | LOG.error("Redis exception occurs while Limit", e); 40 | 41 | return invocation.proceed(); 42 | } else { 43 | throw e; 44 | } 45 | } 46 | 47 | if (status) { 48 | return invocation.proceed(); 49 | } else { 50 | throw new AquariusException("Reach max limited access count=" + limitCount + " within period=" + limitPeriod + " seconds"); 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /aquarius-limit-starter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | aquarius-limit-starter 5 | Nepxion Aquarius Starter For Limit 6 | jar 7 | 4.0.0 8 | Nepxion Aquarius is a list of distribution components based on Redis + Zookeeper with Nepxion Matrix AOP framework 9 | http://www.nepxion.com 10 | 11 | 12 | com.nepxion 13 | aquarius 14 | 2.0.13 15 | 16 | 17 | 18 | 19 | ${project.groupId} 20 | aquarius-common 21 | 22 | 23 | 24 | ${project.groupId} 25 | aquarius-common-redis 26 | 27 | 28 | 29 | ${project.groupId} 30 | aquarius-limit-aop 31 | 32 | 33 | 34 | ${project.groupId} 35 | aquarius-limit-redis 36 | 37 | 38 | 39 | ${project.groupId} 40 | aquarius-limit-local 41 | 42 | 43 | -------------------------------------------------------------------------------- /aquarius-limit-starter/src/main/java/com/nepxion/aquarius/limit/annotation/EnableLimit.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.limit.annotation; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.lang.annotation.Documented; 13 | import java.lang.annotation.ElementType; 14 | import java.lang.annotation.Inherited; 15 | import java.lang.annotation.Retention; 16 | import java.lang.annotation.RetentionPolicy; 17 | import java.lang.annotation.Target; 18 | 19 | import org.springframework.context.annotation.Import; 20 | 21 | import com.nepxion.aquarius.limit.aop.LimitImportSelector; 22 | 23 | @Target(ElementType.TYPE) 24 | @Retention(RetentionPolicy.RUNTIME) 25 | @Documented 26 | @Inherited 27 | @Import(LimitImportSelector.class) 28 | public @interface EnableLimit { 29 | 30 | } -------------------------------------------------------------------------------- /aquarius-limit-starter/src/main/java/com/nepxion/aquarius/limit/aop/LimitImportSelector.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.limit.aop; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.core.Ordered; 13 | import org.springframework.core.annotation.Order; 14 | 15 | import com.nepxion.aquarius.limit.annotation.EnableLimit; 16 | import com.nepxion.aquarius.limit.constant.LimitConstant; 17 | import com.nepxion.matrix.selector.AbstractImportSelector; 18 | import com.nepxion.matrix.selector.RelaxedPropertyResolver; 19 | 20 | @Order(Ordered.LOWEST_PRECEDENCE - 100) 21 | public class LimitImportSelector extends AbstractImportSelector { 22 | @Override 23 | protected boolean isEnabled() { 24 | return new RelaxedPropertyResolver(getEnvironment()).getProperty(LimitConstant.LIMIT_ENABLED, Boolean.class, Boolean.TRUE); 25 | } 26 | } -------------------------------------------------------------------------------- /aquarius-limit-starter/src/main/java/com/nepxion/aquarius/limit/configuration/LimitConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.limit.configuration; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.context.annotation.Configuration; 13 | import org.springframework.context.annotation.Import; 14 | 15 | import com.nepxion.aquarius.common.configuration.AquariusConfiguration; 16 | import com.nepxion.aquarius.limit.local.configuration.LocalLimitConfiguration; 17 | import com.nepxion.aquarius.limit.redis.configuration.RedisLimitConfiguration; 18 | 19 | @Configuration 20 | @Import({ AquariusConfiguration.class, LimitAopConfiguration.class, RedisLimitConfiguration.class, LocalLimitConfiguration.class }) 21 | public class LimitConfiguration { 22 | 23 | } -------------------------------------------------------------------------------- /aquarius-limit-starter/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | com.nepxion.aquarius.limit.annotation.EnableLimit=\ 2 | com.nepxion.aquarius.limit.configuration.LimitConfiguration -------------------------------------------------------------------------------- /aquarius-lock-aop/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | aquarius-lock-aop 5 | Nepxion Aquarius Lock For Aop 6 | jar 7 | 4.0.0 8 | Nepxion Aquarius is a list of distribution components based on Redis + Zookeeper with Nepxion Matrix AOP framework 9 | http://www.nepxion.com 10 | 11 | 12 | com.nepxion 13 | aquarius 14 | 2.0.13 15 | 16 | 17 | 18 | 19 | ${project.groupId} 20 | aquarius-common 21 | 22 | 23 | 24 | ${project.groupId} 25 | matrix-aop-starter 26 | 27 | 28 | -------------------------------------------------------------------------------- /aquarius-lock-aop/src/main/java/com/nepxion/aquarius/lock/LockDelegate.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.lock; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.aopalliance.intercept.MethodInvocation; 13 | 14 | import com.nepxion.aquarius.lock.entity.LockType; 15 | 16 | public interface LockDelegate { 17 | Object invoke(MethodInvocation invocation, LockType lockType, String key, long leaseTime, long waitTime, boolean async, boolean fair) throws Throwable; 18 | } -------------------------------------------------------------------------------- /aquarius-lock-aop/src/main/java/com/nepxion/aquarius/lock/LockExecutor.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.lock; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import com.nepxion.aquarius.lock.entity.LockType; 13 | 14 | public interface LockExecutor { 15 | /** 16 | * 尝试获取锁,如果获取到锁,则返回锁对象,如果未获取到锁,则返回空 17 | * @param lockType 锁的类型,包括LOCK(普通锁),WRITE_LOCK(写锁),READ_LOCK(读锁) 18 | * @param name 锁的名字 19 | * @param key 锁的Key 20 | * @param leaseTime 持锁时间,持锁超过此时间则自动丢弃锁(单位毫秒) 21 | * @param waitTime 没有获取到锁时,等待时间(单位毫秒) 22 | * @param async 是否采用锁的异步执行方式 23 | * @param fair 是否采用公平锁 24 | * @return T 25 | * @throws Exception 异常 26 | */ 27 | T tryLock(LockType lockType, String name, String key, long leaseTime, long waitTime, boolean async, boolean fair) throws Exception; 28 | 29 | T tryLock(LockType lockType, String compositeKey, long leaseTime, long waitTime, boolean async, boolean fair) throws Exception; 30 | 31 | void unlock(T t) throws Exception; 32 | } -------------------------------------------------------------------------------- /aquarius-lock-aop/src/main/java/com/nepxion/aquarius/lock/annotation/Lock.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.lock.annotation; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.lang.annotation.Documented; 13 | import java.lang.annotation.ElementType; 14 | import java.lang.annotation.Inherited; 15 | import java.lang.annotation.Retention; 16 | import java.lang.annotation.RetentionPolicy; 17 | import java.lang.annotation.Target; 18 | 19 | @Target({ ElementType.METHOD, ElementType.TYPE }) 20 | @Retention(RetentionPolicy.RUNTIME) 21 | @Inherited 22 | @Documented 23 | public @interface Lock { 24 | /** 25 | * 锁的名字 26 | * @return String 27 | */ 28 | String name() default ""; 29 | 30 | /** 31 | * 锁的key 32 | * @return String 33 | */ 34 | String key() default ""; 35 | 36 | /** 37 | * 持锁时间,持锁超过此时间则自动丢弃锁 38 | * 单位毫秒,默认5秒 39 | * @return long 40 | */ 41 | long leaseTime() default 5000L; 42 | 43 | /** 44 | * 没有获取到锁时,等待时间 45 | * 单位毫秒,默认60秒 46 | * @return long 47 | */ 48 | long waitTime() default 60000L; 49 | 50 | /** 51 | * 是否采用锁的异步执行方式(异步拿锁,同步阻塞) 52 | * @return boolean 53 | */ 54 | boolean async() default false; 55 | 56 | /** 57 | * 是否采用公平锁 58 | * @return boolean 59 | */ 60 | boolean fair() default false; 61 | } -------------------------------------------------------------------------------- /aquarius-lock-aop/src/main/java/com/nepxion/aquarius/lock/annotation/ReadLock.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.lock.annotation; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.lang.annotation.Documented; 13 | import java.lang.annotation.ElementType; 14 | import java.lang.annotation.Inherited; 15 | import java.lang.annotation.Retention; 16 | import java.lang.annotation.RetentionPolicy; 17 | import java.lang.annotation.Target; 18 | 19 | @Target({ ElementType.METHOD, ElementType.TYPE }) 20 | @Retention(RetentionPolicy.RUNTIME) 21 | @Inherited 22 | @Documented 23 | public @interface ReadLock { 24 | /** 25 | * 锁的名字 26 | * @return String 27 | */ 28 | String name() default ""; 29 | 30 | /** 31 | * 锁的key 32 | * @return String 33 | */ 34 | String key() default ""; 35 | 36 | /** 37 | * 持锁时间,持锁超过此时间则自动丢弃锁 38 | * 单位毫秒,默认5秒 39 | * @return long 40 | */ 41 | long leaseTime() default 5000L; 42 | 43 | /** 44 | * 没有获取到锁时,等待时间 45 | * 单位毫秒,默认60秒 46 | * @return long 47 | */ 48 | long waitTime() default 60000L; 49 | 50 | /** 51 | * 是否采用锁的异步执行方式(异步拿锁,同步阻塞) 52 | * @return boolean 53 | */ 54 | boolean async() default false; 55 | 56 | /** 57 | * 是否采用公平锁 58 | * @return boolean 59 | */ 60 | boolean fair() default false; 61 | } -------------------------------------------------------------------------------- /aquarius-lock-aop/src/main/java/com/nepxion/aquarius/lock/annotation/WriteLock.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.lock.annotation; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.lang.annotation.Documented; 13 | import java.lang.annotation.ElementType; 14 | import java.lang.annotation.Inherited; 15 | import java.lang.annotation.Retention; 16 | import java.lang.annotation.RetentionPolicy; 17 | import java.lang.annotation.Target; 18 | 19 | @Target({ ElementType.METHOD, ElementType.TYPE }) 20 | @Retention(RetentionPolicy.RUNTIME) 21 | @Inherited 22 | @Documented 23 | public @interface WriteLock { 24 | /** 25 | * 锁的名字 26 | * @return String 27 | */ 28 | String name() default ""; 29 | 30 | /** 31 | * 锁的key 32 | * @return String 33 | */ 34 | String key() default ""; 35 | 36 | /** 37 | * 持锁时间,持锁超过此时间则自动丢弃锁 38 | * 单位毫秒,默认5秒 39 | * @return long 40 | */ 41 | long leaseTime() default 5000L; 42 | 43 | /** 44 | * 没有获取到锁时,等待时间 45 | * 单位毫秒,默认60秒 46 | * @return long 47 | */ 48 | long waitTime() default 60000L; 49 | 50 | /** 51 | * 是否采用锁的异步执行方式(异步拿锁,同步阻塞) 52 | * @return boolean 53 | */ 54 | boolean async() default false; 55 | 56 | /** 57 | * 是否采用公平锁 58 | * @return boolean 59 | */ 60 | boolean fair() default false; 61 | } -------------------------------------------------------------------------------- /aquarius-lock-aop/src/main/java/com/nepxion/aquarius/lock/aop/LockAutoScanProxy.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.lock.aop; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.lang.annotation.Annotation; 13 | 14 | import com.nepxion.aquarius.lock.annotation.Lock; 15 | import com.nepxion.aquarius.lock.annotation.ReadLock; 16 | import com.nepxion.aquarius.lock.annotation.WriteLock; 17 | import com.nepxion.matrix.proxy.aop.DefaultAutoScanProxy; 18 | import com.nepxion.matrix.proxy.mode.ProxyMode; 19 | import com.nepxion.matrix.proxy.mode.ScanMode; 20 | 21 | public class LockAutoScanProxy extends DefaultAutoScanProxy { 22 | private static final long serialVersionUID = -957037966342626931L; 23 | 24 | private String[] commonInterceptorNames; 25 | 26 | @SuppressWarnings("rawtypes") 27 | private Class[] methodAnnotations; 28 | 29 | public LockAutoScanProxy(String scanPackages) { 30 | super(scanPackages, ProxyMode.BY_METHOD_ANNOTATION_ONLY, ScanMode.FOR_METHOD_ANNOTATION_ONLY); 31 | } 32 | 33 | @Override 34 | protected String[] getCommonInterceptorNames() { 35 | if (commonInterceptorNames == null) { 36 | commonInterceptorNames = new String[] { "lockInterceptor" }; 37 | } 38 | 39 | return commonInterceptorNames; 40 | } 41 | 42 | @SuppressWarnings("unchecked") 43 | @Override 44 | protected Class[] getMethodAnnotations() { 45 | if (methodAnnotations == null) { 46 | methodAnnotations = new Class[] { Lock.class, ReadLock.class, WriteLock.class }; 47 | } 48 | 49 | return methodAnnotations; 50 | } 51 | } -------------------------------------------------------------------------------- /aquarius-lock-aop/src/main/java/com/nepxion/aquarius/lock/configuration/LockAopConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.lock.configuration; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.beans.factory.annotation.Value; 13 | import org.springframework.context.annotation.Bean; 14 | import org.springframework.context.annotation.Configuration; 15 | 16 | import com.nepxion.aquarius.lock.aop.LockAutoScanProxy; 17 | import com.nepxion.aquarius.lock.aop.LockInterceptor; 18 | import com.nepxion.aquarius.lock.constant.LockConstant; 19 | 20 | @Configuration 21 | public class LockAopConfiguration { 22 | @Value("${" + LockConstant.LOCK_SCAN_PACKAGES + ":}") 23 | private String scanPackages; 24 | 25 | @Bean 26 | public LockAutoScanProxy lockAutoScanProxy() { 27 | return new LockAutoScanProxy(scanPackages); 28 | } 29 | 30 | @Bean 31 | public LockInterceptor lockInterceptor() { 32 | return new LockInterceptor(); 33 | } 34 | } -------------------------------------------------------------------------------- /aquarius-lock-aop/src/main/java/com/nepxion/aquarius/lock/constant/LockConstant.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.lock.constant; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | public class LockConstant { 13 | public static final String LOCK_ENABLED = "lock.enabled"; 14 | 15 | public static final String LOCK_TYPE = "lock.type"; 16 | 17 | public static final String LOCK_TYPE_REDIS = "redisLock"; 18 | public static final String LOCK_TYPE_ZOOKEEPER = "zookeeperLock"; 19 | public static final String LOCK_TYPE_LOCAL = "localLock"; 20 | 21 | public static final String LOCK_AOP_EXCEPTION_IGNORE = "lock.aop.exception.ignore"; 22 | 23 | public static final String LOCK_SCAN_PACKAGES = "lock.scan.packages"; 24 | } -------------------------------------------------------------------------------- /aquarius-lock-aop/src/main/java/com/nepxion/aquarius/lock/entity/LockType.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.lock.entity; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | public enum LockType { 13 | // 普通锁 14 | LOCK("Lock"), 15 | 16 | // 读锁 17 | READ_LOCK("ReadLock"), 18 | 19 | // 写锁 20 | WRITE_LOCK("WriteLock"); 21 | 22 | private String value; 23 | 24 | private LockType(String value) { 25 | this.value = value; 26 | } 27 | 28 | public String getValue() { 29 | return value; 30 | } 31 | 32 | public static LockType fromString(String value) { 33 | for (LockType type : LockType.values()) { 34 | if (type.getValue().equalsIgnoreCase(value.trim())) { 35 | return type; 36 | } 37 | } 38 | 39 | throw new IllegalArgumentException("Mismatched type with value=" + value); 40 | } 41 | 42 | @Override 43 | public String toString() { 44 | return value; 45 | } 46 | } -------------------------------------------------------------------------------- /aquarius-lock-local/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | aquarius-lock-local 5 | Nepxion Aquarius Lock For Local 6 | jar 7 | 4.0.0 8 | Nepxion Aquarius is a list of distribution components based on Redis + Zookeeper with Nepxion Matrix AOP framework 9 | http://www.nepxion.com 10 | 11 | 12 | com.nepxion 13 | aquarius 14 | 2.0.13 15 | 16 | 17 | 18 | 19 | ${project.groupId} 20 | aquarius-lock-aop 21 | 22 | 23 | -------------------------------------------------------------------------------- /aquarius-lock-local/src/main/java/com/nepxion/aquarius/lock/local/condition/LocalLockCondition.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.lock.local.condition; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import com.nepxion.aquarius.common.condition.AquariusCondition; 13 | import com.nepxion.aquarius.lock.constant.LockConstant; 14 | 15 | public class LocalLockCondition extends AquariusCondition { 16 | public LocalLockCondition() { 17 | super(LockConstant.LOCK_TYPE, LockConstant.LOCK_TYPE_LOCAL); 18 | } 19 | } -------------------------------------------------------------------------------- /aquarius-lock-local/src/main/java/com/nepxion/aquarius/lock/local/configuration/LocalLockConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.lock.local.configuration; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.util.concurrent.locks.Lock; 13 | 14 | import org.springframework.context.annotation.Bean; 15 | import org.springframework.context.annotation.Conditional; 16 | import org.springframework.context.annotation.Configuration; 17 | 18 | import com.nepxion.aquarius.lock.LockDelegate; 19 | import com.nepxion.aquarius.lock.LockExecutor; 20 | import com.nepxion.aquarius.lock.local.condition.LocalLockCondition; 21 | import com.nepxion.aquarius.lock.local.impl.LocalLockDelegateImpl; 22 | import com.nepxion.aquarius.lock.local.impl.LocalLockExecutorImpl; 23 | 24 | @Configuration 25 | public class LocalLockConfiguration { 26 | @Bean 27 | @Conditional(LocalLockCondition.class) 28 | public LockDelegate localLockDelegate() { 29 | return new LocalLockDelegateImpl(); 30 | } 31 | 32 | @Bean 33 | @Conditional(LocalLockCondition.class) 34 | public LockExecutor localLockExecutor() { 35 | return new LocalLockExecutorImpl(); 36 | } 37 | } -------------------------------------------------------------------------------- /aquarius-lock-local/src/main/java/com/nepxion/aquarius/lock/local/impl/LocalLockDelegateImpl.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.lock.local.impl; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.util.concurrent.locks.Lock; 13 | 14 | import org.aopalliance.intercept.MethodInvocation; 15 | import org.slf4j.Logger; 16 | import org.slf4j.LoggerFactory; 17 | import org.springframework.beans.factory.annotation.Autowired; 18 | import org.springframework.beans.factory.annotation.Value; 19 | 20 | import com.nepxion.aquarius.common.exception.AquariusException; 21 | import com.nepxion.aquarius.lock.LockDelegate; 22 | import com.nepxion.aquarius.lock.LockExecutor; 23 | import com.nepxion.aquarius.lock.constant.LockConstant; 24 | import com.nepxion.aquarius.lock.entity.LockType; 25 | 26 | public class LocalLockDelegateImpl implements LockDelegate { 27 | private static final Logger LOG = LoggerFactory.getLogger(LocalLockDelegateImpl.class); 28 | 29 | @Autowired 30 | private LockExecutor lockExecutor; 31 | 32 | @Value("${" + LockConstant.LOCK_AOP_EXCEPTION_IGNORE + ":true}") 33 | private Boolean lockAopExceptionIgnore; 34 | 35 | @Override 36 | public Object invoke(MethodInvocation invocation, LockType lockType, String key, long leaseTime, long waitTime, boolean async, boolean fair) throws Throwable { 37 | Lock lock = null; 38 | try { 39 | lock = lockExecutor.tryLock(lockType, key, leaseTime, waitTime, async, fair); 40 | if (lock != null) { 41 | try { 42 | return invocation.proceed(); 43 | } catch (Exception e) { 44 | throw e; 45 | } 46 | } 47 | } catch (Exception e) { 48 | if (lockAopExceptionIgnore) { 49 | LOG.error("Exception occurs while Lock, but still to proceed the invocation", e); 50 | 51 | return invocation.proceed(); 52 | } else { 53 | throw e; 54 | } 55 | } finally { 56 | lockExecutor.unlock(lock); 57 | } 58 | 59 | if (lockAopExceptionIgnore) { 60 | LOG.error("Acquired lock failed, but still to proceed the invocation"); 61 | 62 | return invocation.proceed(); 63 | } else { 64 | throw new AquariusException("Acquired lock failed, stop to proceed the invocation"); 65 | } 66 | } 67 | } -------------------------------------------------------------------------------- /aquarius-lock-redis/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | aquarius-lock-redis 5 | Nepxion Aquarius Lock For Redis 6 | jar 7 | 4.0.0 8 | Nepxion Aquarius is a list of distribution components based on Redis + Zookeeper with Nepxion Matrix AOP framework 9 | http://www.nepxion.com 10 | 11 | 12 | com.nepxion 13 | aquarius 14 | 2.0.13 15 | 16 | 17 | 18 | 19 | ${project.groupId} 20 | aquarius-common-redis 21 | 22 | 23 | 24 | ${project.groupId} 25 | aquarius-lock-aop 26 | 27 | 28 | 29 | org.redisson 30 | redisson 31 | provided 32 | 33 | 34 | -------------------------------------------------------------------------------- /aquarius-lock-redis/src/main/java/com/nepxion/aquarius/lock/redis/condition/RedisLockCondition.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.lock.redis.condition; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import com.nepxion.aquarius.common.condition.AquariusCondition; 13 | import com.nepxion.aquarius.lock.constant.LockConstant; 14 | 15 | public class RedisLockCondition extends AquariusCondition { 16 | public RedisLockCondition() { 17 | super(LockConstant.LOCK_TYPE, LockConstant.LOCK_TYPE_REDIS); 18 | } 19 | } -------------------------------------------------------------------------------- /aquarius-lock-redis/src/main/java/com/nepxion/aquarius/lock/redis/configuration/RedisLockConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.lock.redis.configuration; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.redisson.api.RLock; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.beans.factory.annotation.Value; 15 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 16 | import org.springframework.context.annotation.Bean; 17 | import org.springframework.context.annotation.Conditional; 18 | import org.springframework.context.annotation.Configuration; 19 | 20 | import com.nepxion.aquarius.common.redisson.adapter.RedissonAdapter; 21 | import com.nepxion.aquarius.common.redisson.constant.RedissonConstant; 22 | import com.nepxion.aquarius.common.redisson.handler.RedissonHandler; 23 | import com.nepxion.aquarius.common.redisson.handler.RedissonHandlerImpl; 24 | import com.nepxion.aquarius.lock.LockDelegate; 25 | import com.nepxion.aquarius.lock.LockExecutor; 26 | import com.nepxion.aquarius.lock.redis.condition.RedisLockCondition; 27 | import com.nepxion.aquarius.lock.redis.impl.RedisLockDelegateImpl; 28 | import com.nepxion.aquarius.lock.redis.impl.RedisLockExecutorImpl; 29 | 30 | @Configuration 31 | public class RedisLockConfiguration { 32 | @Value("${" + RedissonConstant.PATH + ":" + RedissonConstant.DEFAULT_PATH + "}") 33 | private String redissonPath; 34 | 35 | @Autowired(required = false) 36 | private RedissonAdapter redissonAdapter; 37 | 38 | @Bean 39 | @Conditional(RedisLockCondition.class) 40 | public LockDelegate redisLockDelegate() { 41 | return new RedisLockDelegateImpl(); 42 | } 43 | 44 | @Bean 45 | @Conditional(RedisLockCondition.class) 46 | public LockExecutor redisLockExecutor() { 47 | return new RedisLockExecutorImpl(); 48 | } 49 | 50 | @Bean 51 | @Conditional(RedisLockCondition.class) 52 | @ConditionalOnMissingBean 53 | public RedissonHandler redissonHandler() { 54 | if (redissonAdapter != null) { 55 | return redissonAdapter.getRedissonHandler(); 56 | } 57 | 58 | return new RedissonHandlerImpl(redissonPath); 59 | } 60 | } -------------------------------------------------------------------------------- /aquarius-lock-redis/src/main/java/com/nepxion/aquarius/lock/redis/impl/RedisLockDelegateImpl.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.lock.redis.impl; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.aopalliance.intercept.MethodInvocation; 13 | import org.redisson.api.RLock; 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | import org.springframework.beans.factory.annotation.Autowired; 17 | import org.springframework.beans.factory.annotation.Value; 18 | 19 | import com.nepxion.aquarius.common.exception.AquariusException; 20 | import com.nepxion.aquarius.lock.LockDelegate; 21 | import com.nepxion.aquarius.lock.LockExecutor; 22 | import com.nepxion.aquarius.lock.constant.LockConstant; 23 | import com.nepxion.aquarius.lock.entity.LockType; 24 | 25 | public class RedisLockDelegateImpl implements LockDelegate { 26 | private static final Logger LOG = LoggerFactory.getLogger(RedisLockDelegateImpl.class); 27 | 28 | @Autowired 29 | private LockExecutor lockExecutor; 30 | 31 | @Value("${" + LockConstant.LOCK_AOP_EXCEPTION_IGNORE + ":true}") 32 | private Boolean lockAopExceptionIgnore; 33 | 34 | @Override 35 | public Object invoke(MethodInvocation invocation, LockType lockType, String key, long leaseTime, long waitTime, boolean async, boolean fair) throws Throwable { 36 | RLock lock = null; 37 | try { 38 | lock = lockExecutor.tryLock(lockType, key, leaseTime, waitTime, async, fair); 39 | if (lock != null) { 40 | try { 41 | return invocation.proceed(); 42 | } catch (Exception e) { 43 | throw e; 44 | } 45 | } 46 | } catch (Exception e) { 47 | if (lockAopExceptionIgnore) { 48 | LOG.error("Redis exception occurs while Lock, but still to proceed the invocation", e); 49 | 50 | return invocation.proceed(); 51 | } else { 52 | throw e; 53 | } 54 | } finally { 55 | lockExecutor.unlock(lock); 56 | } 57 | 58 | if (lockAopExceptionIgnore) { 59 | LOG.error("Acquired redis lock failed, but still to proceed the invocation"); 60 | 61 | return invocation.proceed(); 62 | } else { 63 | throw new AquariusException("Acquired redis lock failed, stop to proceed the invocation"); 64 | } 65 | } 66 | } -------------------------------------------------------------------------------- /aquarius-lock-starter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | aquarius-lock-starter 5 | Nepxion Aquarius Starter For Lock 6 | jar 7 | 4.0.0 8 | Nepxion Aquarius is a list of distribution components based on Redis + Zookeeper with Nepxion Matrix AOP framework 9 | http://www.nepxion.com 10 | 11 | 12 | com.nepxion 13 | aquarius 14 | 2.0.13 15 | 16 | 17 | 18 | 19 | ${project.groupId} 20 | aquarius-common 21 | 22 | 23 | 24 | ${project.groupId} 25 | aquarius-common-redis 26 | 27 | 28 | 29 | ${project.groupId} 30 | aquarius-common-zookeeper 31 | 32 | 33 | 34 | ${project.groupId} 35 | aquarius-lock-aop 36 | 37 | 38 | 39 | ${project.groupId} 40 | aquarius-lock-redis 41 | 42 | 43 | 44 | ${project.groupId} 45 | aquarius-lock-zookeeper 46 | 47 | 48 | 49 | ${project.groupId} 50 | aquarius-lock-local 51 | 52 | 53 | -------------------------------------------------------------------------------- /aquarius-lock-starter/src/main/java/com/nepxion/aquarius/lock/annotation/EnableLock.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.lock.annotation; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.lang.annotation.Documented; 13 | import java.lang.annotation.ElementType; 14 | import java.lang.annotation.Inherited; 15 | import java.lang.annotation.Retention; 16 | import java.lang.annotation.RetentionPolicy; 17 | import java.lang.annotation.Target; 18 | 19 | import org.springframework.context.annotation.Import; 20 | 21 | import com.nepxion.aquarius.lock.aop.LockImportSelector; 22 | 23 | @Target(ElementType.TYPE) 24 | @Retention(RetentionPolicy.RUNTIME) 25 | @Documented 26 | @Inherited 27 | @Import(LockImportSelector.class) 28 | public @interface EnableLock { 29 | 30 | } -------------------------------------------------------------------------------- /aquarius-lock-starter/src/main/java/com/nepxion/aquarius/lock/aop/LockImportSelector.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.lock.aop; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.core.Ordered; 13 | import org.springframework.core.annotation.Order; 14 | 15 | import com.nepxion.aquarius.lock.annotation.EnableLock; 16 | import com.nepxion.aquarius.lock.constant.LockConstant; 17 | import com.nepxion.matrix.selector.AbstractImportSelector; 18 | import com.nepxion.matrix.selector.RelaxedPropertyResolver; 19 | 20 | @Order(Ordered.LOWEST_PRECEDENCE - 100) 21 | public class LockImportSelector extends AbstractImportSelector { 22 | @Override 23 | protected boolean isEnabled() { 24 | return new RelaxedPropertyResolver(getEnvironment()).getProperty(LockConstant.LOCK_ENABLED, Boolean.class, Boolean.TRUE); 25 | } 26 | } -------------------------------------------------------------------------------- /aquarius-lock-starter/src/main/java/com/nepxion/aquarius/lock/configuration/LockConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.lock.configuration; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.context.annotation.Configuration; 13 | import org.springframework.context.annotation.Import; 14 | 15 | import com.nepxion.aquarius.common.configuration.AquariusConfiguration; 16 | import com.nepxion.aquarius.lock.local.configuration.LocalLockConfiguration; 17 | import com.nepxion.aquarius.lock.redis.configuration.RedisLockConfiguration; 18 | import com.nepxion.aquarius.lock.zookeeper.configuration.ZookeeperLockConfiguration; 19 | 20 | @Configuration 21 | @Import({ AquariusConfiguration.class, LockAopConfiguration.class, RedisLockConfiguration.class, ZookeeperLockConfiguration.class, LocalLockConfiguration.class }) 22 | public class LockConfiguration { 23 | 24 | } -------------------------------------------------------------------------------- /aquarius-lock-starter/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | com.nepxion.aquarius.lock.annotation.EnableLock=\ 2 | com.nepxion.aquarius.lock.configuration.LockConfiguration -------------------------------------------------------------------------------- /aquarius-lock-zookeeper/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | aquarius-lock-zookeeper 5 | Nepxion Aquarius Lock For Zookeeper 6 | jar 7 | 4.0.0 8 | Nepxion Aquarius is a list of distribution components based on Redis + Zookeeper with Nepxion Matrix AOP framework 9 | http://www.nepxion.com 10 | 11 | 12 | com.nepxion 13 | aquarius 14 | 2.0.13 15 | 16 | 17 | 18 | 19 | ${project.groupId} 20 | aquarius-common-zookeeper 21 | 22 | 23 | 24 | ${project.groupId} 25 | aquarius-lock-aop 26 | 27 | 28 | 29 | org.apache.curator 30 | curator-framework 31 | provided 32 | 33 | 34 | 35 | org.apache.curator 36 | curator-recipes 37 | provided 38 | 39 | 40 | -------------------------------------------------------------------------------- /aquarius-lock-zookeeper/src/main/java/com/nepxion/aquarius/lock/zookeeper/condition/ZookeeperLockCondition.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.lock.zookeeper.condition; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import com.nepxion.aquarius.common.condition.AquariusCondition; 13 | import com.nepxion.aquarius.lock.constant.LockConstant; 14 | 15 | public class ZookeeperLockCondition extends AquariusCondition { 16 | public ZookeeperLockCondition() { 17 | super(LockConstant.LOCK_TYPE, LockConstant.LOCK_TYPE_ZOOKEEPER); 18 | } 19 | } -------------------------------------------------------------------------------- /aquarius-lock-zookeeper/src/main/java/com/nepxion/aquarius/lock/zookeeper/configuration/ZookeeperLockConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.lock.zookeeper.configuration; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.apache.curator.framework.recipes.locks.InterProcessMutex; 13 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 14 | import org.springframework.context.annotation.Bean; 15 | import org.springframework.context.annotation.Conditional; 16 | import org.springframework.context.annotation.Configuration; 17 | 18 | import com.nepxion.aquarius.common.curator.handler.CuratorHandler; 19 | import com.nepxion.aquarius.common.curator.handler.CuratorHandlerImpl; 20 | import com.nepxion.aquarius.lock.LockDelegate; 21 | import com.nepxion.aquarius.lock.LockExecutor; 22 | import com.nepxion.aquarius.lock.zookeeper.condition.ZookeeperLockCondition; 23 | import com.nepxion.aquarius.lock.zookeeper.impl.ZookeeperLockDelegateImpl; 24 | import com.nepxion.aquarius.lock.zookeeper.impl.ZookeeperLockExecutorImpl; 25 | 26 | @Configuration 27 | public class ZookeeperLockConfiguration { 28 | @Bean 29 | @Conditional(ZookeeperLockCondition.class) 30 | public LockDelegate zookeeperLockDelegate() { 31 | return new ZookeeperLockDelegateImpl(); 32 | } 33 | 34 | @Bean 35 | @Conditional(ZookeeperLockCondition.class) 36 | public LockExecutor zookeeperLockExecutor() { 37 | return new ZookeeperLockExecutorImpl(); 38 | } 39 | 40 | @Bean 41 | @Conditional(ZookeeperLockCondition.class) 42 | @ConditionalOnMissingBean 43 | public CuratorHandler curatorHandler() { 44 | return new CuratorHandlerImpl(); 45 | } 46 | } -------------------------------------------------------------------------------- /aquarius-lock-zookeeper/src/main/java/com/nepxion/aquarius/lock/zookeeper/impl/ZookeeperLockDelegateImpl.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.lock.zookeeper.impl; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.aopalliance.intercept.MethodInvocation; 13 | import org.apache.curator.framework.recipes.locks.InterProcessMutex; 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | import org.springframework.beans.factory.annotation.Autowired; 17 | import org.springframework.beans.factory.annotation.Value; 18 | 19 | import com.nepxion.aquarius.common.exception.AquariusException; 20 | import com.nepxion.aquarius.lock.LockDelegate; 21 | import com.nepxion.aquarius.lock.LockExecutor; 22 | import com.nepxion.aquarius.lock.constant.LockConstant; 23 | import com.nepxion.aquarius.lock.entity.LockType; 24 | 25 | public class ZookeeperLockDelegateImpl implements LockDelegate { 26 | private static final Logger LOG = LoggerFactory.getLogger(ZookeeperLockDelegateImpl.class); 27 | 28 | @Autowired 29 | private LockExecutor lockExecutor; 30 | 31 | @Value("${" + LockConstant.LOCK_AOP_EXCEPTION_IGNORE + ":true}") 32 | private Boolean lockAopExceptionIgnore; 33 | 34 | @Override 35 | public Object invoke(MethodInvocation invocation, LockType lockType, String key, long leaseTime, long waitTime, boolean async, boolean fair) throws Throwable { 36 | InterProcessMutex interProcessMutex = null; 37 | try { 38 | interProcessMutex = lockExecutor.tryLock(lockType, key, leaseTime, waitTime, async, fair); 39 | if (interProcessMutex != null) { 40 | try { 41 | return invocation.proceed(); 42 | } catch (Exception e) { 43 | throw e; 44 | } 45 | } 46 | } catch (Exception e) { 47 | if (lockAopExceptionIgnore) { 48 | LOG.error("Zookeeper exception occurs while Lock, but still to proceed the invocation", e); 49 | 50 | return invocation.proceed(); 51 | } else { 52 | throw e; 53 | } 54 | } finally { 55 | lockExecutor.unlock(interProcessMutex); 56 | } 57 | 58 | if (lockAopExceptionIgnore) { 59 | LOG.error("Acquired zookeeper lock failed, but still to proceed the invocation"); 60 | 61 | return invocation.proceed(); 62 | } else { 63 | throw new AquariusException("Acquired zookeeper lock failed, stop to proceed the invocation"); 64 | } 65 | } 66 | } -------------------------------------------------------------------------------- /aquarius-spring-boot-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | aquarius-spring-boot-example 5 | Nepxion Aquarius Spring Boot Example 6 | jar 7 | 4.0.0 8 | Nepxion Aquarius is a list of distribution components based on Redis + Zookeeper with Nepxion Matrix AOP framework 9 | http://www.nepxion.com 10 | 11 | 12 | com.nepxion 13 | aquarius 14 | 2.0.13 15 | 16 | 17 | 18 | 19 | ${project.groupId} 20 | aquarius-lock-starter 21 | 22 | 23 | 24 | ${project.groupId} 25 | aquarius-cache-starter 26 | 27 | 28 | 29 | ${project.groupId} 30 | aquarius-limit-starter 31 | 32 | 33 | 34 | ${project.groupId} 35 | aquarius-id-generator-starter 36 | 37 | 38 | 39 | com.google.guava 40 | guava 41 | ${guava.version} 42 | 43 | 44 | 45 | org.redisson 46 | redisson 47 | ${redisson.version} 48 | 49 | 50 | 51 | org.apache.curator 52 | curator-framework 53 | ${curator.version} 54 | 55 | 56 | 57 | org.apache.curator 58 | curator-recipes 59 | ${curator.version} 60 | 61 | 62 | 63 | org.springframework.boot 64 | spring-boot-starter-web 65 | 66 | 67 | -------------------------------------------------------------------------------- /aquarius-spring-boot-example/src/main/java/com/nepxion/aquarius/example/adapter/RedissonAdapterImpl.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.adapter; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.io.IOException; 13 | 14 | import org.redisson.config.Config; 15 | 16 | import com.nepxion.aquarius.common.redisson.adapter.RedissonAdapter; 17 | import com.nepxion.aquarius.common.redisson.handler.RedissonHandler; 18 | import com.nepxion.aquarius.common.redisson.handler.RedissonHandlerImpl; 19 | import com.nepxion.aquarius.common.redisson.util.RedissonUtil; 20 | 21 | public class RedissonAdapterImpl implements RedissonAdapter { 22 | @Override 23 | public RedissonHandler getRedissonHandler() { 24 | // 来自远程配置中心的内容 25 | String yamlConfigContent = "..."; 26 | 27 | Config config = null; 28 | try { 29 | config = RedissonUtil.createYamlConfig(yamlConfigContent); 30 | } catch (IOException e) { 31 | e.printStackTrace(); 32 | } 33 | 34 | /*String jsonConfigContent = "..."; 35 | 36 | Config config = null; 37 | try { 38 | config = RedissonUtil.createJsonConfig(jsonConfigContent); 39 | } catch (IOException e) { 40 | e.printStackTrace(); 41 | }*/ 42 | 43 | return new RedissonHandlerImpl(config); 44 | } 45 | } -------------------------------------------------------------------------------- /aquarius-spring-boot-example/src/main/java/com/nepxion/aquarius/example/cache/app1/CacheAopApplication.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.cache.app1; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.boot.SpringApplication; 13 | import org.springframework.boot.autoconfigure.SpringBootApplication; 14 | import org.springframework.context.ConfigurableApplicationContext; 15 | import org.springframework.context.annotation.ComponentScan; 16 | 17 | import com.nepxion.aquarius.cache.annotation.EnableCache; 18 | import com.nepxion.aquarius.example.cache.service.MyService5; 19 | import com.nepxion.aquarius.example.cache.service.MyService6Impl; 20 | 21 | @SpringBootApplication 22 | @EnableCache 23 | @ComponentScan(basePackages = { "com.nepxion.aquarius.example.cache.service" }) 24 | public class CacheAopApplication { 25 | public static void main(String[] args) throws Exception { 26 | ConfigurableApplicationContext applicationContext = SpringApplication.run(CacheAopApplication.class, args); 27 | 28 | // 下面步骤请一步步操作,然后结合Redis Desktop Manager等工具查看效果 29 | MyService5 myService5 = applicationContext.getBean(MyService5.class); 30 | 31 | // 新增缓存Key为1-1,Value为A到Redis,不过期 32 | myService5.doA("1", "1"); 33 | 34 | // 新增缓存Key为2-2,Value为A到Redis,不过期 35 | myService5.doA("2", "2"); 36 | 37 | // 更新缓存Key为1-1,Value为B到Redis,过期时间1分钟 38 | myService5.doB("1", "1"); 39 | 40 | // 清除缓存Key为2-2到Redis,精确匹配,因为注解上allEntries = false 41 | myService5.doC("2", "2"); 42 | 43 | MyService6Impl myService6 = applicationContext.getBean(MyService6Impl.class); 44 | 45 | // 新增缓存Key为3-3,Value为D到Redis,不过期 46 | myService6.doD("3", "3"); 47 | 48 | // 新增缓存Key为4-4,Value为D到Redis,不过期 49 | myService6.doD("4", "4"); 50 | 51 | // 更新缓存Key为3-3,Value为E到Redis,过期时间1分钟 52 | myService6.doE("3", "3"); 53 | 54 | // 清除缓存Key为4-4到Redis,全局模糊匹配,因为注解上allEntries = true 55 | myService6.doF("4", "4"); 56 | } 57 | 58 | // 如下方式,只支持Spring Cloud F版以前的版本 59 | /*@Bean 60 | public EmbeddedServletContainerFactory createEmbeddedServletContainerFactory() { 61 | TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory(); 62 | tomcatFactory.setPort(8081); 63 | 64 | return tomcatFactory; 65 | }*/ 66 | } -------------------------------------------------------------------------------- /aquarius-spring-boot-example/src/main/java/com/nepxion/aquarius/example/cache/service/MyService5.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.cache.service; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import com.nepxion.aquarius.cache.annotation.CacheEvict; 13 | import com.nepxion.aquarius.cache.annotation.CachePut; 14 | import com.nepxion.aquarius.cache.annotation.Cacheable; 15 | 16 | public interface MyService5 { 17 | @Cacheable(name = "cache", key = "#id1 + \"-\" + #id2", expire = -1L) 18 | String doA(String id1, String id2); 19 | 20 | @CachePut(name = "cache", key = "#id1 + \"-\" + #id2", expire = 60000L) 21 | String doB(String id1, String id2); 22 | 23 | @CacheEvict(name = "cache", key = "#id1 + \"-\" + #id2", allEntries = false, beforeInvocation = false) 24 | String doC(String id1, String id2); 25 | } -------------------------------------------------------------------------------- /aquarius-spring-boot-example/src/main/java/com/nepxion/aquarius/example/cache/service/MyService5Impl.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.cache.service; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.util.concurrent.TimeUnit; 13 | 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | import org.springframework.stereotype.Service; 17 | 18 | @Service("myService5Impl") 19 | public class MyService5Impl implements MyService5 { 20 | private static final Logger LOG = LoggerFactory.getLogger(MyService5Impl.class); 21 | 22 | @Override 23 | public String doA(String id1, String id2) { 24 | try { 25 | TimeUnit.MILLISECONDS.sleep(2000L); 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } 29 | 30 | LOG.info("doA"); 31 | 32 | return "A"; 33 | } 34 | 35 | @Override 36 | public String doB(String id1, String id2) { 37 | try { 38 | TimeUnit.MILLISECONDS.sleep(2000L); 39 | } catch (InterruptedException e) { 40 | e.printStackTrace(); 41 | } 42 | 43 | LOG.info("doB"); 44 | 45 | return "B"; 46 | } 47 | 48 | @Override 49 | public String doC(String id1, String id2) { 50 | try { 51 | TimeUnit.MILLISECONDS.sleep(2000L); 52 | } catch (InterruptedException e) { 53 | e.printStackTrace(); 54 | } 55 | 56 | LOG.info("doC"); 57 | 58 | return "C"; 59 | } 60 | } -------------------------------------------------------------------------------- /aquarius-spring-boot-example/src/main/java/com/nepxion/aquarius/example/cache/service/MyService6Impl.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.cache.service; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.util.concurrent.TimeUnit; 13 | 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | import org.springframework.stereotype.Service; 17 | 18 | import com.nepxion.aquarius.cache.annotation.CacheEvict; 19 | import com.nepxion.aquarius.cache.annotation.CachePut; 20 | import com.nepxion.aquarius.cache.annotation.Cacheable; 21 | 22 | @Service("myService6Impl") 23 | public class MyService6Impl { 24 | private static final Logger LOG = LoggerFactory.getLogger(MyService6Impl.class); 25 | 26 | @Cacheable(name = "cache", key = {"#id1 + \"-\" + #id2", "abc"}, expire = -1L) 27 | public String doD(String id1, String id2) { 28 | try { 29 | TimeUnit.MILLISECONDS.sleep(2000L); 30 | } catch (InterruptedException e) { 31 | e.printStackTrace(); 32 | } 33 | 34 | LOG.info("doD"); 35 | 36 | return "D"; 37 | } 38 | 39 | @CachePut(name = "cache", key = {"#id1 + \"-\" + #id2", "abcde"}, expire = 60000L) 40 | public String doE(String id1, String id2) { 41 | try { 42 | TimeUnit.MILLISECONDS.sleep(2000L); 43 | } catch (InterruptedException e) { 44 | e.printStackTrace(); 45 | } 46 | 47 | LOG.info("doE"); 48 | 49 | return "E"; 50 | } 51 | 52 | @CacheEvict(name = "cache", key = {"#id1 + \"-\" + #id2", "abcdef"}, allEntries = true, beforeInvocation = false) 53 | public String doF(String id1, String id2) { 54 | try { 55 | TimeUnit.MILLISECONDS.sleep(2000L); 56 | } catch (InterruptedException e) { 57 | e.printStackTrace(); 58 | } 59 | 60 | LOG.info("doF"); 61 | 62 | return "F"; 63 | } 64 | } -------------------------------------------------------------------------------- /aquarius-spring-boot-example/src/main/java/com/nepxion/aquarius/example/limit/app1/LimitAopApplication.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.limit.app1; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.util.Timer; 13 | import java.util.TimerTask; 14 | 15 | import org.springframework.boot.SpringApplication; 16 | import org.springframework.boot.autoconfigure.SpringBootApplication; 17 | import org.springframework.context.ConfigurableApplicationContext; 18 | import org.springframework.context.annotation.ComponentScan; 19 | 20 | import com.nepxion.aquarius.example.limit.service.MyService7; 21 | import com.nepxion.aquarius.example.limit.service.MyService8Impl; 22 | import com.nepxion.aquarius.limit.annotation.EnableLimit; 23 | 24 | @SpringBootApplication 25 | @EnableLimit 26 | @ComponentScan(basePackages = { "com.nepxion.aquarius.example.limit.service" }) 27 | public class LimitAopApplication { 28 | public static void main(String[] args) throws Exception { 29 | ConfigurableApplicationContext applicationContext = SpringApplication.run(LimitAopApplication.class, args); 30 | 31 | MyService7 myService7 = applicationContext.getBean(MyService7.class); 32 | Timer timer1 = new Timer(); 33 | timer1.scheduleAtFixedRate(new TimerTask() { 34 | public void run() { 35 | new Thread(new Runnable() { 36 | @Override 37 | public void run() { 38 | myService7.doA("X", "Y"); 39 | } 40 | 41 | }).start(); 42 | } 43 | }, 0L, 3000L); 44 | 45 | MyService8Impl myService8 = applicationContext.getBean(MyService8Impl.class); 46 | Timer timer2 = new Timer(); 47 | timer2.scheduleAtFixedRate(new TimerTask() { 48 | public void run() { 49 | new Thread(new Runnable() { 50 | @Override 51 | public void run() { 52 | myService8.doB("X", "Y"); 53 | } 54 | 55 | }).start(); 56 | } 57 | }, 0L, 4000L); 58 | } 59 | 60 | // 如下方式,只支持Spring Cloud F版以前的版本 61 | /*@Bean 62 | public EmbeddedServletContainerFactory createEmbeddedServletContainerFactory() { 63 | TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory(); 64 | tomcatFactory.setPort(8085); 65 | 66 | return tomcatFactory; 67 | }*/ 68 | } -------------------------------------------------------------------------------- /aquarius-spring-boot-example/src/main/java/com/nepxion/aquarius/example/limit/app2/LimitApplication.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.limit.app2; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.util.Timer; 13 | import java.util.TimerTask; 14 | 15 | import org.slf4j.Logger; 16 | import org.slf4j.LoggerFactory; 17 | import org.springframework.boot.SpringApplication; 18 | import org.springframework.boot.autoconfigure.SpringBootApplication; 19 | import org.springframework.context.ConfigurableApplicationContext; 20 | import org.springframework.context.annotation.ComponentScan; 21 | 22 | import com.nepxion.aquarius.limit.LimitExecutor; 23 | import com.nepxion.aquarius.limit.annotation.EnableLimit; 24 | 25 | @SpringBootApplication 26 | @EnableLimit 27 | @ComponentScan(basePackages = { "com.nepxion.aquarius.example.limit.service" }) 28 | public class LimitApplication { 29 | private static final Logger LOG = LoggerFactory.getLogger(LimitApplication.class); 30 | 31 | public static void main(String[] args) throws Exception { 32 | ConfigurableApplicationContext applicationContext = SpringApplication.run(LimitApplication.class, args); 33 | 34 | // 在给定的10秒里最多访问5次(超出次数返回false);等下个10秒开始,才允许再次被访问(返回true),周而复始 35 | LimitExecutor limitExecutor = applicationContext.getBean(LimitExecutor.class); 36 | 37 | Timer timer1 = new Timer(); 38 | timer1.scheduleAtFixedRate(new TimerTask() { 39 | public void run() { 40 | for (int i = 0; i < 3; i++) { 41 | new Thread(new Runnable() { 42 | @Override 43 | public void run() { 44 | try { 45 | LOG.info("Timer1 - Limit={}", limitExecutor.tryAccess("limit", "X-Y", 10, 5)); 46 | } catch (Exception e) { 47 | e.printStackTrace(); 48 | } 49 | } 50 | }).start(); 51 | } 52 | } 53 | }, 0L, 1000L); 54 | 55 | Timer timer2 = new Timer(); 56 | timer2.scheduleAtFixedRate(new TimerTask() { 57 | public void run() { 58 | for (int i = 0; i < 3; i++) { 59 | new Thread(new Runnable() { 60 | @Override 61 | public void run() { 62 | try { 63 | LOG.info("Timer1 - Limit={}", limitExecutor.tryAccess("limit", "X-Y", 10, 5)); 64 | } catch (Exception e) { 65 | e.printStackTrace(); 66 | } 67 | } 68 | }).start(); 69 | } 70 | } 71 | }, 0L, 1500L); 72 | } 73 | 74 | // 如下方式,只支持Spring Cloud F版以前的版本 75 | /*@Bean 76 | public EmbeddedServletContainerFactory createEmbeddedServletContainerFactory() { 77 | TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory(); 78 | tomcatFactory.setPort(8086); 79 | 80 | return tomcatFactory; 81 | }*/ 82 | } -------------------------------------------------------------------------------- /aquarius-spring-boot-example/src/main/java/com/nepxion/aquarius/example/limit/service/MyService7.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.limit.service; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import com.nepxion.aquarius.limit.annotation.Limit; 13 | 14 | public interface MyService7 { 15 | @Limit(name = "limit", key = "#id1 + \"-\" + #id2", limitPeriod = 10, limitCount = 5) 16 | String doA(String id1, String id2); 17 | } -------------------------------------------------------------------------------- /aquarius-spring-boot-example/src/main/java/com/nepxion/aquarius/example/limit/service/MyService7Impl.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.limit.service; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.slf4j.Logger; 13 | import org.slf4j.LoggerFactory; 14 | import org.springframework.stereotype.Service; 15 | 16 | @Service("myService7Impl") 17 | public class MyService7Impl implements MyService7 { 18 | private static final Logger LOG = LoggerFactory.getLogger(MyService7Impl.class); 19 | 20 | @Override 21 | public String doA(String id1, String id2) { 22 | LOG.info("doA"); 23 | 24 | return "A"; 25 | } 26 | } -------------------------------------------------------------------------------- /aquarius-spring-boot-example/src/main/java/com/nepxion/aquarius/example/limit/service/MyService8Impl.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.limit.service; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.slf4j.Logger; 13 | import org.slf4j.LoggerFactory; 14 | import org.springframework.stereotype.Service; 15 | 16 | import com.nepxion.aquarius.limit.annotation.Limit; 17 | 18 | @Service("myService8Impl") 19 | public class MyService8Impl { 20 | private static final Logger LOG = LoggerFactory.getLogger(MyService8Impl.class); 21 | 22 | @Limit(name = "limit", key = "#id1 + \"-\" + #id2", limitPeriod = 10, limitCount = 5) 23 | public String doB(String id1, String id2) { 24 | LOG.info("doB"); 25 | 26 | return "B"; 27 | } 28 | } -------------------------------------------------------------------------------- /aquarius-spring-boot-example/src/main/java/com/nepxion/aquarius/example/lock/app1/LockAopApplication.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.lock.app1; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.boot.SpringApplication; 13 | import org.springframework.boot.autoconfigure.SpringBootApplication; 14 | import org.springframework.context.ConfigurableApplicationContext; 15 | import org.springframework.context.annotation.ComponentScan; 16 | 17 | import com.nepxion.aquarius.example.lock.service.MyService1; 18 | import com.nepxion.aquarius.example.lock.service.MyService2Impl; 19 | import com.nepxion.aquarius.lock.annotation.EnableLock; 20 | 21 | @SpringBootApplication 22 | @EnableLock 23 | @ComponentScan(basePackages = { "com.nepxion.aquarius.example.lock.service" }) 24 | public class LockAopApplication { 25 | public static void main(String[] args) throws Exception { 26 | ConfigurableApplicationContext applicationContext = SpringApplication.run(LockAopApplication.class, args); 27 | 28 | // 执行效果是doA和doC无序打印,即谁拿到锁谁先运行 29 | MyService1 myService1 = applicationContext.getBean(MyService1.class); 30 | for (int i = 0; i < 5; i++) { 31 | new Thread(new Runnable() { 32 | @Override 33 | public void run() { 34 | myService1.doA("X", "Y"); 35 | } 36 | }).start(); 37 | } 38 | 39 | MyService2Impl myService2 = applicationContext.getBean(MyService2Impl.class); 40 | for (int i = 0; i < 5; i++) { 41 | new Thread(new Runnable() { 42 | @Override 43 | public void run() { 44 | myService2.doC("X", "Y"); 45 | } 46 | }).start(); 47 | } 48 | } 49 | 50 | // 如下方式,只支持Spring Cloud F版以前的版本 51 | /*@Bean 52 | public EmbeddedServletContainerFactory createEmbeddedServletContainerFactory() { 53 | TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory(); 54 | tomcatFactory.setPort(8087); 55 | 56 | return tomcatFactory; 57 | }*/ 58 | } -------------------------------------------------------------------------------- /aquarius-spring-boot-example/src/main/java/com/nepxion/aquarius/example/lock/app3/ReadWriteLockAopApplication.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.lock.app3; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.util.Timer; 13 | import java.util.TimerTask; 14 | 15 | import org.slf4j.Logger; 16 | import org.slf4j.LoggerFactory; 17 | import org.springframework.boot.SpringApplication; 18 | import org.springframework.boot.autoconfigure.SpringBootApplication; 19 | import org.springframework.context.ConfigurableApplicationContext; 20 | import org.springframework.context.annotation.ComponentScan; 21 | 22 | import com.nepxion.aquarius.example.lock.service.MyService3; 23 | import com.nepxion.aquarius.example.lock.service.MyService4Impl; 24 | import com.nepxion.aquarius.lock.annotation.EnableLock; 25 | 26 | @SpringBootApplication 27 | @EnableLock 28 | @ComponentScan(basePackages = { "com.nepxion.aquarius.example.lock.service" }) 29 | public class ReadWriteLockAopApplication { 30 | private static final Logger LOG = LoggerFactory.getLogger(ReadWriteLockAopApplication.class); 31 | 32 | public static void main(String[] args) throws Exception { 33 | ConfigurableApplicationContext applicationContext = SpringApplication.run(ReadWriteLockAopApplication.class, args); 34 | 35 | // 执行效果是先打印doW,即拿到写锁,再打印若干个doR,即可以同时拿到若干个读锁 36 | MyService4Impl myService4 = applicationContext.getBean(MyService4Impl.class); 37 | Timer timer1 = new Timer(); 38 | timer1.scheduleAtFixedRate(new TimerTask() { 39 | public void run() { 40 | LOG.info("Start to get write lock..."); 41 | // 写锁逻辑,最高持锁15秒,睡眠10秒,10秒后释放读锁 42 | myService4.doW("X", "Y"); 43 | } 44 | }, 0L, 600000L); 45 | 46 | MyService3 myService3 = applicationContext.getBean(MyService3.class); 47 | Timer timer2 = new Timer(); 48 | timer2.scheduleAtFixedRate(new TimerTask() { 49 | public void run() { 50 | LOG.info("Start to get read lock..."); 51 | for (int i = 0; i < 3; i++) { 52 | new Thread(new Runnable() { 53 | @Override 54 | public void run() { 55 | // 读锁逻辑,最高持锁5秒,睡眠2秒,2秒后释放读锁 56 | myService3.doR("X", "Y"); 57 | } 58 | }).start(); 59 | } 60 | } 61 | }, 2000L, 2000L); 62 | } 63 | 64 | // 如下方式,只支持Spring Cloud F版以前的版本 65 | /*@Bean 66 | public EmbeddedServletContainerFactory createEmbeddedServletContainerFactory() { 67 | TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory(); 68 | tomcatFactory.setPort(8089); 69 | 70 | return tomcatFactory; 71 | }*/ 72 | } -------------------------------------------------------------------------------- /aquarius-spring-boot-example/src/main/java/com/nepxion/aquarius/example/lock/service/MyService1.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.lock.service; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import com.nepxion.aquarius.lock.annotation.Lock; 13 | 14 | public interface MyService1 { 15 | @Lock(name = "lock", key = "#id1 + \"-\" + #id2", leaseTime = 5000L, waitTime = 60000L, async = false, fair = false) 16 | String doA(String id1, String id2); 17 | 18 | String doB(String id1, String id2); 19 | } -------------------------------------------------------------------------------- /aquarius-spring-boot-example/src/main/java/com/nepxion/aquarius/example/lock/service/MyService1Impl.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.lock.service; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.util.concurrent.TimeUnit; 13 | 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | import org.springframework.stereotype.Service; 17 | 18 | @Service("myService1Impl") 19 | public class MyService1Impl implements MyService1 { 20 | private static final Logger LOG = LoggerFactory.getLogger(MyService1Impl.class); 21 | 22 | @Override 23 | public String doA(String id1, String id2) { 24 | try { 25 | TimeUnit.MILLISECONDS.sleep(2000L); 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } 29 | 30 | LOG.info("doA - lock is got"); 31 | 32 | return "A"; 33 | } 34 | 35 | @Override 36 | public String doB(String id1, String id2) { 37 | LOG.info("doB"); 38 | 39 | return "B"; 40 | } 41 | } -------------------------------------------------------------------------------- /aquarius-spring-boot-example/src/main/java/com/nepxion/aquarius/example/lock/service/MyService2Impl.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.lock.service; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.util.concurrent.TimeUnit; 13 | 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | import org.springframework.stereotype.Service; 17 | 18 | import com.nepxion.aquarius.lock.annotation.Lock; 19 | 20 | @Service("myService2Impl") 21 | public class MyService2Impl { 22 | private static final Logger LOG = LoggerFactory.getLogger(MyService2Impl.class); 23 | 24 | @Lock(name = "lock", key = "#id1 + \"-\" + #id2", leaseTime = 5000L, waitTime = 60000L, async = false, fair = false) 25 | public String doC(String id1, String id2) { 26 | try { 27 | TimeUnit.MILLISECONDS.sleep(2000L); 28 | } catch (InterruptedException e) { 29 | e.printStackTrace(); 30 | } 31 | 32 | LOG.info("doC - lock is got"); 33 | 34 | return "C"; 35 | } 36 | 37 | public String doD(String id1, String id2) { 38 | LOG.info("doD"); 39 | 40 | return "D"; 41 | } 42 | } -------------------------------------------------------------------------------- /aquarius-spring-boot-example/src/main/java/com/nepxion/aquarius/example/lock/service/MyService3.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.lock.service; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import com.nepxion.aquarius.lock.annotation.ReadLock; 13 | 14 | public interface MyService3 { 15 | @ReadLock(name = "lock", key = "#id1 + \"-\" + #id2", leaseTime = 5000L, waitTime = 60000L, async = false, fair = false) 16 | String doR(String id1, String id2); 17 | } -------------------------------------------------------------------------------- /aquarius-spring-boot-example/src/main/java/com/nepxion/aquarius/example/lock/service/MyService3Impl.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.lock.service; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.util.concurrent.TimeUnit; 13 | 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | import org.springframework.stereotype.Service; 17 | 18 | @Service("myService3Impl") 19 | public class MyService3Impl implements MyService3 { 20 | private static final Logger LOG = LoggerFactory.getLogger(MyService3Impl.class); 21 | 22 | @Override 23 | public String doR(String id1, String id2) { 24 | try { 25 | TimeUnit.MILLISECONDS.sleep(2000L); 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } 29 | 30 | LOG.info("doR - read lock is got"); 31 | 32 | return "R"; 33 | } 34 | } -------------------------------------------------------------------------------- /aquarius-spring-boot-example/src/main/java/com/nepxion/aquarius/example/lock/service/MyService4Impl.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.lock.service; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import java.util.concurrent.TimeUnit; 13 | 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | import org.springframework.stereotype.Service; 17 | 18 | import com.nepxion.aquarius.lock.annotation.WriteLock; 19 | 20 | @Service("myService4Impl") 21 | public class MyService4Impl { 22 | private static final Logger LOG = LoggerFactory.getLogger(MyService4Impl.class); 23 | 24 | @WriteLock(name = "lock", key = "#id1 + \"-\" + #id2", leaseTime = 15000L, waitTime = 60000L, async = false, fair = false) 25 | public String doW(String id1, String id2) { 26 | try { 27 | TimeUnit.MILLISECONDS.sleep(10000L); 28 | } catch (InterruptedException e) { 29 | e.printStackTrace(); 30 | } 31 | 32 | LOG.info("doW - write lock is got"); 33 | 34 | return "W"; 35 | } 36 | } -------------------------------------------------------------------------------- /aquarius-spring-boot-example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=2222 2 | 3 | # Prefix 4 | prefix=aquarius 5 | 6 | # Lock config 7 | # lock.enabled=true 8 | # redisLock, zookeeperLock, localLock 9 | lock.type=redisLock 10 | # 当切面拦截出现异常,如果忽略该异常,则不影响当前业务方法调用,否则中断当前业务方法调用,缺省为true 11 | # lock.aop.exception.ignore=true 12 | # 扫描含有@Lock,@ReadLock,@WriteLock等注解的接口或者类所在目录 13 | lock.scan.packages=com.nepxion.aquarius.example.lock 14 | 15 | # Cache config 16 | # cache.enabled=true 17 | cache.type=redisCache 18 | # 当切面拦截出现异常,如果忽略该异常,则不影响当前业务方法调用,否则中断当前业务方法调用,缺省为true 19 | # cache.aop.exception.ignore=true 20 | # 全局缓存过期值,单位毫秒(小于等于零,表示永不过期),当注解上没配置该值的时候,以全局值为准,缺省为-1 21 | # cache.expire=-1 22 | # 扫描含有@Cacheable,@CacheEvict,@CachePut等注解的接口或者类所在目录 23 | cache.scan.packages=com.nepxion.aquarius.example.cache 24 | 25 | # Limit config 26 | # limit.enabled=true 27 | # redisLimit, localLimit 28 | limit.type=redisLimit 29 | # 当切面拦截出现异常,如果忽略该异常,则不影响当前业务方法调用,否则中断当前业务方法调用,缺省为true 30 | # limit.aop.exception.ignore=true 31 | # 扫描含有@Limit等注解的接口或者类所在目录 32 | limit.scan.packages=com.nepxion.aquarius.example.limit 33 | 34 | # IdGenerator config 35 | # idgenerator.enabled=true 36 | 37 | # Curator config 38 | curator.connectString=localhost:2181 39 | curator.sessionTimeoutMs=15000 40 | curator.connectionTimeoutMs=15000 41 | # exponentialBackoffRetry, boundedExponentialBackoffRetry, retryNTimes, retryForever, retryUntilElapsed 42 | curator.retryType=retryNTimes 43 | # ExponentialBackoffRetry 44 | curator.exponentialBackoffRetry.baseSleepTimeMs=2000 45 | curator.exponentialBackoffRetry.maxRetries=10 46 | # BoundedExponentialBackoffRetry 47 | curator.boundedExponentialBackoffRetry.baseSleepTimeMs=2000 48 | curator.boundedExponentialBackoffRetry.maxSleepTimeMs=60000 49 | curator.boundedExponentialBackoffRetry.maxRetries=10 50 | # RetryNTimes 51 | curator.retryNTimes.count=10 52 | curator.retryNTimes.sleepMsBetweenRetries=2000 53 | # RetryForever 54 | curator.retryForever.retryIntervalMs=1000 55 | # RetryUntilElapsed 56 | curator.retryUntilElapsed.maxElapsedTimeMs=60000 57 | curator.retryUntilElapsed.sleepMsBetweenRetries=2000 58 | 59 | # Redis config 60 | spring.redis.host=localhost 61 | spring.redis.port=6379 62 | spring.redis.password= 63 | spring.redis.database=0 64 | spring.redis.pool.max-active=8 65 | spring.redis.pool.max-wait=-1 66 | spring.redis.pool.max-idle=8 67 | spring.redis.pool.min-idle=0 68 | 69 | # Redission config - refer to redission.yaml 70 | # 自定义配置文件路径,放在resources目录下和根目录下的文件都会被扫描到 71 | # redisson.path=redisson.yaml 72 | 73 | # Frequent log print 74 | frequent.log.print=true -------------------------------------------------------------------------------- /aquarius-spring-boot-example/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID:- } --- [%15.15t] %-40.40logger{39} : %msg%n 8 | 9 | UTF-8 10 | 11 | 12 | log/aquarius-%d{yyyy-MM-dd}.%i.log 13 | 50MB 14 | 15 | 16 | INFO 17 | 18 | 19 | true 20 | 21 | 22 | 23 | 0 24 | 512 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | %d{yyyy-MM-dd HH:mm:ss.SSS} %levelColor(%5p) %magenta(${PID:- }) --- [%15.15t] %cyan(%-40.40logger{39}) : %msg%n 34 | 35 | 36 | 37 | INFO 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /aquarius-spring-boot-example/src/main/resources/redisson.yaml: -------------------------------------------------------------------------------- 1 | singleServerConfig: 2 | idleConnectionTimeout: 10000 3 | pingTimeout: 1000 4 | connectTimeout: 10000 5 | timeout: 3000 6 | retryAttempts: 3 7 | retryInterval: 1500 8 | reconnectionTimeout: 3000 9 | failedAttempts: 3 10 | password: null 11 | subscriptionsPerConnection: 5 12 | clientName: null 13 | address: "redis://127.0.0.1:6379" 14 | subscriptionConnectionMinimumIdleSize: 1 15 | subscriptionConnectionPoolSize: 50 16 | connectionMinimumIdleSize: 32 17 | connectionPoolSize: 64 18 | database: 0 19 | dnsMonitoringInterval: 5000 20 | threads: 0 21 | nettyThreads: 0 22 | codec: ! {} 23 | transportMode: "NIO" -------------------------------------------------------------------------------- /aquarius-spring-cloud-example/src/main/java/com/nepxion/aquarius/example/AquariusApplication.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import org.springframework.boot.autoconfigure.SpringBootApplication; 13 | import org.springframework.boot.builder.SpringApplicationBuilder; 14 | 15 | import com.nepxion.aquarius.cache.annotation.EnableCache; 16 | import com.nepxion.aquarius.idgenerator.annotation.EnableLocalIdGenerator; 17 | import com.nepxion.aquarius.idgenerator.annotation.EnableRedisIdGenerator; 18 | import com.nepxion.aquarius.idgenerator.annotation.EnableZookeeperIdGenerator; 19 | import com.nepxion.aquarius.limit.annotation.EnableLimit; 20 | import com.nepxion.aquarius.lock.annotation.EnableLock; 21 | 22 | @SpringBootApplication 23 | @EnableLock 24 | @EnableCache 25 | @EnableLimit 26 | @EnableLocalIdGenerator 27 | @EnableRedisIdGenerator 28 | @EnableZookeeperIdGenerator 29 | public class AquariusApplication { 30 | public static void main(String[] args) { 31 | new SpringApplicationBuilder(AquariusApplication.class).run(args); 32 | } 33 | } -------------------------------------------------------------------------------- /aquarius-spring-cloud-example/src/main/java/com/nepxion/aquarius/example/cache/CacheAopController.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.cache; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import io.swagger.annotations.Api; 13 | 14 | import java.util.concurrent.TimeUnit; 15 | 16 | import org.slf4j.Logger; 17 | import org.slf4j.LoggerFactory; 18 | import org.springframework.web.bind.annotation.RequestMapping; 19 | import org.springframework.web.bind.annotation.RequestMethod; 20 | import org.springframework.web.bind.annotation.RestController; 21 | 22 | import com.nepxion.aquarius.cache.annotation.CacheEvict; 23 | import com.nepxion.aquarius.cache.annotation.CachePut; 24 | import com.nepxion.aquarius.cache.annotation.Cacheable; 25 | 26 | @RestController 27 | @Api(tags = { "分布式缓存注解接口" }) 28 | public class CacheAopController { 29 | private static final Logger LOG = LoggerFactory.getLogger(CacheAopController.class); 30 | 31 | @RequestMapping(path = "/doD", method = RequestMethod.GET) 32 | @Cacheable(name = "cache", key = "#id1 + \"-\" + #id2", expire = -1L) 33 | public String doD(String id1, String id2) { 34 | try { 35 | TimeUnit.MILLISECONDS.sleep(2000L); 36 | } catch (InterruptedException e) { 37 | e.printStackTrace(); 38 | } 39 | 40 | LOG.info("doD"); 41 | 42 | return "D"; 43 | } 44 | 45 | @RequestMapping(path = "/doE", method = RequestMethod.GET) 46 | @CachePut(name = "cache", key = "#id1 + \"-\" + #id2", expire = 60000L) 47 | public String doE(String id1, String id2) { 48 | try { 49 | TimeUnit.MILLISECONDS.sleep(2000L); 50 | } catch (InterruptedException e) { 51 | e.printStackTrace(); 52 | } 53 | 54 | LOG.info("doE"); 55 | 56 | return "E"; 57 | } 58 | 59 | @RequestMapping(path = "/doF", method = RequestMethod.GET) 60 | @CacheEvict(name = "cache", key = "#id1 + \"-\" + #id2", allEntries = false, beforeInvocation = false) 61 | public String doF(String id1, String id2) { 62 | try { 63 | TimeUnit.MILLISECONDS.sleep(2000L); 64 | } catch (InterruptedException e) { 65 | e.printStackTrace(); 66 | } 67 | 68 | LOG.info("doF"); 69 | 70 | return "F"; 71 | } 72 | } -------------------------------------------------------------------------------- /aquarius-spring-cloud-example/src/main/java/com/nepxion/aquarius/example/configuration/WebMvcConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.configuration; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import springfox.documentation.builders.ApiInfoBuilder; 13 | import springfox.documentation.builders.PathSelectors; 14 | import springfox.documentation.builders.RequestHandlerSelectors; 15 | import springfox.documentation.service.ApiInfo; 16 | import springfox.documentation.service.Contact; 17 | import springfox.documentation.spi.DocumentationType; 18 | import springfox.documentation.spring.web.plugins.Docket; 19 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 20 | 21 | import org.springframework.beans.factory.annotation.Value; 22 | import org.springframework.context.annotation.Bean; 23 | import org.springframework.context.annotation.Configuration; 24 | import org.springframework.web.servlet.config.annotation.CorsRegistry; 25 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 26 | 27 | @Configuration 28 | @EnableSwagger2 29 | public class WebMvcConfiguration extends WebMvcConfigurerAdapter { 30 | @Value("${spring.application.name}") 31 | private String serviceName; 32 | 33 | @Value("${swagger.service.base.package}") 34 | private String basePackage; 35 | 36 | @Value("${swagger.service.description}") 37 | private String description; 38 | 39 | @Value("${swagger.service.version}") 40 | private String version; 41 | 42 | @Value("${swagger.service.license.name}") 43 | private String license; 44 | 45 | @Value("${swagger.service.license.url}") 46 | private String licenseUrl; 47 | 48 | @Value("${swagger.service.contact.name}") 49 | private String contactName; 50 | 51 | @Value("${swagger.service.contact.url}") 52 | private String contactUrl; 53 | 54 | @Value("${swagger.service.contact.email}") 55 | private String contactEmail; 56 | 57 | @Value("${swagger.service.termsOfServiceUrl}") 58 | private String termsOfServiceUrl; 59 | 60 | @Bean("aquariusDocket") 61 | public Docket createRestApi() { 62 | return new Docket(DocumentationType.SWAGGER_2) 63 | .groupName("aquarius") 64 | .apiInfo(apiInfo()) 65 | .select() 66 | .apis(RequestHandlerSelectors.basePackage(basePackage)) // 扫描该包下的所有需要在Swagger中展示的API,@ApiIgnore注解标注的除外 67 | .paths(PathSelectors.any()) 68 | .build(); 69 | } 70 | 71 | private ApiInfo apiInfo() { 72 | return new ApiInfoBuilder() 73 | .title(serviceName) 74 | .description(description) 75 | .version(version) 76 | .license(license) 77 | .licenseUrl(licenseUrl) 78 | .contact(new Contact(contactName, contactUrl, contactEmail)) 79 | .termsOfServiceUrl(termsOfServiceUrl) 80 | .build(); 81 | } 82 | 83 | // 解决跨域问题 84 | @Override 85 | public void addCorsMappings(CorsRegistry registry) { 86 | registry.addMapping("/**") 87 | .allowedHeaders("*") 88 | .allowedMethods("*") 89 | .allowedOrigins("*"); 90 | } 91 | } -------------------------------------------------------------------------------- /aquarius-spring-cloud-example/src/main/java/com/nepxion/aquarius/example/limit/LimitAopController.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.limit; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import io.swagger.annotations.Api; 13 | 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | import org.springframework.web.bind.annotation.RequestMapping; 17 | import org.springframework.web.bind.annotation.RequestMethod; 18 | import org.springframework.web.bind.annotation.RestController; 19 | 20 | import com.nepxion.aquarius.limit.annotation.Limit; 21 | 22 | @RestController 23 | @Api(tags = { "分布式限流注解接口" }) 24 | public class LimitAopController { 25 | private static final Logger LOG = LoggerFactory.getLogger(LimitAopController.class); 26 | 27 | @RequestMapping(path = "/doG", method = RequestMethod.GET) 28 | @Limit(name = "limit", key = "#id1 + \"-\" + #id2", limitPeriod = 10, limitCount = 5) 29 | public String doG(String id1, String id2) { 30 | LOG.info("doG"); 31 | 32 | return "G"; 33 | } 34 | } -------------------------------------------------------------------------------- /aquarius-spring-cloud-example/src/main/java/com/nepxion/aquarius/example/limit/LimitController.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.limit; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import io.swagger.annotations.Api; 13 | import io.swagger.annotations.ApiOperation; 14 | import io.swagger.annotations.ApiParam; 15 | 16 | import org.springframework.beans.factory.annotation.Autowired; 17 | import org.springframework.web.bind.annotation.RequestMapping; 18 | import org.springframework.web.bind.annotation.RequestMethod; 19 | import org.springframework.web.bind.annotation.RequestParam; 20 | import org.springframework.web.bind.annotation.RestController; 21 | 22 | import com.nepxion.aquarius.limit.LimitExecutor; 23 | 24 | @RestController 25 | @Api(tags = { "分布式限流接口" }) 26 | public class LimitController { 27 | @Autowired 28 | private LimitExecutor limitExecutor; 29 | 30 | @RequestMapping(path = "/tryAccess", method = RequestMethod.GET) 31 | @ApiOperation(value = "请求分布式限流许可", notes = "在给定的时间段里最多的访问限制次数(超出次数返回false);等下个时间段开始,才允许再次被访问(返回true),周而复始", response = Boolean.class, httpMethod = "GET") 32 | public boolean tryAccess( 33 | @RequestParam @ApiParam(value = "资源名字", required = true, defaultValue = "limit") String name, 34 | @RequestParam @ApiParam(value = "资源Key", required = true, defaultValue = "X-Y") String key, 35 | @RequestParam @ApiParam(value = "给定的时间段(单位秒)", required = true, defaultValue = "10") int limitPeriod, 36 | @RequestParam @ApiParam(value = "最多的访问限制次数", required = true, defaultValue = "5") int limitCount) { 37 | try { 38 | return limitExecutor.tryAccess(name, key, limitPeriod, limitCount); 39 | } catch (Exception e) { 40 | throw new IllegalArgumentException(e); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /aquarius-spring-cloud-example/src/main/java/com/nepxion/aquarius/example/lock/LockAopController.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.lock; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import io.swagger.annotations.Api; 13 | 14 | import java.util.concurrent.TimeUnit; 15 | 16 | import org.slf4j.Logger; 17 | import org.slf4j.LoggerFactory; 18 | import org.springframework.web.bind.annotation.RequestMapping; 19 | import org.springframework.web.bind.annotation.RequestMethod; 20 | import org.springframework.web.bind.annotation.RequestParam; 21 | import org.springframework.web.bind.annotation.RestController; 22 | 23 | import com.nepxion.aquarius.lock.annotation.Lock; 24 | import com.nepxion.aquarius.lock.annotation.ReadLock; 25 | import com.nepxion.aquarius.lock.annotation.WriteLock; 26 | 27 | @RestController 28 | @Api(tags = { "分布式锁注解接口" }) 29 | public class LockAopController { 30 | private static final Logger LOG = LoggerFactory.getLogger(LockAopController.class); 31 | 32 | @RequestMapping(path = "/doA", method = RequestMethod.GET) 33 | @Lock(name = "lock", key = "#id1 + \"-\" + #id2", leaseTime = 5000L, waitTime = 60000L, async = false, fair = false) 34 | public String doA(@RequestParam String id1, @RequestParam String id2) { 35 | try { 36 | TimeUnit.MILLISECONDS.sleep(2000L); 37 | } catch (InterruptedException e) { 38 | e.printStackTrace(); 39 | } 40 | 41 | LOG.info("doA - lock is got"); 42 | 43 | return "A"; 44 | } 45 | 46 | @RequestMapping(path = "/doB", method = RequestMethod.GET) 47 | @ReadLock(name = "lock", key = "#id1 + \"-\" + #id2", leaseTime = 5000L, waitTime = 60000L, async = false, fair = false) 48 | public String doB(@RequestParam String id1, @RequestParam String id2) { 49 | LOG.info("doB"); 50 | 51 | return "B"; 52 | } 53 | 54 | @RequestMapping(path = "/doC", method = RequestMethod.GET) 55 | @WriteLock(name = "lock", key = "#id1 + \"-\" + #id2", leaseTime = 5000L, waitTime = 60000L, async = false, fair = false) 56 | public String doC(@RequestParam String id1, @RequestParam String id2) { 57 | LOG.info("doC"); 58 | 59 | return "C"; 60 | } 61 | } -------------------------------------------------------------------------------- /aquarius-spring-cloud-example/src/main/java/com/nepxion/aquarius/example/lock/LockController.java: -------------------------------------------------------------------------------- 1 | package com.nepxion.aquarius.example.lock; 2 | 3 | /** 4 | *

Title: Nepxion Aquarius

5 | *

Description: Nepxion Aquarius

6 | *

Copyright: Copyright (c) 2017-2050

7 | *

Company: Nepxion

8 | * @author Haojun Ren 9 | * @version 1.0 10 | */ 11 | 12 | import io.swagger.annotations.Api; 13 | import io.swagger.annotations.ApiOperation; 14 | import io.swagger.annotations.ApiParam; 15 | 16 | import java.util.concurrent.TimeUnit; 17 | 18 | import org.slf4j.Logger; 19 | import org.slf4j.LoggerFactory; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.web.bind.annotation.RequestMapping; 22 | import org.springframework.web.bind.annotation.RequestMethod; 23 | import org.springframework.web.bind.annotation.RequestParam; 24 | import org.springframework.web.bind.annotation.RestController; 25 | 26 | import com.nepxion.aquarius.lock.LockExecutor; 27 | import com.nepxion.aquarius.lock.entity.LockType; 28 | 29 | @RestController 30 | @Api(tags = { "分布式锁接口" }) 31 | public class LockController { 32 | private static final Logger LOG = LoggerFactory.getLogger(LockController.class); 33 | 34 | @SuppressWarnings("rawtypes") 35 | @Autowired 36 | private LockExecutor lockExecutor; 37 | 38 | @SuppressWarnings("unchecked") 39 | @RequestMapping(path = "/tryLock", method = RequestMethod.GET) 40 | @ApiOperation(value = "获取分布式锁", notes = "尝试获取锁,如果获取到锁,则返回锁对象,如果未获取到锁,则返回空", response = String.class, httpMethod = "GET") 41 | public String tryLock( 42 | @RequestParam @ApiParam(value = "锁的类型", required = true, allowableValues = "Lock, ReadLock, WriteLock", defaultValue = "Lock") String lockType, 43 | @RequestParam @ApiParam(value = "锁的名字", required = true, defaultValue = "lock") String name, 44 | @RequestParam @ApiParam(value = "锁的Key", required = true, defaultValue = "x-y") String key, 45 | @RequestParam @ApiParam(value = "持锁时间(单位毫秒)", required = true, defaultValue = "5000") long leaseTime, 46 | @RequestParam @ApiParam(value = "没有获取到锁时,等待时间(单位毫秒)", required = true, defaultValue = "60000") long waitTime, 47 | @RequestParam @ApiParam(value = "是否采用锁的异步执行方式", required = true, defaultValue = "false") boolean async, 48 | @RequestParam @ApiParam(value = "是否采用公平锁", required = true, defaultValue = "false") boolean fair) { 49 | Object lock = null; 50 | try { 51 | lock = lockExecutor.tryLock(LockType.fromString(lockType), name, key, leaseTime, waitTime, async, fair); 52 | if (lock != null) { 53 | try { 54 | TimeUnit.MILLISECONDS.sleep(2000L); 55 | } catch (InterruptedException e) { 56 | e.printStackTrace(); 57 | } 58 | 59 | LOG.info("doX - lock is got"); 60 | } 61 | } catch (Exception e) { 62 | throw new IllegalArgumentException(e); 63 | } finally { 64 | try { 65 | lockExecutor.unlock(lock); 66 | } catch (Exception e) { 67 | throw new IllegalArgumentException(e); 68 | } 69 | } 70 | 71 | return "lock"; 72 | } 73 | } -------------------------------------------------------------------------------- /aquarius-spring-cloud-example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # Spring cloud config 2 | spring.application.name=aquarius-spring-cloud-service 3 | server.port=2222 4 | 5 | eureka.instance.lease-renewal-interval-in-seconds=10 6 | eureka.instance.lease-expiration-duration-in-seconds=30 7 | 8 | eureka.client.register-with-eureka=true 9 | eureka.client.fetch-registry=false 10 | 11 | eureka.client.serviceUrl.defaultZone=http://localhost:9528/eureka/ 12 | 13 | # Prefix 14 | prefix=aquarius 15 | 16 | # Lock config 17 | # lock.enabled=true 18 | # redisLock, zookeeperLock, localLock 19 | lock.type=redisLock 20 | # 当切面拦截出现异常,如果忽略该异常,则不影响当前业务方法调用,否则中断当前业务方法调用,缺省为true 21 | # lock.aop.exception.ignore=true 22 | # 扫描含有@Lock,@ReadLock,@WriteLock等注解的接口或者类所在目录 23 | lock.scan.packages=com.nepxion.aquarius.example.lock 24 | 25 | # Cache config 26 | # cache.enabled=true 27 | cache.type=redisCache 28 | # 当切面拦截出现异常,如果忽略该异常,则不影响当前业务方法调用,否则中断当前业务方法调用,缺省为true 29 | # cache.aop.exception.ignore=true 30 | # 全局缓存过期值,单位毫秒(小于等于零,表示永不过期),当注解上没配置该值的时候,以全局值为准,缺省为-1 31 | # cache.expire=-1 32 | # 扫描含有@Cacheable,@CacheEvict,@CachePut等注解的接口或者类所在目录 33 | cache.scan.packages=com.nepxion.aquarius.example.cache 34 | 35 | # Limit config 36 | # limit.enabled=true 37 | # redisLimit, localLimit 38 | limit.type=redisLimit 39 | # 当切面拦截出现异常,如果忽略该异常,则不影响当前业务方法调用,否则中断当前业务方法调用,缺省为true 40 | # limit.aop.exception.ignore=true 41 | # 扫描含有@Limit等注解的接口或者类所在目录 42 | limit.scan.packages=com.nepxion.aquarius.example.limit 43 | 44 | # IdGenerator config 45 | # idgenerator.enabled=true 46 | 47 | # Curator config 48 | curator.connectString=localhost:2181 49 | curator.sessionTimeoutMs=15000 50 | curator.connectionTimeoutMs=15000 51 | # exponentialBackoffRetry, boundedExponentialBackoffRetry, retryNTimes, retryForever, retryUntilElapsed 52 | curator.retryType=retryNTimes 53 | # ExponentialBackoffRetry 54 | curator.exponentialBackoffRetry.baseSleepTimeMs=2000 55 | curator.exponentialBackoffRetry.maxRetries=10 56 | # BoundedExponentialBackoffRetry 57 | curator.boundedExponentialBackoffRetry.baseSleepTimeMs=2000 58 | curator.boundedExponentialBackoffRetry.maxSleepTimeMs=60000 59 | curator.boundedExponentialBackoffRetry.maxRetries=10 60 | # RetryNTimes 61 | curator.retryNTimes.count=10 62 | curator.retryNTimes.sleepMsBetweenRetries=2000 63 | # RetryForever 64 | curator.retryForever.retryIntervalMs=1000 65 | # RetryUntilElapsed 66 | curator.retryUntilElapsed.maxElapsedTimeMs=60000 67 | curator.retryUntilElapsed.sleepMsBetweenRetries=2000 68 | 69 | # Redis config 70 | spring.redis.host=localhost 71 | spring.redis.port=6379 72 | spring.redis.password= 73 | spring.redis.database=0 74 | spring.redis.pool.max-active=8 75 | spring.redis.pool.max-wait=-1 76 | spring.redis.pool.max-idle=8 77 | spring.redis.pool.min-idle=0 78 | 79 | # Redission config - refer to redission.yaml 80 | # 自定义配置文件路径,放在resources目录下和根目录下的文件都会被扫描到 81 | # redisson.path=redisson.yaml 82 | 83 | # Frequent log print 84 | frequent.log.print=true 85 | 86 | # Swagger config 87 | swagger.service.base.package=com.nepxion.aquarius 88 | swagger.service.description=Aquarius Spring Cloud Restful APIs 89 | swagger.service.version=1.0.0 90 | swagger.service.license.name=Apache License 2.0 91 | swagger.service.license.url=http://www.apache.org/licenses/LICENSE-2.0 92 | swagger.service.contact.name=Haojun Ren 93 | swagger.service.contact.url=https://github.com/Nepxion/Aquarius 94 | swagger.service.contact.email=1394997@qq.com 95 | swagger.service.termsOfServiceUrl=http://www.nepxion.com -------------------------------------------------------------------------------- /aquarius-spring-cloud-example/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID:- } --- [%15.15t] %-40.40logger{39} : %msg%n 8 | 9 | UTF-8 10 | 11 | 12 | log/aquarius-%d{yyyy-MM-dd}.%i.log 13 | 50MB 14 | 15 | 16 | INFO 17 | 18 | 19 | true 20 | 21 | 22 | 23 | 0 24 | 512 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | %d{yyyy-MM-dd HH:mm:ss.SSS} %levelColor(%5p) %magenta(${PID:- }) --- [%15.15t] %cyan(%-40.40logger{39}) : %msg%n 34 | 35 | 36 | 37 | INFO 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /aquarius-spring-cloud-example/src/main/resources/redisson.yaml: -------------------------------------------------------------------------------- 1 | singleServerConfig: 2 | idleConnectionTimeout: 10000 3 | pingTimeout: 1000 4 | connectTimeout: 10000 5 | timeout: 3000 6 | retryAttempts: 3 7 | retryInterval: 1500 8 | reconnectionTimeout: 3000 9 | failedAttempts: 3 10 | password: null 11 | subscriptionsPerConnection: 5 12 | clientName: null 13 | address: "redis://127.0.0.1:6379" 14 | subscriptionConnectionMinimumIdleSize: 1 15 | subscriptionConnectionPoolSize: 50 16 | connectionMinimumIdleSize: 32 17 | connectionPoolSize: 64 18 | database: 0 19 | dnsMonitoringInterval: 5000 20 | threads: 0 21 | nettyThreads: 0 22 | codec: ! {} 23 | transportMode: "NIO" -------------------------------------------------------------------------------- /deploy.bat: -------------------------------------------------------------------------------- 1 | @echo on 2 | @echo ============================================================= 3 | @echo $ $ 4 | @echo $ Nepxion Aquarius $ 5 | @echo $ $ 6 | @echo $ $ 7 | @echo $ $ 8 | @echo $ Nepxion Studio All Right Reserved $ 9 | @echo $ Copyright (C) 2017-2050 $ 10 | @echo $ $ 11 | @echo ============================================================= 12 | @echo. 13 | @echo off 14 | 15 | @title Nepxion Aquarius 16 | @color 0a 17 | 18 | call mvn clean deploy -DskipTests -e -P release -pl aquarius-lock-starter,aquarius-cache-starter,aquarius-limit-starter,aquarius-id-generator-starter -am 19 | 20 | pause -------------------------------------------------------------------------------- /version.bat: -------------------------------------------------------------------------------- 1 | @echo on 2 | @echo ============================================================= 3 | @echo $ $ 4 | @echo $ Nepxion Aquarius $ 5 | @echo $ $ 6 | @echo $ $ 7 | @echo $ $ 8 | @echo $ Nepxion Studio All Right Reserved $ 9 | @echo $ Copyright (C) 2017-2050 $ 10 | @echo $ $ 11 | @echo ============================================================= 12 | @echo. 13 | @echo off 14 | 15 | @title Nepxion Aquarius 16 | @color 0a 17 | 18 | call mvn versions:set -DgenerateBackupPoms=false -DnewVersion=2.0.13 19 | 20 | pause --------------------------------------------------------------------------------