当前位置:网站首页>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
边栏推荐
- 3311. 最长算术
- SharePoint modification usage analysis report is more than 30 days
- 【最詳細】最新最全Redis面試大全(50道)
- Common methods of file class
- [solved] unknown error 1146
- Introduction of transformation flow
- The embodiment of generics in inheritance and wildcards
- SecureCRT取消Session记录的密码
- sharepoint 2007 versions
- Win 2008 R2 crashed at the final installation stage
猜你喜欢

Take you through the whole process and comprehensively understand the software accidents that belong to testing

Le Seigneur des anneaux: l'anneau du pouvoir

Margin left: -100% understanding in the Grail layout

JS monitors empty objects and empty references

Deep learning parameter initialization (I) Xavier initialization with code

Leetcode 213: 打家劫舍 II

Comparison of advantages and disadvantages between most complete SQL and NoSQL

【开发笔记】基于机智云4G转接板GC211的设备上云APP控制

File operation serialization recursive copy

带你全流程,全方位的了解属于测试的软件事故
随机推荐
Hello world of vertx
The embodiment of generics in inheritance and wildcards
SecureCRT password to cancel session recording
Strategy mode
Understanding of class
2021-07-18
【已解决】win10找不到本地组策略编辑器解决方法
Arduino Serial系列函数 有关print read 的总结
Introduction of buffer flow
When MySQL inserts Chinese into the database, there is a diamond question mark garbled code
[set theory] order relation (partial order relation | partial order set | example of partial order set)
树莓派更新工具链
Basic knowledge about SQL database
La différence entre le let Typescript et le Var
【CMake】CMake链接SQLite库
Advanced API (multithreading)
Split small interface
Introduction of transformation flow
论文学习——鄱阳湖星子站水位时间序列相似度研究
TypeScript let与var的区别