我在AWS Elastic Beanstalk上使用Apache Tomcat / 8.0.47,当我在本地mac上运行时,我可以正常连接并获得良好的响应:
curl --include \
--no-buffer \
--header "Connection: Upgrade" \
--header "Upgrade: websocket" \
--header "Host: example.com:80" \
--header "Origin: http://example.com:80" \
--header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" \
--header "Sec-WebSocket-Version: 13" http://localhost:8080/echo
但如果我进入AWS实例,我只会获得404.
这不是nginx问题,因为我在ssh'd的情况下直接针对Tomcat端口运行。
我花了一天时间寻找解决方案,我感到很茫然。我在网上找到的任何东西似乎都试图解决负载均衡器/ nginx问题。
供参考,这是我的代码:
package com.mypackage.sockets
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint(value = "/echo", configurator=EchoEndpointConfigurator.class)
public class EchoServer {
private Set<Session> userSessions = Collections.synchronizedSet(new HashSet<Session>());
@OnOpen
public void onOpen(Session session) {
System.out.println(session.getId() + " has opened a connection");
userSessions.add(session);
}
@OnClose
public void onClose(Session session) {
System.out.println("Session " + session.getId() + " has ended");
userSessions.remove(session);
}
@OnMessage
public void onMessage(String message, Session session) {
System.out.println("Message from " + session.getId() + ": " + message);
try {
session.getBasicRemote().sendText(message);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
和
package com.mypackage.sockets;
import javax.websocket.server.ServerEndpointConfig.Configurator;
public class EchoEndpointConfigurator extends Configurator {
private static EchoServer echoServer = new EchoServer();
@Override
public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
return (T)echoServer;
}
}
我也有一个web.xml文件,但它只引用了所有工作正常的servlet
可能是Tomcat的一些标志我可能需要在AWS Elastic Beanstalk上设置
答案 0 :(得分:0)
事实证明,尽管curl命令在本地工作而不是远程,但这不是我的问题。通过直接在ec2上暴露该端口,我能够发现它正在端口8080上工作。最后,为了让它在端口80上工作,我从Apache HTTPD更改为Nginx。使用经典的Load Balancer完美工作。尚未找出应用程序负载均衡器。