bulbuls
bulbuls
2016-11-16 10:42
采纳率: 66.7%
浏览 7.8k
已采纳

springmvc websocket +tomcat,同时支持的连接数太小了 怎么办?

问题:我想通过tomcat发布一个websocket程序,代码都已经调通。但是,前端并发最多只能支持260websocket连接就上不去了,以下是我的前端测试代码和tomcat的配置。请大家帮我看看是我写错了么,还是说单个tomcat的瓶颈就是如此?
前端代码

 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script src=" <%=request.getContextPath() %>/js/jquery/jquery-1.11.3.js"></script>
<script src=" <%=request.getContextPath() %>/js/others/sockjs-0.3.min.js"></script>
<script src=" <%=request.getContextPath() %>/js/others/stomp-2.3.3.js"></script>
</head>
<body>
<input type="button" value="连接" onclick="opens()" />
<input type="button" value="断开" onclick="closes()" />
<select id="type">
    <option value=1>点对点</option>
    <option value=2>订阅</option>
    <option value=3>主题订阅</option>
</select>
<input type="button" value="发送" onclick="send()"/>
用户名:<input type="text" id="name" value="bulbuls"/>
<hr/>
<table>
    <tr>
        <td>消息</td>
        <td><input type="text" id="msg"/></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td></td><td></td>
    </tr>
    <tr>
        <td>message</td>
        <td><div id="message"></div></td>
        <td>返回的消息</td>
        <td><div id="recode"></div></td>
    </tr>
</table>
<hr/>

<script>
    var i = 0;
    function opens(){
        i++;
        if(i>300){
            return;
        }
        var socket = new SockJS("http://127.0.0.1:8080/hello");
        var stompClient = Stomp.over(socket);
        stompClient.connect({}, function(frame) {
            message("连接成功....:"+i);
            //全域广播
            stompClient.subscribe('/topic', function(greeting){
                $("#recode").append("<div>"+greeting.body+"</div>");
            });
            opens();
            //主题广播
            /* stompClient.subscribe('/topic/'+$("#name").val(), function(greeting){
                $("#recode").append("<div>"+greeting.body+"</div>");
            }); */
            //点对点方式回调
            /* stompClient.subscribe('/user/'+$("#name").val()+'/point', function(message){
                $("#recode").append("<div>"+message.body+"</div>");
            }); */
        }); 
    }
    function closes(){
        if (stompClient != null) {
            stompClient.disconnect();
            message("断开连接....")
        }
    }
    function send(){
        if(!stompClient || !stompClient.connected){
            message("警告:连接已经断开");
            return;
        }
        var msg = $("#msg").val();
        var name = $("#name").val();
        var type = $("#type").val();
        if(type==1){
            stompClient.send("/app/point", {}, JSON.stringify({"user":"bulbuls","destination":"/point","message":"来自手机的消息"}));
        }else if(type==2){
            stompClient.send("/app/topic", {}, JSON.stringify({"user":"bulbuls","destination":"/topic","message":"来自手机的消息"}));
        }else if(type==3){
            stompClient.send("/app/topic/bulbuls", {}, JSON.stringify({"user":"bulbuls","destination":"/point","message":"来自手机的消息"}));
        }

    }
    function message(mess){
        $("#message").append("<div>"+mess+"</div>");
    }
</script>

</body>
</html>

tomcat server.xml配置

 <Service name="Catalina">

    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="400" minSpareThreads="4"/>

    <Connector connectionTimeout="20000" port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443" maxThreads="300"/>

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>

    <Engine defaultHost="localhost" name="Catalina">

      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
      </Realm>

      <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">


        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log." suffix=".txt"/>

      <Context docBase="C:\soft\apache-tomcat-7.0.73\wtpwebapps\websocket" path="/" reloadable="true" source="org.eclipse.jst.j2ee.server:websocket"/></Host>
    </Engine>
  </Service>

图片说明

图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • bulbuls
    bulbuls 2016-11-17 02:01
    已采纳

    已经解决,原来除了tomcat的连接数限制以外, 浏览器本身对websocket的连接数也有限制,谷歌261左右 IE:16个左右

    点赞 评论
  • u011819687
    林与海 2016-11-16 10:44

    tomcat在并发数达到250之后性能会下降,可以考虑集群

    点赞 评论

相关推荐