Skip to content

Commit

Permalink
[warehouse] feature: Customize Redis DB (#763)
Browse files Browse the repository at this point in the history
* [code]feature:  Modifying `warehouse.store.redis.db` in `application.yml` can specify which database Redis should use.

* [code]feature:  Modifying `warehouse.store.redis.db` in `application.yml` can specify which database Redis should use.
add `saveData` select(db)
  • Loading branch information
zqr10159 authored and tomsun28 committed Jan 16, 2024
1 parent 08cea37 commit 0579c9d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions manager/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ warehouse:
host: 127.0.0.1
port: 6379
password: 123456
#redis使用数据库,默认为DB0
db: 0

alerter:
# custom console url
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,18 @@ public static class RedisProperties {
* redis 访问密码
*/
private String password;
/**
* redis 使用数据库,默认为DB0
*/
private Integer db = 0;

public Integer getDb() {
return db;
}

public void setDb(Integer db) {
this.db = db;
}

public boolean isEnabled() {
return enabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,20 @@
public class RealTimeRedisDataStorage extends AbstractRealTimeDataStorage {

private RedisClient redisClient;
private final Integer db;
private StatefulRedisConnection<String, CollectRep.MetricsData> connection;

public RealTimeRedisDataStorage(WarehouseProperties properties) {
this.serverAvailable = initRedisClient(properties);
this.db = getRedisSelectDb(properties);
}
private Integer getRedisSelectDb(WarehouseProperties properties){
return properties.getStore().getRedis().getDb();
}

@Override
public CollectRep.MetricsData getCurrentMetricsData(@NonNull Long monitorId, @NonNull String metric) {
RedisCommands<String, CollectRep.MetricsData> commands = connection.sync();
commands.select(db);
return commands.hget(String.valueOf(monitorId), metric);
}

Expand All @@ -70,6 +75,7 @@ public void saveData(CollectRep.MetricsData metricsData) {
return;
}
RedisAsyncCommands<String, CollectRep.MetricsData> commands = connection.async();
commands.select(db);
commands.hset(key, hashKey, metricsData).thenAccept(response -> {
if (response) {
log.debug("[warehouse] redis add new data {}:{}.", key, hashKey);
Expand Down

0 comments on commit 0579c9d

Please sign in to comment.