zoukankan      html  css  js  c++  java
  • 集群情况下Session共享解决方案 redis

    1、集群情况下session会产生什么原因?

        由于session存放在服务器端,集群下用户可能访问不同的服务器,则可能session无法共享。

      2、Session共享解决方案

        1)NGINX做的负载均衡可以绑定ip_hash,从而使同一个IP访问同一个服务器 ------------------该方案使得集群失去意义。

        2)利用数据库同步session----------------------太过复杂

        3)利用cookie同步session(保存一个session到本地,再次访问将其带到服务器端)----------------------安全性差、http请求都需要带参数增加了带宽消耗

        4)使用session集群,存放到redis中(spring-session)

       3、spring-session项目,解决session共享问题

    <!--spring boot 与redis应用基本环境配置 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-redis</artifactId>
    </dependency>
    <!--spring session 与redis应用基本环境配置,需要开启redis后才可以使用,不然启动Spring boot会报错 -->
    <dependency>
      <groupId>org.springframework.session</groupId>
      <artifactId>spring-session-data-redis</artifactId>
    </dependency>

    创建SessionConfig

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
    import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
    
    //这个类用配置redis服务器的连接
    //maxInactiveIntervalInSeconds为SpringSession的过期时间(单位:秒)
    @EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800)
    Public class SessionConfig {
    
        // 冒号后的值为没有配置文件时,制动装载的默认值
        @Value("${redis.hostname:localhost}")
        String HostName;
        @Value("${redis.port:6379}")
        int Port;
    
        @Bean
        Public JedisConnectionFactory connectionFactory() {
            JedisConnectionFactory connection = new JedisConnectionFactory();
            connection.setPort(Port);
            connection.setHostName(HostName);
            return connection;
        }
    }

    初始化Session:

    //初始化Session配置
    Public class SessionInitializer extends AbstractHttpSessionApplicationInitializer{
      Public SessionInitializer() {
        super(SessionConfig.class);
        }
    }

    控制层代码

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.SpringApplication;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class SessionController {
    
        @Value("${server.port}")
        private String PORT;
    
        @RequestMapping("/index")
        public String index() {
            return "index:" + PORT;
        }
    
        /**
         * @methodDesc: 功能描述:(往session存放值)
         */
        @RequestMapping("/setSession")
        public String setSession(HttpServletRequest request, String sessionKey, String sessionValue) {
            HttpSession session = request.getSession(true);
            session.setAttribute(sessionKey, sessionValue);
            return "success,port:" + PORT;
        }
    
        /**
         * @methodDesc: 功能描述:(从Session获取值)
         */
        @RequestMapping("/getSession")
        public String getSession(HttpServletRequest request, String sessionKey) {
            HttpSession session =null;
            try {
             session = request.getSession(false);
            } catch (Exception e) {
            e.printStackTrace();
            }
            String value=null;
            if(session!=null){
                value = (String) session.getAttribute(sessionKey);
            }
            return "sessionValue:" + value + ",port:" + PORT;
        }
    
    }

     六、高并发解决方案

       业务数据库  -》 数据水平分割(分区分表分库)、读写分离

      业务应用 -》 逻辑代码优化(算法优化)、公共数据缓存

      应用服务器 -》 反向静态代理、配置优化、负载均衡(apache分发,多tomcat实例)

      系统环境 -》 JVM调优

      页面优化 -》 减少页面连接数、页面尺寸瘦身

      动态资源和静态资源分离

      CDN加速

      服务分布式部署

  • 相关阅读:
    英语生活箴言
    Javascript中最常用的55个经典技巧
    深刻理解Java编程的7个例子
    定制Apache索引样式
    【Androidin全球首发】国产Android Broncho A1 评测,第一印象
    系统程序员成长计划写得又快又好的秘诀(五)
    让adb logcat打印内核调试信息
    系统程序员成长计划写得又快又好的秘诀(三)
    Projects owned by limodev.cn
    Apache Direcotry Indexes目录列表显示样式定制
  • 原文地址:https://www.cnblogs.com/lshan/p/11526572.html
Copyright © 2011-2022 走看看