gpt4 book ai didi

5、Sentinel 教程进阶 setToClient()

转载 作者:大佬之路 更新时间:2024-01-21 22:27:26 29 4
gpt4 key购买 nike

public static boolean setToClient() {
    if (mode == CLUSTER_CLIENT) {
        return true;
    }
    mode = CLUSTER_CLIENT;
    sleepIfNeeded();
    lastModified = TimeUtil.currentTimeMillis();
    return startClient();
}

private static boolean startClient() {
    try {
        EmbeddedClusterTokenServer server = EmbeddedClusterTokenServerProvider.getServer();
        if (server != null) {
            server.stop();
        }
        //!!!
        ClusterTokenClient tokenClient = TokenClientProvider.getClient();
        if (tokenClient != null) {
            tokenClient.start();
            RecordLog.info("[ClusterStateManager] Changing cluster mode to client");
            return true;
        } else {
            RecordLog.warn("[ClusterStateManager] Cannot change to client (no client SPI found)");
            return false;
        }
    } catch (Exception ex) {
        RecordLog.warn("[ClusterStateManager] Error when changing cluster mode to client", ex);
        return false;
    }
}

 

spiclient配置

 

 

DefaultClusterTokenClient

//client属性
private ClusterTransportClient transportClient;

//启动
@Override
public void start() throws Exception {
    if (shouldStart.compareAndSet(false, true)) {
        startClientIfScheduled();
    }
}

private void startClientIfScheduled() throws Exception {
    if (shouldStart.get()) {
        if (transportClient != null) {
            //调用client start方法
            transportClient.start();
        } else {
            RecordLog.warn("[DefaultClusterTokenClient] Cannot start transport client: client not created");
        }
    }
}

public DefaultClusterTokenClient() {
    ClusterClientConfigManager.addServerChangeObserver(new ServerChangeObserver() {
        @Override
        public void onRemoteServerChange(ClusterClientAssignConfig assignConfig) {
            changeServer(assignConfig);
        }
    });
    //在构造方法中会初始化client
    initNewConnection();
}

private void initNewConnection() {
    if (transportClient != null) {
        return;
    }
    String host = ClusterClientConfigManager.getServerHost();
    int port = ClusterClientConfigManager.getServerPort();
    if (StringUtil.isBlank(host) || port <= 0) {
        return;
    }

    try {
        //client 是netty client
        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);
    }
}

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com