I. First, what is Jedis?
Jedis = Java + Redis; // Yes, is this ^ _ ^
Two .Jedis and Redis origin
1.Redis not only use the commands to operate the now largely mainstream languages have client support, such as java, C, C #, C ++, PHP, Node.js, Go and other languages.
In a column in the official website of some of the Java client, there are Jedis, Redisson, Jredis, JDBC-Redis, such as which official recommended Jedis and Redisson. In the enterprise with the most is the Jedis, Here's to the market with the most Jedis to understand Kazakhstan:
2. Jedis also hosted on github, and hosting address: https: //github.com/xetorthio/jedis
Note: Yes, you see that it wants
III. Only know Java, so Java to connect with what Redis
1. First you need to download jar package
The two jar package is a must, you can download well in advance, still project into java, then introduced
2. Write a test class
package com.yinxin.Jedis;
import redis.clients.jedis.Jedis;
public class JedisTest {
public static void main(String[] args) {
//1.设置ip地址和端口
Jedis jedis=new Jedis("172.16.10.118",6379);
//添加数据
jedis.set("name","zhangsan");
//获取数据
String name=jedis.get("name");
System.out.println("获取name属性的值:"+name);
//释放资源
jedis.close();
}
}
If you perform will appear the following results:
① thrown connection timeout, there is no reason to close the firewall port 6379 on the remote server
② linux firewall must be set, so then you need to turn off the firewall, and let it take effect permanently saved
vim /etc/sysconfig/iptables
Add port 6379
service iptables restart
③ turn off the firewall
systemctl stop firewalld.service
After enough off the firewall, the test procedure can be used, as shown:
Java programs and data acquired as a client
3. The connection pool
Code section:
package com.yinxin.Jedis;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class JedisTest {
@Test
public void testJedisPool(){
//获取连接池配置对象,设置配置项
JedisPoolConfig config=new JedisPoolConfig();
//最大连接数
config.setMaxTotal(30);
//最大空闲连接数
config.setMaxIdle(10);
JedisPool jedisPool=new JedisPool(config,"172.16.10.111",6379);
//获得核心对象
Jedis jedis=null;
jedis=jedisPool.getResource();
//设置数据
jedis.set("name","yinxin");
//获取数据
String name=jedis.get("name");
System.out.println("name为["+name+"]");
if(jedis!=null){
jedis.close();
}
//虚拟机关闭时,释放pool资源
if(jedisPool!=null){
jedisPool.close();
}
}
}
Value obtained from the connection pool as: