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
添加依赖
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<!-- 单元测试Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 配置Maven的JDK编译级别 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
单实例连接 本地链接
连接环境检查 https://www.cnblogs.com/JimmyFanHome/p/9973516.html
主机和虚拟机互相ping测试
测试linux的指定端口
1.在cmd下使用telnet指令,telnet ip port
2.ip可以通,端口不行,我们去linux那边看看是不是这个端口不开放,在CentOS7中,使用netstat -lntp来查看linux的处于监听的端口,果然没有。这说明redis服务器还没起来,启动服务器
3.这个时候,去linux的另外一个终端打开客户端连接,是可以的,但是,window下telnet ip port就是不通,程序那边也还是connect time out
4.按照网上的方法,把bind 127.0.0.1注释掉之后,在linux的另外一个终端可以连接redis服务器,其实到这里,redis那边都可以的了,现在就是window连接linux的redis服务器不行,我们来设置一下防火墙(CentOS7),先关掉。
查看防火墙状态:firewall-cmd --state
停止防火墙:systemctl stop firewalld.service
禁止防火墙开机启动:systemctl disable firewalld.service
@Test
public void testJedis() {
// 创建一个Jedis的连接
Jedis jedis = new Jedis("192.168.159.128", 6379);
// 执行redis命令
jedis.set("mytest", "hello world, this is jedis client!");
// 从redis中取值
String result = jedis.get("mytest");
// 打印结果
System.out.println(result);
// 关闭连接
jedis.close();
}
连接池连接
@Test
public void testJedisPool() {
// 创建一连接池对象
JedisPool jedisPool = new JedisPool("192.168.159.128", 6379);
// 从连接池中获得连接
Jedis jedis = jedisPool.getResource();
String result = jedis.get("mytest");
System.out.println(result);
// 关闭连接
jedis.close();
// 关闭连接池
jedisPool.close();
}
连接redis集群
创建JedisCluster类连接redis集群
@Test
public void testJedisCluster() throws Exception {
// 创建一连接,JedisCluster对象,在系统中是单例存在
Set<HostAndPort> nodes = new HashSet<>();
nodes.add(new HostAndPort("192.168.242.129", 7001));
nodes.add(new HostAndPort("192.168.242.129", 7002));
nodes.add(new HostAndPort("192.168.242.129", 7003));
nodes.add(new HostAndPort("192.168.242.129", 7004));
nodes.add(new HostAndPort("192.168.242.129", 7005));
nodes.add(new HostAndPort("192.168.242.129", 7006));
JedisCluster cluster = new JedisCluster(nodes);
// 执行JedisCluster对象中的方法,方法和redis一一对应。
cluster.set("cluster-test", "my jedis cluster test");
String result = cluster.get("cluster-test");
System.out.println(result);
// 程序结束时需要关闭JedisCluster对象
cluster.close();
}
Jedis整合spring
配置spring配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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="true" />
<!-- 在空闲时检查有效性, 默认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.159.128" />
<constructor-arg name="port" value="6379" />
</bean>
<!-- redis集群 -->
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg index="0">
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="192.168.159.128" />
<constructor-arg index="1" value="7001" />
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="192.168.159.128" />
<constructor-arg index="1" value="7002" />
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="192.168.159.128" />
<constructor-arg index="1" value="7003" />
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="192.168.159.128" />
<constructor-arg index="1" value="7004" />
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="192.168.159.128" />
<constructor-arg index="1" value="7005" />
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="192.168.159.128" />
<constructor-arg index="1" value="7006" />
</bean>
</set>
</constructor-arg>
<constructor-arg index="1" ref="jedisPoolConfig"></constructor-arg>
</bean>
</beans>
测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/applicationContext.xml")
public class TestJedis2 {
@Autowired
private JedisPool jedisPool;
@Resource
private JedisCluster cluster;
@Test
public void testJedisPool() {
// 从连接池中获得连接
Jedis jedis = jedisPool.getResource();
String result = jedis.get("mytest");
System.out.println(result);
// 关闭连接
jedis.close();
}
@Test
public void testJedisCluster() throws Exception {
//redis集群
// 执行JedisCluster对象中的方法,方法和redis一一对应。
cluster.set("cluster-test", "my jedis cluster test");
String result = cluster.get("cluster-test");
System.out.println(result);
}
}
389



被折叠的 条评论
为什么被折叠?



