Using go with redis - not able to connect

5/21/2020

I'm trying to connect to Redis service in azure

I use the following code:

    import (
        "fmt"
        "os"
        "github.com/go-redis/redis"
    )


func main() {
     uri := os.Getenv("uri")
     fmt.Println("uri is", uri)
     opt, err := redis.ParseURL(uri)
     if err != nil {
         fmt.Println(err)
      }
     fmt.Println("addr is:”, opt.Addr)
     fmt.Println("db is:”, opt.DB)
     fmt.Println("password is:”, opt.Password)
     //connect to redis
     client = redis.NewClient(opt)
     //Here I want to get connection
     pong, err := client.Ping().Result()

https://github.com/go-redis/redis

https://godoc.org/github.com/go-redis/redis#example-ParseURL

The printed data is like following I was able to the the connection string

uri is rediss://:bBMfQ7wFdkPHr8u%2B2zzNOzUpy85OEjYv7KbPZd8B89M%3D@e49ab3c6-8f72-416a-a6c1-ddfe75gf200e.redis.cache.windows.net:6380


addr is: e49ab3c6-8f72-416a-a6c1-ddfe75gf200e.redis.cache.windows.net:6380
db is: 0
password is: bBMfQ7wFdkPHr8u+2zzNOzUpy85OEjYv7KbPZd8B89M=

The Redis service is up and running and I was able to connect with nodejs application and get the data, any idea why I got the error in go:

dial tcp 40.128.8.87:6380: connect: connection refused

We are trying to migrate some apps from Nodejs to go ...

The strange thing is that in nodejs I use the following which works!

https://github.com/NodeRedis/node-redis

const redisClient = redis.createClient(process.env.uri);

redisClient.on("connect", () => console.log("Redis is connected"));

update

I've provisioned a new instance without ssl which is not working either

This is the logs

uri is redis://:TKq1n%2BO29jsSdIkoysXL%2Btwd6Xi0IO0KNxw%3D@2dca2dx1-20e0-48ca-83ed-20bf52e99b97.redis.cache.windows.net:6379
addr is 2dca2dx1-20e0-48ca-83ed-20bf52e99b97.redis.cache.windows.net:6379
db is 0
password is TKq1n+O29jsSdIkoysXL+twd6Xi0IO0KNxw=
 dial tcp 40.113.7.3:6379: connect: connection refused

Any idea how should I use the GO api to connect to redis properly ?

Btw, when I connected to local Redis it works, but not to azure cache

like this

client := redis.NewClient(&redis.Options{
        Addr: "localhost:6379",
        Password: "",
        DB: 0,
    })

Maybe I miss something in the Redis api, as I do the same (connect to the same service instance ) for nodejs and it works!

-- Beno Odr
azure
azure-redis-cache
go
kubernetes
redis

0 Answers