spring boot:浣跨敤澶氫釜redis鏁版嵁婧?spring boot 2.3.1)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot:浣跨敤澶氫釜redis鏁版嵁婧?spring boot 2.3.1)相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/active' title='active'>active
col store ini pre user action config creat涓€锛屼粈涔堟儏鍐典笅闇€瑕佷娇鐢ㄥ涓猺edis鏁版嵁婧?
涓轰簡缂撳瓨鏁版嵁锛岄€氬父鎴戜滑浼氬湪绾夸笂浣跨敤澶氫釜redis鐨刢luster,
姣忎釜cluster涓紦瀛樹笉鍚岀殑鏁版嵁锛屼互鏂逛究绠$悊.
渚嬪锛氭垜浠紦瀛樹簡鏉傚織鏂囩珷/鍟嗗搧淇℃伅/鍒嗙被椤甸潰
鍚屾椂鎴戜滑鍙堜娇鐢ㄤ竴涓猺edis cluster浣滀负鍒嗗竷寮弒ession
杩欓噷灏变細鏈夊涓猺edis鏁版嵁婧愬湪椤圭洰涓?/p>
璇存槑锛氬垬瀹忕紨鐨勬灦鏋勬.鏋楁槸涓€涓笓娉ㄦ灦鏋勭殑鍗氬锛屽湴鍧€锛?a href="https://www.cnblogs.com/architectforest">https://www.cnblogs.com/architectforest
瀵瑰簲鐨勬簮鐮佸彲浠ヨ闂繖閲岃幏鍙栵細 https://github.com/liuhongdi/
璇存槑锛氫綔鑰?鍒樺畯缂?閭: 371125307@qq.com
浜岋紝婕旂ず椤圭洰璇存槑:
1锛岄」鐩湴鍧€:
https://github.com/liuhongdi/multiredissource
2, 椤圭洰鍘熺悊
瀛樺偍session浣跨敤redis闆嗙兢锛?/p>
鍙﹀浣跨敤涓や釜redis瀹炰緥鍋氱紦瀛?/p>
鍏充簬redis闆嗙兢鐨勬惌寤鸿鍙傝:
https://www.cnblogs.com/architectforest/p/13196749.html
3, 椤圭洰缁撴瀯:
濡傚浘:
涓夛紝椤圭洰閰嶇疆鏂囦欢璇存槑:
application.properties
#default redis,for session
spring.redis.cluster.nodes=172.17.0.2:6379,172.17.0.3:6379,172.17.0.4:6379,172.17.0.5:6379,172.17.0.6:6379,172.17.0.7:6379 spring.redis.cluster.max-redirects=3 spring.redis.password=lhddemo spring.redis.database=0 spring.session.store-type=redis spring.redis.lettuce.pool.max-active=8 spring.redis.lettuce.pool.max-wait=1 spring.redis.lettuce.pool.max-idle=8 spring.redis.lettuce.pool.min-idle=0 #redis1 spring.redis1.host=127.0.0.1 spring.redis1.port=6379 spring.redis1.password=lhddemo spring.redis1.database=0 spring.redis1.lettuce.pool.max-active=8 spring.redis1.lettuce.pool.max-wait=1 spring.redis1.lettuce.pool.max-idle=8 spring.redis1.lettuce.pool.min-idle=0 #redis2 spring.redis2.host=127.0.0.1 spring.redis2.port=6380 spring.redis2.password=lhddemo spring.redis2.database=0 spring.redis2.lettuce.pool.max-active=8 spring.redis2.lettuce.pool.max-wait=1 spring.redis2.lettuce.pool.max-idle=8 spring.redis2.lettuce.pool.min-idle=0
璇存槑锛氬叡3涓猺edis,
瀛樺偍session鐨勯泦缇ゆ湁6涓妭鐐?3涓?浠?:
172.17.0.2:6379,
172.17.0.3:6379,
172.17.0.4:6379,
172.17.0.5:6379,
172.17.0.6:6379,
172.17.0.7:6379
redis1鏄潪闆嗙兢缁撶偣:
127.0.0.1:6379
redis2鏄潪闆嗙兢缁撶偣:
127.0.0.1:6380
鍥涳紝java浠g爜璇存槑
1,RedisConfig.java
@Configuration public class RedisConfig { @Bean @Primary public LettuceConnectionFactory redissessionLettuceConnectionFactory(RedisClusterConfiguration redisSessionRedisConfig, GenericObjectPoolConfig redisSessionPoolConfig) { LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder().commandTimeout(Duration.ofMillis(100)) .poolConfig(redisSessionPoolConfig).build(); return new LettuceConnectionFactory(redisSessionRedisConfig, clientConfig); } @Bean public RedisTemplate<String, String> redisSessionTemplate( @Qualifier("redissessionLettuceConnectionFactory") LettuceConnectionFactory redissessionLettuceConnectionFactory) { RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); //浣跨敤StringRedisSerializer鏉ュ簭鍒楀寲鍜屽弽搴忓垪鍖杛edis鐨刱e redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new StringRedisSerializer()); //寮€鍚簨鍔?/span> redisTemplate.setEnableTransactionSupport(true); redisTemplate.setConnectionFactory(redissessionLettuceConnectionFactory); redisTemplate.afterPropertiesSet(); return redisTemplate; } @Bean @ConditionalOnBean(name = "redis1RedisConfig") public LettuceConnectionFactory redis1LettuceConnectionFactory(RedisStandaloneConfiguration redis1RedisConfig, GenericObjectPoolConfig redis1PoolConfig) { LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder().commandTimeout(Duration.ofMillis(100)) .poolConfig(redis1PoolConfig).build(); return new LettuceConnectionFactory(redis1RedisConfig, clientConfig); } @Bean public RedisTemplate<String, String> redis1Template( @Qualifier("redis1LettuceConnectionFactory") LettuceConnectionFactory redis1LettuceConnectionFactory) { RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); //浣跨敤StringRedisSerializer鏉ュ簭鍒楀寲鍜屽弽搴忓垪鍖杛edis鐨刱e redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new StringRedisSerializer()); //寮€鍚簨鍔?/span> redisTemplate.setEnableTransactionSupport(true); redisTemplate.setConnectionFactory(redis1LettuceConnectionFactory); redisTemplate.afterPropertiesSet(); return redisTemplate; } @Bean @ConditionalOnBean(name = "redis2RedisConfig") public LettuceConnectionFactory redis2LettuceConnectionFactory(RedisStandaloneConfiguration redis2RedisConfig, GenericObjectPoolConfig redis2PoolConfig) { LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder().commandTimeout(Duration.ofMillis(100)) .poolConfig(redis2PoolConfig).build(); return new LettuceConnectionFactory(redis2RedisConfig, clientConfig); } @Bean @ConditionalOnBean(name = "redis2LettuceConnectionFactory") public RedisTemplate<String, String> redis2Template( @Qualifier("redis2LettuceConnectionFactory") LettuceConnectionFactory redis2LettuceConnectionFactory) { RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); redisTemplate.setValueSerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new StringRedisSerializer()); //浣跨敤StringRedisSerializer鏉ュ簭鍒楀寲鍜屽弽搴忓垪鍖杛edis鐨刱e redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); //寮€鍚簨鍔?/span> redisTemplate.setEnableTransactionSupport(true); redisTemplate.setConnectionFactory(redis2LettuceConnectionFactory); redisTemplate.afterPropertiesSet(); return redisTemplate; } @Configuration public static class RedisSessionConfig { @Value("${spring.redis.cluster.nodes}") private String nodes; @Value("${spring.redis.cluster.max-redirects}") private Integer maxRedirects; @Value("${spring.redis.password}") private String password; @Value("${spring.redis.database}") private Integer database; @Value("${spring.redis.lettuce.pool.max-active}") private Integer maxActive; @Value("${spring.redis.lettuce.pool.max-idle}") private Integer maxIdle; @Value("${spring.redis.lettuce.pool.max-wait}") private Long maxWait; @Value("${spring.redis.lettuce.pool.min-idle}") private Integer minIdle; @Bean public GenericObjectPoolConfig redisSessionPoolConfig() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(maxActive); config.setMaxIdle(maxIdle); config.setMinIdle(minIdle); config.setMaxWaitMillis(maxWait); return config; } @Bean public RedisClusterConfiguration redisSessionRedisConfig() { RedisClusterConfiguration config = new RedisClusterConfiguration(); String[] sub = nodes.split(","); List<RedisNode> nodeList = new ArrayList<>(sub.length); String[] tmp; for (String s : sub) { tmp = s.split(":"); nodeList.add(new RedisNode(tmp[0], Integer.valueOf(tmp[1]))); } config.setClusterNodes(nodeList); config.setMaxRedirects(maxRedirects); config.setPassword(RedisPassword.of(password)); return config; } } @Configuration public static class Redis1Config { @Value("${spring.redis1.host}") private String host; @Value("${spring.redis1.port}") private Integer port; @Value("${spring.redis1.password}") private String password; @Value("${spring.redis1.database}") private Integer database; @Value("${spring.redis1.lettuce.pool.max-active}") private Integer maxActive; @Value("${spring.redis1.lettuce.pool.max-idle}") private Integer maxIdle; @Value("${spring.redis1.lettuce.pool.max-wait}") private Long maxWait; @Value("${spring.redis1.lettuce.pool.min-idle}") private Integer minIdle; @Bean public GenericObjectPoolConfig redis1PoolConfig() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(maxActive); config.setMaxIdle(maxIdle); config.setMinIdle(minIdle); config.setMaxWaitMillis(maxWait); return config; } @Bean public RedisStandaloneConfiguration redis1RedisConfig() { RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(); config.setHostName(host); config.setPassword(RedisPassword.of(password)); config.setPort(port); config.setDatabase(database); return config; } } @Configuration @ConditionalOnProperty(name = "host", prefix = "spring.redis2") public static class Redis2Config { @Value("${spring.redis2.host}") private String host; @Value("${spring.redis2.port}") private Integer port; @Value("${spring.redis2.password}") private String password; @Value("${spring.redis2.database}") private Integer database; @Value("${spring.redis2.lettuce.pool.max-active}") private Integer maxActive; @Value("${spring.redis2.lettuce.pool.max-idle}") private Integer maxIdle; @Value("${spring.redis2.lettuce.pool.max-wait}") private Long maxWait; @Value("${spring.redis2.lettuce.pool.min-idle}") private Integer minIdle; @Bean public GenericObjectPoolConfig redis2PoolConfig() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(maxActive); config.setMaxIdle(maxIdle); config.setMinIdle(minIdle); config.setMaxWaitMillis(maxWait); return config; } @Bean public RedisStandaloneConfiguration redis2RedisConfig() { RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(); config.setHostName(host); config.setPassword(RedisPassword.of(password)); config.setPort(port); config.setDatabase(database); return config; } } }
璇存槑锛?/p>
鍒嗗埆鏋勯€犱簡涓変釜redisTemplate:
redisSessionTemplate:璁块棶redis session cluster
redis1Template:璁块棶redis1
redis2Template:璁块棶redis2
鍥犱负瑕佷緵session榛樿浣跨敤锛?br />鎵€浠ョ粰绗竴涓狶ettuceConnectionFactory鍔犱笂@Primary娉ㄨВ
cluster鐨勯厤缃浣跨敤RedisClusterConfiguration绫伙紝
娉ㄦ剰涓?RedisStandaloneConfiguration鍖哄垎
2,CacheController.java
@RestController @RequestMapping("/cache") public class CacheController { @Resource RedisTemplate<String, String> redis1Template; @Resource RedisTemplate<String, String> redis2Template; /* * get redis1 cache */ @RequestMapping("/redis1get") public String redis1Get(HttpServletRequest request){ String goodsname = redis1Template.opsForValue().get("goodsname1"); return goodsname; } /* * write redis1 cache */ @RequestMapping("/redis1set/{name}") public String redis1Set(@PathVariable String name) { //request.getSession().setAttribute("goods", name); redis1Template.opsForValue().set("goodsname1",name); return "ok"; } /* * get redis2 cache * */ @RequestMapping("/redis2get") public String redis2Get(HttpServletRequest request){ String goodsname2 = redis2Template.opsForValue().get("goodsname2"); return goodsname2; } /* * write redis2 cache * */ @RequestMapping("/redis2set/{name}") public String redis2Set(@PathVariable String name) { //request.getSession().setAttribute("goods", name); redis2Template.opsForValue().set("goodsname2",name); return "ok"; } }
璇存槑锛氬redis1鍜宺edis2鍒嗗埆璇诲彇鍜屽啓鍏?/p>
3,SessionController.java
@RestController @RequestMapping("/session") public class SessionController { /* * read session * */ @RequestMapping("/get") public Object getSession(HttpServletRequest request){ Map<String, Object> map = new HashMap<>(); map.put("sessionId", request.getSession().getId()); map.put("user", request.getSession().getAttribute("user")); map.put("maxInactiveInterval", request.getSession().getMaxInactiveInterval()); //map.put("ttl", request.getSession().); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = sdf.format(new Date(request.getSession().getCreationTime())); map.put("creationTime", time); return map; } /* * write session * */ @RequestMapping("/set/{name}") public String setSession(@PathVariable String name, HttpServletRequest request) { request.getSession().setAttribute("user", name); return "ok"; } }
璇存槑锛氬session鐨勮鍙栧拰鍐欏叆
浜旓紝澶歳edis鏁版嵁婧愭晥鏋滄祴璇?/h2>
1,鏌ョ湅涓変釜redis鐨勬暟鎹?
redissession
[root@redis4 /]# /usr/local/soft/redis-6.0.5/bin/redis-cli -a lhddemo -c --cluster call 172.17.0.2:6379 keys * Warning: Using a password with 鈥?a鈥?or 鈥?u鈥?option on the command line interface may not be safe. >>> Calling keys * 172.17.0.2:6379: 172.17.0.4:6379: 172.17.0.3:6379: 172.17.0.6:6379: 172.17.0.7:6379: 172.17.0.5:6379:
redis1:
127.0.0.1:6379> keys *
(empty list or set)
redis2:
127.0.0.1:6380> keys *
(empty list or set)
2,娴嬭瘯session锛?/p>
璁块棶:http://127.0.0.1:8080/session/set/thislaoliu
璁剧疆涓€涓猻ession鍊间负thislaoliu:
璁块棶:http://127.0.0.1:8080/session/get
鏌ヨ鎴戜滑璁剧疆鐨剆ession鍊兼槸鍚︾敓鏁?
浠巖edis鐨勬帶鍒跺彴妫€鏌ュ啓鍏ユ儏鍐?
[root@redis4 /]# /usr/local/soft/redis-6.0.5/bin/redis-cli -a lhddemo -c --cluster call 172.17.0.2:6379 keys * Warning: Using a password with 鈥?/span>-a鈥?/span> or 鈥?/span>-u鈥?/span> option on the command line interface may not be safe. >>> Calling keys * 172.17.0.2:6379: spring:session:sessions:expires:97a889f8-7122-4248-a5fc-4e559cb429e5 172.17.0.4:6379: spring:session:sessions:97a889f8-7122-4248-a5fc-4e559cb429e5 172.17.0.3:6379: spring:session:expirations:1593321720000 172.17.0.6:6379: spring:session:sessions:expires:97a889f8-7122-4248-a5fc-4e559cb429e5 172.17.0.7:6379: spring:session:expirations:1593321720000 172.17.0.5:6379: spring:session:sessions:97a889f8-7122-4248-a5fc-4e559cb429e5
鎴戜滑杩炴帴鍒?72.17.0.4锛屾煡璇ession鐨勫€?
[root@redis3 /]# /usr/local/soft/redis-6.0.5/bin/redis-cli -c -h 172.17.0.4
鐪媠ession鐨勫唴瀹?
172.17.0.4:6379> hgetall spring:session:sessions:97a889f8-7122-4248-a5fc-4e559cb429e5 1) "sessionAttr:user" 2) "xacxedx00x05tx00 thislaoliu" 3) "creationTime" 4) "xacxedx00x05srx00x0ejava.lang.Long;x8bxe4x90xccx8f#xdfx02x00x01Jx00x05valuexrx00x10java.lang.Numberx86xacx95x1dx0bx94xe0x8bx02x00x00xpx00x00x01rxf9CWxb7" 5) "maxInactiveInterval" 6) "xacxedx00x05srx00x11java.lang.Integerx12xe2xa0xa4xf7x81x878x02x00x01Ix00x05valuexrx00x10java.lang.Numberx86xacx95x1dx0bx94xe0x8bx02x00x00xpx00x00a" 7) "lastAccessedTime" 8) "xacxedx00x05srx00x0ejava.lang.Long;x8bxe4x90xccx8f#xdfx02x00x01Jx00x05valuexrx00x10java.lang.Numberx86xacx95x1dx0bx94xe0x8bx02x00x00xpx00x00x01rxf9D)n"
鍙互鐪嬪埌user鐨勫€兼槸鎴戜滑鎵€璁剧疆鐨?thislaoliu
3,娴嬭瘯redis1
璁块棶:http://127.0.0.1:8080/cache/redis1set/cup1
璁剧疆涓€涓猭ey,鍊间负:cup1
璁块棶:http://127.0.0.1:8080/cache/redis1get
杩斿洖鎴戜滑璁剧疆鐨勫€?
杩炴帴鍒皉edis鎺у埗鍙?
[liuhongdi@localhost ~]$ /usr/local/soft/redis/bin/redis-cli
浠巖edis鎺у埗鍙版煡鐪媖v
127.0.0.1:6379> get goodsname1 "cup1"
4,娴嬭瘯redis2
璁块棶:http://127.0.0.1:8080/cache/redis2set/phone1
璁剧疆涓€涓猭ey,鍊间负:phone1
璁块棶:http://127.0.0.1:8080/cache/redis2get
杩斿洖鎴戜滑璁剧疆鐨勫€?
杩炴帴鍒皉edis鎺у埗鍙?/p>
[root@localhost etc]# /usr/local/soft/redis/bin/redis-cli -p 6380
浠庢帶鍒跺彴鏌ョ湅鍊?/p>
127.0.0.1:6380> get goodsname2 "phone1"
鍏紝鏌ョ湅spring boot鐨勭増鏈?/h2>
. ____ _ __ _ _
/\ / ___鈥?/span>_ __ _ _(_)_ __ __ _
( ( )\___ | 鈥?/span>_ | 鈥?/span>_| | 鈥?/span>_ / _` |
\/ ___)| |_)| | | | | || (_| | ) ) ) )
鈥?/span> |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.1.RELEASE)
. ____ _ __ _ _ /\ / ___鈥?/span>_ __ _ _(_)_ __ __ _ ( ( )\___ | 鈥?/span>_ | 鈥?/span>_| | 鈥?/span>_ / _` | \/ ___)| |_)| | | | | || (_| | ) ) ) ) 鈥?/span> |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.1.RELEASE)
以上是关于spring boot:浣跨敤澶氫釜redis鏁版嵁婧?spring boot 2.3.1)的主要内容,如果未能解决你的问题,请参考以下文章
[WebSocket]浣跨敤WebSocket瀹炵幇瀹炴椂澶氫汉绛旈瀵规垬娓告垙
JS 浣跨敤xlsx.core.js 鏁版嵁瀵煎嚭鍒癳xcel