GO连接

<p>本文主要介绍创建ECS并开通白名单后,如何在ECS上安装GO客户端,并通过GO客户端访问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. 配置GO的运行环境后,下载并安装GO客户端。集群实例,<a href="https://github.com/chasex/redis-go-cluster" target="_blank">下载地址</a>。</p> <p>3. 连接Redis实例。</p> <p>执行以下代码连接Redis集群实例:</p> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="background-color:#f7f8fa; vertical-align:top"> <p><span style="font-family:Consolas">package main</span></p> <p>&nbsp;</p> <p><span style="font-family:Consolas">import &quot;github.com/chasex/redis-go-cluster&quot;</span></p> <p><span style="font-family:Consolas">import &quot;fmt&quot;</span></p> <p><span style="font-family:Consolas">import &quot;time&quot;</span></p> <p>&nbsp;</p> <p><span style="font-family:Consolas">func main() {</span></p> <p><span style="font-family:Consolas">cluster, err := redis.NewCluster(</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; &amp;redis.Options{</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StartNodes: []string{&quot;<em>Domain-name</em>:<em>Port</em>&quot;},&nbsp;&nbsp; //修改为Redis集群实例的域名和端口号</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PassWord: &quot;<em>Password</em>&quot;,&nbsp;&nbsp;&nbsp;&nbsp; //修改为Redis实例的密码</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ConnTimeout: 3000 * time.Millisecond,</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ReadTimeout: 3000 * time.Millisecond,</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WriteTimeout: 3000 * time.Millisecond,</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; KeepAlive: 32,</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AliveTime: 60000 * time.Second,</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; })</span></p> <p>&nbsp;</p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; cluster.Do(&quot;SET&quot;,&quot;foo&quot;,&quot;pingan yun&quot;)</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; reply , err := (cluster.Do(&quot;GET&quot;,&quot;foo&quot;))</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; if err != nil {</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fmt.Printf(&quot;Get error :%s\n&quot;,err.Error())</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; }</span></p> <p><span style="font-family:Consolas">&nbsp;&nbsp;&nbsp; fmt.Printf(&quot;%s\n&quot;, reply)</span></p> <p><span style="font-family:Consolas">}</span></p> </td> </tr> </tbody> </table> <p>&nbsp; &nbsp; <img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20190907161733-1f46561e940a.png" style="height:26px; margin:0px; width:65px" />:</p> <p>&nbsp; &nbsp; &bull;&nbsp;&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;&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;&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>,表示通过GO客户端成功连接至Redis实例。</p>
以上内容是否解决了您的问题?
请补全提交信息!
咨询·建议

电话咨询

400-151-8800

邮件咨询

cloud@pingan.com

在线客服

工单支持

解决云产品相关技术问题