添加snowflake的反向解析的rest服务

This commit is contained in:
王康
2017-09-03 14:39:32 +08:00
parent 324dc2326c
commit 12db398a6f
7 changed files with 86 additions and 27 deletions
@@ -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 查询
@@ -24,11 +24,6 @@ public class DynamicDataSource extends AbstractRoutingDataSource {
*/
private static Map<Object, Object> dyanmictargetDataSources;
/**
* 描述 : 当前所使用的数据源
*/
private static final ThreadLocal<DataSource> 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;
}
@@ -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
*
@@ -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;
}
@@ -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<List<String>> 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<Id> reverse(@PathVariable long id);
}
@@ -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<Id> reverse(@PathVariable long id) {
return new RestResponse<>(idWorker.reverse(id));
}
}
@@ -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<Long, Long> map = new HashMap<>();
int maxCount = 1;