redis客户端工具

1.3.0开始支持

redis客户端工具是一个可以通过yml配置,自动初始化连接池的工具,不需要重复做代码的开发。

引入工具包

<dependency>
    <groupId>com.wueasy</groupId>
    <artifactId>wueasy-redis-client</artifactId>
    <version>RELEASE</version>
</dependency>

redis连接配置

  • redis:配置redis连接集合,可以配置多个redis连接
    • test1:redis连接的key,可以配置多个,唯一
      • database:数据库索引(默认为0)
      • host:服务器地址
      • port:服务器连接端口
      • password:服务器连接密码(默认为空)
      • pool:连接池配置
        • maxIdle:接池中的最大空闲连接,默认值也是8
        • minIdle:连接池中的最小空闲连接,默认值也是0
        • maxTotal:如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)
        • maxWaitMillis:等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException
      • timeout:连接超时时间(毫秒)
wueasy :
  redis: #redis配置
    test1:
      database : 1 #数据库索引(默认为0)
      host : 127.0.0.1 #服务器地址
      port : 6379 #服务器连接端口
      password : 123456  #服务器连接密码(默认为空)
      pool : #连接池配置
        maxIdle : 8 # 连接池中的最大空闲连接,默认值也是8。
        minIdle : 0 #连接池中的最小空闲连接,默认值也是0。
        maxTotal : 2000 # 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
        maxWaitMillis : 1000 # 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException
      timeout : 3000 #连接超时时间(毫秒)
    test2:
      database : 2 #数据库索引(默认为0)
      host : 127.0.0.1 #服务器地址
      port : 6379 #服务器连接端口
      password : 123456  #服务器连接密码(默认为空)
      pool : #连接池配置
        maxIdle : 8 # 连接池中的最大空闲连接,默认值也是8。
        minIdle : 0 #连接池中的最小空闲连接,默认值也是0。
        maxTotal : 2000 # 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
        maxWaitMillis : 1000 # 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException
      timeout : 3000 #连接超时时间(毫秒)

java中引用redis客户端

通过@Resource注解注入,并指定name属性。

方式一(string类型)

  • name说明:规则由redis配置的连接key + StringRedisTemplate组成
@Resource(name = "test1StringRedisTemplate")
private StringRedisTemplate test1StringRedisTemplate;

@Resource(name = "test2StringRedisTemplate")
private StringRedisTemplate test2StringRedisTemplate;

方式二(泛型)

2.2.1开始支持。

如果需要使用泛型方式(自定义类型),可以通过此模式。

  • name说明:规则由redis配置的连接key + RedisTemplate组成
@Resource(name = "test1RedisTemplate")
private RedisTemplate<String,User> test1RedisTemplate;

@Resource(name = "test2RedisTemplate")
private RedisTemplate<String,User> test2RedisTemplate;

测试示例


import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.wueasy.Application;



/**
 * redis测试
 * @author: fallsea
 * @version 1.0
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=Application.class)
public class RedisTests {


    @Resource(name = "test1StringRedisTemplate")
    private StringRedisTemplate stringRedisTemplate;

    @Resource(name = "test2StringRedisTemplate")
    private StringRedisTemplate stringRedisTemplate2;


    @Test
    public void testObj()  {

        try{

            for(int i=0;i<1000;i++) {

                Long id = stringRedisTemplate.opsForValue().increment("demo", 1L);

                System.err.println(id);

                Long id2 = stringRedisTemplate2.opsForValue().increment("demo", 1L);

                System.out.println(id2);

                Thread.sleep(500);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }

    }


}

启动类配置

由于spring boot启动时会自动注入redis,所以启动类中需要配置取消redis自动注入,增加RedisAutoConfiguration.class,RedisRepositoriesAutoConfiguration.class

@SpringBootApplication(exclude={RedisAutoConfiguration.class,RedisRepositoriesAutoConfiguration.class})//开启组件扫描和自动配置
Copyright © wueasy.com 2017-2019 all right reserved,powered by Gitbook未经允许,禁止以任何形式传播 修订时间: 2019-10-04

results matching ""

    No results matching ""