添加指定"数据中心"和"机器"生成分布式ID的服务

This commit is contained in:
wangkang
2017-09-05 12:44:25 +08:00
parent 275862dd58
commit 6811ce20f7
6 changed files with 214 additions and 30 deletions
+4
View File
@@ -29,5 +29,9 @@
<groupId>org.itkk.udf</groupId>
<artifactId>udf-starter-cache-redis</artifactId>
</dependency>
<dependency>
<groupId>org.itkk.udf</groupId>
<artifactId>udf-starter-rms</artifactId>
</dependency>
</dependencies>
</project>
@@ -179,6 +179,33 @@ public class IdWorkerInit {
log.info("init IdWorker success = {} , {} , {}", result, this.datacenterId, this.workerId);
}
/**
* 获得缓存值
*
* @param paramDatacenterId 数据中心ID
* @param paramWorkerId 机器ID
* @return 缓存值
*/
public CacheValue getCacheValue(Integer paramDatacenterId, Integer paramWorkerId) {
return redisTemplate.execute((RedisCallback<CacheValue>) connection -> {
try {
//判空
if (datacenterId == null || workerId == null) {
throw new SystemRuntimeException("datacenterId and workerId must not be null");
}
//生成key
String key = cacheRedisProperties.getPrefix().concat(SPLIT).concat(CACHE_NAME).concat(SPLIT).concat(Integer.toString(paramDatacenterId)).concat(SPLIT).concat(Integer.toString(paramWorkerId));
//判断缓存是否存在
if (!connection.exists(serializer.serialize(key))) {
throw new SystemRuntimeException("cache does not exist , key ----> " + key);
}
return (CacheValue) jdkSerializationRedisSerializer.deserialize(connection.get(serializer.serialize(key)));
} finally {
connection.close();
}
});
}
/**
* 定时刷新过期时间
*/
@@ -0,0 +1,124 @@
package org.itkk.udf.id.service;
import org.itkk.udf.core.RestResponse;
import org.itkk.udf.core.exception.SystemRuntimeException;
import org.itkk.udf.id.IdWorkerInit;
import org.itkk.udf.id.IdWorkerProperties;
import org.itkk.udf.id.domain.CacheValue;
import org.itkk.udf.id.domain.Id;
import org.itkk.udf.rms.Rms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* IdWorkerService
*/
@Service
public class IdWorkerService {
/**
* idWorker
*/
@Autowired
private IdWorkerInit idWorkerInit;
/**
* idWorkerProperties
*/
@Autowired
private IdWorkerProperties idWorkerProperties;
/**
* rms
*/
@Autowired
private Rms rms;
/**
* 获得分布式ID[IdWorker随机]
*
* @return ID
*/
public String get() {
return Long.toString(idWorkerInit.get().nextId());
}
/**
* 批量获得分布式ID[IdWorker随机]
*
* @param count 数量
* @return ID
*/
public List<String> get(Integer count) {
//判断最大数量
if (count > idWorkerProperties.getMaxCount()) {
throw new SystemRuntimeException("The number is too large and the maximum setting is " + idWorkerProperties.getMaxCount());
}
//循环生成
List<String> ids = new ArrayList<>();
for (int i = 0; i < count; i++) {
ids.add(Long.toString(idWorkerInit.get().nextId()));
}
return ids;
}
/**
* 解析分布式ID
*
* @param id 分布式ID
* @return ID
*/
public Id reverse(long id) {
return idWorkerInit.get().reverse(id);
}
/**
* 获得分布式ID[指定IdWorker]
*
* @param datacenterId 数据中心ID
* @param workerId workerId
* @return ID
*/
public String workerGet(Integer datacenterId, Integer workerId) {
//定义需要请求的接口
final String serviceCode = "ID_1";
//获得缓存
CacheValue cacheValue = idWorkerInit.getCacheValue(datacenterId, workerId);
//远程请求
ResponseEntity<RestResponse<String>> result = rms.call(cacheValue.getHost(), cacheValue.getPort(), serviceCode, null, null, new ParameterizedTypeReference<RestResponse<String>>() {
}, null);
//返回
return result.getBody().getResult();
}
/**
* 批量获得分布式ID[指定IdWorker]
*
* @param datacenterId 数据中心ID
* @param workerId workerId
* @param count 数量
* @return ID
*/
public List<String> workerGet(Integer datacenterId, Integer workerId, Integer count) {
//定义需要请求的接口
final String serviceCode = "ID_2";
//获得缓存
CacheValue cacheValue = idWorkerInit.getCacheValue(datacenterId, workerId);
//构建uriVariables
Map<String, Object> uriVariables = new HashMap<>();
uriVariables.put("count", count);
//远程请求
ResponseEntity<RestResponse<List<String>>> result = rms.call(cacheValue.getHost(), cacheValue.getPort(), serviceCode, null, null, new ParameterizedTypeReference<RestResponse<List<String>>>() {
}, uriVariables);
//返回
return result.getBody().getResult();
}
}
@@ -28,11 +28,11 @@ import java.util.List;
public interface IIdWorkerController { //NOSONAR
/**
* 获得分布式ID
* 获得分布式ID[IdWorker随机]
*
* @return ID
*/
@ApiOperation(value = "ID_1", notes = "获得分布式ID(注意:ID为长整型,在javascript下会丢失精度,请注意数据类型转换)")
@ApiOperation(value = "ID_1", notes = "获得分布式ID[IdWorker随机](注意:ID为长整型,在javascript下会丢失精度,请注意数据类型转换)")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "header", name = "rmsApplicationName", value = "rms应用名称", required = true, dataType = "string"),
@ApiImplicitParam(paramType = "header", name = "rmsSign", value = "rms认证秘钥", required = true, dataType = "string"),
@@ -42,12 +42,12 @@ public interface IIdWorkerController { //NOSONAR
RestResponse<String> get();
/**
* 批量获得分布式ID
* 批量获得分布式ID[IdWorker随机]
*
* @param count 数量
* @return ID
*/
@ApiOperation(value = "ID_2", notes = "批量获得分布式ID(注意:ID为长整型,在javascript下会丢失精度,请注意数据类型转换)")
@ApiOperation(value = "ID_2", notes = "批量获得分布式ID[IdWorker随机](注意:ID为长整型,在javascript下会丢失精度,请注意数据类型转换)")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "header", name = "rmsApplicationName", value = "rms应用名称", required = true, dataType = "string"),
@ApiImplicitParam(paramType = "header", name = "rmsSign", value = "rms认证秘钥", required = true, dataType = "string"),
@@ -73,4 +73,42 @@ public interface IIdWorkerController { //NOSONAR
@RequestMapping(value = "reverse/{id}", method = RequestMethod.GET)
RestResponse<Id> reverse(@PathVariable long id);
/**
* 获得分布式ID[指定IdWorker]
*
* @param datacenterId 数据中心ID
* @param workerId workerId
* @return ID
*/
@ApiOperation(value = "ID_4", notes = "获得分布式ID[指定IdWorker](注意:ID为长整型,在javascript下会丢失精度,请注意数据类型转换)")
@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 = "datacenterId", value = "数据中心ID", required = true, dataType = "int"),
@ApiImplicitParam(paramType = "path", name = "workerId", value = "机器ID", required = true, dataType = "int")
})
@RequestMapping(value = "worker/{datacenterId}/{workerId}", method = RequestMethod.GET)
RestResponse<String> workerGet(@PathVariable Integer datacenterId, @PathVariable Integer workerId);
/**
* 批量获得分布式ID[指定IdWorker]
*
* @param datacenterId 数据中心ID
* @param workerId workerId
* @param count 数量
* @return ID
*/
@ApiOperation(value = "ID_5", notes = "批量获得分布式ID[指定IdWorker](注意:ID为长整型,在javascript下会丢失精度,请注意数据类型转换)")
@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 = "datacenterId", value = "数据中心ID", required = true, dataType = "int"),
@ApiImplicitParam(paramType = "path", name = "workerId", value = "机器ID", required = true, dataType = "int"),
@ApiImplicitParam(paramType = "path", name = "count", value = "数量", required = true, dataType = "string")
})
@RequestMapping(value = "worker/{datacenterId}/{workerId}/{count}", method = RequestMethod.GET)
RestResponse<List<String>> workerGet(@PathVariable Integer datacenterId, @PathVariable Integer workerId, @PathVariable Integer count);
}
@@ -7,15 +7,12 @@
package org.itkk.udf.id.web;
import org.itkk.udf.core.RestResponse;
import org.itkk.udf.core.exception.SystemRuntimeException;
import org.itkk.udf.id.IdWorkerInit;
import org.itkk.udf.id.IdWorkerProperties;
import org.itkk.udf.id.domain.Id;
import org.itkk.udf.id.service.IdWorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
/**
@@ -27,38 +24,33 @@ import java.util.List;
public class IdWorkerController implements IIdWorkerController {
/**
* idWorker
* idWorkerService
*/
@Autowired
private IdWorkerInit idWorkerInit;
/**
* idWorkerProperties
*/
@Autowired
private IdWorkerProperties idWorkerProperties;
private IdWorkerService idWorkerService;
@Override
public RestResponse<String> get() {
return new RestResponse<>(Long.toString(idWorkerInit.get().nextId()));
return new RestResponse<>(idWorkerService.get());
}
@Override
public RestResponse<List<String>> get(@PathVariable Integer count) {
//判断最大数量
if (count > idWorkerProperties.getMaxCount()) {
throw new SystemRuntimeException("The number is too large and the maximum setting is " + idWorkerProperties.getMaxCount());
}
//循环生成
List<String> ids = new ArrayList<>();
for (int i = 0; i < count; i++) {
ids.add(Long.toString(idWorkerInit.get().nextId()));
}
return new RestResponse<>(ids);
return new RestResponse<>(idWorkerService.get(count));
}
@Override
public RestResponse<Id> reverse(@PathVariable long id) {
return new RestResponse<>(idWorkerInit.get().reverse(id));
return new RestResponse<>(idWorkerService.reverse(id));
}
@Override
public RestResponse<String> workerGet(@PathVariable Integer datacenterId, @PathVariable Integer workerId) {
return new RestResponse<>(idWorkerService.workerGet(datacenterId, workerId));
}
@Override
public RestResponse<List<String>> workerGet(@PathVariable Integer datacenterId, @PathVariable Integer workerId, @PathVariable Integer count) {
return new RestResponse<>(idWorkerService.workerGet(datacenterId, workerId, count));
}
}
@@ -79,8 +79,7 @@ public class Rms {
//获取服务元数据
ServiceMeta serviceMeta = rmsProperties.getService().get(serviceCode);
//构建请求路径
String path = serviceMeta.getIsHttps() ? Constant.HTTPS : Constant.HTTP;
path.concat(host).concat(":").concat(String.valueOf(port)).concat(serviceMeta.getUri());
String path = (serviceMeta.getIsHttps() ? Constant.HTTPS : Constant.HTTP).concat(host).concat(":").concat(String.valueOf(port)).concat(serviceMeta.getUri());
//获得请求方法
String method = getRmsMethod(serviceCode);
//拼装路径参数