美文网首页
Spring Boot基于redis的session共享

Spring Boot基于redis的session共享

作者: yellow_han | 来源:发表于2018-09-14 16:02 被阅读0次

    1、pom文件

            <dependency>
                <groupId>org.springframework.session</groupId>
                <artifactId>spring-session-data-redis</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
    

    2、配置文件

    spring:
      profiles:
        active: prod
      cache:
        type: redis
      redis:
        host: xxx.xxx.xx.xx #redis主机地址
        port: 6379
        password:
    

    默认配置:

    spring.redis.cluster.max-redirects= # Maximum number of redirects to follow when executing commands across the cluster.
    spring.redis.cluster.nodes= # Comma-separated list of "host:port" pairs to bootstrap from.
    spring.redis.database=0 # Database index used by the connection factory.
    spring.redis.url= # Connection URL. Overrides host, port, and password. User is ignored. Example: redis://user:password@example.com:6379
    spring.redis.host=localhost # Redis server host.
    spring.redis.jedis.pool.max-active=8 # Maximum number of connections that can be allocated by the pool at a given time. Use a negative value for no limit.
    spring.redis.jedis.pool.max-idle=8 # Maximum number of "idle" connections in the pool. Use a negative value to indicate an unlimited number of idle connections.
    spring.redis.jedis.pool.max-wait=-1ms # Maximum amount of time a connection allocation should block before throwing an exception when the pool is exhausted. Use a negative value to block indefinitely.
    spring.redis.jedis.pool.min-idle=0 # Target for the minimum number of idle connections to maintain in the pool. This setting only has an effect if it is positive.
    spring.redis.lettuce.pool.max-active=8 # Maximum number of connections that can be allocated by the pool at a given time. Use a negative value for no limit.
    spring.redis.lettuce.pool.max-idle=8 # Maximum number of "idle" connections in the pool. Use a negative value to indicate an unlimited number of idle connections.
    spring.redis.lettuce.pool.max-wait=-1ms # Maximum amount of time a connection allocation should block before throwing an exception when the pool is exhausted. Use a negative value to block indefinitely.
    spring.redis.lettuce.pool.min-idle=0 # Target for the minimum number of idle connections to maintain in the pool. This setting only has an effect if it is positive.
    spring.redis.lettuce.shutdown-timeout=100ms # Shutdown timeout.
    spring.redis.password= # Login password of the redis server.
    spring.redis.port=6379 # Redis server port.
    spring.redis.sentinel.master= # Name of the Redis server.
    spring.redis.sentinel.nodes= # Comma-separated list of "host:port" pairs.
    spring.redis.ssl=false # Whether to enable SSL support.
    spring.redis.timeout= # Connection timeout.
    

    3、config类

    /**
     * spring session配置
     *
     */
    @EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800)  //session过期时间  如果部署多机环境,需要打开注释
    public class SpringSessionConfig {
    
        @Bean
        public LettuceConnectionFactory connectionFactory() {
            return new LettuceConnectionFactory(); 
        }
    }
    

    4、配合nginx便可实现负载均衡session共享。

    相关文章

      网友评论

          本文标题:Spring Boot基于redis的session共享

          本文链接:https://www.haomeiwen.com/subject/irvogftx.html