当前位置:网站首页>How to correctly use vertx to operate redis (3.9.4 with source code analysis)
How to correctly use vertx to operate redis (3.9.4 with source code analysis)
2022-07-01 19:37:00 【Natural player】
1 origin
It's really a special experience .
Increased knowledge reserves :Vert.x Asynchronous connection Redis And operate Redis.
What happened :
The team needs to develop offline algorithm middleware , Integrated into the business side background service ,
The sentinel mode is used in the original middleware Redis, Now we need to add new logic ,
Operate in cluster mode Redis, Middleware is based on Vert.x 3.5.1 A version supporting cluster connection has been modified ,
Meet cluster connection and operation . Everything is wonderful , The test passed .
therefore , Test native Vertx Higher version (>3.8.x) Cluster connection , This is the beginning of the nightmare ,
Because I built it myself Redis The cluster has password authentication ,
therefore , Used Vert.x Different versions of the test cannot be connected , as it cannot be helped , Can only , Get rid of Redis Password authentication of the cluster ,
however ,3.8.x Still unable to connect ,
Last , Used 3.9.4, Successful connection , And completed successfully Redis operation ,
I'd like to share with you as follows , Help developers in need in the future .
important clause :
Vert.x edition :3.9.4
Measured in this paper : Monomers Redis And clusters Redis, And the test results are given ;
Because there is no sentinel environment , therefore , Sentinel mode is not tested , Although the connection example is given , But there are no test results .
2 Vert.x rely on
<!-- https://mvnrepository.com/artifact/io.vertx/vertx-core -->
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>3.9.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.vertx/vertx-redis-client -->
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-redis-client</artifactId>
<version>3.9.4</version>
</dependency>
3 Redis Address base
For unified management Redis Address , Use enumeration management ,
Add... Later Redis Connection configuration , Extract from this enumeration Library ,
The code is as follows :
package com.monkey.java_study.common.enums;
/** * Redis Address base . * * @author xindaqi * @since 2022-07-01 11:10 */
public enum RedisAddressEnum {
// Redis The cluster address
CLUSTER_NODE_1("redis://192.168.211.129:9001"),
CLUSTER_NODE_2("redis://192.168.211.129:9002"),
CLUSTER_NODE_3("redis://192.168.211.129:9003"),
CLUSTER_NODE_4("redis://192.168.211.129:9004"),
CLUSTER_NODE_5("redis://192.168.211.129:9005"),
CLUSTER_NODE_6("redis://192.168.211.129:9006"),
// stand-alone Redis Address
STANDALONE_NODE("redis://:[email protected]/0"),
;
private String address;
RedisAddressEnum(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
}
4 Redis colony
4.1 Configure the address and related parameters :RedisOptions
Vert.x 3.9.4 There are two ways to configure Redis Address ,
Respectively :addConnectionString and setEndpoints,
meanwhile , You can specify the connection type , Cluster is :RedisClientType.CLUSTER
Particular attention : When connecting in cluster mode , The cluster must not be configured with password authentication , Otherwise, you cannot connect to Redis colony
- addConnectionString The way
This way , Add addresses one by one . Address format :redis://ip:port
/** * Redis Cluster connection configuration :addConnectionString The way * * @return Redis Cluster configuration */
private static RedisOptions setClusterRedisAddressOneByOne() {
return new RedisOptions()
.setType(RedisClientType.CLUSTER)
.addConnectionString(RedisAddressEnum.CLUSTER_NODE_1.getAddress())
.addConnectionString(RedisAddressEnum.CLUSTER_NODE_2.getAddress())
.addConnectionString(RedisAddressEnum.CLUSTER_NODE_3.getAddress())
.addConnectionString(RedisAddressEnum.CLUSTER_NODE_4.getAddress())
.addConnectionString(RedisAddressEnum.CLUSTER_NODE_5.getAddress())
.addConnectionString(RedisAddressEnum.CLUSTER_NODE_6.getAddress());
}
- setEndpoints The way
Batch addition Redis Address ,endpoints The type ofList<String>, Therefore, you can batch add .
/** * Redis Cluster connection configuration :setEndpoints The way * * @return Redis Cluster configuration */
private static RedisOptions setClusterRedisAddressBatch() {
List<String> redisAddressList = Stream.of(
RedisAddressEnum.CLUSTER_NODE_1.getAddress(),
RedisAddressEnum.CLUSTER_NODE_2.getAddress(),
RedisAddressEnum.CLUSTER_NODE_3.getAddress(),
RedisAddressEnum.CLUSTER_NODE_4.getAddress(),
RedisAddressEnum.CLUSTER_NODE_5.getAddress(),
RedisAddressEnum.CLUSTER_NODE_6.getAddress()).collect(Collectors.toList());
return new RedisOptions()
.setType(RedisClientType.CLUSTER)
.setEndpoints(redisAddressList);
}
4.2 Source code : Connection type
from Vert.x3.9.4 Source code is known ,Redis There are three types of client connections :STANDALONE( Monomers )、SENTINEL( sentry ) and CLUSTER( colony ),
The source code is shown in the figure below .
Location :io.vertx.redis.client.RedisClientType
4.3 Other default parameters
To configure Redis After the cluster connection address , You can connect directly Redis colony ,
because ,Vert.x3.9.4 At initialization , Default values are automatically configured , Such as :
maxWaitingHandler、maxPoolSize and maxPoolWaiting etc. , The source code is shown in the figure below ,
By default , Connect Redis The type of is monomer (RedisClientType.STANDALONE),
The default in sentinel mode is not to use the slave node :RedisSlaves.NEVER.
Location :io.vertx.redis.client.RedisOptions#init
Why are these parameters loaded , because ,RedisOptions Constructor for :
Location :io.vertx.redis.client.RedisOptions#RedisOptions()
4.4 establish Redis client
The creation example is as follows , By passing in Vertx.vertx() and redisOptions( The parameter object configured above ),
Can get Redis client , Getting the client is the same as Redis Establish a connection ,
Then you can finish CURD operation .
Redis.createClient(Vertx.vertx(), redisOptions);
4.5 Source code :Redis.createClient
Vert.x 3.9.4 establish Redis The client source code is shown in the figure below ,
We can know from the source code , The creation process will be based on type Choose different client types ( Monomers 、 Sentinels and groups ).
Location :io.vertx.redis.client.Redis#createClient(io.vertx.core.Vertx, io.vertx.redis.client.RedisOptions)
4.6 Redis operation :RedisAPI
Vert.x 3.9.4 adopt RedisAPI operation Redis data ,
The test sample is as follows , Use Vert.x You know , Asynchronous operations ,
therefore , Read data using Vetx.x threading :vert.x-eventloop-thread-0
/** * Read data from Redis * * @param redisClient Redis client * @param key Redis Key to query in */
public void readDataFromCluster(Redis redisClient, String key) {
RedisAPI redisAPI = RedisAPI.api(redisClient);
redisAPI.get(key, res -> {
if (res.succeeded()) {
String value = String.valueOf(res.result());
logger.info(">>>>>>>>Read data from redis cluster (key, value)->:({},{})", key, value);
} else {
logger.error(">>>>>>>>Redis cluster read data error:", res.cause());
}
});
}
4.7 Complete example
package com.monkey.java_study.thirdparty.vertx_test;
import com.monkey.java_study.common.enums.RedisAddressEnum;
import io.vertx.core.Vertx;
import io.vertx.redis.client.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/** * Vert.x Connect Redis Cluster service and data reading test . * * @author xindaqi * @since 2022-06-30 12:16 */
public class VertxClusterOpRedisTest {
private static final Logger logger = LoggerFactory.getLogger(VertxClusterOpRedisTest.class);
/** * Redis Cluster clients */
private static final Redis redisClient;
/** * Redis Connection configuration */
private static final RedisOptions redisOptions;
static {
redisOptions = setClusterRedisAddressBatch();
redisClient = Redis.createClient(Vertx.vertx(), redisOptions);
}
/** * Redis Cluster connection configuration :addConnectionString The way * * @return Redis Cluster configuration */
private static RedisOptions setClusterRedisAddressOneByOne() {
return new RedisOptions()
.setType(RedisClientType.CLUSTER)
.addConnectionString(RedisAddressEnum.CLUSTER_NODE_1.getAddress())
.addConnectionString(RedisAddressEnum.CLUSTER_NODE_2.getAddress())
.addConnectionString(RedisAddressEnum.CLUSTER_NODE_3.getAddress())
.addConnectionString(RedisAddressEnum.CLUSTER_NODE_4.getAddress())
.addConnectionString(RedisAddressEnum.CLUSTER_NODE_5.getAddress())
.addConnectionString(RedisAddressEnum.CLUSTER_NODE_6.getAddress());
}
/** * Redis Cluster connection configuration :setEndpoints The way * * @return Redis Cluster configuration */
private static RedisOptions setClusterRedisAddressBatch() {
List<String> redisAddressList = Stream.of(
RedisAddressEnum.CLUSTER_NODE_1.getAddress(),
RedisAddressEnum.CLUSTER_NODE_2.getAddress(),
RedisAddressEnum.CLUSTER_NODE_3.getAddress(),
RedisAddressEnum.CLUSTER_NODE_4.getAddress(),
RedisAddressEnum.CLUSTER_NODE_5.getAddress(),
RedisAddressEnum.CLUSTER_NODE_6.getAddress()).collect(Collectors.toList());
return new RedisOptions()
.setType(RedisClientType.CLUSTER)
.setEndpoints(redisAddressList);
}
public Redis getRedisClient() {
return redisClient;
}
/** * Read data from Redis * * @param redisClient Redis client * @param key Redis Key to query in */
public void readDataFromCluster(Redis redisClient, String key) {
RedisAPI redisAPI = RedisAPI.api(redisClient);
redisAPI.get(key, res -> {
if (res.succeeded()) {
String value = String.valueOf(res.result());
logger.info(">>>>>>>>Read data from redis cluster (key, value)->:({},{})", key, value);
} else {
logger.error(">>>>>>>>Redis cluster read data error:", res.cause());
}
});
}
public static void main(String[] args) {
VertxClusterOpRedisTest vertxOpRedisTest = new VertxClusterOpRedisTest();
vertxOpRedisTest.readDataFromCluster(vertxOpRedisTest.getRedisClient(), "name");
}
}
4.8 test result
The test results show that ,Vert.x 3.9.4 operation Redis Using asynchronous threads :vert.x-enventloop-thread-0,
And finish reading the results .
5 Redis Monomers
With the foundation of the above cluster configuration ,
Configuring monomer connection and sentinel connection is relatively easy to understand ,
Give a complete example directly , The code is as follows :
It should be noted that : To configure Redis You need to configure the database as needed 0-15,
Connection type selection :RedisClientType.STANDALONE,
If it is a monomer, you can not choose , because RedisOptions The default value is :RedisClientType.STANDALONE.
Address format :redis://username:[email protected]:port/db
Parameter description :
| Serial number | Parameters | describe |
|---|---|---|
| 1 | username | No, you can leave it blank |
| 2 | password | Redis Connect the password , No, you can leave it blank |
| 3 | ip | Redis Running host IP |
| 4 | port | Redis port |
| 5 | db | Redis Database number ,0-15 One of them |
Specially :
If there is only password but no user name , It should be configured like this :redis://:[email protected]:port/db
5.1 Complete example
package com.monkey.java_study.thirdparty.vertx_test;
import com.monkey.java_study.common.enums.RedisAddressEnum;
import io.vertx.core.Vertx;
import io.vertx.redis.client.Redis;
import io.vertx.redis.client.RedisAPI;
import io.vertx.redis.client.RedisClientType;
import io.vertx.redis.client.RedisOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/** * Vert.x Connect Redis Single service and data reading test . * * @author xindaqi * @since 2022-06-30 12:16 */
public class VertxStandaloneOpRedisTest {
private static final Logger logger = LoggerFactory.getLogger(VertxStandaloneOpRedisTest.class);
/** * Redis Monomer client */
private static final Redis redisClient;
/** * Redis Connection configuration */
private static final RedisOptions redisOptions;
static {
redisOptions = setStandaloneRedisAddress();
redisClient = Redis.createClient(Vertx.vertx(), redisOptions);
}
/** * To configure Redis Monomer connection address * * @return Redis Monomer configuration */
private static RedisOptions setStandaloneRedisAddress() {
return new RedisOptions()
.setType(RedisClientType.STANDALONE)
.addConnectionString(RedisAddressEnum.STANDALONE_NODE.getAddress());
}
public Redis getRedisClient() {
return redisClient;
}
/** * Read data from Redis * * @param redisClient Redis client * @param key Redis Key to query in */
public void readDataFromStandalone(Redis redisClient, String key) {
RedisAPI redisAPI = RedisAPI.api(redisClient);
redisAPI.get(key, res -> {
if (res.succeeded()) {
String value = String.valueOf(res.result());
logger.info(">>>>>>>>Read data from redis standalone (key, value)->:({},{})", key, value);
} else {
logger.error(">>>>>>>>Redis standalone read data error:", res.cause());
}
});
}
public static void main(String[] args) {
VertxStandaloneOpRedisTest vertxOpRedisTest = new VertxStandaloneOpRedisTest();
vertxOpRedisTest.readDataFromStandalone(vertxOpRedisTest.getRedisClient(), "name");
}
}
5.2 test result

6 Sentinel mode
Empathy , The complete example is as follows ,
It should be noted that , The type is configured as :RedisClientType.SENTINEL,
At the same time, you need to configure the name and role of the main service , namely
MasterName(“my_test”) and Role(RedisRole.MASTER),
Because there are no sentinels for testing Redis, therefore , Here is only a configuration example ,
There is no actual test .
package com.monkey.java_study.thirdparty.vertx_test;
import com.monkey.java_study.common.enums.RedisAddressEnum;
import io.vertx.core.Vertx;
import io.vertx.redis.client.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/** * Vert.x Connect Redis Sentinel service and data reading test . * * @author xindaqi * @since 2022-06-30 12:16 */
public class VertxSentinelOpRedisTest {
private static final Logger logger = LoggerFactory.getLogger(VertxSentinelOpRedisTest.class);
/** * Redis Sentinel client */
private static final Redis redisClient;
/** * Redis Connection configuration */
private static final RedisOptions redisOptions;
static {
redisOptions = setSentinelRedisAddressOneByOne();
redisClient = Redis.createClient(Vertx.vertx(), redisOptions);
}
/** * Redis Sentry connection configuration :addConnectionString The way * * @return Redis Cluster configuration */
private static RedisOptions setSentinelRedisAddressOneByOne() {
return new RedisOptions()
.setType(RedisClientType.SENTINEL)
.setMasterName("my_test")
.setRole(RedisRole.MASTER)
.addConnectionString(RedisAddressEnum.CLUSTER_NODE_1.getAddress())
.addConnectionString(RedisAddressEnum.CLUSTER_NODE_2.getAddress())
.addConnectionString(RedisAddressEnum.CLUSTER_NODE_3.getAddress())
.addConnectionString(RedisAddressEnum.CLUSTER_NODE_4.getAddress())
.addConnectionString(RedisAddressEnum.CLUSTER_NODE_5.getAddress())
.addConnectionString(RedisAddressEnum.CLUSTER_NODE_6.getAddress());
}
/** * Redis Sentry connection configuration :setEndpoints The way * * @return Redis Cluster configuration */
private static RedisOptions setSentinelRedisAddressBatch() {
List<String> redisAddressList = Stream.of(
RedisAddressEnum.CLUSTER_NODE_1.getAddress(),
RedisAddressEnum.CLUSTER_NODE_2.getAddress(),
RedisAddressEnum.CLUSTER_NODE_3.getAddress(),
RedisAddressEnum.CLUSTER_NODE_4.getAddress(),
RedisAddressEnum.CLUSTER_NODE_5.getAddress(),
RedisAddressEnum.CLUSTER_NODE_6.getAddress()).collect(Collectors.toList());
return new RedisOptions()
.setType(RedisClientType.SENTINEL)
.setMasterName("my_test")
.setRole(RedisRole.MASTER)
.setEndpoints(redisAddressList);
}
public Redis getRedisClient() {
return redisClient;
}
/** * Read data from Redis * * @param redisClient Redis client * @param key Redis Key to query in */
public void readDataFromSentinel(Redis redisClient, String key) {
RedisAPI redisAPI = RedisAPI.api(redisClient);
redisAPI.get(key, res -> {
if (res.succeeded()) {
String value = String.valueOf(res.result());
logger.info(">>>>>>>>Read data from redis sentinel (key, value)->:({},{})", key, value);
} else {
logger.error(">>>>>>>>Redis sentinel read data error:", res.cause());
}
});
}
public static void main(String[] args) {
VertxSentinelOpRedisTest vertxOpRedisTest = new VertxSentinelOpRedisTest();
vertxOpRedisTest.readDataFromSentinel(vertxOpRedisTest.getRedisClient(), "name");
}
}
7 Summary
Vert.x 3.9.4 Connect and operate Redis The core :
(1) To configure Redis Connection address , adopt RedisOptions, When instantiating this class , There are default parameters ;
(2)Vert.x3.9.4 Provided Redis The client cannot connect to the one that requires password authentication Redis colony , however , You can connect monomers that need password authentication Redis;
(3) Create a connection to use Redis.createClient, The method is based on type Select the corresponding Redis client : colony (cluster)、 sentry (sentinel) Monomer (standalone);
(4) operation Redis Use RedisAPI complete ;
(5)Vert.x operation Redis It is completed through asynchronous threads :Vert.x The thread of , Such as vert.x-eventloop-thread-0.
边栏推荐
猜你喜欢
![[pytorch record] automatic hybrid accuracy training torch cuda. amp](/img/a5/cf1eb2801380cf2887dfd532d3eb1e.jpg)
[pytorch record] automatic hybrid accuracy training torch cuda. amp

118. Yanghui triangle
![pickle. Load error [attributeerror: can't get attribute 'volatile' on < module '\u main']](/img/98/c4df0747856eda262b82942eedad8f.png)
pickle. Load error [attributeerror: can't get attribute 'volatile' on < module '\u main']

ddr4测试-2

Methods of finding various limits

白盒加密技术浅理解

SIP protocol of gb28181

【森城市】GIS数据漫谈(一)

Summary of SQL query de duplication statistics methods

Solidity - contract structure - error - ^0.8.4 NEW
随机推荐
事务隔离级别 gap锁 死锁
音视频、编解码相关电子书、小工具,打包奉送!
Summary of SQL query de duplication statistics methods
ffmpeg 错误码
Proxy in ES6
Junit单元测试框架详解
Extensive reading of the paper [film: visual reasoning with a general condition layer]
ddr4测试-2
Solidity - contract structure - error - ^0.8.4 NEW
【Go ~ 0到1 】 第五天 7月1 类型别名,自定义类型,接口,包与初始化函数
wireshark报文分析tcp,ftp
Nacos configuration file publishing failed, please check whether the parameters are correct solution
面试题 16.16. 部分排序-双指针法
精耕渠道共谋发展 福昕携手伟仕佳杰开展新产品培训大会
Solution and summary of Nacos startup failure
Live HLS protocol
How to redraw the header of CListCtrl in MFC
线程的并行、并发、生命周期
Solidity - truncated and checked modes of arithmetic operations - new features of 0.8.0
Write it down once Net travel management background CPU Explosion Analysis