Reids的客户端

一、redis自带的客户端(命令行连接方式)(注意:需要先启动服务器)

连本机的服务器:redis-cli
连远程的服务器:redis-cli -h ip地址 -p 端口号    例如 redis-cli -h 127.0.0.1 -p 6379


二、图形用户界面客户端
redis-desktop-manager-0.8.0.3841.exe

1、连接服务器
Reids的客户端

2、测试连接:Test Connection
如果不能连接,则需要设置防火墙

编辑:/etc/sysconfig/iptables 文件
添加:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT

重启防火墙
service iptables restart


3、默认16个数据库
Reids的客户端

redis.conf中可以设置默认数据库的个数,如下:
databases 16

选择数据库的方式:
使用select 加上数据库的下标 就可以选择指定的数据库来使用,下标从0开始
127.0.0.1:6379> select 15
OK
127.0.0.1:6379[15]>

3、图形界面中添加key值


三、Jedis
Redis不仅是使用命令来操作,现在基本上主流的语言都有客户端支持,比如java、C、C#、C++、php、Node.js、Go等。
在官方网站里列一些Java的客户端,有Jedis、Redisson、Jredis、JDBC-Redis、等其中官方推荐使用Jedis和Redisson。 在企业中用的最多的就是Jedis,下面我们就重点学习下Jedis。
Jedis同样也是托管在github上,地址:https://github.com/xetorthio/jedis


Redis不仅可以使用命令来操作,现在基本上主流的语言都有客户端支持,比如java、C、C#、C++、php、Node.js、Go等。
在官方网站里列一些Java的客户端,有Jedis、Redisson、Jredis、JDBC-Redis等。其中官方推荐使用Jedis和Redisson。 企业中用的最多的就是Jedis。
Jedis同样也是托管在github上,地址:https://github.com/xetorthio/jedis

1、maven pom.xml


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
</dependencies>

2、入门

3、使用连接池

4、整合spring

applicationContext.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 连接池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
   <!-- 最大连接数 -->
   <property name="maxTotal" value="30" />
    <!-- 最大空闲连接数 -->
    <property name="maxIdle" value="10" />
    <!-- 每次释放连接的最大数目 -->
    <property name="numTestsPerEvictionRun" value="1024" />
    <!-- 释放连接的扫描间隔(毫秒) -->
    <property name="timeBetweenEvictionRunsMillis" value="30000" />
    <!-- 连接最小空闲时间 -->
    <property name="minEvictableIdleTimeMillis" value="1800000" />
    <!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
    <property name="softMinEvictableIdleTimeMillis" value="10000" />
    <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
<property name="maxWaitMillis" value="1500" />
<!-- 在获取连接的时候检查有效性, 默认false -->
<property name="testOnBorrow" value="false" />
<!-- 在空闲时检查有效性, 默认false -->
<property name="testWhileIdle" value="true" />
<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
<property name="blockWhenExhausted" value="false" />
</bean>
<!-- redis单机 通过连接池 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="close">
   <constructor-arg name="poolConfig" ref="jedisPoolConfig" />
   <constructor-arg name="host" value="192.168.191.100" />
   <constructor-arg name="port" value="6379" />
</bean>
</beans>

测试类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* 整合spring
*/
@Test
public void testJedisPool() {
    ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
    
    JedisPool pool = (JedisPool) context.getBean("jedisPool");
    Jedis jedis = null;
    try {
        jedis = pool.getResource();
        jedis.set("name", "lisi");
        String name = jedis.get("name");
        System.out.println(name);
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (jedis != null) {
            // 关闭连接
            jedis.close();
        }
    }
}


3、默认16个数据库