Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[code]feature: Customize Redis DB #763

Merged
merged 2 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions manager/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,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