Skip to content

Commit 7aa33db

Browse files
committed
add leaf starter
1 parent 5cdc884 commit 7aa33db

File tree

16 files changed

+304
-7
lines changed

16 files changed

+304
-7
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@
128128
<version>${codingapi.framework.version}</version>
129129
</dependency>
130130

131+
<dependency>
132+
<groupId>com.codingapi.springboot</groupId>
133+
<artifactId>springboot-starter-leaf</artifactId>
134+
<version>${codingapi.framework.version}</version>
135+
</dependency>
136+
131137
<dependency>
132138
<groupId>commons-dbutils</groupId>
133139
<artifactId>commons-dbutils</artifactId>

springboot-example/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
<artifactId>springboot-starter-security-jwt</artifactId>
3636
</dependency>
3737

38+
<dependency>
39+
<groupId>com.codingapi.springboot</groupId>
40+
<artifactId>springboot-starter-leaf</artifactId>
41+
</dependency>
42+
3843

3944
<dependency>
4045
<groupId>org.springframework.boot</groupId>

springboot-example/src/main/java/com/codingapi/springboot/example/ExampleApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.codingapi.springboot.example;
22

3+
import com.codingapi.springboot.leaf.EnableLeaf;
34
import org.springframework.boot.SpringApplication;
45
import org.springframework.boot.autoconfigure.SpringBootApplication;
56
import org.springframework.context.annotation.Bean;
@@ -9,6 +10,7 @@
910
import java.util.Locale;
1011

1112
@SpringBootApplication
13+
@EnableLeaf
1214
public class ExampleApplication {
1315

1416
public static void main(String[] args) {

springboot-example/src/main/java/com/codingapi/springboot/example/domain/Demo.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
import com.codingapi.springboot.example.event.DemoNameChangeEvent;
44
import com.codingapi.springboot.framework.event.EventPusher;
5+
import com.codingapi.springboot.leaf.LeafIdGenerate;
56
import lombok.Getter;
67
import lombok.NoArgsConstructor;
78
import lombok.Setter;
89
import lombok.ToString;
910

1011
import javax.persistence.Entity;
11-
import javax.persistence.GeneratedValue;
12-
import javax.persistence.GenerationType;
1312
import javax.persistence.Id;
1413

1514
/**
@@ -21,15 +20,15 @@
2120
@NoArgsConstructor
2221
@ToString
2322
@Entity(name = "t_demo")
24-
public class Demo {
23+
public class Demo implements LeafIdGenerate {
2524

2625
@Id
27-
@GeneratedValue(strategy = GenerationType.AUTO)
2826
private Integer id;
2927

3028
private String name;
3129

3230
public Demo(String name) {
31+
this.id = generateIntId();
3332
this.name = name;
3433
}
3534

springboot-starter-leaf/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717

1818

1919
<dependencies>
20+
21+
<dependency>
22+
<groupId>org.reflections</groupId>
23+
<artifactId>reflections</artifactId>
24+
</dependency>
25+
2026
<dependency>
2127
<groupId>org.perf4j</groupId>
2228
<artifactId>perf4j</artifactId>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.codingapi.springboot.leaf;
2+
3+
import com.sankuai.inf.leaf.IDGen;
4+
import com.sankuai.inf.leaf.segment.SegmentIDGenImpl;
5+
import com.sankuai.inf.leaf.segment.dao.IDAllocDao;
6+
import com.sankuai.inf.leaf.segment.dao.impl.IDAllocDaoImpl;
7+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.Configuration;
10+
11+
@Configuration
12+
public class AutoConfiguration {
13+
14+
@Bean
15+
@ConditionalOnMissingBean
16+
public IDAllocDao allocDao(){
17+
return new IDAllocDaoImpl("jdbc:h2:mem:leaf;DB_CLOSE_DELAY=-1");
18+
}
19+
20+
21+
@Bean
22+
public IDGen idGen(IDAllocDao allocDao){
23+
return new SegmentIDGenImpl(allocDao);
24+
}
25+
26+
@Bean(initMethod = "init")
27+
public Leaf leafClient(IDGen idGen, IDAllocDao allocDao){
28+
return new Leaf(idGen,allocDao);
29+
}
30+
31+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.codingapi.springboot.leaf;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.reflections.Reflections;
5+
import org.reflections.scanners.Scanners;
6+
import org.reflections.util.ConfigurationBuilder;
7+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
8+
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
9+
import org.springframework.core.type.AnnotationMetadata;
10+
11+
import java.util.*;
12+
13+
@Slf4j
14+
public class AutoConfigurationImportSelector implements ImportBeanDefinitionRegistrar {
15+
16+
private Set<Class<? extends LeafIdGenerate>> classes;
17+
18+
@Override
19+
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
20+
String className = importingClassMetadata.getClassName();
21+
List<String> packageNames = new ArrayList<>();
22+
try {
23+
packageNames.add(Class.forName(className).getPackage().getName());
24+
25+
Map<String,Object> annotations = importingClassMetadata.getAnnotationAttributes(EnableLeaf.class.getName());
26+
assert annotations != null;
27+
String[] packages = (String[])annotations.get("scanBasePackages");
28+
packageNames.addAll(Arrays.asList(packages));
29+
30+
} catch (Exception e) {
31+
throw new RuntimeException(e);
32+
}
33+
34+
Reflections reflections = new Reflections(new ConfigurationBuilder()
35+
.forPackages(packageNames.toArray(new String[]{}))
36+
.addScanners(Scanners.TypesAnnotated,Scanners.SubTypes));
37+
38+
this.classes = reflections.getSubTypesOf(LeafIdGenerate.class);
39+
log.debug("classes:{}",classes);
40+
41+
LeafContext.getInstance().setClasses(classes);
42+
43+
}
44+
45+
46+
}
47+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.codingapi.springboot.leaf;
2+
import org.springframework.context.annotation.Import;
3+
4+
import java.lang.annotation.*;
5+
6+
@Target(ElementType.TYPE)
7+
@Retention(RetentionPolicy.RUNTIME)
8+
@Documented
9+
@Import(AutoConfigurationImportSelector.class)
10+
public @interface EnableLeaf {
11+
12+
String[] scanBasePackages() default {};
13+
14+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.codingapi.springboot.leaf;
2+
3+
4+
import com.codingapi.springboot.leaf.exception.LeafServerException;
5+
import com.codingapi.springboot.leaf.exception.NoKeyException;
6+
import com.sankuai.inf.leaf.IDGen;
7+
import com.sankuai.inf.leaf.common.Result;
8+
import com.sankuai.inf.leaf.common.Status;
9+
import com.sankuai.inf.leaf.segment.dao.IDAllocDao;
10+
import com.sankuai.inf.leaf.segment.model.LeafAlloc;
11+
import lombok.AllArgsConstructor;
12+
import lombok.extern.slf4j.Slf4j;
13+
14+
import java.util.Date;
15+
16+
/**
17+
* @author lorne
18+
* @since 1.0.0
19+
*/
20+
@Slf4j
21+
@AllArgsConstructor
22+
class Leaf {
23+
24+
private final IDGen idGen;
25+
private final IDAllocDao idAllocDao;
26+
27+
28+
long segmentGetId(String key){
29+
Result result = idGen.get(key);
30+
if (key == null || key.isEmpty()) {
31+
throw new NoKeyException();
32+
}
33+
if (result.getStatus().equals(Status.EXCEPTION)) {
34+
throw new LeafServerException(result.toString());
35+
}
36+
return result.getId();
37+
}
38+
39+
40+
/**
41+
* 数据库模式添加 数据
42+
* @param key key 关键字
43+
* @param step 每次获取数据长度 2000
44+
* @param maxId 开始的最大Id 1
45+
* @return 执行状态
46+
*/
47+
boolean segmentPush(String key, int step, int maxId) {
48+
LeafAlloc alloc = new LeafAlloc();
49+
alloc.setKey(key);
50+
alloc.setStep(step);
51+
alloc.setMaxId(maxId);
52+
alloc.setUpdateTime(new Date());
53+
return idAllocDao.save(alloc);
54+
}
55+
56+
void initIdGen(){
57+
idGen.init();
58+
}
59+
60+
void init(){
61+
LeafContext.getInstance().setLeaf(this);
62+
}
63+
64+
}
65+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.codingapi.springboot.leaf;
2+
3+
import com.codingapi.springboot.leaf.exception.LeafServerException;
4+
import lombok.Setter;
5+
import lombok.extern.slf4j.Slf4j;
6+
7+
import java.util.Set;
8+
9+
/**
10+
* @author lorne
11+
* @since 1.0.0
12+
*/
13+
@Slf4j
14+
class LeafContext {
15+
16+
private Leaf leaf;
17+
@Setter
18+
private Set<Class<? extends LeafIdGenerate>> classes;
19+
20+
private LeafContext(){
21+
}
22+
23+
private static LeafContext instance;
24+
25+
public static LeafContext getInstance() {
26+
if(instance==null){
27+
synchronized (LeafContext.class){
28+
if(instance==null){
29+
instance = new LeafContext();
30+
}
31+
}
32+
}
33+
return instance;
34+
}
35+
36+
protected void setLeaf(Leaf leaf){
37+
this.leaf = leaf;
38+
this.initClass();
39+
}
40+
41+
42+
long generateId(Class<?> clazz){
43+
return segmentGetId(clazz);
44+
}
45+
46+
private long segmentGetId(Class<?> clazz){
47+
return leaf.segmentGetId(clazz.getName());
48+
}
49+
50+
51+
boolean push(String key, int step, int maxId){
52+
return leaf.segmentPush(key,step,maxId);
53+
}
54+
55+
56+
private void initClass(){
57+
if(classes!=null&&classes.size()>0) {
58+
for (Class<?> clazz : classes) {
59+
try {
60+
LeafContext.getInstance().push(clazz.getName(), 2000, 1);
61+
} catch (Exception e) {
62+
throw new LeafServerException(e);
63+
}
64+
}
65+
}
66+
leaf.initIdGen();
67+
log.info("leaf init finish.");
68+
}
69+
70+
}

0 commit comments

Comments
 (0)