Search in sources :

Example 1 with Server

use of com.alipay.sofa.rpc.server.Server in project hugegraph-common by hugegraph.

the class RpcServer method port.

public int port() {
    this.checkEnabled();
    Server server = this.serverConfig.getServer();
    if (server instanceof BoltServer && server.isStarted()) {
        /*
             * When using random port 0, try to fetch the actual port
             * NOTE: RemotingServer.port() would return the actual port only
             *       if sofa-bolt version >= 1.6.1, please see:
             *       https://github.com/sofastack/sofa-bolt/issues/196
             * TODO: remove this code after adding Server.port() interface:
             *       https://github.com/sofastack/sofa-rpc/issues/1022
             */
        RemotingServer rs = Whitebox.getInternalState(server, "remotingServer");
        return rs.port();
    }
    // When using random port 0, the returned port is not the actual port
    return this.serverConfig.getPort();
}
Also used : BoltServer(com.alipay.sofa.rpc.server.bolt.BoltServer) RemotingServer(com.alipay.remoting.RemotingServer) Server(com.alipay.sofa.rpc.server.Server) RemotingServer(com.alipay.remoting.RemotingServer) BoltServer(com.alipay.sofa.rpc.server.bolt.BoltServer)

Example 2 with Server

use of com.alipay.sofa.rpc.server.Server in project sofa-rpc by sofastack.

the class DefaultProviderBootstrap method doExport.

private void doExport() {
    if (exported) {
        return;
    }
    // 检查参数
    checkParameters();
    String appName = providerConfig.getAppName();
    // key  is the protocol of server,for concurrent safe
    Map<String, Boolean> hasExportedInCurrent = new ConcurrentHashMap<String, Boolean>();
    // 将处理器注册到server
    List<ServerConfig> serverConfigs = providerConfig.getServer();
    for (ServerConfig serverConfig : serverConfigs) {
        String protocol = serverConfig.getProtocol();
        String key = providerConfig.buildKey() + ":" + protocol;
        if (LOGGER.isInfoEnabled(appName)) {
            LOGGER.infoWithApp(appName, "Export provider config : {} with bean id {}", key, providerConfig.getId());
        }
        // 注意同一interface,同一uniqueId,不同server情况
        // 计数器
        AtomicInteger cnt = EXPORTED_KEYS.get(key);
        if (cnt == null) {
            // 没有发布过
            cnt = CommonUtils.putToConcurrentMap(EXPORTED_KEYS, key, new AtomicInteger(0));
        }
        int c = cnt.incrementAndGet();
        hasExportedInCurrent.put(serverConfig.getProtocol(), true);
        int maxProxyCount = providerConfig.getRepeatedExportLimit();
        if (maxProxyCount > 0) {
            if (c > maxProxyCount) {
                decrementCounter(hasExportedInCurrent);
                // 超过最大数量,直接抛出异常
                throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_DUPLICATE_PROVIDER_CONFIG, key, maxProxyCount));
            } else if (c > 1) {
                if (LOGGER.isInfoEnabled(appName)) {
                    LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.WARN_DUPLICATE_PROVIDER_CONFIG, key, c));
                }
            }
        }
    }
    try {
        // 构造请求调用器
        providerProxyInvoker = new ProviderProxyInvoker(providerConfig);
        preProcessProviderTarget(providerConfig, (ProviderProxyInvoker) providerProxyInvoker);
        // 初始化注册中心
        if (providerConfig.isRegister()) {
            List<RegistryConfig> registryConfigs = providerConfig.getRegistry();
            if (CommonUtils.isNotEmpty(registryConfigs)) {
                for (RegistryConfig registryConfig : registryConfigs) {
                    // 提前初始化Registry
                    RegistryFactory.getRegistry(registryConfig);
                }
            }
        }
        // 将处理器注册到server
        for (ServerConfig serverConfig : serverConfigs) {
            try {
                Server server = serverConfig.buildIfAbsent();
                // 注册请求调用器
                server.registerProcessor(providerConfig, providerProxyInvoker);
                if (serverConfig.isAutoStart()) {
                    server.start();
                }
            } catch (SofaRpcRuntimeException e) {
                throw e;
            } catch (Exception e) {
                LOGGER.errorWithApp(appName, LogCodes.getLog(LogCodes.ERROR_REGISTER_PROCESSOR_TO_SERVER, serverConfig.getId()), e);
            }
        }
        // 注册到注册中心
        providerConfig.setConfigListener(new ProviderAttributeListener());
        register();
    } catch (Exception e) {
        decrementCounter(hasExportedInCurrent);
        if (e instanceof SofaRpcRuntimeException) {
            throw e;
        }
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_BUILD_PROVIDER_PROXY), e);
    }
    // 记录一些缓存数据
    RpcRuntimeContext.cacheProviderConfig(this);
    exported = true;
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) Server(com.alipay.sofa.rpc.server.Server) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) ProviderProxyInvoker(com.alipay.sofa.rpc.server.ProviderProxyInvoker) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with Server

use of com.alipay.sofa.rpc.server.Server in project sofa-rpc by sofastack.

the class DefaultProviderBootstrap method unExport.

@Override
public void unExport() {
    if (!exported) {
        return;
    }
    synchronized (this) {
        if (!exported) {
            return;
        }
        String appName = providerConfig.getAppName();
        List<ServerConfig> serverConfigs = providerConfig.getServer();
        for (ServerConfig serverConfig : serverConfigs) {
            String protocol = serverConfig.getProtocol();
            String key = providerConfig.buildKey() + ":" + protocol;
            if (LOGGER.isInfoEnabled(appName)) {
                LOGGER.infoWithApp(appName, "Unexport provider config : {} {}", key, providerConfig.getId() != null ? "with bean id " + providerConfig.getId() : "");
            }
        }
        // 取消注册到注册中心
        unregister();
        providerProxyInvoker = null;
        // 取消将处理器注册到server
        if (serverConfigs != null) {
            for (ServerConfig serverConfig : serverConfigs) {
                Server server = serverConfig.getServer();
                if (server != null) {
                    try {
                        server.unRegisterProcessor(providerConfig, serverConfig.isAutoStart());
                    } catch (Exception e) {
                        if (LOGGER.isWarnEnabled(appName)) {
                            // TODO WARN
                            LOGGER.warnWithApp(appName, "Catch exception when unRegister processor to server: " + serverConfig.getId() + ", but you can ignore if it's called by JVM shutdown hook", e);
                        }
                    }
                }
            }
        }
        providerConfig.setConfigListener(null);
        // 清除缓存状态
        for (ServerConfig serverConfig : serverConfigs) {
            String protocol = serverConfig.getProtocol();
            String key = providerConfig.buildKey() + ":" + protocol;
            AtomicInteger cnt = EXPORTED_KEYS.get(key);
            if (cnt != null && cnt.decrementAndGet() <= 0) {
                EXPORTED_KEYS.remove(key);
            }
        }
        RpcRuntimeContext.invalidateProviderConfig(this);
        exported = false;
    }
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Server(com.alipay.sofa.rpc.server.Server) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Example 4 with Server

use of com.alipay.sofa.rpc.server.Server in project sofa-rpc by sofastack.

the class TripleServerTest method testBizThreadPool.

@Test
public void testBizThreadPool() {
    ServerConfig serverConfig = new ServerConfig();
    serverConfig.setProtocol("tri");
    serverConfig.setPort(50055);
    serverConfig.buildIfAbsent().start();
    Server server = serverConfig.getServer();
    Assert.assertTrue(server instanceof TripleServer);
    ThreadPoolExecutor bizThreadPool = ((TripleServer) server).getBizThreadPool();
    Assert.assertNotNull(bizThreadPool);
    server.stop();
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Server(com.alipay.sofa.rpc.server.Server) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Test(org.junit.Test)

Aggregations

Server (com.alipay.sofa.rpc.server.Server)4 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)3 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 RemotingServer (com.alipay.remoting.RemotingServer)1 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)1 ProviderProxyInvoker (com.alipay.sofa.rpc.server.ProviderProxyInvoker)1 BoltServer (com.alipay.sofa.rpc.server.bolt.BoltServer)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 Test (org.junit.Test)1