当前位置:网站首页>非关系型数据库之Redis【Jedis客户端+Jedis连接集群】
非关系型数据库之Redis【Jedis客户端+Jedis连接集群】
2022-07-28 06:06:00 【捌椒】
这里写目录标题
1. Jedis介绍
Redis不仅是使用命令来操作,现在基本上主流的语言都有客户端支持,比如java、C、C#、C++、php、Node.js、Go等。
在官方网站里列一些Java的客户端,有Jedis、Redisson、Jredis、JDBC-Redis、等其中官方推荐使用Jedis和Redisson。 在企业中用的最多的就是Jedis。
2. 前期准备【设置防火墙】
首先,redis配置 默认禁止远程连接,所以,想用物理机Jedis 访问 redis 虚拟服务器,需要为被访问端口设置防火墙白名单。如果不设置会报错:
redis远程连接失败,连接超时redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutExce
其次,Centos7 默认使用的是firewall作为防火墙,没有 iptable 文件。本文使用的Linux操作系统为CentOS 7。所以下面 只讲解 CentOS 7 如何设置防火墙白名单
2.1 已启动防火墙服务,添加白名单
首先, 修改 redis.conf 配置
- vim redis.conf
- 下面是要修改的内容
bind 127.0.0.1 这个需要注释掉,去掉#
protected-mode no
然后,检查redis默认端口(6379)添加到白名单
- firewall-cmd --zone=public --add-port=6379/tcp --permanent
- 输出
success则为成功
重启防火墙,再次检查端口是否开启firewall-cmd --query-port=6379/tcp
- systemctl restart firewalld
- firewall-cmd --query-port=6379/tcp

2.2 未启动防火墙服务(或提前关闭防火墙服务)
- 修改
/ect/firewalld/zones/public.xml文件,并重启。


3. 工程搭建
- 首先 eclipse 创建一个java 工程
- 然后,添加下面两个 jar 包

3.1 单实例连接 redis
首先,开启服务器
import org.junit.Test;
import redis.clients.jedis.Jedis;
public class JedisClient {
@Test
public void jedisClient() {
Jedis jedis = new Jedis("192.168.188.127",6379);
//选择 0 号数据库 :0-15
jedis.select(0);
//通过jedis赋值
jedis.set("s2", "222");
//通过jedis取值
String result = jedis.get("s2");
System.out.println(result);
jedis.close();
}
}
输出:
222
3.2 使用 jedis 连接池连接 redis
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class JedisClient {
@Test
public void jedisPool() {
//使用jedis连接池连接redis服务器
JedisPool jedispool = new JedisPool("192.168.188.127",6379);
//通过连接池获取Jedis对象
Jedis jedis = jedispool.getResource();
//jedis.set("s4", "444");
String result = jedis.get("s4");
System.out.println(result);
//关闭jedis客户端
jedis.close();
//关闭连接池
jedispool.close();
}
}
输出
444
4. Jedis连接集群
4.1 代码
package jedishm19;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
public class JedisClient {
@Test
public void jedisCluster() {
//创建jedisCluster
Set<HostAndPort> nodes = new HashSet<>();
nodes.add(new HostAndPort("192.168.188.127", 7001));
nodes.add(new HostAndPort("192.168.188.127", 7002));
nodes.add(new HostAndPort("192.168.188.127", 7003));
nodes.add(new HostAndPort("192.168.188.127", 7004));
nodes.add(new HostAndPort("192.168.188.127", 7005));
nodes.add(new HostAndPort("192.168.188.127", 7006));
JedisCluster cluster = new JedisCluster(nodes);
cluster.set("s6", "666");
String result = cluster.get("s6");
System.out.println(result);
cluster.close();
}
}
输出:
666
边栏推荐
- DNA修饰金属锇Os纳米颗粒OsNPS-DNA|DNA修饰金属铱纳米颗粒IrNPS-DNA
- Summary of project experience
- Copper indium sulfide CuInSe2 quantum dots modified DNA (deoxyribonucleic acid) DNA cuinse2qds (Qiyue)
- flowable工作流所有业务概念
- MySQL basic knowledge learning (II)
- Synthesis of dna-ag2sqds DNA modified silver sulfide Ag2S quantum dots
- Don't be afraid of ESD static electricity. This article tells you some solutions
- Date n days ago
- Adjust the array order so that odd numbers precede even numbers - two questions per day
- 【干货】32个EMC标准电路分享!
猜你喜欢

Big talk persistence and redolog

收藏 | 结合个人经验,我总结了这7点EMC相关知识

和为s的两个数字——每日两题
![[shaders realize negative anti color effect _shader effect Chapter 11]](/img/c5/70761374330eb4fb3915c335b7efb8.png)
[shaders realize negative anti color effect _shader effect Chapter 11]

The underlying principles of RDB persistence and AOF persistence of redis

Essential performance optimization topics in the interview~

DNA modified rhodium RH nanoparticles rhnps DNA (DNA modified noble metal nanoparticles)

Which of class A and class B is more stringent in EMC?

2022年湖南工学院ACM集训第五次周测AD题题解

DNA-Ag2SQDs脱氧核糖核酸DNA修饰硫化银Ag2S量子点的合成方法
随机推荐
JS secondary linkage Department
EMC整改思路
常用电子产品行业标准及认证
[JVM optimization] online JVM tuning practice
The penultimate node in the linked list - Double finger
Why is ESD protection so important for integrated circuits? How to protect?
Mysql查看某个表所占内存大小
5g commercial third year: driverless "going up the mountain" and "going to the sea"
User mode vs kernel mode, process vs thread
通过sed 修改conf文件
LeNet5、AlexNet、VGGNet、ResNet
EMC中的基石-电磁兼容滤波知识大全!
cdn.jsdelivr.net不可用,该怎么办
华为交换机拆解,学EMC基本操作
5G 商用第三年:无人驾驶的“上山”与“下海”
动态内存管理知识点
[solution] visual full link log tracking - log tracking system
细说共模干扰和差模干扰
MySQL基础知识学习(二)
0727~面试题梳理