当前位置:网站首页>Vertx's responsive redis client
Vertx's responsive redis client
2022-07-03 07:25:00 【Sleeping Empire】
Introduce
Vertx Response type mysql client , With simple API, Focus on scalability and low overhead , characteristic :
- Support single example redis
- Support Sentinel Pattern
- Support Cluster Pattern
- Master-slave ( Single slice , One node write , Multiple reads )
1. maven Project dependence
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-config-yaml</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-redis-client</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.lance.common</groupId>
<artifactId>vertx-common-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
2.YAML File configuration
server:
port: 9000
redis:
urls:
- redis://127.0.0.1:6380/0
clientType: STANDALONE
poolName: p-red
poolCleanerInterval: 30000
maxPoolSize: 8
maxPoolWaiting: 32
3. Start loading profile , And into the config In the middle
public class RedisApplication {
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
ConfigRetriever retriever = readYaml(vertx);
retriever.getConfig(json -> {
try {
JsonObject object = json.result();
DbHelper dbHelper = new DbHelper(object.getJsonObject("redis"), vertx);
dbHelper.afterPropertiesSet();
DeploymentOptions options = new DeploymentOptions().setConfig(object);
vertx.deployVerticle(MainApp.class.getName(), options);
} catch (Exception ex) {
log.error("===> Vertx start fail: ", ex);
}
});
}
/**
* read yaml config
*
* @param vertx vertx
* @return ConfigRetriever
*/
private static ConfigRetriever readYaml(Vertx vertx) {
ConfigStoreOptions store = new ConfigStoreOptions()
.setType("file")
.setFormat("yaml")
.setOptional(true)
.setConfig(new JsonObject().put("path", "application.yaml"));
return ConfigRetriever.create(vertx, new ConfigRetrieverOptions().addStore(store));
}
}
4.Redis Connection pool configuration
public class DbHelper {
private final JsonObject object;
private final Vertx vertx;
@Getter
private static RedisAPI redis;
/**
* initialization redis Connect
*/
public void afterPropertiesSet() {
ConfigProperties.RedisProperties properties = object.mapTo(ConfigProperties.RedisProperties.class);
if (properties.getUrls() == null || properties.getUrls().length == 0) {
throw new RuntimeException("Redis connect url is not null.");
}
RedisOptions options = new RedisOptions()
.setType(properties.getClientType())
.setPoolName(properties.getPoolName())
.setMaxPoolSize(properties.getMaxPoolSize())
.setMaxPoolWaiting(properties.getMaxPoolWaiting())
.setPoolCleanerInterval(properties.getPoolCleanerInterval());
// password
if (StringUtils.isNotBlank(properties.getPassword())) {
options.setPassword(properties.getPassword());
}
// connect address [redis://localhost:7000, redis://localhost:7001]
for (String url : properties.getUrls()) {
options.addConnectionString(url);
}
// sentinel
if (properties.getClientType().equals(RedisClientType.SENTINEL)) {
options.setRole(properties.getRole()).setMasterName(properties.getMasterName());
}
redis = RedisAPI.api(Redis.createClient(vertx, options));
}
}
5.redis Execution examples
public class UserService {
/**
* find list
*/
public void list(RoutingContext ctx) {
ctx.json(R.data("list"));
}
/**
* find one
*/
public void detail(RoutingContext ctx) {
String userId = ctx.pathParam("userId");
RedisAPI redis = DbHelper.getRedis();
redis.get("hello")
.onSuccess(val -> ctx.json(R.data("===> detail result: " + val)))
.onFailure(event -> ctx.json(R.fail(event.getMessage())));
}
/**
* add user info
*/
public void add(RoutingContext ctx) {
RedisAPI redis = DbHelper.getRedis();
redis.getset("hello", "Hello world!")
.onSuccess(val -> ctx.json(R.data("===> add result: " + val)))
.onFailure(event -> ctx.json(R.fail(event.getMessage())));
}
/**
* update user
*/
public void update(RoutingContext ctx) {
UserInfo user = ctx.getBodyAsJson().mapTo(UserInfo.class);
if (user == null) {
ctx.json(R.fail(" The parameter is empty. "));
return;
}
ctx.json(R.data(user));
}
/**
* delete one
*/
public void delete(RoutingContext ctx) {
String userId = ctx.pathParam("userId");
RedisAPI redis = DbHelper.getRedis();
redis.del(Arrays.asList("hello", userId))
.onSuccess(val -> ctx.json(R.data("===> delete result:" + val)))
.onFailure(event -> ctx.json(R.fail(event.getMessage())));
}
}
6. Full address of the project
边栏推荐
- 【CMake】CMake链接SQLite库
- VMware virtual machine installation
- Sorting, dichotomy
- Distributed ID
- Topic | synchronous asynchronous
- Interfaces and related concepts
- Advanced API (multithreading)
- The difference between typescript let and VaR
- C code production YUV420 planar format file
- IO stream system and FileReader, filewriter
猜你喜欢
Summary of abnormal mechanism of interview
SecureCRT password to cancel session recording
[HCAI] learning summary OSI model
High concurrency memory pool
Introduction of buffer flow
1. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
691. 立方体IV
Flask Foundation
Common methods of file class
Basic components and intermediate components
随机推荐
SQL create temporary table
Topic | synchronous asynchronous
The babbage industrial policy forum
【已解决】Unknown error 1146
How long is the fastest time you can develop data API? One minute is enough for me
Dora (discover offer request recognition) process of obtaining IP address
TCP cumulative acknowledgement and window value update
“百度杯”CTF比赛 2017 二月场,Web:爆破-1
Visit Google homepage to display this page, which cannot be displayed
7.2刷题两个
Advanced API (character stream & net for beginners)
TypeScript let与var的区别
深度学习参数初始化(一)Xavier初始化 含代码
Wireshark software usage
The underlying mechanism of advertising on websites
2. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
Recursion, Fibonacci sequence
萬卷書 - 價值投資者指南 [The Education of a Value Investor]
Hello world of vertx
《指環王:力量之戒》新劇照 力量之戒鑄造者亮相