设置ID最大生成数量

This commit is contained in:
wangkang
2017-08-22 09:41:30 +08:00
parent b3daa15308
commit 42bb4ad5b4
3 changed files with 25 additions and 18 deletions
@@ -1,5 +1,7 @@
package org.itkk.udf.id;
import lombok.Data;
import org.itkk.udf.id.web.IIdWorkerController;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
@@ -12,29 +14,17 @@ import javax.validation.constraints.NotNull;
@Component
@ConfigurationProperties(prefix = "org.itkk.id.properties")
@Validated
@Data
public class IdWorkerProperties {
/**
* 最大个数
*/
private int maxCount = IIdWorkerController.MAX_COUNT;
/**
* ID构造类型
*/
@NotNull
private String type = IdWorker.UUID_TYPE;
/**
* D构造类型
*
* @return String
*/
public String getType() {
return type;
}
/**
* D构造类型
*
* @param type type
*/
public void setType(String type) {
this.type = type;
}
}
@@ -26,6 +26,11 @@ import java.util.List;
@RequestMapping(value = "/service/id")
public interface IIdWorkerController {
/**
* 最大个数
*/
public static final int MAX_COUNT = 1000;
/**
* 获得分布式ID
*
@@ -8,6 +8,7 @@ package org.itkk.udf.id.web;
import org.itkk.udf.core.RestResponse;
import org.itkk.udf.id.IdWorker;
import org.itkk.udf.id.IdWorkerProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@@ -29,6 +30,12 @@ public class IdWorkerController implements IIdWorkerController {
@Autowired
private IdWorker idWorker;
/**
* idWorkerProperties
*/
@Autowired
private IdWorkerProperties idWorkerProperties;
@Override
public RestResponse<String> get() {
return new RestResponse<>(Long.toString(idWorker.nextId()));
@@ -36,6 +43,11 @@ public class IdWorkerController implements IIdWorkerController {
@Override
public RestResponse<List<String>> get(@PathVariable Integer count) {
//判断最大数量
if (count > idWorkerProperties.getMaxCount()) {
throw new RuntimeException("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(idWorker.nextId()));