介绍
Vertx响应式mysql客户端,具有简单的API,关注可伸缩性和低开销, 特性:
- 支持单例redis
- 支持Sentinel模式
- 支持Cluster模式
- 主从(单个分片,一个节点写入,多个读取)
1. maven项目依赖
<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文件配置
server:
port: 9000
redis:
urls:
- redis://127.0.0.1:6380/0
clientType: STANDALONE
poolName: p-red
poolCleanerInterval: 30000
maxPoolSize: 8
maxPoolWaiting: 32
3.启动加载配置文件, 并放入到config中去
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连接池配置
public class DbHelper {
private final JsonObject object;
private final Vertx vertx;
@Getter
private static RedisAPI redis;
/**
* 初始化redis连接
*/
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执行实例
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("参数为空"));
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.项目完整地址
Vertx之Redis客户端 Github 地址
Vertx之Redis客户端 Gitee 地址