云数据库 GEMINIDB-通过Redisson连接实例:使用SingleServer模式连接实例

时间:2024-05-30 17:57:28

使用SingleServer模式连接实例

样例代码

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

public class SingleServerTests {

  private static void testSingleServer() {
    Config config = new Config();
    // 认证用的用户名和密码直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中存放(密码应密文存放、使用时解密),确保安全;
    // 本示例以用户名和密码保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量(环境变量名称请根据自身情况进行设置)EXAMPLE_USERNAME_ENV和EXAMPLE_PASSWORD_ENV。
    String password = System.getenv("EXAMPLE_PASSWORD_ENV");
    config.useSingleServer().setAddress("redis://172.xx.xx.xx:8635")
                            .setPassword(password);
    RedissonClient redisson = Redisson.create(config);
    execute(redisson);   // send requests to database
    redisson.shutdown();
  }

  public static void main(String[] args) {
    testSingleServer();
  }
}
support.huaweicloud.com/redisug-nosql/nosql_11_0202.html