更新时间:2020-05-08 13:41:38
您可以使用多种语言的客户端连接网宿云Redis。由于云数据库Redis提供的数据库服务与原生的数据库服务完全兼容,连接数据库的方式也基本类似,任何兼容Redis协议的客户端都可以访问云数据库Redis版,您可以根据自身应用特点选用任何Redis客户端。
下面提供两种客户端连接方式的示例。
云数据库Redis分为集群版和非集群版,需要分别对应的客户端进行连接。
对于Redis集群版,操作步骤如下:
1.下载并安装redis-py-cluster客户端。
2.在任何一款可以编辑Python的编辑器中输入以下示例代码,即可建立连接通过本地Redis cluster客户端访问数据库。
示例代码:
#! /usr/bin/env python
import redis
from rediscluster import StrictRedisCluster
#这里替换为连接实例的IP地址和端口
startup_nodes = [{"host": "10.250.0.6", "port": "6379"},
{"host": "10.250.0.7", "port": "6379"},
{"host": "10.250.0.8", "port": "6379"},
{"host": "10.250.0.9", "port": "6379"},
{"host": "10.250.0.10", "port": "6379"},
{"host": "10.250.0.11", "port": "6379"}]
redisClient = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True, password='xxxxxx', max_connections=100)
redisClient.set("s1", "string0")
print(redisClient.get("s1"))
print(redisClient.strlen("s1"))
print(redisClient.getrange("s1", "0", "2"))
redisClient.setrange("s1", "6", "1")
print(redisClient.get("s1"))
redisClient.delete("s1")
redisClient.set("s2", "string0")
print(redisClient.get("s2"))
print(redisClient.strlen("s2"))
print(redisClient.getrange("s2", "0", "2"))
redisClient.setrange("s2", "6", "1")
print(redisClient.get("s2"))
redisClient.delete("s2")
redisClient.set("s3", "string0")
print(redisClient.get("s3"))
print(redisClient.strlen("s3"))
redisClient.delete("s3")
对于Redis主从版和单机版,操作步骤如下:
1.下载并安装Python-redis客户端。
2.在任何一款可以编辑Python的编辑器中输入以下示例代码,即可建立连接通过本地Python-redis客户端访问数据库。
示例代码:
#! /usr/bin/env python
import redis
#这里替换为连接实例的IP地址和端口
connectionPool = redis.ConnectionPool(host='11.0.32.7', port=6379, password='xxxxxx', max_connections=100)
redisClient = redis.Redis(connection_pool=connectionPool)
redisClient.set("s0", "string0")
print(redisClient.get("s0"))
print(redisClient.strlen("s0"))
print(redisClient.getrange("s0", "0", "2"))
redisClient.setrange("s0", "6", "1")
print(redisClient.get("s0"))
redisClient.delete("s0")
Jedis客户端访问云数据库Redis也分为集群版和非集群版,操作步骤如下:
1.下载并安装Jedis客户端。
2.Redis集群版需要使用JedisCluster连接,连接示例如下所示。
import redis.clients.jedis.*;
import java.util.*;
public class RedisUtil {
private static String password = "xxxxxx";
private static int timeOut = 10000;
// 可用连接实例的最大数目,默认值为8;
// 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)
private static int maxTotal = 1024;
// 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8
private static int maxIdle = 200;
// 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException
private static int maxWait = 10000;
// 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的
private static boolean testOnBorrow = true;
public static JedisCluster createClusterConnection()
{
Set<HostAndPort> nodes = new HashSet<>();
try
{
JedisPoolConfig jconfig = new JedisPoolConfig();
jconfig.setMaxTotal(maxTotal);
jconfig.setMaxIdle(maxIdle);
jconfig.setMaxWaitMillis(maxWait);
jconfig.setTestOnBorrow(testOnBorrow);
#这里替换为连接Redis集群的各节点IP地址和端口
nodes.add(new HostAndPort("11.0.6.21", 6379));
nodes.add(new HostAndPort("11.0.6.22", 6379));
nodes.add(new HostAndPort("11.0.6.23", 6379));
nodes.add(new HostAndPort("11.0.6.24", 6379));
nodes.add(new HostAndPort("11.0.6.25", 6379));
nodes.add(new HostAndPort("11.0.6.26", 6379));
//parameter that is needed
int connectionTimeout = timeOut;
int soTimeout = timeOut;
int maxAttempts = 5;
JedisCluster cluster = new JedisCluster(nodes, connectionTimeout, soTimeout, maxAttempts, password, jconfig);
return cluster;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
public static void main(String[] args)
{
System.out.println("Hello world!");
JedisCluster js = createClusterConnection();
js.set("s1", "string1");
js.set("s2", "string2");
js.set("s3", "string3");
System.out.println(js.get("s1"));
System.out.println(js.get("s2"));
System.out.println(js.get("s3"));
js.close();
}
}
3.Redis主从版和单机版连接示例如下所示。
import redis.clients.jedis.*;
import java.util.*;
public class RedisUtil {
#这里替换为连接实例的IP地址和端口
private static String address = "11.0.6.28";
private static int port = 6379;
private static String password = "xxxxxx";
private static int timeOut = 10000;
// 可用连接实例的最大数目,默认值为8;
// 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)
private static int maxTotal = 1024;
// 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8
private static int maxIdle = 200;
// 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException
private static int maxWait = 10000;
// 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的
private static boolean testOnBorrow = true;
private static JedisPool createRedisConnectionPool()
{
try
{
JedisPoolConfig jconfig = new JedisPoolConfig();
jconfig.setMaxTotal(maxTotal);
jconfig.setMaxIdle(maxIdle);
jconfig.setMaxWaitMillis(maxWait);
jconfig.setTestOnBorrow(testOnBorrow);
JedisPool jedisPool = new JedisPool(jconfig, address, port, timeOut, password);
return jedisPool;
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
public static void main(String[] args)
{
System.out.println("Hello world!");
JedisPool jedisPool = createRedisConnectionPool();
Jedis jedis = jedisPool.getResource();
jedis.set("foo", "bar");
System.out.println(jedis.get("foo"));
jedis.close();
jedisPool.close();
}
}