SpringBoot SpringSession redis 共用 SESSION

來源:互聯網
上載者:User

標籤:form   並發控制   使用   min   hash   code   get   query   tar   

號稱無縫整合httpsession 共用,

但注意如果存在第三方架構,例如SESSION並發控制,這個是需要自己重寫session名單的.

POM

    <!--redis-->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-data-redis</artifactId>            <version>1.5.4.RELEASE</version>        </dependency>        <!-- redis session -->        <dependency>            <groupId>org.springframework.session</groupId>            <artifactId>spring-session-data-redis</artifactId>            <version>1.3.1.RELEASE</version>        </dependency>
RedisSessionConfig
/** * Created by ZhenWeiLai on 2017/6/11. */@Configuration//maxInactiveIntervalInSeconds session逾時時間,單位秒@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 600)public class RedisSessionConfig {}
RedisCacheConfig
package com.lzw.core.configuration;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Value;import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.CachingConfigurerSupport;import org.springframework.cache.annotation.EnableCaching;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;/** * Created by ZhenWeiLai on 2017/6/11. */@Configuration@EnableCachingpublic class RedisCacheConfig  extends CachingConfigurerSupport {    Logger logger = LoggerFactory.getLogger(RedisCacheConfig.class);    @Value("${spring.redis.host}")    private String host;    @Value("${spring.redis.port}")    private int port;    @Value("${spring.redis.timeout}")    private int timeout;    @Value("${spring.redis.pool.max-idle}")    private int maxIdle;    @Value("${spring.redis.pool.max-wait}")    private long maxWaitMillis;    @Value("${spring.redis.password}")    private String password;    @Bean    public JedisPool redisPoolFactory() {        logger.info("JedisPool注入成功!!");        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();        jedisPoolConfig.setMaxIdle(maxIdle);        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);        return jedisPool;    }    @Bean    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {        RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();        redisTemplate.setConnectionFactory(cf);        return redisTemplate;    }    @Bean    public CacheManager cacheManager(RedisTemplate redisTemplate) {        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);        //預設逾時時間,單位秒        cacheManager.setDefaultExpiration(3000);        //根據緩衝名稱設定逾時時間,0為不逾時        Map<String,Long> expires = new ConcurrentHashMap<>();        cacheManager.setExpires(expires);        return cacheManager;    }}

 application.yml

spring:  datasource:#    readSize: 1#    name: writeDataSource    type: com.alibaba.druid.pool.DruidDataSource    write:      driver-class-name: com.mysql.jdbc.Driver      url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&useSSL=true      username: root      password: 123      initialSize: 10      maxActive: 100      maxWait: 60000      minIdle: 5      timeBetweenEvictionRunsMillis: 60000      minEvictableIdleTimeMillis: 300000      validationQuery: SELECT ‘x‘      testWhileIdle: true      testOnBorrow: false      testOnReturn: false      poolPreparedStatements: true      maxPoolPreparedStatementPerConnectionSize: 20  #redis配置  redis:    host: 192.168.1.11    port: 6379    # REDIS (RedisProperties)    # Redis資料庫索引(預設為0)    database: 0    # Redis伺服器串連密碼(預設為空白)    password:    # 串連池最大串連數(使用負值表示沒有限制)    # 連線逾時時間(毫秒)    timeout: 0    pool:      max-active: 8      # 串連池最大阻塞等待時間(使用負值表示沒有限制)      max-wait: -1      # 串連池中的最大空閑串連      max-idle: 8      # 串連池中的最小空閑串連      min-idle: 0

 

SpringBoot SpringSession redis 共用 SESSION

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.