Search in sources :

Example 1 with ServerServiceInfo

use of com.tencent.polaris.api.plugin.compose.ServerServiceInfo in project polaris-java by polarismesh.

the class InMemoryRegistry method init.

@Override
public void init(InitContext ctx) throws PolarisException {
    // 获取系统服务配置
    Collection<ServerServiceInfo> serverServices = ctx.getServerServices();
    for (ServerServiceInfo serverServiceInfo : serverServices) {
        if (serverServiceInfo.getClusterType() == ClusterType.SERVICE_DISCOVER_CLUSTER) {
            hasDiscoverCluster = true;
        }
        serverServiceMap.put(serverServiceInfo.getServiceKey(), serverServiceInfo);
    }
    // 加载cacheHandler
    ServiceLoader<CacheHandler> handlers = ServiceLoader.load(CacheHandler.class);
    for (CacheHandler handler : handlers) {
        cacheHandlers.put(handler.getTargetEventType(), handler);
    }
    // Load server connector.
    connector = (ServerConnector) ctx.getPlugins().getPlugin(PluginTypes.SERVER_CONNECTOR.getBaseType(), ctx.getValueContext().getServerConnectorProtocol());
    // 构建基础属性
    String persistDir = ctx.getConfig().getConsumer().getLocalCache().getPersistDir();
    int maxReadRetry = ctx.getConfig().getConsumer().getLocalCache().getPersistMaxReadRetry();
    int maxWriteRetry = ctx.getConfig().getConsumer().getLocalCache().getPersistMaxWriteRetry();
    long retryIntervalMs = ctx.getConfig().getConsumer().getLocalCache().getPersistRetryInterval();
    this.serviceRefreshIntervalMs = ctx.getConfig().getConsumer().getLocalCache().getServiceRefreshInterval();
    boolean configPersistEnable = ctx.getConfig().getConsumer().getLocalCache().isPersistEnable();
    persistEnable = configPersistEnable && StringUtils.isNotBlank(persistDir);
    // 启动本地缓存
    if (persistEnable) {
        messagePersistHandler = new MessagePersistHandler(persistDir, maxWriteRetry, maxReadRetry, retryIntervalMs);
        try {
            messagePersistHandler.init();
        } catch (IOException e) {
            throw new PolarisException(ErrorCode.PLUGIN_ERROR, String.format("plugin %s init failed", getName()), e);
        }
        loadFileCache(persistDir);
    }
    NamedThreadFactory namedThreadFactory = new NamedThreadFactory(getName());
    serviceExpireTimeMs = ctx.getConfig().getConsumer().getLocalCache().getServiceExpireTime();
    persistExecutor = Executors.newSingleThreadExecutor(namedThreadFactory);
    expireExecutor = Executors.newSingleThreadScheduledExecutor(namedThreadFactory);
    if (hasDiscoverCluster) {
        serverServicesDiscoverExecutor = new ThreadPoolExecutor(0, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), namedThreadFactory);
    }
}
Also used : NamedThreadFactory(com.tencent.polaris.client.util.NamedThreadFactory) IOException(java.io.IOException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ServerServiceInfo(com.tencent.polaris.api.plugin.compose.ServerServiceInfo) PolarisException(com.tencent.polaris.api.exception.PolarisException) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) CacheHandler(com.tencent.polaris.api.plugin.registry.CacheHandler)

Example 2 with ServerServiceInfo

use of com.tencent.polaris.api.plugin.compose.ServerServiceInfo in project polaris-java by polarismesh.

the class InMemoryRegistry method enhanceServiceEventHandler.

private ServiceEventHandler enhanceServiceEventHandler(ServiceEventHandler eventHandler) {
    ServiceKey serviceKey = eventHandler.getServiceEventKey().getServiceKey();
    ServerServiceInfo info = serverServiceMap.get(serviceKey);
    if (null != info) {
        // 系统服务
        eventHandler.setRefreshInterval(info.getRefreshIntervalMs());
        if (info.getClusterType() != ClusterType.SERVICE_DISCOVER_CLUSTER) {
            eventHandler.setTargetCluster(ClusterType.SERVICE_DISCOVER_CLUSTER);
        } else {
            eventHandler.setTargetCluster(ClusterType.BUILTIN_CLUSTER);
        }
    } else {
        eventHandler.setRefreshInterval(serviceRefreshIntervalMs);
        eventHandler.setTargetCluster(ClusterType.SERVICE_DISCOVER_CLUSTER);
    }
    return eventHandler;
}
Also used : ServerServiceInfo(com.tencent.polaris.api.plugin.compose.ServerServiceInfo) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey)

Aggregations

ServerServiceInfo (com.tencent.polaris.api.plugin.compose.ServerServiceInfo)2 PolarisException (com.tencent.polaris.api.exception.PolarisException)1 CacheHandler (com.tencent.polaris.api.plugin.registry.CacheHandler)1 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)1 NamedThreadFactory (com.tencent.polaris.client.util.NamedThreadFactory)1 IOException (java.io.IOException)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1