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>&nbsp; &nbsp; <img src="https://obs-cn-shanghai.yun.pingan.com/pcp-portal/20190907114901-11179b419b56.png" style="height:31px; margin:0px; width:65px" />:</p> <p>&nbsp; &nbsp; &bull;&nbsp; Jedis版本需要大于等于2.9.0,<a href="https://github.com/xetorthio/jedis">下载地址</a>。</p> <p>&nbsp; &nbsp; &bull;&nbsp; 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>&nbsp;</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">&nbsp; private static JedisCluster pool = null;</span></p> <p><span style="font-family:Consolas">&nbsp; public static JedisCluster getPool() {</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp; &nbsp;if (pool == null) {</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; genericObjectPoolConfig.setMaxIdle(50); //连接池最大空闲连接</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; genericObjectPoolConfig.setMinIdle(20); //连接池最小空闲连接</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; genericObjectPoolConfig.setMaxWaitMillis(3000L);</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; genericObjectPoolConfig.setMaxTotal(100); //最大连接数</span></p> <p>&nbsp;</p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set&lt;HostAndPort&gt; node = new HashSet&lt;HostAndPort&gt;();</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; node.add(new HostAndPort(&quot;<em>Domain-Name</em> &quot;, <em>Port</em>));&nbsp; //修改为Redis实例的域名和端口号</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pool = new JedisCluster(node, 10000, 3000, 5, &quot;<em>Password</em>&quot;, genericObjectPoolConfig);&nbsp;</span></p> <p><span style="font-family:Consolas">//修改为控制台显示的访问密码</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; }</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; return pool;</span></p> <p><span style="font-family:Consolas">&nbsp; }</span></p> <p>&nbsp;</p> <p><span style="font-family:Consolas">&nbsp; public static void main(String[] args)</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; throws Exception</span></p> <p><span style="font-family:Consolas">&nbsp; {</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JedisCluster jc = getPool();</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; jc.set(&quot;foo&quot;, &quot;pingan yun&quot;);</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(jc.get(&quot;foo&quot;));</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch (Exception e)</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p> <p><span style="font-family:Consolas">&nbsp; }</span></p> </td> </tr> </tbody> </table> <p>&nbsp; &nbsp; &nbsp;<img src="https://obs-cn-shanghai.yun.pingan.com/pcp-portal/20190907115131-1b79a8a39d2b.png" style="height:26px; margin:0px; width:65px" />:</p> <p>&nbsp; &nbsp; &bull;&nbsp; <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>&nbsp; &nbsp; &bull;&nbsp; <em>Port</em>:Redis实例的<strong>连接端口</strong>,可参考<a href="https://pinganyun.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance">查看实例基本信息</a>。</p> <p>&nbsp; &nbsp; &bull;&nbsp; <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>
以上内容是否解决了您的问题?
请补全提交信息!
咨询·建议

电话咨询 - 7x24 小时

400-151-8800

邮件咨询

cloud@pingan.com

在线客服

7x24 小时,急速解答

工单支持

解决云产品相关技术问题