我一步一步地跟着https://docs.jboss.org/author/display/WFLY10/Clustering+and+Domain+Setup+Walkthrough。
Wildfly域看起来不错。 我可以在主机和从机主机上读取成功的注册日志。
该教程解释了如何使用mod_cluster配置负载平衡。无论如何,我更喜欢Takin'优势的AWS Elastic Load Balancer服务。
然后,我尝试了所有三种类型的LB(应用程序,网络,经典),但没有一个与我合作。
我写了一个简单的应用来测试我的用例:
前端servlet :
@WebServlet( urlPatterns = {"/test"} )
public class SessionServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Inject SessionBean bean;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
System.out.println("doGet");
process(req, res, false);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
System.out.println("doPost");
process(req, res, true);
}
private void process(HttpServletRequest req, HttpServletResponse res, boolean post) throws ServletException, IOException {
if ( req.getParameter("name")!=null ) {
bean.setName( req.getParameter("name") );
}
req.setAttribute("name", bean.getName());
req.getRequestDispatcher( "/test.jsp" ).forward(req, res);
}
}
会话范围的bean:
@SessionScoped
public class SessionBean implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
public SessionBean() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SessionBean other = (SessionBean) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "SessionBean [name=" + name + "]";
}
}
查看JSP:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html lang="it">
<head>
<title>Session Failover Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta charset="UTF-8" />
</head>
<body>
<h1>You are: <c:if test="${empty name}"><i>nobody</i></c:if> <font color="red">${name}</font></h1>
<br/>
<hr/>
<br/>
<form action="/session-failover-demo/test" method="post">
Who are you:
<input type="text" name="name" value="" />
<input type="submit" name="submit" value="ok" />
</form>
</body>
</html>
web.xml文件有
<display-name>session-failover-demo</display-name>
<distributable/>
最后,我进行了测试:
会话丢失了。我在浏览器开发工具中看到JSESSIONID cookie值已经改变了!
所以我不知道是否应该归咎于Wildfly域配置或AWS ELB。我错过了什么吗?
编辑 Master stdout:https://pastebin.com/XbtVrb40