【问题标题】:Scala-redis Client connection to RedisCloud on Heroku issues关于 Heroku 问题的 Scala-redis 客户端连接到 RedisCloud
【发布时间】:2015-08-23 20:21:05
【问题描述】:

我正在使用新的非阻塞 redis 客户端 https://github.com/debasishg/scala-redis 连接到 Heroku 上的 Rediscloud DB。 所需的连接字符串是例如以下格式。

redis://rediscloud:@pub-redis-.us-east-1-3.4.ec2.garantiadata.com:

请注意,它在连接字符串中包含密码字符串。

我的问题是如何使用 RedisClient 对象根据代码文档提供具有这样签名的密码?

对象 RedisClient {

def apply(host: String, port: Int = 6379, name: String = defaultName,
        settings: RedisClientSettings = RedisClientSettings())(

隐式 refFactory:ActorRefFactory): RedisClient ....

......

......

}

【问题讨论】:

  • 其实是redis://rediscloud:>@pub-redis->.us-east-1-3.4.ec2。 garantiadata.com:>。我想这些字符 被 html 渲染器吞噬了!
  • 您找到解决方案了吗?也面临这个问题。

标签: scala heroku redis


【解决方案1】:

查看Redis文档,其实并没有用户名的概念。 AUTH command 只需要密码。

Scala-redis 使用do the authentication 提供的secret 参数,使用AUTH

Heroku example for Redis Cloud 中,他们提供了如何使用 JedisPool(Java Redis 库)进行连接的示例。他们可以更好地提及它,但在文档中没有任何地方实际使用 Redis URL 中的用户名。

在 Java 示例中,他们简单地拆分了用户信息组件user:password,并仅提取冒号后的第二部分:password。用户名从未使用过。

回到你关于 Scala-redis 的问题,你想要的构造函数是:

RedisClient(override val host: String, override val port: Int,
    override val database: Int = 0, override val secret: Option[Any] = None, override val timeout : Int = 0)

secret 是 Redis URL 的密码部分。

我最终在自己的应用程序中使用了以下内容:

val redisClient = Properties.envOrNone("REDISCLOUD_URL") match {
      case Some(redisUrl) => {
        val redisUri = new URI(redisUrl)
        val host = redisUri.getHost()
        val port = redisUri.getPort()
        val secret = Try(redisUri.getUserInfo().split(":",2).last).toOption
        new RedisClient(host, port, secret = secret)
      }
      case _ => {
          // Deal with missing configuration here
      }
    }

【讨论】:

    猜你喜欢
    • 2016-01-25
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    • 2010-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多