当前位置:网站首页>Redis of non relational database [jedis client +jedis connection cluster]
Redis of non relational database [jedis client +jedis connection cluster]
2022-07-28 07:56:00 【Eight pepper】
Here's the catalog title
1. Jedis Introduce
Redis It's not just using commands to operate , Now basically mainstream languages have client support , such as java、C、C#、C++、php、Node.js、Go etc. .
List some on the official website Java The client of , Yes Jedis、Redisson、Jredis、JDBC-Redis、 It's officially recommended Jedis and Redisson. What is used most in enterprises is Jedis.
2. Preparation 【 Set up firewall 】
First ,redis To configure Remote connections are disabled by default , therefore , Want to use physical machine Jedis visit redis Virtual server , You need to set the firewall white list for the accessed ports . If not set, an error will be reported :
redis Remote connection failed , Connection timeout redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutExce
secondly ,Centos7 The default is firewall As a firewall , No, iptable file . This article uses Linux The operating system is CentOS 7. So below Just explain CentOS 7 How to set firewall white list
2.1 Firewall service started , Add whitelist
First , modify redis.conf To configure
- vim redis.conf
- Here's what to change
bind 127.0.0.1 You need to comment this out , Get rid of #
protected-mode no
then , Check redis Default port (6379) Add to white list
- firewall-cmd --zone=public --add-port=6379/tcp --permanent
- Output
successIt's success
service iptables restart , Check again whether the port is turned on firewall-cmd --query-port=6379/tcp
- systemctl restart firewalld
- firewall-cmd --query-port=6379/tcp

2.2 Firewall service is not started ( Or shut down the firewall service in advance )
- modify
/ect/firewalld/zones/public.xmlfile , And restart .


3. Project construction
- First eclipse Create a java engineering
- then , Add the following two jar package

3.1 Single instance connection redis
First , Turn on the server 
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);
// choice 0 The database :0-15
jedis.select(0);
// adopt jedis assignment
jedis.set("s2", "222");
// adopt jedis Value
String result = jedis.get("s2");
System.out.println(result);
jedis.close();
}
}
Output :
222
3.2 Use jedis Connection pool connection redis
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class JedisClient {
@Test
public void jedisPool() {
// Use jedis Connection pool connection redis The server
JedisPool jedispool = new JedisPool("192.168.188.127",6379);
// Get through connection pool Jedis object
Jedis jedis = jedispool.getResource();
//jedis.set("s4", "444");
String result = jedis.get("s4");
System.out.println(result);
// close jedis client
jedis.close();
// Close connection pool
jedispool.close();
}
}
Output
444
4. Jedis Connect clusters
redis Detailed explanation of cluster construction
4.1 Code
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() {
// establish 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();
}
}
Output :
666
边栏推荐
- 2022/7/27 examination summary
- Information system project manager must recite the core examination site (41) risk management plan
- 辨析覆盖索引/索引覆盖/三星索引
- Adjust the array order so that odd numbers precede even numbers - two questions per day
- 任务管理器中,显示的CPU速度大于它的最大速度【主频】
- DNA修饰金属锇Os纳米颗粒OsNPS-DNA|DNA修饰金属铱纳米颗粒IrNPS-DNA
- .NET 6.0中使用Identity框架实现JWT身份认证与授权
- Mysql, how can we get the number of rows affected by the query?
- Using identity framework to realize JWT identity authentication and authorization in.Net 6.0
- 4.1.4 why set the member variable to private
猜你喜欢

DNA修饰贵金属纳米颗粒|DNA修饰纳米铜颗粒CuNPS-DNA|研究要点

GD32使用ST的HAL库和GD官方库的一些体会

Advanced pointer practice

细说共模干扰和差模干扰

win系统添加打印机

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

The underlying principles of RDB persistence and AOF persistence of redis

XMPP Service Research (II) prosody create account

登录模式:单一服务器模式、单点登录、token模式

DNA-CuInSeQDs近红外CuInSe量子点包裹脱氧核糖核酸DNA
随机推荐
华为高级工程师---BGP路由过滤及社团属性
Dry goods | share an EMC actual case and rectification process
DNA deoxyribonucleic acid modified platinum nanoparticles ptnps DNA | scientific research reagent
微信小程序隐藏滚动条的方法
Protobuf basic grammar summary
JUC原子类: CAS, Unsafe、CAS缺点、ABA问题如何解决详解
Parse tree structure JS
干货|分享一个EMC实际案例及整改过程
Method of hiding scroll bar in wechat applet
MySQL view the memory size of a table
EMC整改思路
Information system project manager must recite the core examination site (41) risk management plan
How do we run batch mode in MySQL?
ESD静电不用怕,本文告诉你一些解决方法
"Wei Lai Cup" 2022 Niuke summer multi school training camp 2 supplementary question record (dghjkl)
Collector原理解析
EMC设计攻略 —时钟
数据化管理洞悉零售及电子商务运营——数据化管理介绍
Use ffmpeg to generate single image + single audio streaming video in batches
YOLO系列损失函数详解