SpringBoot整合redis集群并使用StringRedisTemplate和RedisTemplate简单操作Redis集群

墨蓝 2022-03-09 04:08 964阅读 0赞

Redis集群安装参考:https://blog.csdn.net/weixin_42465125/article/details/87885133

pom依赖:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.3.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>cn.cuit.redis</groupId>
  12. <artifactId>redis-demo</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>redis-demo</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-data-redis</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-test</artifactId>
  31. <scope>test</scope>
  32. </dependency>
  33. </dependencies>
  34. <build>
  35. <plugins>
  36. <plugin>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-maven-plugin</artifactId>
  39. </plugin>
  40. </plugins>
  41. </build>
  42. </project>

properties配置文件:

  1. spring.application.name=spring-boot-redis
  2. # spring.redis.host=192.168.174.221
  3. spring.redis.cluster.nodes=192.168.174.221:6381, 192.168.174.221:6382, 192.168.174.221:6383, 192.168.174.221:6384, 192.168.174.221:6385, 192.168.174.221:6386
  4. # 新版
  5. spring.redis.jedis.pool.max-wait=-1
  6. spring.redis.jedis.pool.max-active=300
  7. spring.redis.jedis.pool.max-idle=100
  8. spring.redis.jedis.pool.min-idle=20
  9. ## 连接超时时间(毫秒)
  10. spring.redis.timeout=60000
  11. ## Redis数据库索引(默认为0)
  12. spring.redis.database=0
  13. #旧版
  14. ## 连接池最大连接数(使用负值表示没有限制)
  15. #spring.redis.pool.max-active=300
  16. ## 连接池最大阻塞等待时间(使用负值表示没有限制)
  17. #spring.redis.pool.max-wait=-1
  18. ## 连接池中的最大空闲连接
  19. #spring.redis.pool.max-idle=100
  20. ## 连接池中的最小空闲连接
  21. #spring.redis.pool.min-idle=20

启动类:

  1. package cn.cuit.redis.redisdemo;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class RedisDemoApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(RedisDemoApplication.class, args);
  8. }
  9. }

测试类:

  1. package cn.cuit.redis.redisdemo.crud;
  2. import javax.annotation.PostConstruct;
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import org.springframework.data.redis.core.ListOperations;
  8. import org.springframework.data.redis.core.RedisTemplate;
  9. import org.springframework.data.redis.core.StringRedisTemplate;
  10. import org.springframework.data.redis.core.ValueOperations;
  11. import org.springframework.test.context.junit4.SpringRunner;
  12. /**
  13. * 这只是一个简单例子,更多参考如下: https://www.cnblogs.com/zwcry/p/9176250.html
  14. * https://www.cnblogs.com/superfj/p/9232482.html
  15. * https://www.cnblogs.com/jiangbei/p/8601107.html
  16. */
  17. @RunWith(SpringRunner.class)
  18. @SpringBootTest
  19. public class RedisClusterTest {
  20. @Autowired
  21. private StringRedisTemplate stringRedisTemplate;
  22. @Autowired
  23. private RedisTemplate<String, String> redisTemplate;
  24. ValueOperations<String, String> stringRedis;
  25. ListOperations<String, String> listRedis;
  26. /**
  27. * 被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,
  28. * 类似于Serclet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后运行。
  29. */
  30. @PostConstruct
  31. public void init() {
  32. stringRedis = stringRedisTemplate.opsForValue();
  33. listRedis = redisTemplate.opsForList();
  34. }
  35. @Test
  36. public void testPutString() {
  37. stringRedis.set("str:name", "CUIT");
  38. }
  39. @Test
  40. public void testGetString() {
  41. System.out.println(">>>>>>>>>>>>>>>>>>>>>" + stringRedis.get("str:name"));
  42. }
  43. @Test
  44. public void testPutList() {
  45. listRedis.leftPushAll("list:key", new String[] { "value1", "value2", "value3" });
  46. }
  47. @Test
  48. public void testPopFromList() {
  49. System.out.println(">>>>>>>>>>>>>>>>>>>>>" + listRedis.rightPop("list:key"));
  50. System.out.println(">>>>>>>>>>>>>>>>>>>>>" + listRedis.leftPop("list:key"));
  51. }
  52. }

测试结果:

testPutString:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjQ2NTEyNQ_size_16_color_FFFFFF_t_70

testGetString:

20190312115039500.png

testPutList:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjQ2NTEyNQ_size_16_color_FFFFFF_t_70 1

testPopFromList :

20190312114441520.png

********************************* 不积跬步无以至千里,不积小流无以成江海 *********************************

发表评论

表情:
评论列表 (有 0 条评论,964人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringBoot整合redis

    一:缓存的应用场景 1:什么是缓存? 在互联网场景下,尤其 2C 端大流量场景下,需要将一些经常展现和不会频繁变更的数据,存放在存取速率更快的地方。缓存就是一个存储器,在技

    相关 springboot整合redis

    一、redis集群原理 redis集群中的所有节点彼此互联,节点内部采用二进制协议优化传输速度和带宽,每个节点都可以与Java客户端连接。redis集群的数据分配采用哈希