feat:refactor SDKContext as static. (#995)
parent
841a39ac33
commit
0ab19edb7a
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.polaris.discovery;
|
||||
|
||||
import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
|
||||
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
|
||||
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||
import com.tencent.polaris.api.exception.PolarisException;
|
||||
import com.tencent.polaris.api.rpc.ServicesResponse;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisDiscoveryHandler}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class PolarisDiscoveryHandlerTest {
|
||||
|
||||
private PolarisDiscoveryHandler polarisDiscoveryHandler;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
PolarisDiscoveryProperties polarisDiscoveryProperties = mock(PolarisDiscoveryProperties.class);
|
||||
doReturn(NAMESPACE_TEST).when(polarisDiscoveryProperties).getNamespace();
|
||||
|
||||
ConsumerAPI consumerAPI = mock(ConsumerAPI.class);
|
||||
ServicesResponse servicesResponse = mock(ServicesResponse.class);
|
||||
doReturn(servicesResponse).when(consumerAPI).getServices(any());
|
||||
|
||||
PolarisSDKContextManager polarisSDKContextManager = mock(PolarisSDKContextManager.class);
|
||||
doReturn(consumerAPI).when(polarisSDKContextManager).getConsumerAPI();
|
||||
polarisDiscoveryHandler = new PolarisDiscoveryHandler(polarisDiscoveryProperties, polarisSDKContextManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetServices() throws PolarisException {
|
||||
ServicesResponse servicesResponse = polarisDiscoveryHandler.getServices();
|
||||
assertThat(servicesResponse).isNotNull();
|
||||
}
|
||||
}
|
@ -1,76 +0,0 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.polaris.loadbalancer;
|
||||
|
||||
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
|
||||
import com.tencent.polaris.router.api.core.RouterAPI;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisLoadBalancerAutoConfiguration}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisRouterAutoConfigurationTest {
|
||||
|
||||
private final ApplicationContextRunner blockingContextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(
|
||||
PolarisLoadBalancerTest.class,
|
||||
PolarisContextAutoConfiguration.class,
|
||||
PolarisLoadBalancerAutoConfiguration.class))
|
||||
.withPropertyValues("spring.cloud.loadbalancer.configurations=polaris", "spring.application.name=test");
|
||||
|
||||
private final ApplicationContextRunner noPolarisContextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(
|
||||
PolarisLoadBalancerTest.class,
|
||||
PolarisContextAutoConfiguration.class,
|
||||
PolarisLoadBalancerAutoConfiguration.class));
|
||||
|
||||
/**
|
||||
* Test for BlockingDiscovery.
|
||||
*/
|
||||
@Test
|
||||
public void test1() {
|
||||
this.blockingContextRunner.run(context -> {
|
||||
assertThat(context).hasSingleBean(RouterAPI.class);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for no Polaris.
|
||||
*/
|
||||
@Test
|
||||
public void test2() {
|
||||
this.noPolarisContextRunner.run(context -> {
|
||||
assertThat(context).hasSingleBean(RouterAPI.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
static class PolarisLoadBalancerTest {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.tsf.adapter.config;
|
||||
|
||||
import com.tencent.polaris.circuitbreak.api.flow.CircuitBreakerFlow;
|
||||
import com.tencent.polaris.client.api.SDKContext;
|
||||
|
||||
/**
|
||||
* TsfRouterFlow.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
public class TsfCircuitBreakerFlow implements CircuitBreakerFlow {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return PolarisTsfFlowConfigModifier.TSF_FLOW_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSDKContext(SDKContext sdkContext) {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.tsf.adapter.config;
|
||||
|
||||
import com.tencent.polaris.client.api.SDKContext;
|
||||
import com.tencent.polaris.ratelimit.api.flow.LimitFlow;
|
||||
|
||||
/**
|
||||
* TsfRouterFlow.
|
||||
*
|
||||
* @author sean yu
|
||||
*/
|
||||
public class TsfLimitFlow implements LimitFlow {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return PolarisTsfFlowConfigModifier.TSF_FLOW_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSDKContext(SDKContext sdkContext) {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
com.tencent.cloud.tsf.adapter.config.TsfCircuitBreakerFlow
|
@ -0,0 +1 @@
|
||||
com.tencent.cloud.tsf.adapter.config.TsfLimitFlow
|
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.plugin.featureenv;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link FeatureEnvAutoConfiguration}.
|
||||
*
|
||||
* @author Hoatian Zhang
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class FeatureEnvAutoConfigurationTest {
|
||||
|
||||
private final ApplicationContextRunner enabledApplicationContextRunner = new ApplicationContextRunner();
|
||||
private final ApplicationContextRunner disabledApplicationContextRunner = new ApplicationContextRunner();
|
||||
|
||||
@Test
|
||||
public void testEnabled() {
|
||||
this.enabledApplicationContextRunner.withConfiguration(AutoConfigurations.of(FeatureEnvAutoConfiguration.class))
|
||||
.run(context -> {
|
||||
assertThat(context).hasSingleBean(FeatureEnvProperties.class);
|
||||
assertThat(context).hasSingleBean(FeatureEnvRouterRequestInterceptor.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDisabled() {
|
||||
this.disabledApplicationContextRunner.withConfiguration(AutoConfigurations.of(FeatureEnvAutoConfiguration.class))
|
||||
.withPropertyValues("spring.cloud.tencent.plugin.router.feature-env.enabled=false")
|
||||
.run(context -> {
|
||||
assertThat(context).doesNotHaveBean(FeatureEnvProperties.class);
|
||||
assertThat(context).doesNotHaveBean(FeatureEnvRouterRequestInterceptor.class);
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,207 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.polaris.context;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.tencent.cloud.polaris.context.config.PolarisContextProperties;
|
||||
import com.tencent.polaris.api.control.Destroyable;
|
||||
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||
import com.tencent.polaris.api.core.ProviderAPI;
|
||||
import com.tencent.polaris.assembly.api.AssemblyAPI;
|
||||
import com.tencent.polaris.assembly.factory.AssemblyAPIFactory;
|
||||
import com.tencent.polaris.circuitbreak.api.CircuitBreakAPI;
|
||||
import com.tencent.polaris.circuitbreak.factory.CircuitBreakAPIFactory;
|
||||
import com.tencent.polaris.client.api.SDKContext;
|
||||
import com.tencent.polaris.factory.api.DiscoveryAPIFactory;
|
||||
import com.tencent.polaris.factory.api.RouterAPIFactory;
|
||||
import com.tencent.polaris.ratelimit.api.core.LimitAPI;
|
||||
import com.tencent.polaris.ratelimit.factory.LimitAPIFactory;
|
||||
import com.tencent.polaris.router.api.core.RouterAPI;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
/**
|
||||
* Manager for static Polaris SDK context.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisSDKContextManager {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(PolarisSDKContextManager.class);
|
||||
|
||||
/**
|
||||
* Constant for checking before destroy SDK context.
|
||||
*/
|
||||
public volatile static boolean isRegistered = false;
|
||||
private volatile static SDKContext sdkContext;
|
||||
private volatile static ProviderAPI providerAPI;
|
||||
private volatile static ConsumerAPI consumerAPI;
|
||||
private volatile static RouterAPI routerAPI;
|
||||
private volatile static CircuitBreakAPI circuitBreakAPI;
|
||||
private volatile static LimitAPI limitAPI;
|
||||
private volatile static AssemblyAPI assemblyAPI;
|
||||
private final PolarisContextProperties properties;
|
||||
private final Environment environment;
|
||||
private final List<PolarisConfigModifier> modifierList;
|
||||
|
||||
public PolarisSDKContextManager(PolarisContextProperties properties, Environment environment, List<PolarisConfigModifier> modifierList) {
|
||||
this.properties = properties;
|
||||
this.environment = environment;
|
||||
this.modifierList = modifierList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Don't call this method directly.
|
||||
*/
|
||||
public static void innerDestroy() {
|
||||
if (Objects.nonNull(sdkContext)) {
|
||||
try {
|
||||
// destroy ProviderAPI
|
||||
if (Objects.nonNull(providerAPI)) {
|
||||
((AutoCloseable) providerAPI).close();
|
||||
providerAPI = null;
|
||||
}
|
||||
|
||||
// destroy ConsumerAPI
|
||||
if (Objects.nonNull(consumerAPI)) {
|
||||
((AutoCloseable) consumerAPI).close();
|
||||
consumerAPI = null;
|
||||
}
|
||||
|
||||
// destroy RouterAPI
|
||||
if (Objects.nonNull(routerAPI)) {
|
||||
((Destroyable) routerAPI).destroy();
|
||||
routerAPI = null;
|
||||
}
|
||||
|
||||
// destroy CircuitBreakAPI
|
||||
if (Objects.nonNull(circuitBreakAPI)) {
|
||||
((Destroyable) circuitBreakAPI).destroy();
|
||||
circuitBreakAPI = null;
|
||||
}
|
||||
|
||||
// destroy LimitAPI
|
||||
if (Objects.nonNull(limitAPI)) {
|
||||
((AutoCloseable) limitAPI).close();
|
||||
limitAPI = null;
|
||||
}
|
||||
|
||||
// destroy AssemblyAPI
|
||||
if (Objects.nonNull(assemblyAPI)) {
|
||||
((Destroyable) assemblyAPI).destroy();
|
||||
assemblyAPI = null;
|
||||
}
|
||||
|
||||
sdkContext.destroy();
|
||||
sdkContext = null;
|
||||
LOG.info("Polaris SDK context is destroyed.");
|
||||
}
|
||||
catch (Throwable throwable) {
|
||||
LOG.error("destroy Polaris SDK context failed.", throwable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void init() {
|
||||
if (null == sdkContext) {
|
||||
try {
|
||||
// init SDKContext
|
||||
sdkContext = SDKContext.initContextByConfig(properties.configuration(modifierList,
|
||||
() -> environment.getProperty("spring.cloud.client.ip-address"),
|
||||
() -> environment.getProperty("server.port", Integer.class, 0)));
|
||||
sdkContext.init();
|
||||
|
||||
// init ProviderAPI
|
||||
providerAPI = DiscoveryAPIFactory.createProviderAPIByContext(sdkContext);
|
||||
|
||||
// init ConsumerAPI
|
||||
consumerAPI = DiscoveryAPIFactory.createConsumerAPIByContext(sdkContext);
|
||||
|
||||
// init RouterAPI
|
||||
routerAPI = RouterAPIFactory.createRouterAPIByContext(sdkContext);
|
||||
|
||||
// init CircuitBreakAPI
|
||||
circuitBreakAPI = CircuitBreakAPIFactory.createCircuitBreakAPIByContext(sdkContext);
|
||||
|
||||
// init LimitAPI
|
||||
limitAPI = LimitAPIFactory.createLimitAPIByContext(sdkContext);
|
||||
|
||||
// init AssemblyAPI
|
||||
assemblyAPI = AssemblyAPIFactory.createAssemblyAPIByContext(sdkContext);
|
||||
|
||||
// add shutdown hook
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||
long startTimestamp = System.currentTimeMillis();
|
||||
long delay = 0;
|
||||
while (true) {
|
||||
if (!isRegistered || delay >= 60000) {
|
||||
innerDestroy();
|
||||
break;
|
||||
}
|
||||
else {
|
||||
delay = System.currentTimeMillis() - startTimestamp;
|
||||
}
|
||||
}
|
||||
}));
|
||||
LOG.info("create Polaris SDK context successfully. properties: {}, ", properties);
|
||||
}
|
||||
catch (Throwable throwable) {
|
||||
LOG.error("create Polaris SDK context failed. properties: {}, ", properties, throwable);
|
||||
throw throwable;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public SDKContext getSDKContext() {
|
||||
init();
|
||||
return sdkContext;
|
||||
}
|
||||
|
||||
public ProviderAPI getProviderAPI() {
|
||||
init();
|
||||
return providerAPI;
|
||||
}
|
||||
|
||||
public ConsumerAPI getConsumerAPI() {
|
||||
init();
|
||||
return consumerAPI;
|
||||
}
|
||||
|
||||
public RouterAPI getRouterAPI() {
|
||||
init();
|
||||
return routerAPI;
|
||||
}
|
||||
|
||||
public CircuitBreakAPI getCircuitBreakAPI() {
|
||||
init();
|
||||
return circuitBreakAPI;
|
||||
}
|
||||
|
||||
public LimitAPI getLimitAPI() {
|
||||
init();
|
||||
return limitAPI;
|
||||
}
|
||||
|
||||
public AssemblyAPI getAssemblyAPI() {
|
||||
return assemblyAPI;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue