netty-socketio+nginx+redis做socket服务集群

socketio服务器

//集群通过Redis进行数据通道交互,配置如下
        Config redissonConfig = new Config();
        redissonConfig.useSingleServer().setPassword("xxxxxx").setAddress("http://127.0.0.1:6379");

        RedissonClient redisson = Redisson.create(redissonConfig);
        RedissonStoreFactory redisStoreFactory = new RedissonStoreFactory(redisson);

        Configuration config = new Configuration();
        config.setHostname(host);
        config.setPort(port);
        config.setStoreFactory(redisStoreFactory);

        final SocketIOServer server = new SocketIOServer(config);
        server.addConnectListener(new ConnectListener() {
            @Override
            public void onConnect(final SocketIOClient client) {
                //进行token授权验证
                final String token = client.getHandshakeData().getSingleUrlParam("token");
                userService.getUser(token, new MyBack() {
                    @Override
                    public void error() {
                        logger.info("用户口令不正确,来自{}", "WebSocket");
                        ChatObject data = new ChatObject();
                        data.setMessage("用户口令不正确!连接关闭。");
                        client.sendEvent("OAUTH_ERROR",data);
                        client.disconnect();
                    }

                    @Override
                    public void successs() {
                        String username = UserManager.getUser(token);
                        logger.info("用户成功登陆:{},来自{}", username, "WebSocket");
                        ChatObject data = new ChatObject();
                        data.setMessage("用户成功登陆:" + username);
                        client.sendEvent("OAUTH_SUCCESS",data);
                    }
                });

                UUID sessionId = client.getSessionId();
            }
        });
        server.addEventListener("chatevent", ChatObject.class, new DataListener() {
            @Override
            public void onData(SocketIOClient client, ChatObject data, AckRequest ackRequest) {
                // broadcast messages to all clients
                server.getBroadcastOperations().sendEvent("chatevent", data);
            }
        });
        server.start();

Nginx配置

使用Sticky作为负载均衡解决方案:安装配置参考:https://blog.csdn.net/yyysylvia/article/details/80198021

nginx完全配置


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  600;

    #gzip  on;

    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    upstream socket_nodes { 
        sticky;
        server 10.200.81.70:9901;
        server 10.200.81.70:9902;
        #session_sticky;
    }

    server {
        listen       8099;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

	location /socket.io {
            proxy_redirect off;
            proxy_buffering off;
            proxy_set_header       Host $host:$server_port;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            client_max_body_size    10m;
            client_body_buffer_size 128k;
            proxy_connect_timeout   1000;
            proxy_send_timeout      1000;
            proxy_read_timeout      1000;
            proxy_buffer_size       256k;
            proxy_buffers           128 256k;
            proxy_busy_buffers_size 256k;
            proxy_temp_file_write_size 256k; 
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_pass      http://socket_nodes;   
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

 

你可能感兴趣的:(socketio)