com.alibaba.csp.sentinel.cluster.client.config.ClusterClientConfigManager Java Examples

The following examples show how to use com.alibaba.csp.sentinel.cluster.client.config.ClusterClientConfigManager. 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: NettyTransportClient.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
private Bootstrap initClientBootstrap() {
    Bootstrap b = new Bootstrap();
    eventLoopGroup = new NioEventLoopGroup();
    b.group(eventLoopGroup)
        .channel(NioSocketChannel.class)
        .option(ChannelOption.TCP_NODELAY, true)
        .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
        .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, ClusterClientConfigManager.getConnectTimeout())
        .handler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                clientHandler = new TokenClientHandler(currentState, disconnectCallback);

                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast(new LengthFieldBasedFrameDecoder(1024, 0, 2, 0, 2));
                pipeline.addLast(new NettyResponseDecoder());
                pipeline.addLast(new LengthFieldPrepender(2));
                pipeline.addLast(new NettyRequestEncoder());
                pipeline.addLast(clientHandler);
            }
        });

    return b;
}
 
Example #2
Source File: ModifyClusterClientConfigHandler.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    String data = request.getParam("data");
    if (StringUtil.isBlank(data)) {
        return CommandResponse.ofFailure(new IllegalArgumentException("empty data"));
    }
    try {
        data = URLDecoder.decode(data, "utf-8");
        RecordLog.info("[ModifyClusterClientConfigHandler] Receiving cluster client config: " + data);
        ClusterClientStateEntity entity = JSON.parseObject(data, ClusterClientStateEntity.class);

        ClusterClientConfigManager.applyNewConfig(entity.toClientConfig());
        ClusterClientConfigManager.applyNewAssignConfig(entity.toAssignConfig());

        return CommandResponse.ofSuccess(CommandConstants.MSG_SUCCESS);
    } catch (Exception e) {
        RecordLog.warn("[ModifyClusterClientConfigHandler] Decode client cluster config error", e);
        return CommandResponse.ofFailure(e, "decode client cluster config error");
    }
}
 
Example #3
Source File: DefaultClusterTokenClient.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
private void initNewConnection() {
    if (transportClient != null) {
        return;
    }
    String host = ClusterClientConfigManager.getServerHost();
    int port = ClusterClientConfigManager.getServerPort();
    if (StringUtil.isBlank(host) || port <= 0) {
        return;
    }

    try {
        this.transportClient = new NettyTransportClient(host, port);
        this.serverDescriptor = new TokenServerDescriptor(host, port);
        RecordLog.info("[DefaultClusterTokenClient] New client created: " + serverDescriptor);
    } catch (Exception ex) {
        RecordLog.warn("[DefaultClusterTokenClient] Failed to initialize new token client", ex);
    }
}
 
Example #4
Source File: DefaultClusterTokenClient.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
private void initNewConnection() {
    if (transportClient != null) {
        return;
    }
    String host = ClusterClientConfigManager.getServerHost();
    int port = ClusterClientConfigManager.getServerPort();
    if (StringUtil.isBlank(host) || port <= 0) {
        return;
    }

    try {
        this.transportClient = new NettyTransportClient(host, port);
        this.serverDescriptor = new TokenServerDescriptor(host, port);
        RecordLog.info("[DefaultClusterTokenClient] New client created: " + serverDescriptor);
    } catch (Exception ex) {
        RecordLog.warn("[DefaultClusterTokenClient] Failed to initialize new token client", ex);
    }
}
 
Example #5
Source File: ModifyClusterClientConfigHandler.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    String data = request.getParam("data");
    if (StringUtil.isBlank(data)) {
        return CommandResponse.ofFailure(new IllegalArgumentException("empty data"));
    }
    try {
        data = URLDecoder.decode(data, "utf-8");
        RecordLog.info("[ModifyClusterClientConfigHandler] Receiving cluster client config: " + data);
        ClusterClientStateEntity entity = JSON.parseObject(data, ClusterClientStateEntity.class);

        ClusterClientConfigManager.applyNewConfig(entity.toClientConfig());
        ClusterClientConfigManager.applyNewAssignConfig(entity.toAssignConfig());

        return CommandResponse.ofSuccess(CommandConstants.MSG_SUCCESS);
    } catch (Exception e) {
        RecordLog.warn("[ModifyClusterClientConfigHandler] Decode client cluster config error", e);
        return CommandResponse.ofFailure(e, "decode client cluster config error");
    }
}
 
Example #6
Source File: NettyTransportClient.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
private Bootstrap initClientBootstrap() {
    Bootstrap b = new Bootstrap();
    eventLoopGroup = new NioEventLoopGroup();
    b.group(eventLoopGroup)
        .channel(NioSocketChannel.class)
        .option(ChannelOption.TCP_NODELAY, true)
        .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
        .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, ClusterClientConfigManager.getConnectTimeout())
        .handler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                clientHandler = new TokenClientHandler(currentState, disconnectCallback);

                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast(new LengthFieldBasedFrameDecoder(1024, 0, 2, 0, 2));
                pipeline.addLast(new NettyResponseDecoder());
                pipeline.addLast(new LengthFieldPrepender(2));
                pipeline.addLast(new NettyRequestEncoder());
                pipeline.addLast(clientHandler);
            }
        });

    return b;
}
 
Example #7
Source File: ClusterFlowClientController.java    From sentinel-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 加载集群客户端配置
 * 主要是集群服务端的相关连接信息
 */
private void loadClusterClientConfig(){
    ClusterClientAssignConfig assignConfig = new ClusterClientAssignConfig();
    assignConfig.setServerHost(CLUSTER_SERVER_HOST);
    assignConfig.setServerPort(CLUSTER_SERVER_PORT);
    ClusterClientConfigManager.applyNewAssignConfig(assignConfig);

    ClusterClientConfig clientConfig = new ClusterClientConfig();
    clientConfig.setRequestTimeout(REQUEST_TIME_OUT);
    ClusterClientConfigManager.applyNewConfig(clientConfig);
}
 
Example #8
Source File: FetchClusterClientConfigHandler.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    ClusterClientStateEntity stateVO = new ClusterClientStateEntity()
        .setServerHost(ClusterClientConfigManager.getServerHost())
        .setServerPort(ClusterClientConfigManager.getServerPort())
        .setRequestTimeout(ClusterClientConfigManager.getRequestTimeout());
    if (TokenClientProvider.isClientSpiAvailable()) {
        stateVO.setClientState(TokenClientProvider.getClient().getState());
    } else {
        stateVO.setClientState(ClientConstants.CLIENT_STATUS_OFF);
    }
    return CommandResponse.ofSuccess(JSON.toJSONString(stateVO));
}
 
Example #9
Source File: DefaultClusterTokenClient.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
public DefaultClusterTokenClient() {
    ClusterClientConfigManager.addServerChangeObserver(new ServerChangeObserver() {
        @Override
        public void onRemoteServerChange(ClusterClientAssignConfig assignConfig) {
            changeServer(assignConfig);
        }
    });
    initNewConnection();
}
 
Example #10
Source File: NettyTransportClient.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public ClusterResponse sendRequest(ClusterRequest request) throws Exception {
    if (!isReady()) {
        throw new SentinelClusterException(ClusterErrorMessages.CLIENT_NOT_READY);
    }
    if (!validRequest(request)) {
        throw new SentinelClusterException(ClusterErrorMessages.BAD_REQUEST);
    }
    int xid = getCurrentId();
    try {
        request.setId(xid);

        channel.writeAndFlush(request);

        ChannelPromise promise = channel.newPromise();
        TokenClientPromiseHolder.putPromise(xid, promise);

        if (!promise.await(ClusterClientConfigManager.getRequestTimeout())) {
            throw new SentinelClusterException(ClusterErrorMessages.REQUEST_TIME_OUT);
        }

        SimpleEntry<ChannelPromise, ClusterResponse> entry = TokenClientPromiseHolder.getEntry(xid);
        if (entry == null || entry.getValue() == null) {
            // Should not go through here.
            throw new SentinelClusterException(ClusterErrorMessages.UNEXPECTED_STATUS);
        }
        return entry.getValue();
    } finally {
        TokenClientPromiseHolder.remove(xid);
    }
}
 
Example #11
Source File: DemoClusterInitFunc.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void initClientServerAssignProperty() {
    // Cluster map format:
    // [{"clientSet":["112.12.88.66@8729","112.12.88.67@8727"],"ip":"112.12.88.68","machineId":"112.12.88.68@8728","port":11111}]
    // machineId: <ip@commandPort>, commandPort for port exposed to Sentinel dashboard (transport module)
    ReadableDataSource<String, ClusterClientAssignConfig> clientAssignDs = new NacosDataSource<>(remoteAddress, groupId,
        clusterMapDataId, source -> {
        List<ClusterGroupEntity> groupList = JSON.parseObject(source, new TypeReference<List<ClusterGroupEntity>>() {});
        return Optional.ofNullable(groupList)
            .flatMap(this::extractClientAssignment)
            .orElse(null);
    });
    ClusterClientConfigManager.registerServerAssignProperty(clientAssignDs.getProperty());
}
 
Example #12
Source File: ClusterFlowClientController.java    From sentinel-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 为ClusterClientConfig注册一个SentinelProperty
 * 这样的话可以动态的更改这些配置
 */
private void registerClusterClientProperty() {
    String clientConfigDataId = "cluster-client-config";
    // 初始化一个配置ClusterClientConfig的 Nacos 数据源
    ReadableDataSource<String, ClusterClientConfig> clientConfigDS = new NacosDataSource<>(REMOTE_ADDRESS, GROUP_ID, clientConfigDataId,
            source -> JSON.parseObject(source, new TypeReference<ClusterClientConfig>() {}));
    ClusterClientConfigManager.registerClientConfigProperty(clientConfigDS.getProperty());

    String clientAssignConfigDataId = "cluster-client-assign-config";
    // 初始化一个配置ClusterClientAssignConfig的 Nacos 数据源
    ReadableDataSource<String, ClusterClientAssignConfig> clientAssignConfigDS = new NacosDataSource<>(REMOTE_ADDRESS, GROUP_ID, clientAssignConfigDataId,
            source -> JSON.parseObject(source, new TypeReference<ClusterClientAssignConfig>() {}));
    ClusterClientConfigManager.registerServerAssignProperty(clientAssignConfigDS.getProperty());

}
 
Example #13
Source File: ClusterFlowClientController.java    From sentinel-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 为ClusterClientConfig注册一个SentinelProperty
 * 这样的话可以动态的更改这些配置
 */
private void registerClusterClientProperty() {
    String clientConfigDataId = "cluster-client-config";
    // 初始化一个配置ClusterClientConfig的 Nacos 数据源
    ReadableDataSource<String, ClusterClientConfig> clientConfigDS = new NacosDataSource<>(REMOTE_ADDRESS, GROUP_ID, clientConfigDataId,
            source -> JSON.parseObject(source, new TypeReference<ClusterClientConfig>() {}));
    ClusterClientConfigManager.registerClientConfigProperty(clientConfigDS.getProperty());

    String clientAssignConfigDataId = "cluster-client-assign-config";
    // 初始化一个配置ClusterClientAssignConfig的 Nacos 数据源
    ReadableDataSource<String, ClusterClientAssignConfig> clientAssignConfigDS = new NacosDataSource<>(REMOTE_ADDRESS, GROUP_ID, clientAssignConfigDataId,
            source -> JSON.parseObject(source, new TypeReference<ClusterClientAssignConfig>() {}));
    ClusterClientConfigManager.registerServerAssignProperty(clientAssignConfigDS.getProperty());
}
 
Example #14
Source File: ClusterFlowClientController.java    From sentinel-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 加载集群客户端配置
 * 主要是集群服务端的相关连接信息
 */
private void loadClusterClientConfig(){
    ClusterClientAssignConfig assignConfig = new ClusterClientAssignConfig();
    assignConfig.setServerHost(CLUSTER_SERVER_HOST);
    assignConfig.setServerPort(CLUSTER_SERVER_PORT);
    ClusterClientConfigManager.applyNewAssignConfig(assignConfig);

    ClusterClientConfig clientConfig = new ClusterClientConfig();
    clientConfig.setRequestTimeout(REQUEST_TIME_OUT);
    ClusterClientConfigManager.applyNewConfig(clientConfig);
}
 
Example #15
Source File: FetchClusterClientConfigHandler.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    ClusterClientStateEntity stateVO = new ClusterClientStateEntity()
        .setServerHost(ClusterClientConfigManager.getServerHost())
        .setServerPort(ClusterClientConfigManager.getServerPort())
        .setRequestTimeout(ClusterClientConfigManager.getRequestTimeout());
    if (TokenClientProvider.isClientSpiAvailable()) {
        stateVO.setClientState(TokenClientProvider.getClient().getState());
    } else {
        stateVO.setClientState(ClientConstants.CLIENT_STATUS_OFF);
    }
    return CommandResponse.ofSuccess(JSON.toJSONString(stateVO));
}
 
Example #16
Source File: DefaultClusterTokenClient.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
public DefaultClusterTokenClient() {
    ClusterClientConfigManager.addServerChangeObserver(new ServerChangeObserver() {
        @Override
        public void onRemoteServerChange(ClusterClientAssignConfig assignConfig) {
            changeServer(assignConfig);
        }
    });
    initNewConnection();
}
 
Example #17
Source File: NettyTransportClient.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Override
public ClusterResponse sendRequest(ClusterRequest request) throws Exception {
    if (!isReady()) {
        throw new SentinelClusterException(ClusterErrorMessages.CLIENT_NOT_READY);
    }
    if (!validRequest(request)) {
        throw new SentinelClusterException(ClusterErrorMessages.BAD_REQUEST);
    }
    int xid = getCurrentId();
    try {
        request.setId(xid);

        channel.writeAndFlush(request);

        ChannelPromise promise = channel.newPromise();
        TokenClientPromiseHolder.putPromise(xid, promise);

        if (!promise.await(ClusterClientConfigManager.getRequestTimeout())) {
            throw new SentinelClusterException(ClusterErrorMessages.REQUEST_TIME_OUT);
        }

        SimpleEntry<ChannelPromise, ClusterResponse> entry = TokenClientPromiseHolder.getEntry(xid);
        if (entry == null || entry.getValue() == null) {
            // Should not go through here.
            throw new SentinelClusterException(ClusterErrorMessages.UNEXPECTED_STATUS);
        }
        return entry.getValue();
    } finally {
        TokenClientPromiseHolder.remove(xid);
    }
}
 
Example #18
Source File: DemoClusterInitFunc.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private void initClientServerAssignProperty() {
    // Cluster map format:
    // [{"clientSet":["112.12.88.66@8729","112.12.88.67@8727"],"ip":"112.12.88.68","machineId":"112.12.88.68@8728","port":11111}]
    // machineId: <ip@commandPort>, commandPort for port exposed to Sentinel dashboard (transport module)
    ReadableDataSource<String, ClusterClientAssignConfig> clientAssignDs = new NacosDataSource<>(remoteAddress, groupId,
        clusterMapDataId, source -> {
        List<ClusterGroupEntity> groupList = JSON.parseObject(source, new TypeReference<List<ClusterGroupEntity>>() {});
        return Optional.ofNullable(groupList)
            .flatMap(this::extractClientAssignment)
            .orElse(null);
    });
    ClusterClientConfigManager.registerServerAssignProperty(clientAssignDs.getProperty());
}
 
Example #19
Source File: DemoClusterInitFunc.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
private void initClientConfigProperty() {
    ReadableDataSource<String, ClusterClientConfig> clientConfigDs = new NacosDataSource<>(remoteAddress, groupId,
        configDataId, source -> JSON.parseObject(source, new TypeReference<ClusterClientConfig>() {}));
    ClusterClientConfigManager.registerClientConfigProperty(clientConfigDs.getProperty());
}
 
Example #20
Source File: DemoClusterInitFunc.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
private void initClientConfigProperty() {
    ReadableDataSource<String, ClusterClientConfig> clientConfigDs = new NacosDataSource<>(remoteAddress, groupId,
        configDataId, source -> JSON.parseObject(source, new TypeReference<ClusterClientConfig>() {}));
    ClusterClientConfigManager.registerClientConfigProperty(clientConfigDs.getProperty());
}