登录
首页 >  Golang >  Go问答

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

来源:stackoverflow

时间:2024-03-17 16:00:30 474浏览 收藏

使用 Redis 架构 URL 连接到 Redis 服务器时,可能遇到连接困难。这可能是由于 URL 格式不正确或库配置不当所致。本文将探讨使用 redix.v3 和 go-redis 库连接到 Redis 服务器的常见问题,并提供解决方案。其中包括使用 go-redis 中的 ParseURL 函数解析 URL,以及解决 redix.v3 中的紧急错误。

问题内容

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

redis://h:[电子邮件受保护]:38799

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

使用 redix.v3 时,我在使用上面类似的 url 时遇到紧急错误。

go-redis 上,我收到一个关于 url 中冒号太多的错误,我尝试使用此 url [redis://h:[email protected]]:38799 这是在某些帖子中建议的。

还是没有运气。有人成功连接到 redis 服务器吗?

redix.v3 的代码和错误

func main() {
    fmt.println("running")
    client, err := radix.newpool("tcp", "redis://h:[email protected]: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:[email protected]: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:[email protected]:38799: too many colons in address

解决方案


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

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

今天关于《使用 Redis 架构 URL 连接到 Redis 服务器》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>