Skip to content

Commit 1168a64

Browse files
committed
add leaf starter ..
1 parent aeebb04 commit 1168a64

22 files changed

+832
-6
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<commons-io.version>2.11.0</commons-io.version>
4040
<commons-dbutils.version>1.7</commons-dbutils.version>
4141
<org.reflections.version>0.10.2</org.reflections.version>
42+
<perf4j.version>0.9.16</perf4j.version>
4243

4344
</properties>
4445

@@ -74,6 +75,12 @@
7475
<version>${org.reflections.version}</version>
7576
</dependency>
7677

78+
<dependency>
79+
<groupId>org.perf4j</groupId>
80+
<artifactId>perf4j</artifactId>
81+
<version>${perf4j.version}</version>
82+
</dependency>
83+
7784
<dependency>
7885
<groupId>com.alibaba</groupId>
7986
<artifactId>fastjson</artifactId>

springboot-starter-leaf/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
<groupId>org.reflections</groupId>
2121
<artifactId>reflections</artifactId>
2222
</dependency>
23+
24+
<dependency>
25+
<groupId>org.perf4j</groupId>
26+
<artifactId>perf4j</artifactId>
27+
</dependency>
28+
2329
<dependency>
2430
<groupId>org.springframework.data</groupId>
2531
<artifactId>spring-data-commons</artifactId>

springboot-starter-leaf/src/main/java/com/codingapi/springboot/leaf/AutoConfiguration.java

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

3+
import com.codingapi.springboot.leaf.exception.InitException;
4+
import com.codingapi.springboot.leaf.segment.dao.IDAllocDao;
5+
import com.codingapi.springboot.leaf.segment.dao.impl.IDAllocDaoImpl;
6+
import com.codingapi.springboot.leaf.service.SegmentService;
37
import org.springframework.context.annotation.Bean;
48
import org.springframework.context.annotation.Configuration;
59

@@ -10,8 +14,18 @@
1014
@Configuration
1115
public class AutoConfiguration {
1216

17+
@Bean
18+
public IDAllocDao allocDao(){
19+
return new IDAllocDaoImpl();
20+
}
21+
22+
@Bean
23+
public SegmentService segmentService(IDAllocDao allocDao) throws InitException {
24+
return new SegmentService(allocDao);
25+
}
26+
1327
@Bean(initMethod = "init")
14-
public LeafClient leafClient(){
15-
return new LeafClient();
28+
public LeafClient leafClient(SegmentService segmentService){
29+
return new LeafClient(segmentService);
1630
}
1731
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.codingapi.springboot.leaf;
2+
3+
4+
import com.codingapi.springboot.leaf.common.Result;
5+
6+
public interface IDGen {
7+
8+
Result get(String key);
9+
10+
boolean init();
11+
12+
}

springboot-starter-leaf/src/main/java/com/codingapi/springboot/leaf/LeafClient.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,38 @@
11
package com.codingapi.springboot.leaf;
22

3+
import com.codingapi.springboot.leaf.common.Result;
4+
import com.codingapi.springboot.leaf.common.Status;
5+
import com.codingapi.springboot.leaf.exception.InitException;
6+
import com.codingapi.springboot.leaf.exception.LeafServerException;
7+
import com.codingapi.springboot.leaf.exception.NoKeyException;
8+
import com.codingapi.springboot.leaf.segment.model.LeafAlloc;
9+
import com.codingapi.springboot.leaf.service.SegmentService;
310
import lombok.extern.slf4j.Slf4j;
411

12+
import java.util.Date;
13+
514
/**
615
* @author lorne
716
* @since 1.0.0
817
*/
918
@Slf4j
1019
public class LeafClient {
1120

21+
private SegmentService segmentService;
22+
23+
public LeafClient(SegmentService segmentService) {
24+
this.segmentService = segmentService;
25+
}
26+
1227
public long segmentGetId(String key){
13-
//todo leaf
14-
return 1;
28+
Result result = segmentService.getId(key);
29+
if (key == null || key.isEmpty()) {
30+
throw new NoKeyException();
31+
}
32+
if (result.getStatus().equals(Status.EXCEPTION)) {
33+
throw new LeafServerException(result.toString());
34+
}
35+
return result.getId();
1536
}
1637

1738

@@ -22,8 +43,14 @@ public long segmentGetId(String key){
2243
* @param maxId 开始的最大Id 1
2344
* @return 执行状态
2445
*/
25-
public boolean segmentPush(String key, int step, int maxId){
46+
public boolean segmentPush(String key, int step, int maxId) throws InitException {
2647
//todo leaf
48+
LeafAlloc alloc = new LeafAlloc();
49+
alloc.setKey(key);
50+
alloc.setStep(step);
51+
alloc.setMaxId(maxId);
52+
alloc.setUpdateTime(new Date());
53+
segmentService.save(alloc);
2754
return true;
2855
}
2956

springboot-starter-leaf/src/main/java/com/codingapi/springboot/leaf/LeafUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.codingapi.springboot.leaf;
22

33
import lombok.Setter;
4+
import lombok.SneakyThrows;
45
import lombok.extern.slf4j.Slf4j;
56

67
import java.util.Set;
@@ -50,7 +51,8 @@ private long segmentGetId(Class<?> clazz){
5051
}
5152

5253

53-
boolean push(String key,int step,int maxId){
54+
@SneakyThrows
55+
boolean push(String key, int step, int maxId){
5456
if(leafClient==null){
5557
throw new RuntimeException("springboot init finish then to running.");
5658
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.codingapi.springboot.leaf.common;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
8+
@Setter
9+
@Getter
10+
@AllArgsConstructor
11+
public class CheckVO {
12+
13+
private long timestamp;
14+
private int workID;
15+
16+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.codingapi.springboot.leaf.common;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
import java.io.IOException;
6+
import java.util.Properties;
7+
8+
@Slf4j
9+
public class PropertyFactory {
10+
11+
private static final Properties prop = new Properties();
12+
static {
13+
try {
14+
prop.load(PropertyFactory.class.getClassLoader().getResourceAsStream("leaf.properties"));
15+
} catch (IOException e) {
16+
log.warn("Load Properties Ex", e);
17+
}
18+
}
19+
20+
public static Properties getProperties() {
21+
return prop;
22+
}
23+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.codingapi.springboot.leaf.common;
2+
3+
import lombok.*;
4+
5+
@Setter
6+
@Getter
7+
@ToString
8+
@AllArgsConstructor
9+
@NoArgsConstructor
10+
public class Result {
11+
12+
private long id;
13+
private Status status;
14+
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.codingapi.springboot.leaf.common;
2+
3+
public enum Status {
4+
SUCCESS,
5+
EXCEPTION
6+
}

0 commit comments

Comments
 (0)