diff --git a/udf-starter-dal-mybatis/src/main/java/org/itkk/udf/dal/mybatis/plugin/pagequery/PageInterceptor.java b/udf-starter-dal-mybatis/src/main/java/org/itkk/udf/dal/mybatis/plugin/pagequery/PageInterceptor.java index c5f4366..91e7faf 100644 --- a/udf-starter-dal-mybatis/src/main/java/org/itkk/udf/dal/mybatis/plugin/pagequery/PageInterceptor.java +++ b/udf-starter-dal-mybatis/src/main/java/org/itkk/udf/dal/mybatis/plugin/pagequery/PageInterceptor.java @@ -66,7 +66,7 @@ public class PageInterceptor implements Interceptor { private String countSuffix = "_COUNT"; @Override - public Object intercept(Invocation invocation) throws Throwable { + public Object intercept(Invocation invocation) throws Throwable { // NOSONAR try { //常量 final int num2 = 2; @@ -123,7 +123,7 @@ public class PageInterceptor implements Interceptor { String pageSql = generatePageSql(boundSql.getSql(), pr); BoundSql pageBoundSql = new BoundSql(configuration, pageSql, boundSql.getParameterMappings(), parameter); //设置动态参数 - for (String key : additionalParameters.keySet()) { + for (String key : additionalParameters.keySet()) { // NOSONAR pageBoundSql.setAdditionalParameter(key, additionalParameters.get(key)); } //执行分页查询 @@ -164,10 +164,10 @@ public class PageInterceptor implements Interceptor { CacheKey countKey = executor.createCacheKey(countMs, parameter, RowBounds.DEFAULT, boundSql); //调用方言获取 count sql String countSql = new SqlParser().getSmartCountSql(boundSql.getSql(), "1"); - //countKey.update(countSql); + //获得BoundSql BoundSql countBoundSql = new BoundSql(countMs.getConfiguration(), countSql, boundSql.getParameterMappings(), parameter); //当使用动态 SQL 时,可能会产生临时的参数,这些参数需要手动设置到新的 BoundSql 中 - for (String key : additionalParameters.keySet()) { + for (String key : additionalParameters.keySet()) { //NOSONAR countBoundSql.setAdditionalParameter(key, additionalParameters.get(key)); } //执行 count 查询 diff --git a/udf-starter-datasource-dynamic/src/main/java/org/itkk/udf/datasource/dynamic/DynamicDataSource.java b/udf-starter-datasource-dynamic/src/main/java/org/itkk/udf/datasource/dynamic/DynamicDataSource.java index 6154ddc..9448a62 100644 --- a/udf-starter-datasource-dynamic/src/main/java/org/itkk/udf/datasource/dynamic/DynamicDataSource.java +++ b/udf-starter-datasource-dynamic/src/main/java/org/itkk/udf/datasource/dynamic/DynamicDataSource.java @@ -24,11 +24,6 @@ public class DynamicDataSource extends AbstractRoutingDataSource { */ private static Map dyanmictargetDataSources; - /** - * 描述 : 当前所使用的数据源 - */ - private static final ThreadLocal CURRENTDATASOURCE = new ThreadLocal<>(); - static { if (null == DynamicDataSource.dyanmictargetDataSources) { DynamicDataSource.dyanmictargetDataSources = new HashMap<>(); @@ -47,15 +42,6 @@ public class DynamicDataSource extends AbstractRoutingDataSource { @Autowired private IBuildDataSource buildDataSource; - /** - * 当前所使用的数据源 - * - * @return 数据源 - */ - public static DataSource getCurrentDataSource() { - return CURRENTDATASOURCE.get(); - } - @Override protected Object determineCurrentLookupKey() { //NOSONAR //拿到code @@ -94,13 +80,6 @@ public class DynamicDataSource extends AbstractRoutingDataSource { throw new DynamicDataSourceException("dataSourceCode : " + dataSourceCode + " not exist or not enable"); } log.debug("switch to the Data Source : " + dataSourceCode); - Object dataSource = dyanmictargetDataSources.get(dataSourceCode); - if (dataSource != null) { - CURRENTDATASOURCE.set((DataSource) dataSource); - } else { - CURRENTDATASOURCE.set(null); - } - log.debug("Sets the current data source : " + dataSourceCode); return dataSourceCode; } diff --git a/udf-starter-id/src/main/java/org/itkk/udf/id/IdWorker.java b/udf-starter-id/src/main/java/org/itkk/udf/id/IdWorker.java index c8cfc40..441207c 100644 --- a/udf-starter-id/src/main/java/org/itkk/udf/id/IdWorker.java +++ b/udf-starter-id/src/main/java/org/itkk/udf/id/IdWorker.java @@ -9,6 +9,7 @@ package org.itkk.udf.id; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.itkk.udf.core.exception.SystemRuntimeException; +import org.itkk.udf.id.domain.Id; import java.io.IOException; import java.lang.management.ManagementFactory; @@ -155,6 +156,21 @@ public class IdWorker { log.info("datacenterId:{},workerId:{}", this.datacenterId, this.workerId); } + /** + * 反解析ID + * + * @param id id + * @return id信息 + */ + public Id reverse(long id) { + Id reverseId = new Id(); + reverseId.setSequence((id) & ~(-1L << sequenceBits)); + reverseId.setWorkerId((id >> workerIdShift) & ~(-1L << workerIdBits)); + reverseId.setDatacenterId((id >> datacenterIdShift) & ~(-1L << datacenterIdBits)); + reverseId.setTimestamp((id >> timestampLeftShift) + twepoch); + return reverseId; + } + /** * 描述 : 下一个ID * diff --git a/udf-starter-id/src/main/java/org/itkk/udf/id/domain/Id.java b/udf-starter-id/src/main/java/org/itkk/udf/id/domain/Id.java new file mode 100644 index 0000000..d51b076 --- /dev/null +++ b/udf-starter-id/src/main/java/org/itkk/udf/id/domain/Id.java @@ -0,0 +1,43 @@ +package org.itkk.udf.id.domain; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.io.Serializable; + +/** + * Id + */ +@Data +@ApiModel(description = "分布式ID信息") +public class Id implements Serializable { + /** + * 描述 : ID + */ + private static final long serialVersionUID = 1L; + + /** + * 机器ID( 0 - 31 ) + */ + @ApiModelProperty(value = "机器ID( 0 - 31 )", required = true, dataType = "long") + private long workerId; + + /** + * 数据中心ID( 0 - 31 ) + */ + @ApiModelProperty(value = "数据中心ID( 0 - 31 )", required = true, dataType = "long") + private long datacenterId; + + /** + * 时间戳 + */ + @ApiModelProperty(value = "时间戳", required = true, dataType = "long") + private long timestamp; + + /** + * 序列号 + */ + @ApiModelProperty(value = "序列号", required = true, dataType = "long") + private long sequence; +} diff --git a/udf-starter-id/src/main/java/org/itkk/udf/id/web/IIdWorkerController.java b/udf-starter-id/src/main/java/org/itkk/udf/id/web/IIdWorkerController.java index c5c1b99..7846675 100644 --- a/udf-starter-id/src/main/java/org/itkk/udf/id/web/IIdWorkerController.java +++ b/udf-starter-id/src/main/java/org/itkk/udf/id/web/IIdWorkerController.java @@ -11,6 +11,7 @@ import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.itkk.udf.core.RestResponse; +import org.itkk.udf.id.domain.Id; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @@ -61,4 +62,20 @@ public interface IIdWorkerController { //NOSONAR @RequestMapping(value = "{count}", method = RequestMethod.GET) RestResponse> get(@PathVariable Integer count); + /** + * 解析分布式ID + * + * @param id 分布式ID + * @return ID + */ + @ApiOperation(value = "ID_3", notes = "解析分布式ID") + @ApiImplicitParams({ + @ApiImplicitParam(paramType = "header", name = "rmsApplicationName", value = "rms应用名称", required = true, dataType = "string"), + @ApiImplicitParam(paramType = "header", name = "rmsSign", value = "rms认证秘钥", required = true, dataType = "string"), + @ApiImplicitParam(paramType = "header", name = "rmsServiceCode", value = "rms接口编号", required = true, dataType = "string"), + @ApiImplicitParam(paramType = "path", name = "id", value = "分布式ID", required = true, dataType = "long") + }) + @RequestMapping(value = "reverse/{id}", method = RequestMethod.GET) + RestResponse reverse(@PathVariable long id); + } diff --git a/udf-starter-id/src/main/java/org/itkk/udf/id/web/IdWorkerController.java b/udf-starter-id/src/main/java/org/itkk/udf/id/web/IdWorkerController.java index 64aedf3..baf566f 100644 --- a/udf-starter-id/src/main/java/org/itkk/udf/id/web/IdWorkerController.java +++ b/udf-starter-id/src/main/java/org/itkk/udf/id/web/IdWorkerController.java @@ -10,6 +10,7 @@ import org.itkk.udf.core.RestResponse; import org.itkk.udf.core.exception.SystemRuntimeException; import org.itkk.udf.id.IdWorker; import org.itkk.udf.id.IdWorkerProperties; +import org.itkk.udf.id.domain.Id; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @@ -55,4 +56,9 @@ public class IdWorkerController implements IIdWorkerController { } return new RestResponse<>(ids); } + + @Override + public RestResponse reverse(@PathVariable long id) { + return new RestResponse<>(idWorker.reverse(id)); + } } diff --git a/udf-starter-id/src/main/test/java/org/itkk/udf/id/IdWorkerTest.java b/udf-starter-id/src/main/test/java/org/itkk/udf/id/IdWorkerTest.java index ffee1a6..07938bb 100644 --- a/udf-starter-id/src/main/test/java/org/itkk/udf/id/IdWorkerTest.java +++ b/udf-starter-id/src/main/test/java/org/itkk/udf/id/IdWorkerTest.java @@ -1,7 +1,6 @@ package org.itkk.udf.id; import lombok.extern.slf4j.Slf4j; -import org.junit.Ignore; import org.junit.Test; import java.text.SimpleDateFormat; @@ -13,7 +12,6 @@ import java.util.Map; public class IdWorkerTest { @Test - @Ignore public void id() { Map map = new HashMap<>(); int maxCount = 1;