Docker+nginx负载均衡+nginx动静分离+tomcat服务+maven+spring+springMVC+SpringSession+redis(Session一致性)

 

问题:上次 Docker+nginx负载均衡+nginx动静分离+tomcat服务 一文中提到利用nginx实现负载均衡可能会出现session不一致性问题。例如登陆页面是由tomcat1提供的,即该用户的session存储在tomcat1中。登陆跳转页面时却由tomcat2提供,而tomcat2并没有该用用户的session,最终出现先bug

解决办法:SpringSession+redis实现session一致性

部署结构如下

Docker+nginx负载均衡+nginx动静分离+tomcat服务+maven+spring+springMVC+SpringSession+redis(Session一致性)_第1张图片

1、利用Maven创建SpringMVC项目

2、修改web.xml如下


         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

Archetype Created Web Application
   
   
        index.jsp
   

   
   
        springMVC
        org.springframework.web.servlet.DispatcherServlet
       
           
            contextConfigLocation
            /WEB-INF/dispatcher-servlet.xml
       

        1
        true
   

   
        springMVC
        /
   

   
   
        contextConfigLocation
        /WEB-INF/applicationContext.xml
   

   
        org.springframework.web.context.ContextLoaderListener
   

   
        springSessionRepositoryFilter
        org.springframework.web.filter.DelegatingFilterProxy
   

   
        springSessionRepositoryFilter
        /*
   



 

3、dispatcher-servlet.xml修改如下


       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
   
   
   
   
   
   
   
   
   
   
   
   
     
   
   
   
       
       
       
       
   

4、applicationContext.xml修改如下


       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
   
                  class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
       
   

   
       
       
       
       
       
       
   

              class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
       
       
       
       
       
   

5、修改pom.xml文件如下




  4.0.0

  spring-session-redis
  mytest
  1.0-SNAPSHOT
  war
  mytest Maven Webapp
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
  

  


    
      org.springframework.session
      spring-session-data-redis
      1.2.1.RELEASE
    
    
      redis.clients
      jedis
      2.8.1
    

    
      javax.servlet
      javax.servlet-api
      3.1.0
    
    
      javax.servlet.jsp
      jsp-api
      2.2
    
    
      javax.servlet
      jstl
      1.2
    
    
    
      mysql
      mysql-connector-java
      5.1.35
    
    
    
      org.springframework
      spring-web
      4.2.6.RELEASE
    
    
      org.springframework
      spring-webmvc
      4.2.6.RELEASE
    
    
      org.springframework
      spring-context
      4.2.6.RELEASE
    
    
      org.springframework
      spring-test
      4.2.6.RELEASE
    
    
      org.springframework
      spring-jdbc
      4.2.6.RELEASE
    

    
    
      org.apache.commons
      commons-lang3
      3.4
    
    
      commons-fileupload
      commons-fileupload
      1.3.1
    


    
      junit
      junit
      4.11
      test
    
  

  
    mytest
    
      
        
          maven-clean-plugin
          3.0.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.7.0
        
        
          maven-surefire-plugin
          2.20.1
        
        
          maven-war-plugin
          3.2.0
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  
创建

6、创建一个控制器

Docker+nginx负载均衡+nginx动静分离+tomcat服务+maven+spring+springMVC+SpringSession+redis(Session一致性)_第2张图片

 

7、indexController的内容如下

package example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping(value = "springSession-redis-test")
public class IndexController {
    @RequestMapping(value = "login")
    public String login(HttpServletRequest request, String username){
        String name=request.getParameter("name");
        String pwd=request.getParameter("pwd");
        request.getSession().setAttribute("user", name);
        return "login";
    }
    @RequestMapping(value = "downloader")
    public String index(HttpServletRequest request, Model model){
        String username = request.getSession().getAttribute("user").toString();
        if(null==username||"".equals(username)){
            return "error";
        }
        model.addAttribute("user", username);
        return "index";
    }
}

8、往下自己创建相应的文件

Docker+nginx负载均衡+nginx动静分离+tomcat服务+maven+spring+springMVC+SpringSession+redis(Session一致性)_第3张图片

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: computer
  Date: 2018/12/6
  Time: 15:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title




本网页有tomcat1提供

Hello World! 欢迎登陆Session共享系统

账号:
密码:

login.jsp

<%--
  Created by IntelliJ IDEA.
  User: computer
  Date: 2018/12/6
  Time: 15:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>





本网页有tomcat1提供

欢迎! ${user} 登陆Session共享系统

请输入下载地址:

/views/index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Index
    


本网页有tomcat1提供

欢迎! ${user} 看到这里说明你已经登陆了,下载就先呵呵

error.jsp

<%--
  Created by IntelliJ IDEA.
  User: computer
  Date: 2018/12/6
  Time: 15:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


本网页有tomcat1提供

老铁!你没登陆也想下载,滚!!!

为了展示效果自己制作两个war文件,你懂的

9、补充以下docker怎么创建redis,conf文件如下自己到官网下载,命令如下

docker run -p 6379:6379 –name redis -v /root/redis/redis.conf:/etc/redis/redis.conf -v /root/redis/data:/data -d redis:3.2 redis-server /etc/redis/redis.conf –appendonly yes 

10、结果如下

Docker+nginx负载均衡+nginx动静分离+tomcat服务+maven+spring+springMVC+SpringSession+redis(Session一致性)_第4张图片

 

Docker+nginx负载均衡+nginx动静分离+tomcat服务+maven+spring+springMVC+SpringSession+redis(Session一致性)_第5张图片

Docker+nginx负载均衡+nginx动静分离+tomcat服务+maven+spring+springMVC+SpringSession+redis(Session一致性)_第6张图片

Docker+nginx负载均衡+nginx动静分离+tomcat服务+maven+spring+springMVC+SpringSession+redis(Session一致性)_第7张图片

此时此刻是不是尿裤了(哈哈哈!!  ! )

没有session一致性处理根据本无法下载,他会显示error.jsp内容

 

后记:还没完,大家去公司跟老板干事,一天发现自己装了无数个tomcat,做了很多负载均衡网速还是这么慢,甚至有时候直接断掉。然后又向老板要钱买服务。哈哈哈,这里很定被BOSS臭骂一段。为什么?毫无疑问,数据库崩了啊兄弟!问题已经不在tomcat那里,数据量太大,一个数据库吃不消,所以要把数据库分离出来做集群!(数据库读写分离下期见咯)

 

 

 

 

 

你可能感兴趣的:(架构)