Jedis连接
<p>本文主要介绍创建ECS并开通白名单后,如何在ECS上安装Jedis客户端,并通过Jedis客户端访问Redis实例。</p>
<p><span style="font-size:18px"><strong>前提条件</strong></span></p>
<ol>
<li>您已成功创建Redis实例,可参考创<a href="https://pinganyun.com/ssr/help/database/Redis/Getting_Started.Create_Redis" target="_blank">建Redis实例</a>,且实例处于运行中。</li>
<li>您已成功创建ECS,可参考创<a href="https://pinganyun.com/ssr/help/database/Redis/Getting_Started.Create_ECS" target="_blank">建ECS</a>,且实例处于运行中。</li>
<li>您已成功开通白名单,可参考开<a href="https://pinganyun.com/ssr/help/database/Redis/Getting_Started.Create_Whitelist" target="_blank">通白名单</a>。</li>
</ol>
<p><span style="font-size:18px"><strong>注意事项</strong></span></p>
<p>ECS的操作系统请选择CentOS 7.x、RedHat Linux 7.x、Oracle Linux 7.x及以上的版本。</p>
<p><span style="font-size:18px"><strong>操作步骤</strong></span></p>
<p>1. 登录ECS,可参考<a href="https://pinganyun.com/ssr/help/compute/ecs/Quick_Start.Linux_Quick_Start.Login_Instance" target="_blank">登录Linux云主机</a>。</p>
<p>2. 下载并安装Jedis客户端和common-pool2。</p>
<p> <img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20190907114901-11179b419b56.png" style="height:31px; margin:0px; width:65px" />:</p>
<p> • Jedis版本需要大于等于2.9.0,<a href="https://github.com/xetorthio/jedis">下载地址</a>。</p>
<p> • commons-pool2版本需要大于等于2.4.2,<a href="http://commons.apache.org/proper/commons-pool/download_pool.cgi">下载地址</a>。</p>
<p>3. 连接Redis实例。在Eclipse客户端,创建一个Project,并执行以下代码。</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa; vertical-align:top">
<p><span style="font-family:Consolas">import java.util.HashSet;</span></p>
<p><span style="font-family:Consolas">import java.util.Set;</span></p>
<p><span style="font-family:Consolas">import org.apache.commons.pool2.impl.GenericObjectPoolConfig;</span></p>
<p><span style="font-family:Consolas">import redis.clients.jedis.HostAndPort;</span></p>
<p><span style="font-family:Consolas">import redis.clients.jedis.JedisCluster;</span></p>
<p> </p>
<p><span style="font-family:Consolas">public class pacloud_redis_usage</span></p>
<p><span style="font-family:Consolas">{</span></p>
<p><span style="font-family:Consolas"> private static JedisCluster pool = null;</span></p>
<p><span style="font-family:Consolas"> public static JedisCluster getPool() {</span></p>
<p><span style="font-family:Consolas"> if (pool == null) {</span></p>
<p><span style="font-family:Consolas"> GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();</span></p>
<p><span style="font-family:Consolas"> genericObjectPoolConfig.setMaxIdle(50); //连接池最大空闲连接</span></p>
<p><span style="font-family:Consolas"> genericObjectPoolConfig.setMinIdle(20); //连接池最小空闲连接</span></p>
<p><span style="font-family:Consolas"> genericObjectPoolConfig.setMaxWaitMillis(3000L);</span></p>
<p><span style="font-family:Consolas"> genericObjectPoolConfig.setMaxTotal(100); //最大连接数</span></p>
<p> </p>
<p><span style="font-family:Consolas"> Set<HostAndPort> node = new HashSet<HostAndPort>();</span></p>
<p><span style="font-family:Consolas"> node.add(new HostAndPort("<em>Domain-Name</em> ", <em>Port</em>)); //修改为Redis实例的域名和端口号</span></p>
<p><span style="font-family:Consolas"> pool = new JedisCluster(node, 10000, 3000, 5, "<em>Password</em>", genericObjectPoolConfig); </span></p>
<p><span style="font-family:Consolas">//修改为控制台显示的访问密码</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> return pool;</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p> </p>
<p><span style="font-family:Consolas"> public static void main(String[] args)</span></p>
<p><span style="font-family:Consolas"> throws Exception</span></p>
<p><span style="font-family:Consolas"> {</span></p>
<p><span style="font-family:Consolas"> JedisCluster jc = getPool();</span></p>
<p><span style="font-family:Consolas"> try {</span></p>
<p><span style="font-family:Consolas"> jc.set("foo", "pingan yun");</span></p>
<p><span style="font-family:Consolas"> System.out.println(jc.get("foo"));</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> catch (Exception e)</span></p>
<p><span style="font-family:Consolas"> {</span></p>
<p><span style="font-family:Consolas"> e.printStackTrace();</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> }</span></p>
</td>
</tr>
</tbody>
</table>
<p> <img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20190907115131-1b79a8a39d2b.png" style="height:26px; margin:0px; width:65px" />:</p>
<p> • <em>Domain-Name</em>:Redis实例的<strong>访问域名</strong>,可参考<a href="https://pinganyun.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance" target="_blank">查看实例基本信息</a>。</p>
<p> • <em>Port</em>:Redis实例的<strong>连接端口</strong>,可参考<a href="https://pinganyun.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance">查看实例基本信息</a>。</p>
<p> • <em>Password</em>:Redis实例的<strong>密码</strong>,在创建Redis实例时设置(可参考<a href="https://pinganyun.com/ssr/help/database/Redis/User_Guide.Instance_Management.Create_Instance">创建实例</a>),可参考<a href="https://pinganyun.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance">查看实例基本信息</a>查看。</p>
<p><span style="font-size:18px"><strong>执行结果</strong></span></p>
<p>系统返回<strong>pingan yun</strong>,表示通过Jedis客户端成功连接至Redis实例。</p>
提交成功!非常感谢您的反馈,我们会继续努力做到更好!