SpringBoot整合Redis集群

系统管理员 2022-05-30 03:38 298阅读 0赞

1.在pom.xml中加入redis的依赖
2.在application.properties加入整合redis的配置。(spring.redis.cluster.nodes=….)
3.新建一个RedisConfig.java文件,在此类的上面加入@Configuration注解,则此类相当于applicationContext.xml配置文件。

4.在RedisConfig类中写一个getJedisCluster方法,方法上加入@Bean注解,便可通过DI注入了。如下:

  1. package com.example.demo.config;
  2. import java.util.HashSet;
  3. import java.util.Set;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import redis.clients.jedis.HostAndPort;
  8. import redis.clients.jedis.JedisCluster;
  9. @Configuration//此注解相当于将此类变为applicationContext.xml,即配置文件
  10. public class RedisConfig {
  11. //注入集群节点信息:即application.properties中的spring.redis.cluster.nodes
  12. @Value("${spring.redis.cluster.nodes}")
  13. private String clusterNodes;
  14. @Bean
  15. public JedisCluster getJedisCluster(){
  16. String cNodes[]=clusterNodes.split(",");
  17. Set<HostAndPort> nodes=new HashSet<>();
  18. for(String node:cNodes){
  19. String hp[]=node.split(":");
  20. nodes.add(new HostAndPort(hp[0], Integer.parseInt(hp[1])));
  21. }
  22. JedisCluster jedisCluster=new JedisCluster(nodes);
  23. return jedisCluster;
  24. }
  25. }

5.在controller类里,注入JedisCluster集群对象

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. <groupId>com.example</groupId>
  6. <artifactId>SpringBootDemo</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>SpringBootDemo</name>
  10. <description>Demo project for Spring Boot</description>
  11. <!--springboot父工程相关依赖-->
  12. <parent>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-parent</artifactId>
  15. <version>2.0.0.RELEASE</version>
  16. <relativePath/> <!-- lookup parent from repository -->
  17. </parent>
  18. <properties>
  19. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  20. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  21. <java.version>1.8</java.version>
  22. </properties>
  23. <dependencies>
  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. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-devtools</artifactId>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-starter-freemarker</artifactId>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-starter-data-redis</artifactId>
  44. <version>1.5.2.RELEASE</version>
  45. </dependency>
  46. <!-- springboot整合mybatisk框架所需的依赖 -->
  47. <dependency>
  48. <groupId>org.mybatis.spring.boot</groupId>
  49. <artifactId>mybatis-spring-boot-starter</artifactId>
  50. <version>1.1.1</version>
  51. </dependency>
  52. <!-- mysql的依赖 -->
  53. <dependency>
  54. <groupId>mysql</groupId>
  55. <artifactId>mysql-connector-java</artifactId>
  56. </dependency>
  57. </dependencies>
  58. <build>
  59. <plugins>
  60. <plugin>
  61. <groupId>org.springframework.boot</groupId>
  62. <artifactId>spring-boot-maven-plugin</artifactId>
  63. </plugin>
  64. </plugins>
  65. </build>
  66. </project>

application.properties文件如下:

  1. #springboot整合mybatis框架
  2. #在pom.xml文件中加入整合mybatis的依赖
  3. #加载mybatis的配置文件
  4. mybatis.mapper-locations=classpath:mapper/*Mapper.xml
  5. mybatis.type-aliases-package=com.example.demo.pojo
  6. #数据源的配置
  7. spring.datasource.url=jdbc:mysql://localhost:3306/test
  8. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  9. spring.datasource.data-username=root
  10. spring.datasource.data-password=123456
  11. #整合Redis
  12. #spring.redis.host=192.168.66.66
  13. #spring.redis.port=6379
  14. #整合Redis集群
  15. spring.redis.cluster.nodes= 192.168.66.66:7001,192.168.66.66:7002,192.168.66.66:7002,192.168.66.66:7003,192.168.66.66:7004,192.168.66.66:7005,192.168.66.66:7006,192.168.66.66:7007,192.168.66.66:7008

发表评论

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

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

相关阅读

    相关 SpringBoot整合redis

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

    相关 springboot整合redis

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