Skip to content

Commit 8719350

Browse files
committed
test jap worker id complete
1 parent 2734470 commit 8719350

File tree

48 files changed

+133
-239
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+133
-239
lines changed

.idea/encodings.xml

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@
4343

4444
<modules>
4545
<module>uid-generator</module>
46-
<module>uid-worker-mybatis-jdbc-spring-boot-starter</module>
46+
<module>uid-generator-mybatis-jdbc-spring-boot-starter</module>
4747
<module>uid-generator-api</module>
4848
<module>uid-generator-spring-boot-starter</module>
49-
<module>uid-worker-mybatis-r2dbc-spring-boot-starter</module>
50-
<module>uid-worker-jpa-r2dbc-spring-boot-starter</module>
51-
<module>uid-worker-jap-jdbc-spring-boot-starter</module>
49+
<module>uid-generator-mybatis-r2dbc-spring-boot-starter</module>
50+
<module>uid-generator-jpa-r2dbc-spring-boot-starter</module>
51+
<module>uid-generator-jap-jdbc-spring-boot-starter</module>
5252
</modules>
5353

5454
<properties>

uid-generator-api/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,5 @@
2929
<artifactId>reactor-core</artifactId>
3030
</dependency>
3131

32-
<dependency>
33-
<groupId>io.projectreactor</groupId>
34-
<artifactId>reactor-test</artifactId>
35-
<scope>test</scope>
36-
</dependency>
37-
3832
</dependencies>
3933
</project>

uid-worker-jap-jdbc-spring-boot-starter/pom.xml renamed to uid-generator-jap-jdbc-spring-boot-starter/pom.xml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<version>1.0.5-SNAPSHOT</version>
1010
</parent>
1111

12-
<artifactId>uid-worker-jap-jdbc-spring-boot-starter</artifactId>
12+
<artifactId>uid-generator-jap-jdbc-spring-boot-starter</artifactId>
1313

1414
<properties>
1515
<maven.compiler.source>15</maven.compiler.source>
@@ -20,7 +20,7 @@
2020
<dependencies>
2121
<dependency>
2222
<groupId>io.github.cooperlyt</groupId>
23-
<artifactId>uid-worker-id-db-provider</artifactId>
23+
<artifactId>uid-generator-spring-boot-starter</artifactId>
2424
<version>1.0.5-SNAPSHOT</version>
2525
</dependency>
2626

@@ -40,7 +40,11 @@
4040
<scope>test</scope>
4141
</dependency>
4242

43-
43+
<dependency>
44+
<groupId>io.projectreactor</groupId>
45+
<artifactId>reactor-test</artifactId>
46+
<scope>test</scope>
47+
</dependency>
4448

4549
</dependencies>
4650

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.transaction.annotation.Transactional;
77
import reactor.core.publisher.Mono;
88

9+
import java.util.Date;
910
import java.util.concurrent.CompletableFuture;
1011

1112

@@ -28,6 +29,9 @@ protected Mono<Long> assignWorkerId(WorkerNodeType type, String host, String por
2829
.type(type.value())
2930
.hostName(host)
3031
.port(port)
32+
.launchDate(new Date())
33+
.created(new Date())
34+
.modified(new Date())
3135
.build()
3236
).getId())));
3337
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
import java.util.Map;
1515
//extends HibernateJpaConfiguration
1616
@Configuration
17-
@EnableJpaRepositories
17+
@EnableJpaRepositories("io.github.cooperlyt.cloud.uid.worker.jpa.jdbc.repositories")
1818
@EntityScan("io.github.cooperlyt.cloud.uid.worker.jpa.jdbc.entities")
19-
public class WorkerIdAutoConfigure extends HibernateJpaAutoConfiguration {
19+
public class WorkerIdAutoConfigure {
2020

2121

2222
private final WorkerNodeRepository workerNodeRepository;
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
import lombok.Data;
2121
import lombok.NoArgsConstructor;
2222

23+
import javax.persistence.*;
2324
import java.util.Date;
2425

2526
/**
2627
* Entity for M_WORKER_NODE
2728
*
2829
* @author yutianbao
2930
*/
31+
@Table(name = "WORKER_NODE")
32+
@Entity
3033
@Data
3134
@Builder
3235
@NoArgsConstructor
@@ -36,6 +39,8 @@ public class WorkerNodeEntity {
3639
/**
3740
* Entity unique id (table unique)
3841
*/
42+
@Id
43+
@GeneratedValue(strategy = GenerationType.IDENTITY)
3944
private long id;
4045

4146
/**
@@ -67,5 +72,4 @@ public class WorkerNodeEntity {
6772
* Last modified
6873
*/
6974
private Date modified;
70-
7175
}
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
import org.junit.jupiter.api.Test;
66
import org.junit.jupiter.api.extension.ExtendWith;
77
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
89
import org.springframework.boot.test.context.SpringBootTest;
910
import org.springframework.test.context.junit.jupiter.SpringExtension;
11+
import reactor.test.StepVerifier;
12+
13+
import javax.persistence.EntityManager;
1014

1115
import static org.junit.jupiter.api.Assertions.*;
1216

@@ -17,19 +21,17 @@
1721
class WorkerIdAssignerTest {
1822

1923

24+
@Autowired
25+
private EntityManager entityManager;
2026
@Autowired
2127
private HostWorkerIdAssigner workerIdAssigner;
2228

2329
@Test
2430
public void testWorkerId(){
25-
assertTrue(workerIdAssigner.assignWorkerId() > 1L);
2631

32+
workerIdAssigner.assignWorkerId().as(StepVerifier::create)
33+
.assertNext(id -> {assertTrue(id > 1); System.out.println("id is:" + id);})
34+
.verifyComplete();
2735
}
2836

29-
@Test
30-
public void test(){
31-
long a = 4L;
32-
a = a % 3L;
33-
log.debug("return" + a);
34-
}
3537
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
spring:
3+
datasource:
4+
driver-class-name: org.mariadb.jdbc.Driver
5+
url: jdbc:mariadb://127.0.0.1:3306/corp?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&allowMultiQueries=true
6+
username: root
7+
password: 5257mq
8+
jpa:
9+
show-sql: true
10+
database: mysql
11+
12+
13+
# 以下为可选配置, 如未指定将采用默认值
14+
uid:
15+
timeBits: 30 # 时间位, 默认:30
16+
workerBits: 16 # 机器位, 默认:16
17+
seqBits: 7 # 序列号, 默认:7
18+
epochStr: "2023-02-10" # 初始时间, 默认:"2019-02-20"
19+
maxBackwardSeconds: 1 # 时钟回拨最长容忍时间(秒), 默认:1
20+
enableFutureTime: false # 允许使用未来时间生成ID, 默认: false
21+
CachedUidGenerator: # CachedUidGenerator相关参数
22+
boostPower: 3 # RingBuffer size扩容参数, 可提高UID生成的吞吐量, 默认:3
23+
paddingFactor: 50 # 指定何时向RingBuffer中填充UID, 取值为百分比(0, 100), 默认为50
24+
#scheduleInterval: 60 # 默认:不配置此项, 即不实用Schedule线程. 如需使用, 请指定Schedule线程时间间隔, 单位:秒
25+
26+
27+
logging:
28+
level:
29+
pro.chenggang: DEBUG
30+
org.springframework.r2dbc: DEBUG
31+
io.github.cooperlyt: DEBUG

uid-worker-jpa-r2dbc-spring-boot-starter/pom.xml renamed to uid-generator-jpa-r2dbc-spring-boot-starter/pom.xml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<version>1.0.5-SNAPSHOT</version>
1010
</parent>
1111

12-
<artifactId>uid-worker-jpa-r2dbc-spring-boot-starter</artifactId>
12+
<artifactId>uid-generator-jpa-r2dbc-spring-boot-starter</artifactId>
1313

1414
<properties>
1515
<maven.compiler.source>15</maven.compiler.source>
@@ -20,7 +20,7 @@
2020
<dependencies>
2121
<dependency>
2222
<groupId>io.github.cooperlyt</groupId>
23-
<artifactId>uid-worker-id-db-provider</artifactId>
23+
<artifactId>uid-generator-spring-boot-starter</artifactId>
2424
<version>1.0.5-SNAPSHOT</version>
2525
</dependency>
2626

@@ -41,6 +41,14 @@
4141
<scope>test</scope>
4242
<version>1.1.3</version>
4343
</dependency>
44+
45+
46+
<dependency>
47+
<groupId>io.projectreactor</groupId>
48+
<artifactId>reactor-test</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
4452
</dependencies>
4553

4654
</project>
Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import org.springframework.transaction.annotation.Transactional;
77
import reactor.core.publisher.Mono;
88

9-
import java.util.Date;
9+
import java.time.LocalDateTime;
10+
import java.util.Optional;
1011

1112

1213
public class WorkerIdAssigner extends HostWorkerIdAssigner {
@@ -22,15 +23,21 @@ public WorkerIdAssigner(WorkerNodeRepository workerNodeRepository) {
2223
protected Mono<Long> assignWorkerId(WorkerNodeType type, String host, String port) {
2324
return workerNodeRepository
2425
.getWorkerNodeByHostNameAndPort(host,port)
25-
.switchIfEmpty(workerNodeRepository.save(
26-
WorkerNodeEntity
27-
.builder()
28-
.launchDate(new Date())
29-
.type(type.value())
30-
.hostName(host)
31-
.port(port)
32-
.build()
33-
))
26+
.map(Optional::of)
27+
.defaultIfEmpty(Optional.empty())
28+
.flatMap(entity -> entity.map(Mono::just).orElse(
29+
workerNodeRepository.save(
30+
WorkerNodeEntity
31+
.builder()
32+
.launchDate(LocalDateTime.now())
33+
.created(LocalDateTime.now())
34+
.modified(LocalDateTime.now())
35+
.type(type.value())
36+
.hostName(host)
37+
.port(port)
38+
.build()
39+
))
40+
)
3441
.map(WorkerNodeEntity::getId);
3542
}
3643
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
1212

1313
@ConditionalOnClass(R2dbcEntityOperations.class)
14-
@EnableR2dbcRepositories
14+
@EnableR2dbcRepositories("io.github.cooperlyt.cloud.uid.worker.jpa.r2dbc.repositories")
1515
@EntityScan("io.github.cooperlyt.cloud.uid.worker.jpa.r2dbc.entities")
1616
@Configuration
1717
public class WorkerIdAutoConfigure {
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@
1919
import lombok.Builder;
2020
import lombok.Data;
2121
import lombok.NoArgsConstructor;
22+
import org.springframework.data.annotation.Id;
23+
import org.springframework.data.relational.core.mapping.Table;
2224

25+
import java.time.LocalDateTime;
2326
import java.util.Date;
2427

2528
/**
2629
* Entity for M_WORKER_NODE
2730
*
2831
* @author yutianbao
2932
*/
33+
@Table("WORKER_NODE")
3034
@Data
3135
@Builder
3236
@NoArgsConstructor
@@ -36,6 +40,7 @@ public class WorkerNodeEntity {
3640
/**
3741
* Entity unique id (table unique)
3842
*/
43+
@Id
3944
private long id;
4045

4146
/**
@@ -56,16 +61,16 @@ public class WorkerNodeEntity {
5661
/**
5762
* Worker launch date, default now
5863
*/
59-
private Date launchDate = new Date();
64+
private LocalDateTime launchDate = LocalDateTime.now();
6065

6166
/**
6267
* Created time
6368
*/
64-
private Date created;
69+
private LocalDateTime created = LocalDateTime.now();
6570

6671
/**
6772
* Last modified
6873
*/
69-
private Date modified;
74+
private LocalDateTime modified = LocalDateTime.now();
7075

7176
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.springframework.stereotype.Repository;
66
import reactor.core.publisher.Mono;
77

8+
import java.util.Optional;
9+
810
@Repository
911
public interface WorkerNodeRepository extends ReactiveCrudRepository<WorkerNodeEntity,Long> {
1012

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.springframework.beans.factory.annotation.Autowired;
88
import org.springframework.boot.test.context.SpringBootTest;
99
import org.springframework.test.context.junit.jupiter.SpringExtension;
10+
import reactor.test.StepVerifier;
1011

1112
import static org.junit.jupiter.api.Assertions.*;
1213

@@ -21,7 +22,10 @@ class WorkerIdAssignerTest {
2122

2223
@Test
2324
public void testWorkerId(){
24-
assertTrue(workerIdAssigner.assignWorkerId() > 1L);
25+
workerIdAssigner.assignWorkerId().as(StepVerifier::create)
26+
.assertNext(id -> {assertTrue(id > 0); System.out.println("id is:" + id);})
27+
.verifyComplete();
28+
2529

2630
}
2731

0 commit comments

Comments
 (0)