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

The following examples show how to use com.alipay.sofa.rpc.config.ServerConfig#setProtocol() . 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: 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 2
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 3
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 4
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 5
Source File: ServerConfigContainer.java    From sofa-rpc-boot-projects with Apache License 2.0 4 votes vote down vote up
/**
 * 创建 rest ServerConfig。rest 的 配置需要外层 starter 设置默认值。
 *
 * @return rest ServerConfig
 */
ServerConfig createRestServerConfig() {
    String hostName = sofaBootRpcProperties.getRestHostname();
    String portStr = sofaBootRpcProperties.getRestPort();
    String ioThreadSizeStr = sofaBootRpcProperties.getRestIoThreadSize();
    String contextPath = sofaBootRpcProperties.getRestContextPath();
    String restThreadPoolMaxSizeStr = sofaBootRpcProperties.getRestThreadPoolMaxSize();
    String maxRequestSizeStr = sofaBootRpcProperties.getRestMaxRequestSize();
    String telnetStr = sofaBootRpcProperties.getRestTelnet();
    String daemonStr = sofaBootRpcProperties.getRestDaemon();

    String allowedOrigins = sofaBootRpcProperties.getRestAllowedOrigins();
    int port;
    int ioThreadCount;
    int restThreadPoolMaxSize;
    int maxRequestSize;
    boolean telnet;
    boolean daemon;

    if (!StringUtils.hasText(hostName)) {
        hostName = null;
    }

    if (!StringUtils.hasText(portStr)) {
        port = SofaBootRpcConfigConstants.REST_PORT_DEFAULT;
    } else {
        port = Integer.parseInt(portStr);
    }

    if (!StringUtils.hasText(ioThreadSizeStr)) {
        ioThreadCount = SofaBootRpcConfigConstants.REST_IO_THREAD_COUNT_DEFAULT;
    } else {
        ioThreadCount = Integer.parseInt(ioThreadSizeStr);
    }

    if (!StringUtils.hasText(restThreadPoolMaxSizeStr)) {
        restThreadPoolMaxSize = SofaBootRpcConfigConstants.REST_EXECUTOR_THREAD_COUNT_DEFAULT;
    } else {
        restThreadPoolMaxSize = Integer.parseInt(restThreadPoolMaxSizeStr);
    }

    if (!StringUtils.hasText(maxRequestSizeStr)) {
        maxRequestSize = SofaBootRpcConfigConstants.REST_MAX_REQUEST_SIZE_DEFAULT;
    } else {
        maxRequestSize = Integer.parseInt(maxRequestSizeStr);
    }

    if (!StringUtils.hasText(telnetStr)) {
        telnet = SofaBootRpcConfigConstants.REST_TELNET_DEFAULT;
    } else {
        telnet = Boolean.parseBoolean(telnetStr);
    }

    if (!StringUtils.hasText(daemonStr)) {
        daemon = SofaBootRpcConfigConstants.REST_DAEMON_DEFAULT;
    } else {
        daemon = Boolean.parseBoolean(daemonStr);
    }

    Map<String, String> parameters = new HashMap<String, String>();

    if (StringUtils.hasText(allowedOrigins)) {
        parameters.put(RpcConstants.ALLOWED_ORIGINS, allowedOrigins);
    }

    ServerConfig serverConfig = new ServerConfig()
        .setPort(port)
        .setIoThreads(ioThreadCount)
        .setMaxThreads(restThreadPoolMaxSize)
        .setPayload(maxRequestSize)
        .setTelnet(telnet)
        .setDaemon(daemon)
        .setParameters(parameters);

    if (!StringUtils.isEmpty(contextPath)) {
        serverConfig.setContextPath(contextPath);
    }

    serverConfig.setAutoStart(false);
    serverConfig.setProtocol(SofaBootRpcConfigConstants.RPC_PROTOCOL_REST);
    addCommonServerConfig(serverConfig);

    serverConfig.setBoundHost(hostName);

    return serverConfig;
}
 
Example 6
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 7
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 8
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() {

        }
    });
}