Redis实践(4.1)SpringBoot 测试主从RedisTemplate

Redis
placeholder image
admin 发布于:2023-05-28 00:04:30
阅读:loading

前文中使用SpringBoot Data Redis项目集成了Redis的主从架构接入,本篇主要来测试一下整合后从项目中获取的RedisTemplate上下文信息以及操作redis相关的API,所以本文使用两种方式来获取RedisTemplate实例,分别是使用Spring注解和Spring Bean组件获取的工具类的形式,通过不同的RedisTemplate实例拿到不同的ConnectionFactory来验证各个实例操作的都是对应的Redis环境,详见下文。

1.注解代码

通过注解的形式注入6个容器对象,分别是2个写Redis和4个读Redis,同时包含了RedisTemplate和StringRedisTemplate,参考如下代码:

package cn.chendd.redis;

import ...

/**
* 使用@Resource注入的方式来测试redis的6个实现类,关注各个实例输出的服务器参数
*
* @author chendd
* @date 2023/5/16 21:40
*/
public class RedisContextInjectTest extends BaseTest {

   @Resource(name = RedisConstants.READ1_REDIS_TEMPLATE)
   private RedisTemplate read1RedisTemplate;
   @Resource(name = RedisConstants.READ2_REDIS_TEMPLATE)
   private RedisTemplate read2RedisTemplate;
   @Resource(name = RedisConstants.WRITE_REDIS_TEMPLATE)
   private RedisTemplate writeRedisTemplate;

   @Resource(name = RedisConstants.READ1_STRING_REDIS_TEMPLATE)
   private StringRedisTemplate read1StringRedisTemplate;
   @Resource(name = RedisConstants.READ2_STRING_REDIS_TEMPLATE)
   private StringRedisTemplate read2StringRedisTemplate;
   @Resource(name = RedisConstants.WRITE_STRING_REDIS_TEMPLATE)
   private StringRedisTemplate writeStringRedisTemplate;
   
   @Test
   public void contextRedisTemplate() {
       LettuceConnectionFactory write = (LettuceConnectionFactory) this.writeRedisTemplate.getConnectionFactory();
       assert write != null;
       System.out.println(MessageFormat.format("write host = {0} , port = {1}", write.getHostName(), write.getPort()));
       LettuceConnectionFactory read1 = (LettuceConnectionFactory) this.read1RedisTemplate.getConnectionFactory();
       assert read1 != null;
       System.out.println(MessageFormat.format("read1 host = {0} , port = {1}", read1.getHostName(), read1.getPort()));
       LettuceConnectionFactory read2 = (LettuceConnectionFactory) this.read2RedisTemplate.getConnectionFactory();
       assert read2 != null;
       System.out.println(MessageFormat.format("read2 host = {0} , port = {1}", read2.getHostName(), read2.getPort()));
   }

   @Test
   public void contextStringRedisTemplate() {
       LettuceConnectionFactory write = (LettuceConnectionFactory) this.writeStringRedisTemplate.getConnectionFactory();
       assert write != null;
       System.out.println(MessageFormat.format("write host = {0} , port = {1}", write.getHostName(), write.getPort()));
       LettuceConnectionFactory read1 = (LettuceConnectionFactory) this.read1StringRedisTemplate.getConnectionFactory();
       assert read1 != null;
       System.out.println(MessageFormat.format("read1 host = {0} , port = {1}", read1.getHostName(), read1.getPort()));
       LettuceConnectionFactory read2 = (LettuceConnectionFactory) this.read2StringRedisTemplate.getConnectionFactory();
       assert read2 != null;
       System.out.println(MessageFormat.format("read2 host = {0} , port = {1}", read2.getHostName(), read2.getPort()));
   }

}

2.运行输出

注解版RedisTemplate.gif

3.工具类代码

package cn.chendd.redis;

import ...;

/**
* 使用工具类获取的方式来测试redis的实例,关注各个实例输出的服务器参数
*
* @author chendd
* @date 2023/5/19 21:30
*/
public class RedisContextGetTest extends BaseTest {

   private RedisTemplate readRedisTemplate;
   private RedisTemplate writeRedisTemplate;

   private StringRedisTemplate writeStringRedisTemplate;
   private StringRedisTemplate readStringRedisTemplate;

   @Before
   public void init() {
       this.writeRedisTemplate = RedisUtils.getWriteRedisTemplate();
       this.writeStringRedisTemplate = RedisUtils.getWriteStringRedisTemplate();

       this.readRedisTemplate = RedisUtils.getReadRedisTemplate();
       this.readStringRedisTemplate = RedisUtils.getReadStringRedisTemplate();
   }

   @Test
   public void contextRedisTemplate() {
       LettuceConnectionFactory write = (LettuceConnectionFactory) this.writeRedisTemplate.getConnectionFactory();
       assert write != null;
       System.out.println(MessageFormat.format("write host = {0} , port = {1}", write.getHostName(), write.getPort()));
       LettuceConnectionFactory read = (LettuceConnectionFactory) this.readRedisTemplate.getConnectionFactory();
       assert read != null;
       System.out.println(MessageFormat.format("read1 host = {0} , port = {1}", read.getHostName(), read.getPort()));
   }

   @Test
   public void contextStringRedisTemplate() {
       LettuceConnectionFactory write = (LettuceConnectionFactory) this.writeStringRedisTemplate.getConnectionFactory();
       assert write != null;
       System.out.println(MessageFormat.format("write host = {0} , port = {1}", write.getHostName(), write.getPort()));
       LettuceConnectionFactory read = (LettuceConnectionFactory) this.readStringRedisTemplate.getConnectionFactory();
       assert read != null;
       System.out.println(MessageFormat.format("read1 host = {0} , port = {1}", read.getHostName(), read.getPort()));
   }

}

4.运行输出

工具类版RedisTemplate.gif


 点赞


 发表评论

当前回复:作者

 评论列表


留言区