Search in sources :

Example 1 with ValueContext

use of com.tencent.polaris.api.plugin.common.ValueContext in project spring-cloud-tencent by Tencent.

the class PolarisContextAutoConfiguration method polarisContext.

@Bean(name = "polarisContext", initMethod = "init", destroyMethod = "destroy")
@ConditionalOnMissingBean
public SDKContext polarisContext(PolarisContextProperties properties, StaticMetadataManager staticMetadataManager) throws PolarisException {
    SDKContext sdkContext = SDKContext.initContextByConfig(properties.configuration());
    // init current instance location info from environment
    ValueContext valueContext = sdkContext.getValueContext();
    String region = staticMetadataManager.getRegion();
    String zone = staticMetadataManager.getZone();
    String campus = staticMetadataManager.getCampus();
    if (StringUtils.isNotBlank(region)) {
        valueContext.setValue(LocationLevel.region.name(), region);
    }
    if (StringUtils.isNotBlank(zone)) {
        valueContext.setValue(LocationLevel.zone.name(), zone);
    }
    if (StringUtils.isNotBlank(campus)) {
        valueContext.setValue(LocationLevel.campus.name(), campus);
    }
    return sdkContext;
}
Also used : SDKContext(com.tencent.polaris.client.api.SDKContext) ValueContext(com.tencent.polaris.api.plugin.common.ValueContext) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 2 with ValueContext

use of com.tencent.polaris.api.plugin.common.ValueContext in project polaris-java by polarismesh.

the class SDKContext method initContextByConfig.

/**
 * 通过配置对象初始化SDK上下文
 *
 * @param config 配置对象
 * @return SDK上下文
 * @throws PolarisException 初始化过程的异常
 */
public static SDKContext initContextByConfig(Configuration config) throws PolarisException {
    try {
        ((ConfigurationImpl) config).setDefault();
        config.verify();
    } catch (IllegalArgumentException e) {
        throw new PolarisException(ErrorCode.INVALID_CONFIG, "fail to verify configuration", e);
    }
    ServiceLoader<TypeProvider> providers = ServiceLoader.load(TypeProvider.class);
    List<PluginType> types = new ArrayList<>();
    for (TypeProvider provider : providers) {
        types.addAll(provider.getTypes());
    }
    PluginManager manager = new PluginManager(types);
    ValueContext valueContext = new ValueContext();
    valueContext.setHost(parseHost(config));
    valueContext.setServerConnectorProtocol(parseServerConnectorProtocol(config));
    SDKContext initContext = new SDKContext(config, manager, valueContext);
    try {
        manager.initPlugins(initContext);
    } catch (Throwable e) {
        manager.destroyPlugins();
        if (e instanceof PolarisException) {
            throw e;
        }
        throw new PolarisException(ErrorCode.PLUGIN_ERROR, "plugin error", e);
    }
    return initContext;
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) PluginManager(com.tencent.polaris.api.plugin.impl.PluginManager) ValueContext(com.tencent.polaris.api.plugin.common.ValueContext) ArrayList(java.util.ArrayList) TypeProvider(com.tencent.polaris.api.plugin.TypeProvider) ConfigurationImpl(com.tencent.polaris.factory.config.ConfigurationImpl) PluginType(com.tencent.polaris.api.plugin.PluginType)

Aggregations

ValueContext (com.tencent.polaris.api.plugin.common.ValueContext)2 PolarisException (com.tencent.polaris.api.exception.PolarisException)1 PluginType (com.tencent.polaris.api.plugin.PluginType)1 TypeProvider (com.tencent.polaris.api.plugin.TypeProvider)1 PluginManager (com.tencent.polaris.api.plugin.impl.PluginManager)1 SDKContext (com.tencent.polaris.client.api.SDKContext)1 ConfigurationImpl (com.tencent.polaris.factory.config.ConfigurationImpl)1 ArrayList (java.util.ArrayList)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 Bean (org.springframework.context.annotation.Bean)1