Skip to content

Commit aeebb04

Browse files
committed
add leaf starter
1 parent 22111b8 commit aeebb04

File tree

13 files changed

+266
-4
lines changed

13 files changed

+266
-4
lines changed

pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44
<packaging>pom</packaging>
5+
<modules>
6+
<module>springboot-starter-leaf</module>
7+
</modules>
58
<parent>
69
<groupId>org.springframework.boot</groupId>
710
<artifactId>spring-boot-starter-parent</artifactId>
@@ -35,6 +38,7 @@
3538
<jsonwebtoken.jjwt.version>0.11.5</jsonwebtoken.jjwt.version>
3639
<commons-io.version>2.11.0</commons-io.version>
3740
<commons-dbutils.version>1.7</commons-dbutils.version>
41+
<org.reflections.version>0.10.2</org.reflections.version>
3842

3943
</properties>
4044

@@ -63,6 +67,13 @@
6367

6468
<dependencyManagement>
6569
<dependencies>
70+
71+
<dependency>
72+
<groupId>org.reflections</groupId>
73+
<artifactId>reflections</artifactId>
74+
<version>${org.reflections.version}</version>
75+
</dependency>
76+
6677
<dependency>
6778
<groupId>com.alibaba</groupId>
6879
<artifactId>fastjson</artifactId>
@@ -106,6 +117,11 @@
106117
<version>${codingapi.framework.version}</version>
107118
</dependency>
108119

120+
<dependency>
121+
<groupId>com.codingapi.springboot</groupId>
122+
<artifactId>springboot-starter-leaf</artifactId>
123+
<version>${codingapi.framework.version}</version>
124+
</dependency>
109125

110126
<dependency>
111127
<groupId>commons-dbutils</groupId>

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
<dependency>
3944
<groupId>org.springframework.boot</groupId>
4045
<artifactId>spring-boot-starter-web</artifactId>

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,16 +20,16 @@
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.IDENTITY)
2826
private Integer id;
2927

3028
private String name;
3129

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

3635
public void changeName(String name){

springboot-example/src/main/resources/application.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
server.port=8085
3+
24
spring.datasource.driver-class-name=org.h2.Driver
35
spring.datasource.url=jdbc:h2:file:./demo.db
46

springboot-starter-leaf/pom.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>springboot-parent</artifactId>
7+
<groupId>com.codingapi.springboot</groupId>
8+
<version>1.1.2</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>springboot-starter-leaf</artifactId>
13+
14+
<properties>
15+
<java.version>1.8</java.version>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.reflections</groupId>
21+
<artifactId>reflections</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.springframework.data</groupId>
25+
<artifactId>spring-data-commons</artifactId>
26+
</dependency>
27+
</dependencies>
28+
29+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.codingapi.springboot.leaf;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
/**
7+
* @author lorne
8+
* @since 1.0.0
9+
*/
10+
@Configuration
11+
public class AutoConfiguration {
12+
13+
@Bean(initMethod = "init")
14+
public LeafClient leafClient(){
15+
return new LeafClient();
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.info("classes:{}",classes);
40+
41+
LeafUtils.getInstance().setClasses(classes);
42+
43+
}
44+
45+
46+
47+
48+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.codingapi.springboot.leaf;
2+
3+
import org.springframework.context.annotation.Import;
4+
5+
import java.lang.annotation.*;
6+
7+
@Target(ElementType.TYPE)
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Documented
10+
@Import(AutoConfigurationImportSelector.class)
11+
public @interface EnableLeaf {
12+
13+
String[] scanBasePackages() default {};
14+
15+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.codingapi.springboot.leaf;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
/**
6+
* @author lorne
7+
* @since 1.0.0
8+
*/
9+
@Slf4j
10+
public class LeafClient {
11+
12+
public long segmentGetId(String key){
13+
//todo leaf
14+
return 1;
15+
}
16+
17+
18+
/**
19+
* 数据库模式添加 数据
20+
* @param key key 关键字
21+
* @param step 每次获取数据长度 2000
22+
* @param maxId 开始的最大Id 1
23+
* @return 执行状态
24+
*/
25+
public boolean segmentPush(String key, int step, int maxId){
26+
//todo leaf
27+
return true;
28+
}
29+
30+
public void init(){
31+
LeafUtils.getInstance().setLeafClient(this);
32+
}
33+
34+
}

0 commit comments

Comments
 (0)