Spring Boot锛堝崄涓€锛塕edis闆嗘垚浠嶥ocker瀹夎鍒板垎甯冨紡Session鍏变韩
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot锛堝崄涓€锛塕edis闆嗘垚浠嶥ocker瀹夎鍒板垎甯冨紡Session鍏变韩相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/rar' title='rar'>rar
浜屻€佸湪Docker涓畨瑁匯edis
2.1 涓嬭浇闀滃儚
璁块棶瀹樼綉锛歨ttps://hub.docker.com/r/library/redis/ 閫夋嫨涓嬭浇鐗堟湰锛屾湰鏂囬€夋嫨鏈€鏂癝table 4.0.11
浣跨敤鍛戒护鎷夊彇闀滃儚锛?/p>
docker pull redis:4.0.11
2.2 鍚姩瀹瑰櫒
鍚姩Redis鍛戒护濡備笅锛?/p>
docker run --name myredis -p 6379:6379 -d redis:4.0.11 redis-server --appendonly yes
鍛戒护璇存槑锛?/p>
- --name 璁剧疆鍒悕
- -p 鏄犲皠瀹夸富绔彛鍒板鍣ㄧ鍙?/li>
- -d 鍚庡彴杩愯
- redis-server --appendonly yes 鍦ㄥ鍣ㄥ惎鍔ㄦ墽琛宺edis-server鍚姩鍛戒护锛屾墦寮€redis鎸佷箙鍖?/li>
鍚姩鎴愬姛涔嬪悗浣跨敤鍛戒护锛?/p>
docker ps
鏌ョ湅redis杩愯璇锋眰锛屽涓嬪浘涓鸿繍琛屾垚鍔燂細
涓夈€丷edis闆嗘垚
寮€鍙戠幆澧?/p>
- Spring Boot 2.0.4 RELEASE
- Manven
3.1 娣诲姞渚濊禆
鍦╬om.xml娣诲姞濡備笅渚濊禆锛?/p>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
娉ㄦ剰涓嶈渚濊禆鈥渟pring-boot-starter-redis鈥濆畠鏄棫鐗堟湰锛屾柊鐗堝凡缁忚縼绉诲埌鈥渟pring-boot-starter-data-redis鈥濅簡銆?/p>
3.2 閰嶇疆Redis
鍦╝pplication.properties杩涜濡備笅璁剧疆锛?/p>
# Redis 閰嶇疆
# Redis鏈嶅姟鍣ㄥ湴鍧€
spring.redis.host=127.0.0.1
# Redis鏈嶅姟鍣ㄨ繛鎺ュ瘑鐮侊紙榛樿涓虹┖锛?spring.redis.password=
# Redis鏈嶅姟鍣ㄨ繛鎺ョ鍙?spring.redis.port=6379
# Redis鍒嗙墖锛堥粯璁や负0锛塕edis榛樿鏈?6涓垎鐗?spring.redis.database=0
# 杩炴帴姹犳渶澶ц繛鎺ユ暟锛堜娇鐢ㄨ礋鍊艰〃绀烘病鏈夐檺鍒讹級
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=10000
# 鎸囧畾spring鐨勭紦瀛樹负redis
spring.cache.type=redis
娉ㄦ剰锛歴pring.redis.timeout涓嶈璁剧疆涓?锛岃缃负0鏌ヨRedis鏃朵細鎶ラ敊锛屽洜涓烘煡璇㈣繛鎺ユ椂闂村お鐭簡銆?/p>
3.3 Redis浣跨敤
瀹屾垚浠ヤ笂閰嶇疆涔嬪悗灏卞彲浠ュ啓浠g爜鎿嶄綔Redis浜嗭紝绀轰緥浠g爜濡備笅锛?/p>
@Autowired
private StringRedisTemplate stringRedisTemplate;
@RequestMapping("/")
public String doTest() {
String _key = "time"; //缂撳瓨key
stringRedisTemplate.opsForValue().set(_key, String.valueOf(new Date().getTime())); //redis瀛樺€? return stringRedisTemplate.opsForValue().get(_key); //redis鍙栧€?}
鏇村鎿嶄綔锛?/p>
- stringRedisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS); 鍚憆edis閲屽瓨鍏ユ暟鎹拰璁剧疆缂撳瓨鏃堕棿锛?/li>
- stringRedisTemplate.hasKey("keyName"); 妫€鏌ey鏄惁瀛樺湪锛岃繑鍥瀊oolean锛?/li>
鍥涖€佸0鏄庡紡缂撳瓨
涓轰簡绠€鍖栫紦瀛樺彲浠ョ洿鎺ヤ娇鐢ㄥ0鍚嶅紡缂撳瓨锛屽彲浠ョ渷鍘昏缃紦瀛樺拰璇诲彇缂撳瓨鐨勪唬鐮侊紝浣跨敤璧锋潵浼氭柟渚垮緢澶氥€?/p>
澹版槑寮忕紦瀛樹娇鐢ㄦ楠ゅ涓嬶細
4.1 璁剧疆Redis缂撳瓨
鍦╬om.xml鏂囦欢璁剧疆缂撳瓨涓篟edis锛屼唬鐮佸涓嬶細
spring.cache.type=redis
4.2 寮€鍚叏灞€缂撳瓨
鍦ㄥ惎鍔ㄦ枃浠禔pplication.java璁剧疆寮€鍚紦瀛橈紝浠g爜濡備笅锛?/p>
@SpringBootApplication
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
4.3 浣跨敤娉ㄨВ
娉ㄨВ濡備笅锛?/p>
- @Cacheable 璁剧疆骞惰鍙栫紦瀛橈紙绗竴娆¤缃互鍚庣洿鎺ヨ鍙栵級锛?/li>
- @CachePut 鏇存柊缂撳瓨锛堟瘡娆″垹闄ゅ苟鏇存柊缂撳瓨缁撴灉锛夛紱
- @CacheEvict 鍒犻櫎缂撳瓨锛堝彧鍒犻櫎缂撳瓨锛夛紱
閫氱敤灞炴€э細
- value 缂撳瓨鍚嶇О锛?/li>
- key 浣跨敤SpEL琛ㄨ揪寮忚嚜瀹氫箟鐨勭紦瀛楰ey锛屾瘮濡傦細#name鏄互鍙傛暟name涓簁ey鐨勭紦瀛橈紝#resule.name鏄互杩斿洖缁撴灉鐨刵ame浣滀负key鐨勭紦瀛橈紱
4.3.1 @Cacheable 浣跨敤
绀轰緥浠g爜濡備笅锛?/p>
// 缂撳瓨key
private final String _CacheKey = "userCacheKeyTime";
@RequestMapping("/")
@Cacheable(value = _CacheKey)
public String index() {
System.out.println("set cache");
return "cache:" + new Date().getTime();
}
鍙湁棣栨璁块棶鐨勬椂鍊欎細鍦ㄦ帶鍒跺彴鎵撳嵃鈥渟et cache鈥濅俊鎭紝涔嬪悗鐩存帴杩斿洖Redis缁撴灉浜嗭紝涓嶄細鍦ㄦ湁娣诲姞鐨勬墦鍗颁俊鎭嚭鐜般€?/p>
4.3.2 @CachePut 浣跨敤
绀轰緥浠g爜濡備笅锛?/p>
// 缂撳瓨key
private final String _CacheKey = "userCacheKeyTime";
@RequestMapping("/put")
@CachePut(value = _CacheKey)
public String putCache() {
System.out.println("update cache");
return "update cache:" + new Date().getTime();
}
璁块棶http://xxx/put 姣忔浼氭妸鏈€鏂扮殑鏁版嵁瀛樺偍缂撳瓨璧锋潵銆?/p>
4.3.3 @CacheEvict 浣跨敤
绀轰緥浠g爜濡備笅锛?/p>
// 缂撳瓨key
private final String _CacheKey = "userCacheKeyTime";
@RequestMapping("/del")
@CacheEvict(value = _CacheKey)
public String delCache() {
System.out.println("缂撳瓨鍒犻櫎");
return "delete cache:" + new Date().getTime();
}
璁块棶http://xxx/del 鍙細鍒犻櫎缂撳瓨锛岄櫎姝や箣鍚庝笉浼氳繘琛屼换浣曟搷浣溿€?/p>
浜斻€佸垎甯冨紡Session鍏变韩
鍦ㄥ垎甯冨紡绯荤粺涓璖ession鍏变韩鏈夊緢澶氱鏂规锛岃€屾妸Session鎵樼鍦ㄧ紦瀛樹腑鏄渶甯哥敤鐨勬柟妗堜箣涓€锛屼笅闈㈡潵鐪婼ession鍦≧edis涓殑鎵樼姝ラ銆?/p>
5.1 娣诲姞渚濊禆
鍦╬om.xml涓坊鍔犲涓嬪紩鐢細
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
5.2 寮€鍚疭ession鍔熻兘
鍦ㄥ惎鍔ㄧ被Application.java鐨勭被娉ㄨВ娣诲姞寮€鍚疭ession锛屼唬鐮佸涓嬶細
@SpringBootApplication
@EnableCaching
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800)
public class RedisApplication {
public static void main(String[] args) {
SpringApplication.run(RedisApplication.class, args);
}
}
鍏朵腑maxInactiveIntervalInSeconds涓篠ession杩囨湡鏃堕棿锛岄粯璁?0鍒嗛挓锛岃缃崟浣嶄负绉掋€?/p>
5.3 Session浣跨敤
鎺ヤ笅鏉ョ紪鍐欎竴娈典唬鐮佹潵娴嬭瘯涓€涓婼ession锛岀ず渚嬩唬鐮佸涓嬶細
@RequestMapping("/uid")
public String testSession(HttpSession session) {
UUID uid = (UUID) session.getAttribute("uid");
if (uid == null) {
uid = UUID.randomUUID();
}
session.setAttribute("uid", uid);
return session.getId();
}
杩炵画璁块棶涓ゆ璇锋眰涔嬪悗锛屾煡鐪嬫帶鍒跺彴淇℃伅濡備笅鍥撅細
鍙互鐪嬪嚭锛屼袱娆¤闂殑SessionId鏄竴鏍风殑锛岃繖涓椂鍊欏湪鏌ョ湅Redis 瀹㈡埛绔紝濡備笅鍥撅細
鍙戠幇Redis閲屽瓨鍌ㄧ殑Session杩囨湡鏃堕棿涔熸槸瀵圭殑锛岀鍚堟垜浠殑璁剧疆銆?/p>
5.4 鍒嗗竷寮忕郴缁熷叡浜玈ession
鍥犱负鎶奡ession鎵樼缁欏悓涓€鍙癛edis鏈嶅姟鍣ㄤ簡锛屾墍浠ession鍦⊿pring Boot涓寜鐓у涓婃柟寮忓湪閰嶇疆澶氬彴鏈嶅姟鍣紝寰楀埌鐨凷ession鏄竴鏍风殑銆?/p>
绀轰緥婧愮爜涓嬭浇锛歨ttps://github.com/vipstone/springboot-example/tree/master/springboot-redis
鍙傝€冭祫鏂?/strong>
Spring boot涓璕edis鐨勪娇鐢細http://www.ityouknow.com/springboot/2016/03/06/spring-boot-redis.html
以上是关于Spring Boot锛堝崄涓€锛塕edis闆嗘垚浠嶥ocker瀹夎鍒板垎甯冨紡Session鍏变韩的主要内容,如果未能解决你的问题,请参考以下文章
javaweb瀛︿範鎬荤粨锛堝崄涓€锛夆€斺€旇嚜瀹氫箟鏍囩寮€鍙戝簱
Spring Boot闆嗘垚smart-doc鐢熸垚api鏂囨。
Kiwi Syslog Web Access涓嶢ctive Directory闆嗘垚璁よ瘉