Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
呈铭 committed Feb 26, 2024
1 parent 98044de commit 1c62a9b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public SofaRpcEndpoint sofaRpcEndPoint() {
@Bean
@ConditionalOnMissingBean
@ConditionalOnAvailableEndpoint(endpoint = ReadinessEndpoint.class)
@ConditionalOnBean(ReadinessCheckListener.class)
public HealthCheckProviderConfigDelayRegisterChecker healthCheckProviderConfigDelayRegisterChecker(ReadinessCheckListener readinessCheckListener) {
return new HealthCheckProviderConfigDelayRegisterChecker(readinessCheckListener);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.boot.actuator.autoconfigure.rpc;

import com.alipay.sofa.boot.actuator.autoconfigure.health.ReadinessAutoConfiguration;
import com.alipay.sofa.boot.actuator.health.HealthCheckerProcessor;
import com.alipay.sofa.boot.actuator.health.HealthIndicatorProcessor;
import com.alipay.sofa.boot.actuator.health.ReadinessCheckCallbackProcessor;
Expand Down Expand Up @@ -48,15 +49,22 @@ 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
.withBean(RpcStartApplicationListener.class)
.withBean(MockReadinessCheckListenerConfiguration.class)
.run((context) -> assertThat(context)
.hasSingleBean(RpcAfterHealthCheckCallback.class)
.hasSingleBean(SofaRpcEndpoint.class)
.hasSingleBean(HealthCheckProviderConfigDelayRegisterChecker.class));
.hasSingleBean(SofaRpcEndpoint.class));
}

@Test
Expand All @@ -66,8 +74,7 @@ void runWhenNotExposedShouldNotHaveReadinessBeans() {
.withPropertyValues("management.endpoints.web.exposure.include=info")
.run((context) -> assertThat(context)
.doesNotHaveBean(RpcAfterHealthCheckCallback.class)
.doesNotHaveBean(SofaRpcEndpoint.class)
.doesNotHaveBean(HealthCheckProviderConfigDelayRegisterChecker.class));
.doesNotHaveBean(SofaRpcEndpoint.class));
}

@Test
Expand All @@ -77,17 +84,15 @@ void runWhenRpcClassNotExist() {
.withClassLoader(new FilteredClassLoader(RpcStartApplicationListener.class))
.run((context) -> assertThat(context)
.doesNotHaveBean(RpcAfterHealthCheckCallback.class)
.doesNotHaveBean(SofaRpcEndpoint.class)
.doesNotHaveBean(HealthCheckProviderConfigDelayRegisterChecker.class));
.doesNotHaveBean(SofaRpcEndpoint.class));
}

@Test
void runWhenRpcBeanNotExist() {
this.contextRunner
.run((context) -> assertThat(context)
.doesNotHaveBean(RpcAfterHealthCheckCallback.class)
.doesNotHaveBean(SofaRpcEndpoint.class)
.doesNotHaveBean(HealthCheckProviderConfigDelayRegisterChecker.class));
.doesNotHaveBean(SofaRpcEndpoint.class));
}

static class MockReadinessCheckListenerConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public HealthCheckProviderConfigDelayRegisterChecker(ReadinessCheckListener read

@Override
public boolean allowRegister() {
return ReadinessState.ACCEPTING_TRAFFIC.equals(readinessCheckListener.getReadinessState());
return !ReadinessState.REFUSING_TRAFFIC.equals(readinessCheckListener.getReadinessState());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ public void setProviderRegisterBlackList(List<String> providerRegisterBlackList)
this.providerRegisterBlackList = providerRegisterBlackList;
}

public boolean getEnableDelayRegister() {
public boolean isEnableDelayRegister() {
return enableDelayRegister;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package com.alipay.sofa.boot.autoconfigure.rpc;

import com.alipay.sofa.boot.actuator.rpc.HealthCheckProviderConfigDelayRegisterChecker;
import com.alipay.sofa.boot.autoconfigure.condition.ConditionalOnSwitch;
import com.alipay.sofa.boot.autoconfigure.rpc.SofaRpcAutoConfiguration.RegistryConfigurationImportSelector;
import com.alipay.sofa.boot.autoconfigure.runtime.SofaRuntimeAutoConfiguration;
Expand All @@ -25,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;
Expand Down Expand Up @@ -75,17 +75,16 @@ public class SofaRpcAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public ProviderConfigContainer providerConfigContainer(SofaBootRpcProperties sofaBootRpcProperties,
ObjectProvider<HealthCheckProviderConfigDelayRegisterChecker> healthCheckProviderConfigDelayRegisterChecker) {
ObjectProvider<ProviderConfigDelayRegisterChecker> providerConfigDelayRegisterCheckers) {
ProviderConfigContainer providerConfigContainer = new ProviderConfigContainer();
providerConfigContainer.setProviderRegisterWhiteList(sofaBootRpcProperties
.getProviderRegisterWhiteList());
providerConfigContainer.setProviderRegisterBlackList(sofaBootRpcProperties
.getProviderRegisterBlackList());
providerConfigContainer
.setProviderConfigDelayRegister(healthCheckProviderConfigDelayRegisterChecker.stream()
.collect(Collectors.toList()));
providerConfigContainer.setProviderConfigDelayRegister(providerConfigDelayRegisterCheckers
.stream().collect(Collectors.toList()));
providerConfigContainer.setEnableDelayRegister(sofaBootRpcProperties
.getEnableDelayRegister());
.isEnableDelayRegister());
return providerConfigContainer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,17 @@ public Collection<ProviderConfig> getAllProviderConfig() {
public void publishAllProviderConfig() {
for (ProviderConfig providerConfig : getAllProviderConfig()) {
int delay = providerConfig.getDelay();
// 没有配置延时加载则直接去注册中心注册服务
if (!enableDelayRegister && delay <= 0) {
doPublishProviderConfig(providerConfig);
} else {
if (enableDelayRegister && delay > 0) {
// 根据延时时间异步去注册中心注册服务
if (scheduledExecutorService == null) {
scheduledExecutorService = new SofaScheduledThreadPoolExecutor(1, "async-register-bean",
scheduledExecutorService = new SofaScheduledThreadPoolExecutor(1, "rpc-provider-delay-register",
SofaBootConstants.SOFA_BOOT_SPACE_NAME);
}
scheduledExecutorService.schedule(() -> doDelayPublishProviderConfig(providerConfig,
providerConfigDelayRegisterCheckerList), delay, TimeUnit.MILLISECONDS);
} else {
// 没有配置延时加载则直接去注册中心注册服务
doPublishProviderConfig(providerConfig);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class DelayRegisterTests {
*/
@Test
public void testDelayRegister() throws InterruptedException {
// 首次发起调用、期望返回 RPC-020060001: Cannot get the service address of service
// 首次发起调用、期望返回 RPC cannot find service
Assertions.assertThatThrownBy(() -> delayRegisterService.sayHello("hi")).isInstanceOf(SofaRouteException.class).
hasMessageContaining("Cannot get the service address of service [com.alipay.sofa.smoke.tests.rpc.boot.bean.delayregister.DelayRegisterService:1.0], please check the registry log.");
// 因为在 META-INF/sofa-rpc/rpc-config.json 配置了5s
Expand Down

0 comments on commit 1c62a9b

Please sign in to comment.