diff --git a/sofa-boot-project/sofa-boot-actuator-autoconfigure/src/main/java/com/alipay/sofa/boot/actuator/autoconfigure/rpc/RpcActuatorAutoConfiguration.java b/sofa-boot-project/sofa-boot-actuator-autoconfigure/src/main/java/com/alipay/sofa/boot/actuator/autoconfigure/rpc/RpcActuatorAutoConfiguration.java index f26909185..792c160d5 100644 --- a/sofa-boot-project/sofa-boot-actuator-autoconfigure/src/main/java/com/alipay/sofa/boot/actuator/autoconfigure/rpc/RpcActuatorAutoConfiguration.java +++ b/sofa-boot-project/sofa-boot-actuator-autoconfigure/src/main/java/com/alipay/sofa/boot/actuator/autoconfigure/rpc/RpcActuatorAutoConfiguration.java @@ -16,7 +16,10 @@ */ package com.alipay.sofa.boot.actuator.autoconfigure.rpc; +import com.alipay.sofa.boot.actuator.autoconfigure.health.ReadinessAutoConfiguration; +import com.alipay.sofa.boot.actuator.health.ReadinessCheckListener; import com.alipay.sofa.boot.actuator.health.ReadinessEndpoint; +import com.alipay.sofa.boot.actuator.rpc.HealthCheckProviderConfigDelayRegisterChecker; import com.alipay.sofa.boot.actuator.rpc.RpcAfterHealthCheckCallback; import com.alipay.sofa.boot.actuator.rpc.SofaRpcEndpoint; import com.alipay.sofa.boot.autoconfigure.rpc.SofaRpcAutoConfiguration; @@ -35,7 +38,7 @@ * @author huzijie * @version RpcActuatorAutoConfiguration.java, v 0.1 2023年02月08日 4:45 PM huzijie Exp $ */ -@AutoConfiguration(after = SofaRpcAutoConfiguration.class) +@AutoConfiguration(after = { SofaRpcAutoConfiguration.class, ReadinessAutoConfiguration.class }) @ConditionalOnClass(RpcStartApplicationListener.class) @ConditionalOnBean(RpcStartApplicationListener.class) public class RpcActuatorAutoConfiguration { @@ -53,4 +56,12 @@ public RpcAfterHealthCheckCallback rpcAfterHealthCheckCallback(RpcStartApplicati public SofaRpcEndpoint sofaRpcEndPoint() { return new SofaRpcEndpoint(); } + + @Bean + @ConditionalOnMissingBean + @ConditionalOnAvailableEndpoint(endpoint = ReadinessEndpoint.class) + @ConditionalOnBean(ReadinessCheckListener.class) + public HealthCheckProviderConfigDelayRegisterChecker healthCheckProviderConfigDelayRegisterChecker(ReadinessCheckListener readinessCheckListener) { + return new HealthCheckProviderConfigDelayRegisterChecker(readinessCheckListener); + } } diff --git a/sofa-boot-project/sofa-boot-actuator-autoconfigure/src/test/java/com/alipay/sofa/boot/actuator/autoconfigure/rpc/RpcActuatorAutoConfigurationTests.java b/sofa-boot-project/sofa-boot-actuator-autoconfigure/src/test/java/com/alipay/sofa/boot/actuator/autoconfigure/rpc/RpcActuatorAutoConfigurationTests.java index bd0e3073a..a72f858c0 100644 --- a/sofa-boot-project/sofa-boot-actuator-autoconfigure/src/test/java/com/alipay/sofa/boot/actuator/autoconfigure/rpc/RpcActuatorAutoConfigurationTests.java +++ b/sofa-boot-project/sofa-boot-actuator-autoconfigure/src/test/java/com/alipay/sofa/boot/actuator/autoconfigure/rpc/RpcActuatorAutoConfigurationTests.java @@ -16,6 +16,8 @@ */ package com.alipay.sofa.boot.actuator.autoconfigure.rpc; +import com.alipay.sofa.boot.actuator.autoconfigure.health.ReadinessAutoConfiguration; +import com.alipay.sofa.boot.actuator.rpc.HealthCheckProviderConfigDelayRegisterChecker; import com.alipay.sofa.boot.actuator.rpc.RpcAfterHealthCheckCallback; import com.alipay.sofa.boot.actuator.rpc.SofaRpcEndpoint; import com.alipay.sofa.rpc.boot.context.RpcStartApplicationListener; @@ -41,6 +43,15 @@ public class RpcActuatorAutoConfigurationTests { .withPropertyValues( "management.endpoints.web.exposure.include=readiness,rpc"); + @Test + void runShouldHaveHealthCheckProviderConfigDelayRegisterChecker() { + this.contextRunner + .withUserConfiguration(ReadinessAutoConfiguration.class) + .withBean(RpcStartApplicationListener.class) + .run((context) -> assertThat(context) + .hasSingleBean(HealthCheckProviderConfigDelayRegisterChecker.class)); + } + @Test void runShouldHaveRpcActuatorBeans() { this.contextRunner diff --git a/sofa-boot-project/sofa-boot-actuator/src/main/java/com/alipay/sofa/boot/actuator/rpc/HealthCheckProviderConfigDelayRegisterChecker.java b/sofa-boot-project/sofa-boot-actuator/src/main/java/com/alipay/sofa/boot/actuator/rpc/HealthCheckProviderConfigDelayRegisterChecker.java new file mode 100644 index 000000000..7526ca42d --- /dev/null +++ b/sofa-boot-project/sofa-boot-actuator/src/main/java/com/alipay/sofa/boot/actuator/rpc/HealthCheckProviderConfigDelayRegisterChecker.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alipay.sofa.boot.actuator.rpc; + +import com.alipay.sofa.boot.actuator.health.ReadinessCheckListener; +import com.alipay.sofa.rpc.boot.container.ProviderConfigDelayRegisterChecker; +import org.springframework.boot.availability.ReadinessState; + +/** + * ProviderConfigDelayRegisterChecker的具体实现 + */ +public class HealthCheckProviderConfigDelayRegisterChecker implements + ProviderConfigDelayRegisterChecker { + + private final ReadinessCheckListener readinessCheckListener; + + public HealthCheckProviderConfigDelayRegisterChecker(ReadinessCheckListener readinessCheckListener) { + this.readinessCheckListener = readinessCheckListener; + } + + @Override + public boolean allowRegister() { + return ReadinessState.REFUSING_TRAFFIC != readinessCheckListener.getReadinessState(); + } +} diff --git a/sofa-boot-project/sofa-boot-actuator/src/test/java/com/alipay/sofa/boot/actuator/rpc/HealthCheckProviderConfigDelayRegisterCheckerTest.java b/sofa-boot-project/sofa-boot-actuator/src/test/java/com/alipay/sofa/boot/actuator/rpc/HealthCheckProviderConfigDelayRegisterCheckerTest.java new file mode 100644 index 000000000..7aa708bd1 --- /dev/null +++ b/sofa-boot-project/sofa-boot-actuator/src/test/java/com/alipay/sofa/boot/actuator/rpc/HealthCheckProviderConfigDelayRegisterCheckerTest.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alipay.sofa.boot.actuator.rpc; + +import com.alipay.sofa.boot.actuator.health.ReadinessCheckListener; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.boot.availability.ReadinessState; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Test for {@link HealthCheckProviderConfigDelayRegisterChecker} + */ +@ExtendWith(MockitoExtension.class) +public class HealthCheckProviderConfigDelayRegisterCheckerTest { + + @InjectMocks + private HealthCheckProviderConfigDelayRegisterChecker healthCheckProviderConfigDelayRegisterChecker; + + @Mock + private ReadinessCheckListener readinessCheckListener; + + @Test + public void testAllowRegister() { + // Setup + Mockito.doReturn(ReadinessState.ACCEPTING_TRAFFIC).when(readinessCheckListener) + .getReadinessState(); + // Run the test + boolean result = healthCheckProviderConfigDelayRegisterChecker.allowRegister(); + + // Verify the results + assertThat(result).isTrue(); + } + +} diff --git a/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/rpc/SofaBootRpcProperties.java b/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/rpc/SofaBootRpcProperties.java index deecd1e37..cbeaa6890 100644 --- a/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/rpc/SofaBootRpcProperties.java +++ b/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/rpc/SofaBootRpcProperties.java @@ -342,6 +342,11 @@ public class SofaBootRpcProperties implements EnvironmentAware { private List providerRegisterBlackList; + /** + * 是否开启延时注册功能 + */ + private boolean enableDelayRegister; + public boolean isEnableAutoPublish() { return enableAutoPublish; } @@ -941,6 +946,14 @@ public void setProviderRegisterBlackList(List providerRegisterBlackList) this.providerRegisterBlackList = providerRegisterBlackList; } + public boolean isEnableDelayRegister() { + return enableDelayRegister; + } + + public void setEnableDelayRegister(boolean enableDelayRegister) { + this.enableDelayRegister = enableDelayRegister; + } + @Override public void setEnvironment(Environment environment) { this.environment = environment; diff --git a/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/rpc/SofaRpcAutoConfiguration.java b/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/rpc/SofaRpcAutoConfiguration.java index 3e3955f2f..ddbe0fcd9 100644 --- a/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/rpc/SofaRpcAutoConfiguration.java +++ b/sofa-boot-project/sofa-boot-autoconfigure/src/main/java/com/alipay/sofa/boot/autoconfigure/rpc/SofaRpcAutoConfiguration.java @@ -24,6 +24,7 @@ import com.alipay.sofa.rpc.boot.config.RegistryConfigureProcessor; import com.alipay.sofa.rpc.boot.container.ConsumerConfigContainer; import com.alipay.sofa.rpc.boot.container.ProviderConfigContainer; +import com.alipay.sofa.rpc.boot.container.ProviderConfigDelayRegisterChecker; import com.alipay.sofa.rpc.boot.container.RegistryConfigContainer; import com.alipay.sofa.rpc.boot.container.ServerConfigContainer; import com.alipay.sofa.rpc.boot.context.RpcStartApplicationListener; @@ -55,6 +56,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; /** * {@link EnableAutoConfiguration Auto-configuration} for sofa Rpc. @@ -72,12 +74,17 @@ public class SofaRpcAutoConfiguration { @Bean @ConditionalOnMissingBean - public ProviderConfigContainer providerConfigContainer(SofaBootRpcProperties sofaBootRpcProperties) { + public ProviderConfigContainer providerConfigContainer(SofaBootRpcProperties sofaBootRpcProperties, + ObjectProvider providerConfigDelayRegisterCheckers) { ProviderConfigContainer providerConfigContainer = new ProviderConfigContainer(); providerConfigContainer.setProviderRegisterWhiteList(sofaBootRpcProperties .getProviderRegisterWhiteList()); providerConfigContainer.setProviderRegisterBlackList(sofaBootRpcProperties .getProviderRegisterBlackList()); + providerConfigContainer.setProviderConfigDelayRegister(providerConfigDelayRegisterCheckers + .stream().collect(Collectors.toList())); + providerConfigContainer.setEnableDelayRegister(sofaBootRpcProperties + .isEnableDelayRegister()); return providerConfigContainer; } diff --git a/sofa-boot-project/sofa-boot-autoconfigure/src/test/java/com/alipay/sofa/boot/autoconfigure/rpc/SofaRpcAutoConfigurationTests.java b/sofa-boot-project/sofa-boot-autoconfigure/src/test/java/com/alipay/sofa/boot/autoconfigure/rpc/SofaRpcAutoConfigurationTests.java index 97cbeb7c7..8e8ac4cf1 100644 --- a/sofa-boot-project/sofa-boot-autoconfigure/src/test/java/com/alipay/sofa/boot/autoconfigure/rpc/SofaRpcAutoConfigurationTests.java +++ b/sofa-boot-project/sofa-boot-autoconfigure/src/test/java/com/alipay/sofa/boot/autoconfigure/rpc/SofaRpcAutoConfigurationTests.java @@ -21,6 +21,7 @@ import com.alipay.sofa.rpc.boot.config.FaultToleranceConfigurator; import com.alipay.sofa.rpc.boot.config.RegistryConfigureProcessor; import com.alipay.sofa.rpc.boot.container.ProviderConfigContainer; +import com.alipay.sofa.rpc.boot.container.ProviderConfigDelayRegisterChecker; import com.alipay.sofa.rpc.boot.container.RegistryConfigContainer; import com.alipay.sofa.rpc.boot.container.ServerConfigContainer; import com.alipay.sofa.rpc.boot.runtime.adapter.processor.ConsumerMockProcessor; @@ -383,6 +384,46 @@ void customProviderConfigContainer() { }); } + @Test + void customProviderConfigContainerForEnableDelayRegister() { + this.contextRunner.withPropertyValues( + "sofa.boot.rpc.enableDelayRegister=true"). + run(context -> { + ProviderConfigContainer providerConfigContainer = context.getBean(ProviderConfigContainer.class); + boolean enableDelayRegister = providerConfigContainer.isEnableDelayRegister(); + assertThat(enableDelayRegister).isTrue(); + }); + } + + @Test + void customProviderConfigContainerForProviderConfigDelayRegisterChecker() { + this.contextRunner.withUserConfiguration(ProviderConfigDelayRegisterCheckerConfiguration.class) + .run(context -> { + ProviderConfigContainer providerConfigContainer = context.getBean(ProviderConfigContainer.class); + List checkers = + providerConfigContainer.getProviderConfigDelayRegisterCheckerList(); + assertThat(checkers.size()).isEqualTo(1); + assertThat(checkers.get(0).allowRegister()).isTrue(); + }); + } + + @Configuration(proxyBeanMethods = false) + static class ProviderConfigDelayRegisterCheckerConfiguration { + + @Bean + public ProviderConfigDelayRegisterChecker firstChecker() { + return new FirstChecker(); + } + } + + static class FirstChecker implements ProviderConfigDelayRegisterChecker { + + @Override + public boolean allowRegister() { + return true; + } + } + @Configuration(proxyBeanMethods = false) static class CustomProviderConfigContainerConfiguration { @Bean diff --git a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/container/ProviderConfigContainer.java b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/container/ProviderConfigContainer.java index 582ba3fcf..6ace6f752 100644 --- a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/container/ProviderConfigContainer.java +++ b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/container/ProviderConfigContainer.java @@ -16,6 +16,8 @@ */ package com.alipay.sofa.rpc.boot.container; +import com.alipay.sofa.boot.constant.SofaBootConstants; +import com.alipay.sofa.common.thread.SofaScheduledThreadPoolExecutor; import com.alipay.sofa.rpc.boot.config.SofaBootRpcConfigConstants; import com.alipay.sofa.rpc.boot.log.SofaBootRpcLoggerFactory; import com.alipay.sofa.rpc.boot.runtime.binding.RpcBinding; @@ -33,6 +35,7 @@ import java.util.List; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.TimeUnit; /** * ProviderConfig持有者.维护编程界面级别的RPC组件。 @@ -58,6 +61,21 @@ public class ProviderConfigContainer { private final ConcurrentMap RPC_SERVICE_CONTAINER = new ConcurrentHashMap( 256); + /** + * 用来延时发布的线程池 + */ + private SofaScheduledThreadPoolExecutor scheduledExecutorService; + + /** + * 用于判断是否允许延迟服务发布至注册中心的扩展点、需要所有拓展点都通过 + */ + private List providerConfigDelayRegisterCheckerList; + + /** + * 是否开启延时加载,兼容老逻辑 + */ + private boolean enableDelayRegister; + /** * 增加 ProviderConfig * @@ -127,32 +145,60 @@ public Collection getAllProviderConfig() { */ public void publishAllProviderConfig() { for (ProviderConfig providerConfig : getAllProviderConfig()) { + int delay = providerConfig.getDelay(); + if (enableDelayRegister && delay > 0) { + // 根据延时时间异步去注册中心注册服务 + if (scheduledExecutorService == null) { + scheduledExecutorService = new SofaScheduledThreadPoolExecutor(1, "rpc-provider-delay-register", + SofaBootConstants.SOFA_BOOT_SPACE_NAME); + } + scheduledExecutorService.schedule(() -> doDelayPublishProviderConfig(providerConfig, + providerConfigDelayRegisterCheckerList), delay, TimeUnit.MILLISECONDS); + } else { + // 没有配置延时加载则直接去注册中心注册服务 + doPublishProviderConfig(providerConfig); + } + } + } - ServerConfig serverConfig = (ServerConfig) providerConfig.getServer().get(0); - if (!serverConfig.getProtocol().equalsIgnoreCase( - SofaBootRpcConfigConstants.RPC_PROTOCOL_DUBBO)) { - if (allowProviderRegister(providerConfig)) { - providerConfig.setRegister(true); - } else { - LOGGER.info("Provider will not register: [{}]", providerConfig.buildKey()); + private void doDelayPublishProviderConfig(ProviderConfig providerConfig, + List providerConfigDelayRegisterCheckerList) { + for (ProviderConfigDelayRegisterChecker providerConfigDelayRegisterChecker : providerConfigDelayRegisterCheckerList) { + if (!providerConfigDelayRegisterChecker.allowRegister()) { + if (LOGGER.isWarnEnabled()) { + LOGGER.warn("service publish failed, interfaceId [{}], please check.", + providerConfig.getInterfaceId()); } + return; + } + } + doPublishProviderConfig(providerConfig); + } - List registrys = providerConfig.getRegistry(); - for (RegistryConfig registryConfig : registrys) { + private void doPublishProviderConfig(ProviderConfig providerConfig) { + ServerConfig serverConfig = (ServerConfig) providerConfig.getServer().get(0); + if (!serverConfig.getProtocol().equalsIgnoreCase( + SofaBootRpcConfigConstants.RPC_PROTOCOL_DUBBO)) { + if (allowProviderRegister(providerConfig)) { + providerConfig.setRegister(true); + } else { + LOGGER.info("Provider will not register: [{}]", providerConfig.buildKey()); + } - Registry registry = RegistryFactory.getRegistry(registryConfig); - registry.init(); - registry.start(); + List registrys = providerConfig.getRegistry(); + for (RegistryConfig registryConfig : registrys) { - registry.register(providerConfig); + Registry registry = RegistryFactory.getRegistry(registryConfig); + registry.init(); + registry.start(); - if (LOGGER.isInfoEnabled()) { - LOGGER.info("service published. interfaceId[" - + providerConfig.getInterfaceId() + "]; protocol[" - + serverConfig.getProtocol() + "]"); - } - } + registry.register(providerConfig); + if (LOGGER.isInfoEnabled()) { + LOGGER.info("service published. interfaceId[" + + providerConfig.getInterfaceId() + "]; protocol[" + + serverConfig.getProtocol() + "]"); + } } } } @@ -257,4 +303,20 @@ public List getProviderRegisterWhiteList() { public List getProviderRegisterBlackList() { return providerRegisterBlackList; } + + public void setProviderConfigDelayRegister(List providerConfigDelayRegisterCheckerList) { + this.providerConfigDelayRegisterCheckerList = providerConfigDelayRegisterCheckerList; + } + + public List getProviderConfigDelayRegisterCheckerList() { + return providerConfigDelayRegisterCheckerList; + } + + public void setEnableDelayRegister(boolean enableDelayRegister) { + this.enableDelayRegister = enableDelayRegister; + } + + public boolean isEnableDelayRegister() { + return enableDelayRegister; + } } diff --git a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/container/ProviderConfigDelayRegisterChecker.java b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/container/ProviderConfigDelayRegisterChecker.java new file mode 100644 index 000000000..a4746235b --- /dev/null +++ b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/container/ProviderConfigDelayRegisterChecker.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alipay.sofa.rpc.boot.container; + +/** + * 用于判断是否允许延迟服务发布至注册中心的扩展点 + */ +public interface ProviderConfigDelayRegisterChecker { + + /** + * 是否允许注册服务到注册中心 + * + * @return + */ + boolean allowRegister(); +} diff --git a/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/main/java/com/alipay/sofa/smoke/tests/rpc/boot/bean/delayregister/DelayRegisterService.java b/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/main/java/com/alipay/sofa/smoke/tests/rpc/boot/bean/delayregister/DelayRegisterService.java new file mode 100644 index 000000000..8f2f3f713 --- /dev/null +++ b/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/main/java/com/alipay/sofa/smoke/tests/rpc/boot/bean/delayregister/DelayRegisterService.java @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alipay.sofa.smoke.tests.rpc.boot.bean.delayregister; + +/** + * @author chengming + * @version DelayRegisterService.java, v 0.1 2024年02月26日 3:37 PM chengming + */ +public interface DelayRegisterService { + + String sayHello(String string); +} diff --git a/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/main/java/com/alipay/sofa/smoke/tests/rpc/boot/bean/delayregister/DelayRegisterServiceImpl.java b/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/main/java/com/alipay/sofa/smoke/tests/rpc/boot/bean/delayregister/DelayRegisterServiceImpl.java new file mode 100644 index 000000000..39bdd1bfd --- /dev/null +++ b/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/main/java/com/alipay/sofa/smoke/tests/rpc/boot/bean/delayregister/DelayRegisterServiceImpl.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alipay.sofa.smoke.tests.rpc.boot.bean.delayregister; + +/** + * @author chengming + * @version DelayRegisterServiceImpl.java, v 0.1 2024年02月26日 3:39 PM chengming + */ +public class DelayRegisterServiceImpl implements DelayRegisterService { + + @Override + public String sayHello(String string) { + return string; + } +} diff --git a/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/main/resources/spring/test_only_delay_register.xml b/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/main/resources/spring/test_only_delay_register.xml new file mode 100644 index 000000000..8f16189f6 --- /dev/null +++ b/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/main/resources/spring/test_only_delay_register.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterFailTests.java b/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterFailTests.java new file mode 100644 index 000000000..dfb2aac0e --- /dev/null +++ b/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterFailTests.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alipay.sofa.smoke.tests.rpc.delayregister; + +import org.junit.jupiter.api.Test; +import org.springframework.test.context.TestPropertySource; + +/** + * Tests for rpc provider delay register when health check failed. + * + * @author huzijie + * @version DelayRegisterFailTests.java, v 0.1 2024年02月26日 20:15 huzijie Exp $ + */ +@TestPropertySource(properties = { "delayregister.healthcheck.result=false", "test-uniqueId=fail" }) +public class DelayRegisterFailTests extends DelayRegisterTestsBase { + + @Test + public void testDelayRegister() throws InterruptedException { + registerFail(); + + Thread.sleep(3000); + + registerFail(); + } +} diff --git a/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterSuccessTests.java b/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterSuccessTests.java new file mode 100644 index 000000000..f46b192b9 --- /dev/null +++ b/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterSuccessTests.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alipay.sofa.smoke.tests.rpc.delayregister; + +import org.junit.jupiter.api.Test; +import org.springframework.test.context.TestPropertySource; + +/** + * Tests for rpc provider delay register when health check success. + * + * @author huzijie + * @version DelayRegisterSuccessTests.java, v 0.1 2024年02月26日 20:15 huzijie Exp $ + */ +@TestPropertySource(properties = { "delayregister.healthcheck.result=true", "test-uniqueId=success" }) +public class DelayRegisterSuccessTests extends DelayRegisterTestsBase { + + @Test + public void testDelayRegister() throws InterruptedException { + registerFail(); + + Thread.sleep(3000); + + registerSuccess(); + } +} diff --git a/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterTestsBase.java b/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterTestsBase.java new file mode 100644 index 000000000..63201c81b --- /dev/null +++ b/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterTestsBase.java @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 com.alipay.sofa.smoke.tests.rpc.delayregister; + +import com.alipay.sofa.boot.actuator.health.ReadinessCheckCallback; +import com.alipay.sofa.rpc.core.exception.SofaRouteException; +import com.alipay.sofa.smoke.tests.rpc.boot.RpcSofaBootApplication; +import com.alipay.sofa.smoke.tests.rpc.boot.bean.delayregister.DelayRegisterService; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.actuate.health.Health; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.ImportResource; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** + * Base tests for rpc provider delay register. + * + * @author chengming + * @version DelayRegisterTests.java, v 0.1 2024年02月26日 3:58 PM chengming + */ +@SpringBootTest(classes = RpcSofaBootApplication.class, properties = { + "sofa.boot.rpc.registry.address=zookeeper://127.0.0.1:2181", + "sofa.boot.rpc.enable-auto-publish=true", + "sofa.boot.rpc.enable-delay-register=true" }) +@Import(DelayRegisterTestsBase.DelayRegisterConfiguration.class) +public class DelayRegisterTestsBase { + + @BeforeAll + public static void setUp() { + System.setProperty("provider.delay", "2000"); + } + + @AfterAll + public static void clear() { + System.clearProperty("provider.delay"); + } + + @Autowired + private DelayRegisterService delayRegisterService; + + protected void registerSuccess() { + String hi = delayRegisterService.sayHello("hi"); + assertThat(hi).isEqualTo("hi"); + } + + protected void registerFail() { + assertThatThrownBy(() -> delayRegisterService.sayHello("hi")).isInstanceOf(SofaRouteException.class). + hasMessageContaining("Cannot get the service address of service"); + } + + @Configuration + @ImportResource("/spring/test_only_delay_register.xml") + static class DelayRegisterConfiguration { + + } + + public static class CustomReadinessCallBack implements ReadinessCheckCallback { + + @Value("${delayregister.healthcheck.result}") + private boolean result; + + @Override + public Health onHealthy(ApplicationContext applicationContext) { + return result ? Health.up().build() : Health.down().build(); + } + } +}