查看原文
其他

SpringBoot集成Redis集群就这么Easy

孙亮亮 SpringForAll社区 2021-05-26

上一篇写了springboot与mongo的整合,那么这次就写一下springboot与redis的结合,这里使用的是redis集群模式(主从),主从环境的搭建,请参考redis集群搭建

搭建完redis集群环境后,开始springboot之旅

1、REDIS介绍

redis的介绍及应用场景参考 redis介绍

2、项目构建

我们还是从redis项目构建开始说起,首先还是进入的spring官网, 从这里开始构建项目,如下图

当然也可以自己添加pom文件,如下

  1. <dependency>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-data-redis</artifactId>

  4. </dependency>

3、添加配置文件

application.properties中

  1. #-----------------------

  2. # start redis cluster

  3. jedis.cluster.nodesString= 103.45.12.176:7000,47.88.221.76:7000,103.45.12.176:7001,47.88.221.76:7001,103.45.12.176:7002,47.88.221.76:7002

  4. jedis.cluster.testWhileIdle=true

  5. jedis.cluster.connectionTimeout=60

  6. jedis.cluster.soTimeout=60

  7. jedis.cluster.maxAttempts=1000

  8. jedis.cluster.password=12597758

  9. # end redis cluster config

  10. #---------------------------------

4、JEDIS配置类的编写

这里是核心jedisCluster这个bean的创建

  1. @Configuration

  2. @ConfigurationProperties(prefix = "jedis.cluster")

  3. public class JedisClusterConfig {

  4. private String nodesString;

  5. private Boolean testWhileIdle;

  6. private Integer connectionTimeout;

  7. private Integer soTimeout;

  8. private Integer maxAttempts;

  9. private String password;

  10. public Boolean getTestWhileIdle() {

  11. return testWhileIdle;

  12. }


  13. public void setTestWhileIdle(Boolean testWhileIdle) {

  14. this.testWhileIdle = testWhileIdle;

  15. }


  16. public Integer getConnectionTimeout() {

  17. return connectionTimeout;

  18. }


  19. public String getPassword() {

  20. return password;

  21. }


  22. public void setPassword(String password) {

  23. this.password = password;

  24. }


  25. public void setConnectionTimeout(Integer connectionTimeout) {

  26. this.connectionTimeout = connectionTimeout;

  27. }


  28. public Integer getSoTimeout() {

  29. return soTimeout;

  30. }


  31. public void setSoTimeout(Integer soTimeout) {

  32. this.soTimeout = soTimeout;

  33. }


  34. public Integer getMaxAttempts() {

  35. return maxAttempts;

  36. }


  37. public void setMaxAttempts(Integer maxAttempts) {

  38. this.maxAttempts = maxAttempts;

  39. }


  40. public String getNodesString() {

  41. return nodesString;

  42. }


  43. public void setNodesString(String nodesString) {

  44. this.nodesString = nodesString;

  45. }


  46. @Bean

  47. public JedisCluster jedisCluster() {


  48. String[] nodes = nodesString.split(",");

  49. Set<HostAndPort> nodeSet = new HashSet<HostAndPort>(nodes.length);


  50. for (String node : nodes) {

  51. String[] nodeAttrs = node.trim().split(":");

  52. HostAndPort hap = new HostAndPort(nodeAttrs[0], Integer.valueOf(nodeAttrs[1]));

  53. nodeSet.add(hap);

  54. }

  55. JedisPoolConfig poolConfig = new JedisPoolConfig();

  56. // TODO:password and poolconfig

  57. JedisCluster jc = new JedisCluster(nodeSet, connectionTimeout, soTimeout, maxAttempts,password, poolConfig);

  58. return jc;

  59. }

  60. }

5、通用接口的编写

经过这几篇的博客,可能也发现很多接口的定义,然后由不同的业务类去实现,面向接口的编程也是经历过近期的一个项目才有了比较深的理解,这些都是跟朱哥和军哥学习的,代码编写的规范,对于一个程序员来说,越早养成越好

  1. public interface NoSqlClient<T> {

  2. void set(final String key, final T value);


  3. void set(String key, T value, Long expiry);


  4. <T> boolean exist(Class<T> clazz, String key);


  5. T get(final String key, Class<T> clazz);


  6. <T> void delete(String key);


  7. <T> void hashSet(String key, String hashKey, T hashValue);


  8. Object hashGet(String key, String hashKey, Class<?> hashValueClazz);

  9. }

6、接下来是JEDIS接口的实现

  1. @Service

  2. public class JedisClusterClient<T> implements NoSqlClient<T> {

  3. protected final Logger logger = LoggerFactory.getLogger(this.getClass());


  4. @Autowired

  5. private JedisCluster jedisCluster;

  6. public void set(String key, T value){

  7. logger.info("-------------"+jedisCluster);

  8. jedisCluster.set(key,JSON.toJSONString(value,true));

  9. }


  10. @Override

  11. public void set(String key, T value, Long expiry) {


  12. }


  13. @Override

  14. public <T1> boolean exist(Class<T1> clazz, String key) {

  15. checkRedisKey(key);

  16. return jedisCluster.exists(key);

  17. }


  18. @Override

  19. public T get(String key, Class<T> clazz) {

  20. checkRedisKey(key);

  21. String value = jedisCluster.get(key);

  22. return JSON.parseObject(value, clazz);

  23. }


  24. @Override

  25. public <T1> void delete(String key) {

  26. checkRedisKey(key);

  27. jedisCluster.del(key);

  28. }


  29. @Override

  30. public <T1> void hashSet(String key, String hashKey, T1 hashValue) {

  31. checkRedisKey(key);

  32. jedisCluster.hset(key, hashKey, JSON.toJSONString(hashValue));

  33. }


  34. @Override

  35. public Object hashGet(String key, String hashKey, Class<?> hashValueClazz) {

  36. return null;

  37. }

  38. /**

  39. * 检查Redis参数

  40. *

  41. * @param key

  42. * @param value

  43. * @return

  44. */

  45. private String checkRedisNoSqlSupport(final String key, final T value) {

  46. checkRedisKey(key);

  47. if (value == null)

  48. throw new RedisException("value is null!");

  49. String stringValue = null;

  50. try {

  51. stringValue = JSON.toJSONString(value,true);

  52. } catch (JSONException e) {

  53. throw new RedisException("value toJSONString exception!");

  54. }

  55. if (StringUtils.isEmpty(stringValue))

  56. throw new RedisException("value is null!");

  57. return stringValue;

  58. }


  59. /**

  60. * 检查Redis key,redis key 长度建议不要超过1M,redis支持512M

  61. * @param key

  62. */

  63. private void checkRedisKey(final String key) {

  64. if (StringUtils.isEmpty(key))

  65. throw new RedisException("key is null!");

  66. if (key.length() > 1000000L)

  67. throw new RedisException("length of key greater than 1M!");

  68. }

  69. }

这里我只写了几个基础的核心配置,包括一些异常的处理,这里都没有贴出代码,当然代码在文章最后也会给出,不必担心

7、业务DAO的实现

不同的业务只需继承上面的类即可

  1. @Component

  2. public class UserInfoCacheDao extends JedisClusterClient<UserInfo> {

  3. public void setUserInfo(UserInfo userInfo){

  4. super.set("lessons.user:1",userInfo);

  5. }

  6. }

这里同样也是只写了一个set方法

8、单元测试

  1. @RunWith(SpringJUnit4ClassRunner.class)

  2. @SpringBootTest

  3. public class UserInfoCacheDaoTest {

  4. @Autowired

  5. private UserInfoCacheDao userInfoCacheDao;

  6. @Test

  7. public void setUserInfo() throws Exception {

  8. UserInfo userInfo = new UserInfo();

  9. userInfo.setAge(12);

  10. userInfo.setId(1000l);

  11. userInfo.setName("admin");

  12. userInfo.setNickname("管理员");

  13. userInfo.setPassword("123445");

  14. userInfoCacheDao.setUserInfo(userInfo);

  15. }


  16. }

添加完成后,去查看发现,已经有已经生成。

github中lessons-6

推荐:Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现

上一篇:一起了解下Java多线程基础

    您可能也对以下帖子感兴趣

    文章有问题?点此查看未经处理的缓存