|
|
|
@ -17,6 +17,7 @@
|
|
|
|
|
|
|
|
|
|
package com.tencent.cloud.polaris.context;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
|
@ -25,6 +26,7 @@ import com.tencent.polaris.api.control.Destroyable;
|
|
|
|
|
import com.tencent.polaris.api.core.ConsumerAPI;
|
|
|
|
|
import com.tencent.polaris.api.core.LosslessAPI;
|
|
|
|
|
import com.tencent.polaris.api.core.ProviderAPI;
|
|
|
|
|
import com.tencent.polaris.api.utils.CollectionUtils;
|
|
|
|
|
import com.tencent.polaris.assembly.api.AssemblyAPI;
|
|
|
|
|
import com.tencent.polaris.assembly.factory.AssemblyAPIFactory;
|
|
|
|
|
import com.tencent.polaris.circuitbreak.api.CircuitBreakAPI;
|
|
|
|
@ -53,7 +55,8 @@ public class PolarisSDKContextManager {
|
|
|
|
|
* Constant for checking before destroy SDK context.
|
|
|
|
|
*/
|
|
|
|
|
public volatile static boolean isRegistered = false;
|
|
|
|
|
private volatile static SDKContext sdkContext;
|
|
|
|
|
private volatile static SDKContext configSDKContext;
|
|
|
|
|
private volatile static SDKContext serviceSdkContext;
|
|
|
|
|
private volatile static ProviderAPI providerAPI;
|
|
|
|
|
private volatile static ConsumerAPI consumerAPI;
|
|
|
|
|
private volatile static LosslessAPI losslessAPI;
|
|
|
|
@ -75,7 +78,7 @@ public class PolarisSDKContextManager {
|
|
|
|
|
* Don't call this method directly.
|
|
|
|
|
*/
|
|
|
|
|
public static void innerDestroy() {
|
|
|
|
|
if (Objects.nonNull(sdkContext)) {
|
|
|
|
|
if (Objects.nonNull(serviceSdkContext)) {
|
|
|
|
|
try {
|
|
|
|
|
// destroy ProviderAPI
|
|
|
|
|
if (Objects.nonNull(providerAPI)) {
|
|
|
|
@ -119,9 +122,9 @@ public class PolarisSDKContextManager {
|
|
|
|
|
assemblyAPI = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Objects.nonNull(sdkContext)) {
|
|
|
|
|
sdkContext.destroy();
|
|
|
|
|
sdkContext = null;
|
|
|
|
|
if (Objects.nonNull(serviceSdkContext)) {
|
|
|
|
|
serviceSdkContext.destroy();
|
|
|
|
|
serviceSdkContext = null;
|
|
|
|
|
}
|
|
|
|
|
LOG.info("Polaris SDK context is destroyed.");
|
|
|
|
|
}
|
|
|
|
@ -131,35 +134,58 @@ public class PolarisSDKContextManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Used for config data.
|
|
|
|
|
*/
|
|
|
|
|
public static SDKContext innerGetConfigSDKContext() {
|
|
|
|
|
if (configSDKContext == null) {
|
|
|
|
|
throw new IllegalArgumentException("configSDKContext is not initialized.");
|
|
|
|
|
}
|
|
|
|
|
return configSDKContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void innerConfigDestroy() {
|
|
|
|
|
try {
|
|
|
|
|
if (Objects.nonNull(configSDKContext)) {
|
|
|
|
|
configSDKContext.destroy();
|
|
|
|
|
configSDKContext = null;
|
|
|
|
|
}
|
|
|
|
|
LOG.info("Polaris SDK config context is destroyed.");
|
|
|
|
|
}
|
|
|
|
|
catch (Throwable throwable) {
|
|
|
|
|
LOG.info("Polaris SDK config context is destroyed failed.", throwable);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void init() {
|
|
|
|
|
if (null == sdkContext) {
|
|
|
|
|
if (null == serviceSdkContext) {
|
|
|
|
|
try {
|
|
|
|
|
// init SDKContext
|
|
|
|
|
sdkContext = SDKContext.initContextByConfig(properties.configuration(modifierList,
|
|
|
|
|
serviceSdkContext = SDKContext.initContextByConfig(properties.configuration(modifierList,
|
|
|
|
|
() -> environment.getProperty("spring.cloud.client.ip-address"),
|
|
|
|
|
() -> environment.getProperty("spring.cloud.polaris.local-port", Integer.class, 0)));
|
|
|
|
|
sdkContext.init();
|
|
|
|
|
serviceSdkContext.init();
|
|
|
|
|
|
|
|
|
|
// init ProviderAPI
|
|
|
|
|
providerAPI = DiscoveryAPIFactory.createProviderAPIByContext(sdkContext);
|
|
|
|
|
providerAPI = DiscoveryAPIFactory.createProviderAPIByContext(serviceSdkContext);
|
|
|
|
|
|
|
|
|
|
// init losslessAPI
|
|
|
|
|
losslessAPI = DiscoveryAPIFactory.createLosslessAPIByContext(sdkContext);
|
|
|
|
|
losslessAPI = DiscoveryAPIFactory.createLosslessAPIByContext(serviceSdkContext);
|
|
|
|
|
|
|
|
|
|
// init ConsumerAPI
|
|
|
|
|
consumerAPI = DiscoveryAPIFactory.createConsumerAPIByContext(sdkContext);
|
|
|
|
|
consumerAPI = DiscoveryAPIFactory.createConsumerAPIByContext(serviceSdkContext);
|
|
|
|
|
|
|
|
|
|
// init RouterAPI
|
|
|
|
|
routerAPI = RouterAPIFactory.createRouterAPIByContext(sdkContext);
|
|
|
|
|
routerAPI = RouterAPIFactory.createRouterAPIByContext(serviceSdkContext);
|
|
|
|
|
|
|
|
|
|
// init CircuitBreakAPI
|
|
|
|
|
circuitBreakAPI = CircuitBreakAPIFactory.createCircuitBreakAPIByContext(sdkContext);
|
|
|
|
|
circuitBreakAPI = CircuitBreakAPIFactory.createCircuitBreakAPIByContext(serviceSdkContext);
|
|
|
|
|
|
|
|
|
|
// init LimitAPI
|
|
|
|
|
limitAPI = LimitAPIFactory.createLimitAPIByContext(sdkContext);
|
|
|
|
|
limitAPI = LimitAPIFactory.createLimitAPIByContext(serviceSdkContext);
|
|
|
|
|
|
|
|
|
|
// init AssemblyAPI
|
|
|
|
|
assemblyAPI = AssemblyAPIFactory.createAssemblyAPIByContext(sdkContext);
|
|
|
|
|
assemblyAPI = AssemblyAPIFactory.createAssemblyAPIByContext(serviceSdkContext);
|
|
|
|
|
|
|
|
|
|
// add shutdown hook
|
|
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
|
|
|
@ -182,11 +208,25 @@ public class PolarisSDKContextManager {
|
|
|
|
|
throw throwable;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
initConfig();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Used for config data.
|
|
|
|
|
*/
|
|
|
|
|
public static void setConfigSDKContext(SDKContext context) {
|
|
|
|
|
if (configSDKContext == null) {
|
|
|
|
|
configSDKContext = context;
|
|
|
|
|
// add shutdown hook
|
|
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread(PolarisSDKContextManager::innerConfigDestroy));
|
|
|
|
|
LOG.info("create Polaris config SDK context successfully.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SDKContext getSDKContext() {
|
|
|
|
|
init();
|
|
|
|
|
return sdkContext;
|
|
|
|
|
return serviceSdkContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ProviderAPI getProviderAPI() {
|
|
|
|
@ -222,4 +262,36 @@ public class PolarisSDKContextManager {
|
|
|
|
|
public AssemblyAPI getAssemblyAPI() {
|
|
|
|
|
return assemblyAPI;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SDKContext getConfigSDKContext() {
|
|
|
|
|
initConfig();
|
|
|
|
|
return configSDKContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void initConfig() {
|
|
|
|
|
// get modifiers for configuration.
|
|
|
|
|
List<PolarisConfigModifier> configModifierList = new ArrayList<>();
|
|
|
|
|
for (PolarisConfigModifier modifier : modifierList) {
|
|
|
|
|
if (modifier instanceof PolarisConfigurationConfigModifier) {
|
|
|
|
|
configModifierList.add(modifier);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (null == configSDKContext && CollectionUtils.isNotEmpty(configModifierList)) {
|
|
|
|
|
try {
|
|
|
|
|
// init config SDKContext
|
|
|
|
|
configSDKContext = SDKContext.initContextByConfig(properties.configuration(configModifierList,
|
|
|
|
|
() -> environment.getProperty("spring.cloud.client.ip-address"),
|
|
|
|
|
() -> environment.getProperty("spring.cloud.polaris.local-port", Integer.class, 0)));
|
|
|
|
|
configSDKContext.init();
|
|
|
|
|
|
|
|
|
|
// add shutdown hook
|
|
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread(PolarisSDKContextManager::innerConfigDestroy));
|
|
|
|
|
LOG.info("create Polaris config SDK context successfully. properties: {}, ", properties);
|
|
|
|
|
}
|
|
|
|
|
catch (Throwable throwable) {
|
|
|
|
|
LOG.error("create Polaris config SDK context failed. properties: {}, ", properties, throwable);
|
|
|
|
|
throw throwable;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|