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> </p>
<p><span style="font-family:Consolas">import "github.com/chasex/redis-go-cluster"</span></p>
<p><span style="font-family:Consolas">import "fmt"</span></p>
<p><span style="font-family:Consolas">import "time"</span></p>
<p> </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"> &redis.Options{</span></p>
<p><span style="font-family:Consolas"> StartNodes: []string{"<em>Domain-name</em>:<em>Port</em>"}, //修改为Redis集群实例的域名和端口号</span></p>
<p><span style="font-family:Consolas"> PassWord: "<em>Password</em>", //修改为Redis实例的密码</span></p>
<p><span style="font-family:Consolas"> ConnTimeout: 3000 * time.Millisecond,</span></p>
<p><span style="font-family:Consolas"> ReadTimeout: 3000 * time.Millisecond,</span></p>
<p><span style="font-family:Consolas"> WriteTimeout: 3000 * time.Millisecond,</span></p>
<p><span style="font-family:Consolas"> KeepAlive: 32,</span></p>
<p><span style="font-family:Consolas"> AliveTime: 60000 * time.Second,</span></p>
<p><span style="font-family:Consolas"> })</span></p>
<p> </p>
<p><span style="font-family:Consolas"> cluster.Do("SET","foo","pingan yun")</span></p>
<p><span style="font-family:Consolas"> reply , err := (cluster.Do("GET","foo"))</span></p>
<p><span style="font-family:Consolas"> if err != nil {</span></p>
<p><span style="font-family:Consolas"> fmt.Printf("Get error :%s\n",err.Error())</span></p>
<p><span style="font-family:Consolas"> }</span></p>
<p><span style="font-family:Consolas"> fmt.Printf("%s\n", reply)</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/20190907161733-1f46561e940a.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>,表示通过GO客户端成功连接至Redis实例。</p>
提交成功!非常感谢您的反馈,我们会继续努力做到更好!