springboot整合redis集群详解

向右看齐 2022-02-24 10:06 948阅读 0赞

springboot整合redis集群详解(附源码)

该篇文章接上一篇文正:springboot2.0+spring cloud+eureka(分布式项目)项目搭建详细教程(附加源码),本篇文章的内容下面也会有源码(此处不涉及如何搭建redis集群相关教程,请另行百度),请读者放心;

整合流程:

  • 添加redis依赖;
  • 配置文件修改(yml文件中添加redis相关属性信息)
  • 获取redis集群相关对象,并对redis进行操作 ;

以下为详细步骤:

- 第一步:添加redis依赖;
在这里插入图片描述

  1. <dependency>
  2. <!-- springboot的parents依赖中有对版本号的控制,此处不需要添加版本号相关信息 -->
  3. <groupId>redis.clients</groupId>
  4. <artifactId>jedis</artifactId>
  5. </dependency>

- 第二步:yml配置文件信息添加,配置文件中信息的具体含义已经在注释中写清楚;
在这里插入图片描述

  1. spring:
  2. redis:
  3. cluster:
  4. #设置key的生存时间,当key过期时,它会被自动删除;
  5. expire-seconds: 120
  6. #设置命令的执行时间,如果超过这个时间,则报错;
  7. command-timeout: 5000
  8. #设置redis集群的节点信息,其中namenode为域名解析,通过解析域名来获取相应的地址;
  9. nodes: namenode22:6379,datanode23:6379,datanode24:6379,datanode25:6379,datanode26:6379,datanode27:6379
  • 第三步:编写相关java代码;
    • 1)、读取application.yml配置文件中的属性信息到bean中,并注入到spring容器;

    package com.example.springbootdemoconsumer.redisConfig;

  1. import org.springframework.boot.context.properties.ConfigurationProperties;
  2. import org.springframework.stereotype.Component;
  3. /**
  4. * @author YangPeng
  5. * @Title: RedisProperties
  6. * @ProjectName springbootdemo
  7. * @Description: 使用ConfigurationProperties注解读取yml文件中的字段值,并使用Component注入到spring容器中;
  8. * @date 2019/4/3-17:52
  9. */
  10. //依赖注入
  11. @Component
  12. //该注解用于读取配置文件中的属性,其中prefix表示前缀;
  13. @ConfigurationProperties(prefix = "spring.redis.cluster")
  14. public class RedisProperties {
  15. private int expireSeconds;
  16. private String nodes;
  17. private int commandTimeout;
  18. public int getExpireSeconds() {
  19. return expireSeconds;
  20. }
  21. public void setExpireSeconds(int expireSeconds) {
  22. this.expireSeconds = expireSeconds;
  23. }
  24. public String getNodes() {
  25. return nodes;
  26. }
  27. public void setNodes(String nodes) {
  28. this.nodes = nodes;
  29. }
  30. public int getCommandTimeout() {
  31. return commandTimeout;
  32. }
  33. public void setCommandTimeout(int commandTimeout) {
  34. this.commandTimeout = commandTimeout;
  35. }
  36. @Override
  37. public String toString() {
  38. return "RedisProperties{" +
  39. "expireSeconds=" + expireSeconds +
  40. ", nodes='" + nodes + '\'' +
  41. ", commandTimeout=" + commandTimeout +
  42. '}';
  43. }
  44. }
    • 2)、根据注入的RedisProperties对象来获取JedisCluster对象;

    package com.example.springbootdemoconsumer.redisConfig;

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import redis.clients.jedis.HostAndPort;
  5. import redis.clients.jedis.JedisCluster;
  6. import java.util.HashSet;
  7. import java.util.Set;
  8. /**
  9. * @Title: RedisConfig
  10. * @ProjectName springbootdemo
  11. * @Description: TODO
  12. * @author YangPeng
  13. * @date 2019/4/3-18:17
  14. */
  15. @Configuration
  16. public class RedisConfig {
  17. @Autowired
  18. private RedisProperties redisProperties;
  19. @Bean
  20. public JedisCluster getJedisCluster(){
  21. //获取redis集群的ip及端口号等相关信息;
  22. String[] serverArray = redisProperties.getNodes().split(",");
  23. Set<HostAndPort> nodes = new HashSet<>();
  24. //遍历add到HostAndPort中;
  25. for (String ipPort : serverArray) {
  26. String[] ipPortPair = ipPort.split(":");
  27. nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
  28. }
  29. //构建对象并返回;
  30. return new JedisCluster(nodes, redisProperties.getCommandTimeout());
  31. }
  32. }
    • 3)、创建一个操作redis集群的接口,并对该接口进行实现,实现类中写了对redis操作的主要的方法;
      接口如下:

    package com.example.springbootdemoconsumer.redisConfig;

  1. /**
  2. * @author YangPeng
  3. * @Title: JedisClient
  4. * @ProjectName springbootdemo
  5. * @Description: TODO
  6. * @date 2019/4/3-18:29
  7. */
  8. public interface JedisClient {
  9. String set(String key, String value);
  10. String get(String key);
  11. Boolean exists(String key);
  12. Long expire(String key, int seconds);
  13. Long ttl(String key);
  14. Long incr(String key);
  15. Long hset(String key, String field, String value);
  16. String hget(String key, String field);
  17. Long hdel(String key, String... field);
  18. }

实现类如下:

  1. package com.example.springbootdemoconsumer.service;
  2. import com.example.springbootdemoconsumer.redisConfig.JedisClient;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.stereotype.Component;
  6. import redis.clients.jedis.JedisCluster;
  7. /**
  8. * @Title: JedisClientCluster
  9. * @ProjectName springbootdemo
  10. * @Description: TODO
  11. * @author YangPeng
  12. * @date 2019/4/3-18:30
  13. */
  14. @Component
  15. public class JedisClientCluster implements JedisClient {
  16. @Autowired
  17. private JedisCluster jedisCluster;
  18. @Override
  19. public String set(String key, String value) {
  20. return jedisCluster.set(key, value);
  21. }
  22. @Override
  23. public String get(String key) {
  24. return jedisCluster.get(key);
  25. }
  26. @Override
  27. public Boolean exists(String key) {
  28. return jedisCluster.exists(key);
  29. }
  30. @Override
  31. public Long expire(String key, int seconds) {
  32. return jedisCluster.expire(key, seconds);
  33. }
  34. @Override
  35. public Long ttl(String key) {
  36. return jedisCluster.ttl(key);
  37. }
  38. @Override
  39. public Long incr(String key) {
  40. return jedisCluster.incr(key);
  41. }
  42. @Override
  43. public Long hset(String key, String field, String value) {
  44. return jedisCluster.hset(key, field, value);
  45. }
  46. @Override
  47. public String hget(String key, String field) {
  48. return jedisCluster.hget(key, field);
  49. }
  50. @Override
  51. public Long hdel(String key, String... field) {
  52. return jedisCluster.hdel(key, field);
  53. }
  54. }
  • 第四步:编写Controller,进行测试;

    package com.example.springbootdemoconsumer.controller;

  1. import com.example.springbootdemoconsumer.redisConfig.RedisConfig;
  2. import com.example.springbootdemoconsumer.redisConfig.RedisProperties;
  3. import com.example.springbootdemoconsumer.service.JedisClientCluster;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. /**
  8. * @Title: RedisController
  9. * @ProjectName springbootdemo
  10. * @Description: TODO
  11. * @author YangPeng
  12. * @date 2019/4/3-17:55
  13. */
  14. @RestController
  15. public class RedisController {
  16. @Autowired
  17. private RedisProperties redisProperties;
  18. @Autowired
  19. private RedisConfig redisConfig;
  20. @Autowired
  21. private JedisClientCluster jedisClientCluster;
  22. @RequestMapping(value = "getRedisValue")
  23. public String getRedisValue(){
  24. System.out.println(redisProperties.toString());
  25. System.out.println(redisConfig.getJedisCluster().getClusterNodes());
  26. System.out.println(jedisClientCluster.get("yp"));
  27. jedisClientCluster.set("12","12");
  28. System.out.println(jedisClientCluster.get("12"));
  29. return jedisClientCluster.get("12");
  30. }
  31. }

输出如下:
在这里插入图片描述至此,springboot整合redis集群讲解完毕,如有问题,请留言,谢谢(ps:源码地址:https://github.com/yanpgeng/springbootdemo 该源码中包含springboot+springcloud+fegin等相关内容,后续会对该工程进行持续的完善,比如多数据源,整合jsp、token等相关内容)

发表评论

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

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

相关阅读

    相关 SpringBoot整合redis

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

    相关 springboot整合redis

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