doudiyu1639 2018-10-08 05:10
浏览 663

使用Redis架构URL连接到Redis服务器

I'm trying to connect to a Redis server which is hosted on a url like this.

redis://h:asdfqwer1234asdf@ec2-111-1-1-1.compute-1.amazonaws.com:38799

I have tried to use 2 libraries and none of them is able to connect to the server. I've used redix.v3 and go-redis.

With redix.v3 I get panic error when using the above like URL.

On go-redis I got an error about there being too many colons in the url and I've tried using this url [redis://h:asdfqwer1234asdf@ec2-111-1-1-1.compute-1.amazonaws.com]:38799 which was suggested on some post.

Still no luck. Has anyone successfully connected to a Redis server?

Code and Error for redix.v3

func main() {
    fmt.Println("running")
    client, err := radix.NewPool("tcp", "redis://h:asdfqwer1234asdf@ec2-111-1-1-1.compute-1.amazonaws.com:38799", 10)
    if err != nil {
        // handle error
    }

    var fooVal string
    err = client.Do(radix.Cmd(&fooVal, "SET", "foo", "hello"))
    fmt.Println(err, fooVal)
}

Error:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x4f2b7e]

goroutine 1 [running]:
github.com/mediocregopher/radix%2ev3.(*Pool).getExisting(0x0, 0x0, 0x0, 0x0)
    /home/aks/go/src/github.com/mediocregopher/radix.v3/pool.go:365 +0x4e
github.com/mediocregopher/radix%2ev3.(*Pool).get(0x0, 0x40aa78, 0x51afe0, 0x525120)
    /home/aks/go/src/github.com/mediocregopher/radix.v3/pool.go:403 +0x2f
github.com/mediocregopher/radix%2ev3.(*Pool).Do(0x0, 0x7f6478467fd0, 0xc0000e2070, 0x0, 0x0)
    /home/aks/go/src/github.com/mediocregopher/radix.v3/pool.go:440 +0x37
main.main()
    /home/aks/hello.go:17 +0x19e
exit status 2

Code and error for go-redis

client := redis.NewClient(&redis.Options{
        Addr:     "redis://h:asdfqwer1234asdf@ec2-111-1-1-1.compute-1.amazonaws.com:38799",
        Password: "", // no password set
        DB:       0,  // use default DB
    })

// setup eviction policy on the redis client
client.ConfigSet("maxmemory", Config.RedisMaxMemory)
client.ConfigSet("maxmemory-policy", "allkeys-lru")

_, err := client.Ping().Result()

if err != nil {
    log.Println("Redis: failed to connect", err)
} else {
    log.Println("Redis: connected")
}

The Error:

2018/10/08 10:57:29 Redis: failed to connect dial tcp: address redis://h:asdfqwer1234asdf@ec2-111-1-1-1.compute-1.amazonaws.com:38799: too many colons in address
  • 写回答

1条回答 默认 最新

  • douliao8760 2018-10-08 05:24
    关注

    You can use the ParseURL function in go-redis

    opt, _ := redis.ParseURL("redis://:qwerty@localhost:6379")
    client := redis.NewClient(opt)
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向