spring Boot redis連線池配置,Spring Session配置
阿新 • • 發佈:2018-12-30
1.POM配置
<!-- redis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <!-- spring-session --> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency>
2.配置檔案application.properties
# REDIS (RedisProperties) # Redis資料庫索引(預設為0) spring.redis.database=0 # Redis伺服器地址 spring.redis.host=localhost # Redis伺服器連線埠 spring.redis.port=6379 # Redis伺服器連線密碼(預設為空) spring.redis.password=admin # 連線池最大連線數(使用負值表示沒有限制) spring.redis.pool.max-active=8 # 連線池最大阻塞等待時間(使用負值表示沒有限制) spring.redis.pool.max-wait=-1 # 連線池中的最大空閒連線 spring.redis.pool.max-idle=8 # 連線池中的最小空閒連線 spring.redis.pool.min-idle=0 # 連線超時時間(毫秒) spring.redis.timeout=0 #spring-session 使用 spring.session.store-type=none
3.redis連結池配置屬性
package com.config; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.session.data.redis.config.ConfigureRedisAction; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; /** * Created by Administrator on 2017/5/9. */ @Configuration public class RedisConfig extends CachingConfigurerSupport { Logger logger = LoggerFactory.getLogger(RedisConfig.class); /** * SpringSession 需要注意的就是redis需要2.8以上版本,然後開啟事件通知,在redis配置檔案裡面加上 * notify-keyspace-events Ex * Keyspace notifications功能預設是關閉的(預設地,Keyspace 時間通知功能是禁用的,因為它或多或少會使用一些CPU的資源)。 * 或是使用如下命令: * redis-cli config set notify-keyspace-events Egx * 如果你的Redis不是你自己維護的,比如你是使用阿里雲的Redis資料庫,你不能夠更改它的配置,那麼可以使用如下方法:在applicationContext.xml中配置 * <util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/> * @return */ @Bean public ConfigureRedisAction configureRedisAction() { return ConfigureRedisAction.NO_OP; } @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注入成功!!"); logger.info("redis地址:" + host + ":" + port); JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMaxWaitMillis(maxWaitMillis); JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password); // JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout); //無密碼情況 return jedisPool; } }
redis工具類
package com.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
* Created by Administrator on 2017/5/9.
*/
@Component("redisClient")
public class RedisClient {
@Autowired
private JedisPool jedisPool;
public void set(String key, String value) throws Exception {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set(key, value);
} finally {
//返還到連線池
jedis.close();
}
}
public String get(String key) throws Exception {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.get(key);
} finally {
//返還到連線池
jedis.close();
}
}
}
redis應用
redisClient.set("redis_key", "abc");
String value=redisClient.get("redis_key");
logger.info("redis_key value:{}", value);
4.application 新增啟用springsession註解
@EnableRedisHttpSession //spring session
和session一樣正常使用即可
@RequestMapping("/setSession")
public String setSession(HttpServletRequest request) throws Exception {
request.getSession().setAttribute("user", "message");
return null;
}
@RequestMapping("/getSession")
public String getSession(HttpServletRequest request) throws Exception {
String message= (String)request.getSession().getAttribute("user");
logger.info("spring Session message:{}", message);
return null;
}