1. 程式人生 > 其它 >Sentinel教程進階五setToClient()

Sentinel教程進階五setToClient()

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;
        }
    }

spi client配置

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);
        }
    }