From c0178bf640eff4981cf197f9932aa4c23e4b3c26 Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Tue, 25 Jul 2023 13:29:01 +0800 Subject: [PATCH] feat: support `CustomizeSegmentIdProperties`/`CustomizeSnowflakeIdProperties`. (#383) --- .../boot/starter/CustomizeIdProperties.java | 19 +++++++++++++++++++ .../CosIdSegmentAutoConfiguration.java | 12 ++++++++---- .../segment/CustomizeSegmentIdProperties.java | 19 +++++++++++++++++++ .../segment/SegmentIdBeanRegistrar.java | 10 +++++++++- .../CosIdSnowflakeAutoConfiguration.java | 9 +++++++-- .../CustomizeSnowflakeIdProperties.java | 19 +++++++++++++++++++ .../snowflake/SnowflakeIdBeanRegistrar.java | 10 +++++++++- .../CosIdSegmentAutoConfigurationTest.java | 5 +++-- .../CosIdSnowflakeAutoConfigurationTest.java | 8 +++----- gradle.properties | 2 +- 10 files changed, 97 insertions(+), 16 deletions(-) create mode 100644 cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/CustomizeIdProperties.java create mode 100644 cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/CustomizeSegmentIdProperties.java create mode 100644 cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/snowflake/CustomizeSnowflakeIdProperties.java diff --git a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/CustomizeIdProperties.java b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/CustomizeIdProperties.java new file mode 100644 index 0000000000..4a4d35ad2d --- /dev/null +++ b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/CustomizeIdProperties.java @@ -0,0 +1,19 @@ +/* + * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)]. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package me.ahoo.cosid.spring.boot.starter; + +@FunctionalInterface +public interface CustomizeIdProperties

{ + void customize(P idProperties); +} diff --git a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdSegmentAutoConfiguration.java b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdSegmentAutoConfiguration.java index ac01bb99bb..70615613ef 100644 --- a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdSegmentAutoConfiguration.java +++ b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdSegmentAutoConfiguration.java @@ -25,6 +25,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; +import org.springframework.lang.Nullable; /** * CosId Segment AutoConfiguration. @@ -61,15 +62,18 @@ public CosIdLifecyclePrefetchWorkerExecutorService lifecycleSegmentChainId(Prefe @Bean public SegmentIdBeanRegistrar segmentIdBeanRegistrar(IdSegmentDistributorFactory distributorFactory, - IdGeneratorProvider idGeneratorProvider, - PrefetchWorkerExecutorService prefetchWorkerExecutorService, - ConfigurableApplicationContext applicationContext) { + IdGeneratorProvider idGeneratorProvider, + PrefetchWorkerExecutorService prefetchWorkerExecutorService, + ConfigurableApplicationContext applicationContext, + @Nullable + CustomizeSegmentIdProperties customizeSegmentIdProperties + ) { return new SegmentIdBeanRegistrar(cosIdProperties, segmentIdProperties, distributorFactory, idGeneratorProvider, prefetchWorkerExecutorService, - applicationContext); + applicationContext, customizeSegmentIdProperties); } } diff --git a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/CustomizeSegmentIdProperties.java b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/CustomizeSegmentIdProperties.java new file mode 100644 index 0000000000..0c16000878 --- /dev/null +++ b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/CustomizeSegmentIdProperties.java @@ -0,0 +1,19 @@ +/* + * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)]. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package me.ahoo.cosid.spring.boot.starter.segment; + +import me.ahoo.cosid.spring.boot.starter.CustomizeIdProperties; + +public interface CustomizeSegmentIdProperties extends CustomizeIdProperties { +} diff --git a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java index b5ba0e402e..dd4532a4fb 100644 --- a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java +++ b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/segment/SegmentIdBeanRegistrar.java @@ -31,6 +31,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.lang.Nullable; @Slf4j public class SegmentIdBeanRegistrar implements InitializingBean { @@ -40,19 +41,23 @@ public class SegmentIdBeanRegistrar implements InitializingBean { private final IdGeneratorProvider idGeneratorProvider; private final PrefetchWorkerExecutorService prefetchWorkerExecutorService; private final ConfigurableApplicationContext applicationContext; + @Nullable + private final CustomizeSegmentIdProperties customizeSegmentIdProperties; public SegmentIdBeanRegistrar(CosIdProperties cosIdProperties, SegmentIdProperties segmentIdProperties, IdSegmentDistributorFactory distributorFactory, IdGeneratorProvider idGeneratorProvider, PrefetchWorkerExecutorService prefetchWorkerExecutorService, - ConfigurableApplicationContext applicationContext) { + ConfigurableApplicationContext applicationContext, + @Nullable CustomizeSegmentIdProperties customizeSegmentIdProperties) { this.cosIdProperties = cosIdProperties; this.segmentIdProperties = segmentIdProperties; this.distributorFactory = distributorFactory; this.idGeneratorProvider = idGeneratorProvider; this.prefetchWorkerExecutorService = prefetchWorkerExecutorService; this.applicationContext = applicationContext; + this.customizeSegmentIdProperties = customizeSegmentIdProperties; } @Override @@ -61,6 +66,9 @@ public void afterPropertiesSet() { } public void register() { + if (customizeSegmentIdProperties != null) { + customizeSegmentIdProperties.customize(segmentIdProperties); + } SegmentIdProperties.ShardIdDefinition shareIdDefinition = segmentIdProperties.getShare(); if (shareIdDefinition.isEnabled()) { registerIdDefinition(IdGeneratorProvider.SHARE, shareIdDefinition); diff --git a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/snowflake/CosIdSnowflakeAutoConfiguration.java b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/snowflake/CosIdSnowflakeAutoConfiguration.java index 4d0712c2b7..e61fc73d1a 100644 --- a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/snowflake/CosIdSnowflakeAutoConfiguration.java +++ b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/snowflake/CosIdSnowflakeAutoConfiguration.java @@ -25,6 +25,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; +import org.springframework.lang.Nullable; /** * CosId Snowflake AutoConfiguration. @@ -53,7 +54,10 @@ public SnowflakeIdBeanRegistrar snowflakeIdBeanRegistrar(final InstanceId instan IdGeneratorProvider idGeneratorProvider, MachineIdDistributor machineIdDistributor, ClockBackwardsSynchronizer clockBackwardsSynchronizer, - ConfigurableApplicationContext applicationContext) { + ConfigurableApplicationContext applicationContext, + @Nullable + CustomizeSnowflakeIdProperties customizeSnowflakeIdProperties + ) { return new SnowflakeIdBeanRegistrar(cosIdProperties, machineProperties, snowflakeIdProperties, @@ -61,7 +65,8 @@ public SnowflakeIdBeanRegistrar snowflakeIdBeanRegistrar(final InstanceId instan idGeneratorProvider, machineIdDistributor, clockBackwardsSynchronizer, - applicationContext); + applicationContext, + customizeSnowflakeIdProperties); } } diff --git a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/snowflake/CustomizeSnowflakeIdProperties.java b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/snowflake/CustomizeSnowflakeIdProperties.java new file mode 100644 index 0000000000..0f7f399add --- /dev/null +++ b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/snowflake/CustomizeSnowflakeIdProperties.java @@ -0,0 +1,19 @@ +/* + * Copyright [2021-present] [ahoo wang (https://github.com/Ahoo-Wang)]. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package me.ahoo.cosid.spring.boot.starter.snowflake; + +import me.ahoo.cosid.spring.boot.starter.CustomizeIdProperties; + +public interface CustomizeSnowflakeIdProperties extends CustomizeIdProperties { +} diff --git a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/snowflake/SnowflakeIdBeanRegistrar.java b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/snowflake/SnowflakeIdBeanRegistrar.java index 7dafeb7ede..1abf911036 100644 --- a/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/snowflake/SnowflakeIdBeanRegistrar.java +++ b/cosid-spring-boot-starter/src/main/java/me/ahoo/cosid/spring/boot/starter/snowflake/SnowflakeIdBeanRegistrar.java @@ -29,6 +29,7 @@ import com.google.common.base.MoreObjects; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.lang.Nullable; import java.time.ZoneId; @@ -41,6 +42,8 @@ public class SnowflakeIdBeanRegistrar implements InitializingBean { private final MachineIdDistributor machineIdDistributor; private final ClockBackwardsSynchronizer clockBackwardsSynchronizer; private final ConfigurableApplicationContext applicationContext; + @Nullable + private final CustomizeSnowflakeIdProperties customizeSnowflakeIdProperties; public SnowflakeIdBeanRegistrar(CosIdProperties cosIdProperties, MachineProperties machineProperties, @@ -49,7 +52,8 @@ public SnowflakeIdBeanRegistrar(CosIdProperties cosIdProperties, IdGeneratorProvider idGeneratorProvider, MachineIdDistributor machineIdDistributor, ClockBackwardsSynchronizer clockBackwardsSynchronizer, - ConfigurableApplicationContext applicationContext) { + ConfigurableApplicationContext applicationContext, + @Nullable CustomizeSnowflakeIdProperties customizeSnowflakeIdProperties) { this.cosIdProperties = cosIdProperties; this.machineProperties = machineProperties; this.snowflakeIdProperties = snowflakeIdProperties; @@ -58,6 +62,7 @@ public SnowflakeIdBeanRegistrar(CosIdProperties cosIdProperties, this.machineIdDistributor = machineIdDistributor; this.clockBackwardsSynchronizer = clockBackwardsSynchronizer; this.applicationContext = applicationContext; + this.customizeSnowflakeIdProperties = customizeSnowflakeIdProperties; } @Override @@ -66,6 +71,9 @@ public void afterPropertiesSet() { } public void register() { + if (customizeSnowflakeIdProperties != null) { + customizeSnowflakeIdProperties.customize(snowflakeIdProperties); + } SnowflakeIdProperties.ShardIdDefinition shareIdDefinition = snowflakeIdProperties.getShare(); if (shareIdDefinition.isEnabled()) { registerIdDefinition(IdGeneratorProvider.SHARE, shareIdDefinition); diff --git a/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdSegmentAutoConfigurationTest.java b/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdSegmentAutoConfigurationTest.java index eaafdfa8a8..4ce514a924 100644 --- a/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdSegmentAutoConfigurationTest.java +++ b/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/segment/CosIdSegmentAutoConfigurationTest.java @@ -2,7 +2,6 @@ import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; -import me.ahoo.cosid.segment.SegmentId; import me.ahoo.cosid.segment.concurrent.PrefetchWorkerExecutorService; import me.ahoo.cosid.spring.boot.starter.CosIdAutoConfiguration; @@ -25,13 +24,15 @@ void contextLoads() { .withPropertyValues("spring.datasource.url=jdbc:mysql://localhost:3306/cosid_db") .withPropertyValues("spring.datasource.username=root") .withPropertyValues("spring.datasource.password=root") + .withBean(CustomizeSegmentIdProperties.class, () -> idProperties -> idProperties.getProvider().put("test", new SegmentIdProperties.IdDefinition())) .withUserConfiguration(CosIdAutoConfiguration.class, DataSourceAutoConfiguration.class, CosIdJdbcSegmentAutoConfiguration.class, CosIdSegmentAutoConfiguration.class) .run(context -> { assertThat(context) .hasSingleBean(CosIdSegmentAutoConfiguration.class) .hasSingleBean(PrefetchWorkerExecutorService.class) .hasSingleBean(CosIdLifecyclePrefetchWorkerExecutorService.class) - .hasSingleBean(SegmentId.class) + .hasBean("__share__SegmentId") + .hasBean("testSegmentId") ; }); } diff --git a/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/snowflake/CosIdSnowflakeAutoConfigurationTest.java b/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/snowflake/CosIdSnowflakeAutoConfigurationTest.java index 5326f281ca..a3e132fb72 100644 --- a/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/snowflake/CosIdSnowflakeAutoConfigurationTest.java +++ b/cosid-spring-boot-starter/src/test/java/me/ahoo/cosid/spring/boot/starter/snowflake/CosIdSnowflakeAutoConfigurationTest.java @@ -16,12 +16,9 @@ import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; import me.ahoo.cosid.machine.ClockBackwardsSynchronizer; -import me.ahoo.cosid.snowflake.SnowflakeId; import me.ahoo.cosid.machine.InstanceId; import me.ahoo.cosid.machine.MachineId; import me.ahoo.cosid.machine.MachineStateStorage; -import me.ahoo.cosid.machine.ManualMachineIdDistributor; -import me.ahoo.cosid.machine.k8s.StatefulSetMachineIdDistributor; import me.ahoo.cosid.spring.boot.starter.CosIdAutoConfiguration; import me.ahoo.cosid.spring.boot.starter.machine.ConditionalOnCosIdMachineEnabled; import me.ahoo.cosid.spring.boot.starter.machine.CosIdLifecycleMachineIdDistributor; @@ -29,7 +26,6 @@ import me.ahoo.cosid.spring.boot.starter.machine.MachineProperties; import org.junit.jupiter.api.Test; -import org.junitpioneer.jupiter.SetEnvironmentVariable; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.cloud.commons.util.UtilAutoConfiguration; @@ -47,6 +43,7 @@ void contextLoads() { .withPropertyValues(ConditionalOnCosIdMachineEnabled.ENABLED_KEY + "=true") .withPropertyValues(ConditionalOnCosIdSnowflakeEnabled.ENABLED_KEY + "=true") .withPropertyValues(MachineProperties.PREFIX + ".distributor.manual.machineId=1") + .withBean(CustomizeSnowflakeIdProperties.class, () -> idProperties -> idProperties.getProvider().put("test", new SnowflakeIdProperties.IdDefinition())) .withUserConfiguration(UtilAutoConfiguration.class, CosIdAutoConfiguration.class, CosIdMachineAutoConfiguration.class, @@ -60,7 +57,8 @@ void contextLoads() { .hasSingleBean(ClockBackwardsSynchronizer.class) .hasSingleBean(MachineId.class) .hasSingleBean(CosIdLifecycleMachineIdDistributor.class) - .hasSingleBean(SnowflakeId.class) + .hasBean("__share__SnowflakeId") + .hasBean("testSnowflakeId") ; }); } diff --git a/gradle.properties b/gradle.properties index 6fe1e6b117..61293350ac 100644 --- a/gradle.properties +++ b/gradle.properties @@ -11,7 +11,7 @@ # limitations under the License. # group=me.ahoo.cosid -version=2.2.4 +version=2.2.5 description=Universal, flexible, high-performance distributed ID generator. website=https://github.com/Ahoo-Wang/CosId issues=https://github.com/Ahoo-Wang/CosId/issues