提交 f54c45c9 编写于 作者: 无难事者若执's avatar 无难事者若执

feat : id生成框架 spring项目引入自动加载

上级 8d2752b7
<mxfile host="drawio-plugin" mdified="2023-08-26T06:35:13.778Z" agent="5.0 (Macintosh; Intel Mac OS X 10_16_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36" version="13.7.9" etag="KnpRbxr8W5eJYHEtHvRz" type="embed"><diagram id="1bsDMg0hRZ06JWtRajhb" name="Page-1"><mxGraphModel dx="510" dy="311" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0"><root><mxCell id="0"/><mxCell id="1" parent="0"/><mxCell id="2" value="根据不同算法生成ID" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#ffe6cc;strokeColor=#d79b00;" vertex="1" parent="1"><mxGeometry x="140" y="240" width="120" height="60" as="geometry"/></mxCell><mxCell id="4" value="ID" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1"><mxGeometry x="140" y="220" width="40" height="20" as="geometry"/></mxCell></root></mxGraphModel></diagram></mxfile>
\ No newline at end of file
...@@ -7,13 +7,20 @@ ...@@ -7,13 +7,20 @@
<groupId>com.kongxiang</groupId> <groupId>com.kongxiang</groupId>
<artifactId>kongxiang-spring</artifactId> <artifactId>kongxiang-spring</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.11.RELEASE</version>
<relativePath></relativePath>
</parent>
<properties> <properties>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version> <spring-boot.version>2.3.11.RELEASE</spring-boot.version>
<spring-cloud-alibaba.version>2.2.2.RELEASE</spring-cloud-alibaba.version> <spring-cloud-alibaba.version>2.2.2.RELEASE</spring-cloud-alibaba.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version> <spring-cloud.version>Hoxton.SR9</spring-cloud.version>
</properties> </properties>
...@@ -41,38 +48,7 @@ ...@@ -41,38 +48,7 @@
</dependencies> </dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
...@@ -88,18 +64,8 @@ ...@@ -88,18 +64,8 @@
<!--<plugin> <!--<plugin>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version> <version>2.3.11.RELEASE</version>
<configuration>
&lt;!&ndash; <mainClass>com.kx.raindrop.SpringCloudDddApplication</mainClass>&ndash;&gt;
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>--> </plugin>-->
</plugins> </plugins>
</build> </build>
......
package com.kx.config;
import org.springframework.context.annotation.ComponentScan;
/**
* @author kongxiang
*/
@ComponentScan("com.kx.utils.id")
public class IdAutoConfiguration {
}
package com.kx.utils.id;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* id算法注册器
* @author kongxiang
*/
@Component
@Slf4j
public class IdAlgorithmRegister {
/**
* 获取所有类型是 IdGenerator
*/
@Autowired
private List<? extends IdGenerator> idGeneratorList;
protected Map<String ,IdGenerator> algorithmMap = new ConcurrentHashMap<>(8);
@PostConstruct
public void init(){
if (idGeneratorList!= null ){
idGeneratorList.forEach(idGenerator -> {
algorithmMap.put(idGenerator.getAlgorithm(),idGenerator);
log.info(">> Load id generator successful -- : algorithm : [ {} ] ", idGenerator.getAlgorithm());
});
}
}
/**
* 注册生成算法
* @param algorithm
* @param idGenerator
* @return 注册结果
*/
protected boolean register(String algorithm, IdGenerator idGenerator){
boolean registerStatus = false;
if (!StringUtils.isEmpty(algorithm) && idGenerator != null ){
algorithmMap.put(algorithm,idGenerator);
registerStatus = true;
}
return registerStatus;
}
/**
* 获取算法生成器类
* @param algorithm 算法
* @return 算法生成器
*/
public IdGenerator getAlgorithm(String algorithm){
IdGenerator idGenerator = this.algorithmMap.get(algorithm);
if (idGenerator == null ){
throw new UnsupportedOperationException("id generate is not supported : " + algorithm);
}
return idGenerator;
}
/**
* 是否存有算法生成器
* @param algorithm 算法
* @return
*/
protected boolean containAlgorithm(String algorithm){
return this.algorithmMap.containsKey(algorithm);
}
/**
* 列出所有支持的算法
* @return
*/
protected Set<String> supportAlgorithms(){
return this.algorithmMap.keySet();
}
}
package com.kx.utils.id;
/**
* Id 生成器
*
* @author kongxiang
*/
public interface IdGenerator {
/**
* 具体生成id的算法实现
* @return
*/
String generate();
/**
* 获取算法标识
* @return
*/
String getAlgorithm();
}
package com.kx.utils.id;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Set;
/**
* id 相关能力
* @author kongxiang
*/
@Component
public class IdService {
@Autowired
private IdAlgorithmRegister idAlgorithmRegister;
/**
* 生成指定算法的id
* @param algorithm 算法
* @return id
*/
public String generate(String algorithm){
return idAlgorithmRegister.getAlgorithm(algorithm).generate();
}
/**
* 生成id 默认uuid算法
* @return id
*/
public String generate(){
return idAlgorithmRegister.getAlgorithm("uuid").generate();
}
/**
* 显示支持的所有算法
* @return 支持的算法列表
*/
public Set<String> supports(){
return idAlgorithmRegister.supportAlgorithms();
}
}
package com.kx.utils.id.impl;
import com.kx.utils.id.IdGenerator;
import org.springframework.stereotype.Component;
import java.util.UUID;
/**
* 本地机器uuid id 生成器
* @author kongxiang
*/
@Component
public class LocalUuidGenerator implements IdGenerator {
@Override
public String generate() {
return UUID.randomUUID().toString().replace("-","");
}
@Override
public String getAlgorithm() {
return "uuid";
}
}
package com.kx.utils.idempotent.annotation;
/**
* 幂等注解
*
*/
public @interface Idempotent {
String value() default "";
}
package com.kx.utils.idempotent.impl;
import org.springframework.stereotype.Component;
/**
* <pre>接口幂等性方案: 防重token令牌实现幂等性
* </pre>
*
* 针对客户端连续点击或者调用方的超时重试等情况,例如提交订单,此种操作就可以用 Token 的机制实现防止重复提交。
* 简单的说就是调用方在调用接口的时候先向后端请求一个全局 ID(Token),请求的时候携带这个全局 ID 一起请求(Token 最好将其放到 Headers 中),后端需要对这个 Token 作为 Key,用户信息作为 Value 到 Redis 中进行键值内容校验,如果 Key 存在且 Value 匹配就执行删除命令,然后正常执行后面的业务逻辑。如果不存在对应的 Key 或 Value 不匹配就返回重复执行的错误信息,这样来保证幂等操作。
* <pre>
*
* 使用操作:
* 插入操作
* 更新操作
* 删除操作
* </pre>
*
* @author kongxiang
*/
@Component
public class AntiDuplicateTokenHandler {
}
package com.kx.utils.token;
import lombok.Data;
import java.util.Date;
/**
* Token 令牌
* 领域对象
* @author kongxiang
*/
@Data
public class Token {
/**
* token 值
*/
private String value;
/**
* token生成时间
*/
private Date createTime;
/**
* token过期时间
*/
private Long expireMillSecond;
/**
* 是否过期
* @return
*/
public boolean isExpire(){
return System.currentTimeMillis() - expireMillSecond > createTime.getTime();
}
}
package com.kx.utils.token;
/**
* @author kongxiang
*/
public interface TokenGenerator {
}
# Auto config
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.kx.config.IdAutoConfiguration
\ No newline at end of file
package com.kx.test;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class IdTest {
@Test
public void IdGen(){
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册