Java Code Examples for com.alipay.sofa.rpc.config.ServerConfig#setPort()

The following examples show how to use com.alipay.sofa.rpc.config.ServerConfig#setPort() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServerConfigContainerTest.java    From sofa-rpc-boot-projects with Apache License 2.0 6 votes vote down vote up
@Test
public void testCustomServerConfig() {

    final ServerConfig serverConfig = new ServerConfig();
    serverConfig.setPort(123);
    final String protocol = "xxx";
    serverConfigContainer.registerCustomServerConfig(protocol, serverConfig);

    ServerConfig serverConfig2 = serverConfigContainer.getServerConfig(protocol);

    Assert.assertEquals(123, serverConfig2.getPort());
    Assert.assertEquals(serverConfig.getPort(), serverConfig2.getPort());

    boolean result = false;
    serverConfigContainer.unRegisterCustomServerConfig(protocol);
    try {
        serverConfigContainer.getServerConfig(protocol);

    } catch (Exception e) {
        Assert.assertTrue(e instanceof SofaBootRpcRuntimeException);
        result = true;
    }

    Assert.assertTrue(result);

}
 
Example 2
Source File: ServerConfigContainer.java    From sofa-rpc-boot-projects with Apache License 2.0 5 votes vote down vote up
/**
 * 创建 h2c ServerConfig。rest 的 配置不需要外层 starter 设置默认值。
 *
 * @return H2c 的服务端配置信息
 */
ServerConfig createH2cServerConfig() {
    String portStr = sofaBootRpcProperties.getH2cPort();
    String h2cThreadPoolCoreSizeStr = sofaBootRpcProperties.getH2cThreadPoolCoreSize();
    String h2cThreadPoolMaxSizeStr = sofaBootRpcProperties.getH2cThreadPoolMaxSize();
    String acceptsSizeStr = sofaBootRpcProperties.getH2cAcceptsSize();
    String h2cThreadPoolQueueSizeStr = sofaBootRpcProperties.getH2cThreadPoolQueueSize();

    ServerConfig serverConfig = new ServerConfig();

    if (StringUtils.hasText(portStr)) {
        serverConfig.setPort(Integer.parseInt(portStr));
    } else {
        serverConfig.setPort(SofaBootRpcConfigConstants.H2C_PORT_DEFAULT);
    }

    if (StringUtils.hasText(h2cThreadPoolMaxSizeStr)) {
        serverConfig.setMaxThreads(Integer.parseInt(h2cThreadPoolMaxSizeStr));
    }

    if (StringUtils.hasText(h2cThreadPoolCoreSizeStr)) {
        serverConfig.setCoreThreads(Integer.parseInt(h2cThreadPoolCoreSizeStr));
    }

    if (StringUtils.hasText(acceptsSizeStr)) {
        serverConfig.setAccepts(Integer.parseInt(acceptsSizeStr));
    }

    if (StringUtils.hasText(h2cThreadPoolQueueSizeStr)) {
        serverConfig.setQueues(Integer.parseInt(h2cThreadPoolQueueSizeStr));
    }

    serverConfig.setAutoStart(false);
    return serverConfig.setProtocol(SofaBootRpcConfigConstants.RPC_PROTOCOL_H2C);
}
 
Example 3
Source File: ServerConfigContainer.java    From sofa-rpc-boot-projects with Apache License 2.0 5 votes vote down vote up
/**
 * 创建 bolt ServerConfig。rest 的 配置不需要外层 starter 设置默认值。
 *
 * @return Bolt 的服务端配置信息
 */
ServerConfig createBoltServerConfig() {
    String portStr = sofaBootRpcProperties.getBoltPort();
    String boltThreadPoolCoreSizeStr = sofaBootRpcProperties.getBoltThreadPoolCoreSize();
    String boltThreadPoolMaxSizeStr = sofaBootRpcProperties.getBoltThreadPoolMaxSize();
    String acceptsSizeStr = sofaBootRpcProperties.getBoltAcceptsSize();
    String boltThreadPoolQueueSizeStr = sofaBootRpcProperties.getBoltThreadPoolQueueSize();

    ServerConfig serverConfig = new ServerConfig();

    if (StringUtils.hasText(portStr)) {
        serverConfig.setPort(Integer.parseInt(portStr));
    } else {
        serverConfig.setPort(SofaBootRpcConfigConstants.BOLT_PORT_DEFAULT);
    }

    if (StringUtils.hasText(boltThreadPoolMaxSizeStr)) {
        serverConfig.setMaxThreads(Integer.parseInt(boltThreadPoolMaxSizeStr));
    }

    if (StringUtils.hasText(boltThreadPoolCoreSizeStr)) {
        serverConfig.setCoreThreads(Integer.parseInt(boltThreadPoolCoreSizeStr));
    }

    if (StringUtils.hasText(acceptsSizeStr)) {
        serverConfig.setAccepts(Integer.parseInt(acceptsSizeStr));
    }

    if (StringUtils.hasText(boltThreadPoolQueueSizeStr)) {
        serverConfig.setQueues(Integer.parseInt(boltThreadPoolQueueSizeStr));
    }

    serverConfig.setAutoStart(false);
    serverConfig.setProtocol(SofaBootRpcConfigConstants.RPC_PROTOCOL_BOLT);

    addCommonServerConfig(serverConfig);

    return serverConfig;
}
 
Example 4
Source File: ServerConfigContainer.java    From sofa-rpc-boot-projects with Apache License 2.0 5 votes vote down vote up
/**
 * 创建 dubbo ServerConfig。会设置 Dubbo 的默认端口,其余配置不会由外层 Starter 设置默认值。
 *
 * @return dubbo ServerConfig
 */
ServerConfig createDubboServerConfig() {
    String portStr = sofaBootRpcProperties.getDubboPort();
    String ioThreadSizeStr = sofaBootRpcProperties.getDubboIoThreadSize();
    String dubboThreadPoolMaxSizeStr = sofaBootRpcProperties.getDubboThreadPoolMaxSize();
    String dubboAcceptsSizeStr = sofaBootRpcProperties.getDubboAcceptsSize();

    ServerConfig serverConfig = new ServerConfig();

    if (StringUtils.hasText(portStr)) {
        serverConfig.setPort(Integer.parseInt(portStr));
    } else {
        serverConfig.setPort(SofaBootRpcConfigConstants.DUBBO_PORT_DEFAULT);
    }

    if (StringUtils.hasText(ioThreadSizeStr)) {
        serverConfig.setIoThreads(Integer.parseInt(ioThreadSizeStr));
    }

    if (StringUtils.hasText(dubboThreadPoolMaxSizeStr)) {
        serverConfig.setMaxThreads(Integer.parseInt(dubboThreadPoolMaxSizeStr));
    }

    if (StringUtils.hasText(dubboAcceptsSizeStr)) {
        serverConfig.setAccepts(Integer.parseInt(dubboAcceptsSizeStr));
    }

    serverConfig.setAutoStart(false);
    serverConfig.setProtocol(SofaBootRpcConfigConstants.RPC_PROTOCOL_DUBBO);

    addCommonServerConfig(serverConfig);

    return serverConfig;

}
 
Example 5
Source File: RestServerTest.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Test
public void startAllowedOrigins() {
    String host = "127.0.0.1";
    int port = 18801;
    ServerConfig serverConfig = new ServerConfig();
    serverConfig.setBoundHost(host);
    serverConfig.setPort(port);
    serverConfig.setProtocol(RpcConstants.PROTOCOL_TYPE_REST);
    Map<String, String> map = new HashMap<String, String>();
    final String ip1 = "http://127.0.0.1";
    final String ip2 = "http://127.0.0.2";
    map.put(RpcConstants.ALLOWED_ORIGINS, ip1 + "," + ip2 + ",");
    serverConfig.setParameters(map);
    RestServer server = new RestServer();
    server.init(serverConfig);
    server.start();
    Assert.assertTrue(server.started);
    Assert.assertTrue(NetUtils.canTelnet(host, port, 1000));

    server.stop();

    //主要是判断一下filter加进去了没有

    Set<Object> filter = JAXRSProviderManager.getCustomProviderInstances();
    CorsFilter corsFilter = (CorsFilter) filter.iterator().next();
    Set<String> allows = corsFilter.getAllowedOrigins();
    Assert.assertEquals(2, allows.size());
    Assert.assertTrue(allows.contains(ip1));
    Assert.assertTrue(allows.contains(ip2));
}
 
Example 6
Source File: ServerFactory.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 确定下Server的host和port
 *
 * @param serverConfig 服务器配置
 */
private static void resolveServerConfig(ServerConfig serverConfig) {
    // 绑定到指定网卡 或全部网卡
    String boundHost = serverConfig.getBoundHost();
    if (boundHost == null) {
        String host = serverConfig.getHost();
        if (StringUtils.isBlank(host)) {
            host = SystemInfo.getLocalHost();
            serverConfig.setHost(host);
            // windows绑定到0.0.0.0的某个端口以后,其它进程还能绑定到该端口
            boundHost = SystemInfo.isWindows() ? host : NetUtils.ANYHOST;
        } else {
            boundHost = host;
        }
        serverConfig.setBoundHost(boundHost);
    }

    // 绑定的端口
    if (serverConfig.isAdaptivePort()) {
        int oriPort = serverConfig.getPort();
        int port = NetUtils.getAvailablePort(boundHost, oriPort,
            RpcConfigs.getIntValue(RpcOptions.SERVER_PORT_END));
        if (port != oriPort) {
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("Changed port from {} to {} because the config port is disabled", oriPort, port);
            }
            serverConfig.setPort(port);
        }
    }
}
 
Example 7
Source File: ServerConfigContainerTest.java    From sofa-rpc-boot-projects with Apache License 2.0 4 votes vote down vote up
@Test
public void testCustomServerConfigTwice() {

    final ServerConfig serverConfig = new ServerConfig();
    serverConfig.setPort(123);
    final String protocol = "xxx";
    serverConfigContainer.registerCustomServerConfig(protocol, serverConfig);

    ServerConfig serverConfig2 = serverConfigContainer.getServerConfig(protocol);

    Assert.assertEquals(123, serverConfig2.getPort());
    Assert.assertEquals(serverConfig.getPort(), serverConfig2.getPort());

    boolean twiceResult = serverConfigContainer.registerCustomServerConfig(protocol, serverConfig);

    Assert.assertFalse(twiceResult);

}
 
Example 8
Source File: Http2ClearTextServerTest.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Test
public void start() throws InterruptedException {

    String host = "127.0.0.1";
    int port = 17701;
    ServerConfig serverConfig = new ServerConfig();
    serverConfig.setBoundHost(host);
    serverConfig.setPort(port);
    serverConfig.setProtocol(RpcConstants.PROTOCOL_TYPE_H2C);

    Http2ClearTextServer server = new Http2ClearTextServer();
    server.init(serverConfig);
    server.start();
    Assert.assertTrue(server.started);
    Assert.assertTrue(NetUtils.canTelnet(host, port, 1000));

    // start use bound port will throw exception
    ServerConfig serverConfig2 = new ServerConfig();
    serverConfig2.setBoundHost(host);
    serverConfig2.setPort(port);
    serverConfig2.setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT);
    Http2ClearTextServer server2 = new Http2ClearTextServer();
    server2.init(serverConfig2);
    boolean error = false;
    try {
        server2.start();
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);

    server.stop();
    Assert.assertFalse(server.started);
    Thread.sleep(1000); // 升级bolt后删除此行
    Assert.assertFalse(NetUtils.canTelnet(host, port, 1000));

    server.start();
    Assert.assertTrue(server.started);
    Assert.assertTrue(NetUtils.canTelnet(host, port, 1000));

    server.stop();
    Assert.assertFalse(server.started);
    Thread.sleep(1000); // 升级bolt后删除此行
    Assert.assertFalse(NetUtils.canTelnet(host, port, 1000));

    server.destroy();
}
 
Example 9
Source File: BoltServerTest.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Test
public void start() throws Exception {
    String host = "127.0.0.1";
    int port = 17701;
    ServerConfig serverConfig = new ServerConfig();
    serverConfig.setBoundHost(host);
    serverConfig.setPort(port);
    serverConfig.setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT);

    BoltServer server = new BoltServer();
    server.init(serverConfig);
    server.start();
    Assert.assertTrue(server.started);
    Assert.assertTrue(NetUtils.canTelnet(host, port, 1000));

    // start use bound port will throw exception
    ServerConfig serverConfig2 = new ServerConfig();
    serverConfig2.setBoundHost(host);
    serverConfig2.setPort(port);
    serverConfig2.setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT);
    BoltServer server2 = new BoltServer();
    server2.init(serverConfig2);
    boolean error = false;
    try {
        server2.start();
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);

    server.stop();
    Assert.assertFalse(server.started);
    Thread.sleep(1000); // 升级bolt后删除此行
    Assert.assertFalse(NetUtils.canTelnet(host, port, 1000));

    server.start();
    Assert.assertTrue(server.started);
    Assert.assertTrue(NetUtils.canTelnet(host, port, 1000));

    server.stop();
    Assert.assertFalse(server.started);
    Thread.sleep(1000); // 升级bolt后删除此行
    Assert.assertFalse(NetUtils.canTelnet(host, port, 1000));

    server.destroy();
}
 
Example 10
Source File: RestServerTest.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Test
public void start() {
    String host = "127.0.0.1";
    int port = 18801;
    ServerConfig serverConfig = new ServerConfig();
    serverConfig.setBoundHost(host);
    serverConfig.setPort(port);
    serverConfig.setProtocol(RpcConstants.PROTOCOL_TYPE_REST);

    RestServer server = new RestServer();
    server.init(serverConfig);
    server.start();
    Assert.assertTrue(server.started);
    Assert.assertTrue(NetUtils.canTelnet(host, port, 1000));
    // 重复启动
    server.start();

    server.stop();
    Assert.assertFalse(server.started);
    Assert.assertFalse(NetUtils.canTelnet(host, port, 1000));
    // 重复关闭
    server.stop();

    // 销毁
    server.init(serverConfig);
    server.start();
    Assert.assertTrue(server.started);
    Assert.assertTrue(NetUtils.canTelnet(host, port, 1000));
    server.destroy(null);

    // 销毁
    server.init(serverConfig);
    server.start();
    Assert.assertTrue(server.started);
    Assert.assertTrue(NetUtils.canTelnet(host, port, 1000));
    server.destroy(new Destroyable.DestroyHook() {
        @Override
        public void preDestroy() {

        }

        @Override
        public void postDestroy() {

        }
    });
}