【问题标题】:Connect to a redis server using redis schema URL使用 redis 架构 URL 连接到 redis 服务器
【发布时间】:2020-07-16 04:36:17
【问题描述】:

我正在尝试连接到托管在这样的 url 上的 Redis 服务器。

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

我尝试使用 2 个库,但没有一个能够连接到服务器。我用过redix.v3go-redis

redix.v3 使用上述类似 URL 时出现恐慌错误。

go-redis 上,我收到一个错误,提示网址中有太多冒号,我尝试使用此网址 [redis://h:asdfqwer1234asdf@ec2-111-1-1-1.compute-1.amazonaws.com]:38799 在一些帖子中被建议。

仍然没有运气。有没有人成功连接到 Redis 服务器?

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)
}

错误:

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

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")
}

错误:

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

【问题讨论】:

标签: go redis


【解决方案1】:

可以在go-redis中使用ParseURL函数

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

【讨论】:

    【解决方案2】:

    如果您使用go-redis,请从客户端地址中删除redis://,以避免出现too many colons in address 错误。

    【讨论】:

      猜你喜欢
      • 2017-09-16
      • 2021-06-23
      • 1970-01-01
      • 2019-08-17
      • 2013-02-25
      • 2017-03-01
      • 2012-03-17
      • 2013-07-25
      • 1970-01-01
      相关资源
      最近更新 更多