com.alibaba.csp.sentinel.cluster.server.config.ServerTransportConfig Java Examples

The following examples show how to use com.alibaba.csp.sentinel.cluster.server.config.ServerTransportConfig. 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: ModifyClusterServerTransportConfigHandler.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    String portValue = request.getParam("port");
    if (StringUtil.isBlank(portValue)) {
        return CommandResponse.ofFailure(new IllegalArgumentException("invalid empty port"));
    }
    String idleSecondsValue = request.getParam("idleSeconds");
    if (StringUtil.isBlank(idleSecondsValue)) {
        return CommandResponse.ofFailure(new IllegalArgumentException("invalid empty idleSeconds"));
    }
    try {
        int port = Integer.valueOf(portValue);
        int idleSeconds = Integer.valueOf(idleSecondsValue);

        ClusterServerConfigManager.loadGlobalTransportConfig(new ServerTransportConfig()
            .setPort(port).setIdleSeconds(idleSeconds));
        return CommandResponse.ofSuccess("success");
    } catch (NumberFormatException e) {
        return CommandResponse.ofFailure(new IllegalArgumentException("invalid parameter"));
    } catch (Exception ex) {
        return CommandResponse.ofFailure(new IllegalArgumentException("unexpected error"));
    }
}
 
Example #2
Source File: SentinelDefaultTokenServer.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
private synchronized void changeServerConfig(ServerTransportConfig config) {
    if (config == null || config.getPort() <= 0) {
        return;
    }
    int newPort = config.getPort();
    if (newPort == port) {
        return;
    }
    try {
        if (server != null) {
            stopServer();
        }
        this.server = new NettyTransportServer(newPort);
        this.port = newPort;
        startServerIfScheduled();
    } catch (Exception ex) {
        RecordLog.warn("[SentinelDefaultTokenServer] Failed to apply modification to token server", ex);
    }
}
 
Example #3
Source File: ScanIdleConnectionTask.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    try {
        int idleSeconds = ClusterServerConfigManager.getIdleSeconds();
        long idleTimeMillis = idleSeconds * 1000;
        if (idleTimeMillis < 0) {
            idleTimeMillis = ServerTransportConfig.DEFAULT_IDLE_SECONDS * 1000;
        }
        long now = System.currentTimeMillis();
        List<Connection> connections = connectionPool.listAllConnection();
        for (Connection conn : connections) {
            if ((now - conn.getLastReadTime()) > idleTimeMillis) {
                RecordLog.info(
                    String.format("[ScanIdleConnectionTask] The connection <%s:%d> has been idle for <%d>s. "
                        + "It will be closed now.", conn.getRemoteIP(), conn.getRemotePort(), idleSeconds)
                );
                conn.close();
            }
        }
    } catch (Throwable t) {
        RecordLog.warn("[ScanIdleConnectionTask] Failed to clean-up idle tasks", t);
    }
}
 
Example #4
Source File: ModifyClusterServerTransportConfigHandler.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    String portValue = request.getParam("port");
    if (StringUtil.isBlank(portValue)) {
        return CommandResponse.ofFailure(new IllegalArgumentException("invalid empty port"));
    }
    String idleSecondsValue = request.getParam("idleSeconds");
    if (StringUtil.isBlank(idleSecondsValue)) {
        return CommandResponse.ofFailure(new IllegalArgumentException("invalid empty idleSeconds"));
    }
    try {
        int port = Integer.valueOf(portValue);
        int idleSeconds = Integer.valueOf(idleSecondsValue);

        ClusterServerConfigManager.loadGlobalTransportConfig(new ServerTransportConfig()
            .setPort(port).setIdleSeconds(idleSeconds));
        return CommandResponse.ofSuccess("success");
    } catch (NumberFormatException e) {
        return CommandResponse.ofFailure(new IllegalArgumentException("invalid parameter"));
    } catch (Exception ex) {
        return CommandResponse.ofFailure(new IllegalArgumentException("unexpected error"));
    }
}
 
Example #5
Source File: ScanIdleConnectionTask.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    try {
        int idleSeconds = ClusterServerConfigManager.getIdleSeconds();
        long idleTimeMillis = idleSeconds * 1000;
        if (idleTimeMillis < 0) {
            idleTimeMillis = ServerTransportConfig.DEFAULT_IDLE_SECONDS * 1000;
        }
        long now = System.currentTimeMillis();
        List<Connection> connections = connectionPool.listAllConnection();
        for (Connection conn : connections) {
            if ((now - conn.getLastReadTime()) > idleTimeMillis) {
                RecordLog.info(
                    String.format("[ScanIdleConnectionTask] The connection <%s:%d> has been idle for <%d>s. "
                        + "It will be closed now.", conn.getRemoteIP(), conn.getRemotePort(), idleSeconds)
                );
                conn.close();
            }
        }
    } catch (Throwable t) {
        RecordLog.warn("[ScanIdleConnectionTask] Failed to clean-up idle tasks", t);
    }
}
 
Example #6
Source File: SentinelDefaultTokenServer.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
private synchronized void changeServerConfig(ServerTransportConfig config) {
    if (config == null || config.getPort() <= 0) {
        return;
    }
    int newPort = config.getPort();
    if (newPort == port) {
        return;
    }
    try {
        if (server != null) {
            stopServer();
        }
        this.server = new NettyTransportServer(newPort);
        this.port = newPort;
        startServerIfScheduled();
    } catch (Exception ex) {
        RecordLog.warn("[SentinelDefaultTokenServer] Failed to apply modification to token server", ex);
    }
}
 
Example #7
Source File: ClusterServer.java    From sentinel-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 加载namespace的集合以及ServerTransportConfig
 * 最好还要再为他们每个都注册一个SentinelProperty,这样的话可以动态的修改这些配置项
 */
private void loadServerConfig(){
    // 加载namespace
    ClusterServerConfigManager.loadServerNamespaceSet(Collections.singleton(APP_NAME));
    // 加载ServerTransportConfig
    ClusterServerConfigManager.loadGlobalTransportConfig(new ServerTransportConfig()
            .setIdleSeconds(600)
            .setPort(CLUSTER_SERVER_PORT));
}
 
Example #8
Source File: FetchClusterServerInfoCommandHandler.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    JSONObject info = new JSONObject();
    JSONArray connectionGroups = new JSONArray();
    Set<String> namespaceSet = ClusterServerConfigManager.getNamespaceSet();
    for (String namespace : namespaceSet) {
        ConnectionGroup group = ConnectionManager.getOrCreateConnectionGroup(namespace);
        if (group != null) {
            connectionGroups.add(group);
        }
    }

    ServerTransportConfig transportConfig = new ServerTransportConfig()
        .setPort(ClusterServerConfigManager.getPort())
        .setIdleSeconds(ClusterServerConfigManager.getIdleSeconds());
    ServerFlowConfig flowConfig = new ServerFlowConfig()
        .setExceedCount(ClusterServerConfigManager.getExceedCount())
        .setMaxOccupyRatio(ClusterServerConfigManager.getMaxOccupyRatio())
        .setIntervalMs(ClusterServerConfigManager.getIntervalMs())
        .setSampleCount(ClusterServerConfigManager.getSampleCount())
        .setMaxAllowedQps(ClusterServerConfigManager.getMaxAllowedQps());

    JSONArray requestLimitData = buildRequestLimitData(namespaceSet);

    info.fluentPut("port", ClusterServerConfigManager.getPort())
        .fluentPut("connection", connectionGroups)
        .fluentPut("requestLimitData", requestLimitData)
        .fluentPut("transport", transportConfig)
        .fluentPut("flow", flowConfig)
        .fluentPut("namespaceSet", namespaceSet)
        .fluentPut("embedded", ClusterServerConfigManager.isEmbedded());

    // Since 1.5.0 the appName is carried so that the caller can identify the appName of the token server.
    info.put("appName", AppNameUtil.getAppName());

    return CommandResponse.ofSuccess(info.toJSONString());
}
 
Example #9
Source File: FetchClusterServerConfigHandler.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private CommandResponse<String> globalConfigResult() {
    ServerTransportConfig transportConfig = new ServerTransportConfig()
        .setPort(ClusterServerConfigManager.getPort())
        .setIdleSeconds(ClusterServerConfigManager.getIdleSeconds());
    ServerFlowConfig flowConfig = new ServerFlowConfig()
        .setExceedCount(ClusterServerConfigManager.getExceedCount())
        .setMaxOccupyRatio(ClusterServerConfigManager.getMaxOccupyRatio())
        .setIntervalMs(ClusterServerConfigManager.getIntervalMs())
        .setSampleCount(ClusterServerConfigManager.getSampleCount());
    JSONObject config = new JSONObject()
        .fluentPut("transport", transportConfig)
        .fluentPut("flow", flowConfig)
        .fluentPut("namespaceSet", ClusterServerConfigManager.getNamespaceSet());
    return CommandResponse.ofSuccess(config.toJSONString());
}
 
Example #10
Source File: SentinelDefaultTokenServer.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
public SentinelDefaultTokenServer(boolean embedded) {
    this.embedded = embedded;
    ClusterServerConfigManager.addTransportConfigChangeObserver(new ServerTransportConfigObserver() {
        @Override
        public void onTransportConfigChange(ServerTransportConfig config) {
            changeServerConfig(config);
        }
    });
    initNewServer();
}
 
Example #11
Source File: ClusterServerDemo.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    // Not embedded mode by default (alone mode).
    ClusterTokenServer tokenServer = new SentinelDefaultTokenServer();

    // A sample for manually load config for cluster server.
    // It's recommended to use dynamic data source to cluster manage config and rules.
    // See the sample in DemoClusterServerInitFunc for detail.
    ClusterServerConfigManager.loadGlobalTransportConfig(new ServerTransportConfig()
        .setIdleSeconds(600)
        .setPort(11111));
    ClusterServerConfigManager.loadServerNamespaceSet(Collections.singleton(DemoConstants.APP_NAME));

    // Start the server.
    tokenServer.start();
}
 
Example #12
Source File: DemoClusterInitFunc.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void initServerTransportConfigProperty() {
    ReadableDataSource<String, ServerTransportConfig> serverTransportDs = new NacosDataSource<>(remoteAddress, groupId,
        clusterMapDataId, source -> {
        List<ClusterGroupEntity> groupList = JSON.parseObject(source, new TypeReference<List<ClusterGroupEntity>>() {});
        return Optional.ofNullable(groupList)
            .flatMap(this::extractServerTransportConfig)
            .orElse(null);
    });
    ClusterServerConfigManager.registerServerTransportProperty(serverTransportDs.getProperty());
}
 
Example #13
Source File: DemoClusterInitFunc.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private void initServerTransportConfigProperty() {
    ReadableDataSource<String, ServerTransportConfig> serverTransportDs = new NacosDataSource<>(remoteAddress, groupId,
        clusterMapDataId, source -> {
        List<ClusterGroupEntity> groupList = JSON.parseObject(source, new TypeReference<List<ClusterGroupEntity>>() {});
        return Optional.ofNullable(groupList)
            .flatMap(this::extractServerTransportConfig)
            .orElse(null);
    });
    ClusterServerConfigManager.registerServerTransportProperty(serverTransportDs.getProperty());
}
 
Example #14
Source File: ClusterServer.java    From sentinel-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 为ServerTransportConfig注册一个SentinelProperty
 * 这样的话可以动态的更改这些配置
 */
private void registerServerTransportProperty() {
    String serverTransportDataId = "cluster-server-transport-config";
    // 初始化一个配置服务端通道配置的 Nacos 数据源
    ReadableDataSource<String, ServerTransportConfig> transportConfigDs = new NacosDataSource<>(REMOTE_ADDRESS,
            GROUP_ID, serverTransportDataId,
            source -> JSON.parseObject(source, new TypeReference<ServerTransportConfig>() {}));
    ClusterServerConfigManager.registerServerTransportProperty(transportConfigDs.getProperty());
}
 
Example #15
Source File: FetchClusterServerInfoCommandHandler.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    JSONObject info = new JSONObject();
    JSONArray connectionGroups = new JSONArray();
    Set<String> namespaceSet = ClusterServerConfigManager.getNamespaceSet();
    for (String namespace : namespaceSet) {
        ConnectionGroup group = ConnectionManager.getOrCreateConnectionGroup(namespace);
        if (group != null) {
            connectionGroups.add(group);
        }
    }

    ServerTransportConfig transportConfig = new ServerTransportConfig()
        .setPort(ClusterServerConfigManager.getPort())
        .setIdleSeconds(ClusterServerConfigManager.getIdleSeconds());
    ServerFlowConfig flowConfig = new ServerFlowConfig()
        .setExceedCount(ClusterServerConfigManager.getExceedCount())
        .setMaxOccupyRatio(ClusterServerConfigManager.getMaxOccupyRatio())
        .setIntervalMs(ClusterServerConfigManager.getIntervalMs())
        .setSampleCount(ClusterServerConfigManager.getSampleCount())
        .setMaxAllowedQps(ClusterServerConfigManager.getMaxAllowedQps());

    JSONArray requestLimitData = buildRequestLimitData(namespaceSet);

    info.fluentPut("port", ClusterServerConfigManager.getPort())
        .fluentPut("connection", connectionGroups)
        .fluentPut("requestLimitData", requestLimitData)
        .fluentPut("transport", transportConfig)
        .fluentPut("flow", flowConfig)
        .fluentPut("namespaceSet", namespaceSet)
        .fluentPut("embedded", ClusterServerConfigManager.isEmbedded());

    // Since 1.5.0 the appName is carried so that the caller can identify the appName of the token server.
    info.put("appName", AppNameUtil.getAppName());

    return CommandResponse.ofSuccess(info.toJSONString());
}
 
Example #16
Source File: FetchClusterServerConfigHandler.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private CommandResponse<String> globalConfigResult() {
    ServerTransportConfig transportConfig = new ServerTransportConfig()
        .setPort(ClusterServerConfigManager.getPort())
        .setIdleSeconds(ClusterServerConfigManager.getIdleSeconds());
    ServerFlowConfig flowConfig = new ServerFlowConfig()
        .setExceedCount(ClusterServerConfigManager.getExceedCount())
        .setMaxOccupyRatio(ClusterServerConfigManager.getMaxOccupyRatio())
        .setIntervalMs(ClusterServerConfigManager.getIntervalMs())
        .setSampleCount(ClusterServerConfigManager.getSampleCount());
    JSONObject config = new JSONObject()
        .fluentPut("transport", transportConfig)
        .fluentPut("flow", flowConfig)
        .fluentPut("namespaceSet", ClusterServerConfigManager.getNamespaceSet());
    return CommandResponse.ofSuccess(config.toJSONString());
}
 
Example #17
Source File: SentinelDefaultTokenServer.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
public SentinelDefaultTokenServer(boolean embedded) {
    this.embedded = embedded;
    ClusterServerConfigManager.addTransportConfigChangeObserver(new ServerTransportConfigObserver() {
        @Override
        public void onTransportConfigChange(ServerTransportConfig config) {
            changeServerConfig(config);
        }
    });
    initNewServer();
}
 
Example #18
Source File: ClusterServerDemo.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    // Not embedded mode by default (alone mode).
    ClusterTokenServer tokenServer = new SentinelDefaultTokenServer();

    // A sample for manually load config for cluster server.
    // It's recommended to use dynamic data source to cluster manage config and rules.
    // See the sample in DemoClusterServerInitFunc for detail.
    ClusterServerConfigManager.loadGlobalTransportConfig(new ServerTransportConfig()
        .setIdleSeconds(600)
        .setPort(11111));
    ClusterServerConfigManager.loadServerNamespaceSet(Collections.singleton(DemoConstants.APP_NAME));

    // Start the server.
    tokenServer.start();
}
 
Example #19
Source File: DemoClusterInitFunc.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
private Optional<ServerTransportConfig> extractServerTransportConfig(List<ClusterGroupEntity> groupList) {
    return groupList.stream()
        .filter(this::machineEqual)
        .findAny()
        .map(e -> new ServerTransportConfig().setPort(e.getPort()).setIdleSeconds(600));
}
 
Example #20
Source File: DemoClusterInitFunc.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
private Optional<ServerTransportConfig> extractServerTransportConfig(List<ClusterGroupEntity> groupList) {
    return groupList.stream()
        .filter(this::machineEqual)
        .findAny()
        .map(e -> new ServerTransportConfig().setPort(e.getPort()).setIdleSeconds(600));
}