Srping boot 整合redis集群

亦凉 2022-04-03 05:27 259阅读 0赞

得益于springboot的自动配置功能,使得框架搭建变得更简单方便。

仅需三步:①pom.xml文件引入依赖 ②pplication.properties

1、代码结构

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ppYWhhbzExODY_size_16_color_FFFFFF_t_70

2、pom.xml

  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.1.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.redis</groupId>
  12. <artifactId>demo</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>redisDemo</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-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-test</artifactId>
  27. <scope>test</scope>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-data-redis</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.data</groupId>
  35. <artifactId>spring-data-redis</artifactId>
  36. <version>2.0.8.RELEASE</version>
  37. <scope>test</scope>
  38. </dependency>
  39. </dependencies>
  40. <build>
  41. <plugins>
  42. <plugin>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-maven-plugin</artifactId>
  45. </plugin>
  46. </plugins>
  47. </build>
  48. </project>

3、 application.properties

  1. spring.redis.cluster.nodes=192.168.251.51:6371,192.168.251.51:6372,192.168.251.51:6373,192.168.251.51:6374,192.168.251.51:6375,192.168.251.51:6376
  2. spring.redis.password=

4、测试类

  1. package com.redis.demo;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.boot.test.context.SpringBootTest;
  5. import org.springframework.data.redis.core.StringRedisTemplate;
  6. import org.springframework.test.context.junit4.SpringRunner;
  7. import javax.annotation.Resource;
  8. import java.util.concurrent.TimeUnit;
  9. /**
  10. * Created by Liuxd on 2018-12-20.
  11. */
  12. @RunWith(SpringRunner.class)
  13. @SpringBootTest
  14. public class ApplicationTests {
  15. @Resource(name = "stringRedisTemplate")
  16. private StringRedisTemplate stringRedisTemplate;
  17. @Test
  18. public void TestSet() {
  19. String key = "IKey";
  20. stringRedisTemplate.opsForValue().set(key, "123");
  21. stringRedisTemplate.expire(key, 60, TimeUnit.SECONDS);
  22. String str = stringRedisTemplate.opsForValue().get(key);
  23. System.out.println("打印缓存中的值:" + str);
  24. }
  25. }

5、打印结果

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ppYWhhbzExODY_size_16_color_FFFFFF_t_70 1

发表评论

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

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

相关阅读

    相关 SpringBoot整合redis

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

    相关 springboot整合redis

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