@ -17,6 +17,7 @@
package com.tencent.cloud.polaris.context ;
package com.tencent.cloud.polaris.context ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.List ;
import java.util.Objects ;
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.ConsumerAPI ;
import com.tencent.polaris.api.core.LosslessAPI ;
import com.tencent.polaris.api.core.LosslessAPI ;
import com.tencent.polaris.api.core.ProviderAPI ;
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.api.AssemblyAPI ;
import com.tencent.polaris.assembly.factory.AssemblyAPIFactory ;
import com.tencent.polaris.assembly.factory.AssemblyAPIFactory ;
import com.tencent.polaris.circuitbreak.api.CircuitBreakAPI ;
import com.tencent.polaris.circuitbreak.api.CircuitBreakAPI ;
@ -53,7 +55,8 @@ public class PolarisSDKContextManager {
* Constant for checking before destroy SDK context .
* Constant for checking before destroy SDK context .
* /
* /
public volatile static boolean isRegistered = false ;
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 ProviderAPI providerAPI ;
private volatile static ConsumerAPI consumerAPI ;
private volatile static ConsumerAPI consumerAPI ;
private volatile static LosslessAPI losslessAPI ;
private volatile static LosslessAPI losslessAPI ;
@ -75,7 +78,7 @@ public class PolarisSDKContextManager {
* Don ' t call this method directly .
* Don ' t call this method directly .
* /
* /
public static void innerDestroy ( ) {
public static void innerDestroy ( ) {
if ( Objects . nonNull ( s dkContext) ) {
if ( Objects . nonNull ( s erviceS dkContext) ) {
try {
try {
// destroy ProviderAPI
// destroy ProviderAPI
if ( Objects . nonNull ( providerAPI ) ) {
if ( Objects . nonNull ( providerAPI ) ) {
@ -119,9 +122,9 @@ public class PolarisSDKContextManager {
assemblyAPI = null ;
assemblyAPI = null ;
}
}
if ( Objects . nonNull ( s dkContext) ) {
if ( Objects . nonNull ( s erviceS dkContext) ) {
s dkContext. destroy ( ) ;
s erviceS dkContext. destroy ( ) ;
s dkContext = null ;
s erviceS dkContext = null ;
}
}
LOG . info ( "Polaris SDK context is destroyed." ) ;
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 ( ) {
public void init ( ) {
if ( null = = sdkContext ) {
if ( null = = s erviceS dkContext) {
try {
try {
// init SDKContext
// init SDKContext
sdkContext = SDKContext . initContextByConfig ( properties . configuration ( modifierList ,
s erviceS dkContext = SDKContext . initContextByConfig ( properties . configuration ( modifierList ,
( ) - > environment . getProperty ( "spring.cloud.client.ip-address" ) ,
( ) - > environment . getProperty ( "spring.cloud.client.ip-address" ) ,
( ) - > environment . getProperty ( "spring.cloud.polaris.local-port" , Integer . class , 0 ) ) ) ;
( ) - > environment . getProperty ( "spring.cloud.polaris.local-port" , Integer . class , 0 ) ) ) ;
sdkContext . init ( ) ;
s erviceS dkContext. init ( ) ;
// init ProviderAPI
// init ProviderAPI
providerAPI = DiscoveryAPIFactory . createProviderAPIByContext ( sdkContext ) ;
providerAPI = DiscoveryAPIFactory . createProviderAPIByContext ( s erviceS dkContext) ;
// init losslessAPI
// init losslessAPI
losslessAPI = DiscoveryAPIFactory . createLosslessAPIByContext ( sdkContext ) ;
losslessAPI = DiscoveryAPIFactory . createLosslessAPIByContext ( s erviceS dkContext) ;
// init ConsumerAPI
// init ConsumerAPI
consumerAPI = DiscoveryAPIFactory . createConsumerAPIByContext ( sdkContext ) ;
consumerAPI = DiscoveryAPIFactory . createConsumerAPIByContext ( s erviceS dkContext) ;
// init RouterAPI
// init RouterAPI
routerAPI = RouterAPIFactory . createRouterAPIByContext ( sdkContext ) ;
routerAPI = RouterAPIFactory . createRouterAPIByContext ( s erviceS dkContext) ;
// init CircuitBreakAPI
// init CircuitBreakAPI
circuitBreakAPI = CircuitBreakAPIFactory . createCircuitBreakAPIByContext ( sdkContext ) ;
circuitBreakAPI = CircuitBreakAPIFactory . createCircuitBreakAPIByContext ( s erviceS dkContext) ;
// init LimitAPI
// init LimitAPI
limitAPI = LimitAPIFactory . createLimitAPIByContext ( s dkContext) ;
limitAPI = LimitAPIFactory . createLimitAPIByContext ( s erviceS dkContext) ;
// init AssemblyAPI
// init AssemblyAPI
assemblyAPI = AssemblyAPIFactory . createAssemblyAPIByContext ( s dkContext) ;
assemblyAPI = AssemblyAPIFactory . createAssemblyAPIByContext ( s erviceS dkContext) ;
// add shutdown hook
// add shutdown hook
Runtime . getRuntime ( ) . addShutdownHook ( new Thread ( ( ) - > {
Runtime . getRuntime ( ) . addShutdownHook ( new Thread ( ( ) - > {
@ -182,11 +208,25 @@ public class PolarisSDKContextManager {
throw throwable ;
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 ( ) {
public SDKContext getSDKContext ( ) {
init ( ) ;
init ( ) ;
return sdkContext ;
return s erviceS dkContext;
}
}
public ProviderAPI getProviderAPI ( ) {
public ProviderAPI getProviderAPI ( ) {
@ -222,4 +262,36 @@ public class PolarisSDKContextManager {
public AssemblyAPI getAssemblyAPI ( ) {
public AssemblyAPI getAssemblyAPI ( ) {
return assemblyAPI ;
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 ;
}
}
}
}
}