soul同步数据-websocket源码分析

通过官网资料了解到,soul支持websocket,http,zookeeper等同步方式。
如果是通过websocket 同步策略,则将变更后的数据主动推送给 soul-web,并且在网关层,会有对应的WebsocketCacheHandler 处理器处理来处 admin 的数据推送。

这列应该是官网资料没有及时更新
最新的类是WebsocketSyncDataService

{
        String[] urls = StringUtils.split(websocketConfig.getUrls(), ",");

//可以看到,将url拿到之后,以urls的数量创建了ScheduledThreadPoolExecutor线程池。
        executor = new ScheduledThreadPoolExecutor(urls.length, SoulThreadFactory.create("websocket-connect", true));

//遍历urls,创建SoulWebsocketClient,并添加到clients集合中。
        for (String url : urls) {
            try {
                clients.add(new SoulWebsocketClient(new URI(url), Objects.requireNonNull(pluginDataSubscriber), metaDataSubscribers, authDataSubscribers));
            } catch (URISyntaxException e) {
                log.error("websocket url({}) is error", url, e);
            }
        }
        try {
            for (WebSocketClient client : clients) {
            
//在最后遍历clients集合,这里使用了第三方的websocket包进行连接。
                boolean success = client.connectBlocking(3000, TimeUnit.MILLISECONDS);
                if (success) {
                    log.info("websocket connection is successful.....");
                } else {
                    log.error("websocket connection is error.....");
                }
                
//注意这里使用调度线程池进行断线重连,30秒进行一次
                executor.scheduleAtFixedRate(() -> {
                    try {
                        if (client.isClosed()) {
//如果连接关闭,进行重连
                            boolean reconnectSuccess = client.reconnectBlocking();
                            if (reconnectSuccess) {
                                log.info("websocket reconnect is successful.....");
                            } else {
                                log.error("websocket reconnection is error.....");
                            }
                        }
                    } catch (InterruptedException e) {
                        log.error("websocket connect is error :{}", e.getMessage());
                    }
                }, 10, 30, TimeUnit.SECONDS);
            }
            /* client.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxyaddress", 80)));*/
        } catch (InterruptedException e) {
            log.info("websocket connection...exception....", e);
        }

    }
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页