首页 > 解决方案 > 连接到 Redis Elasticache 实例,启用了传输中加密,无需使用 Scala 进行身份验证

问题描述

我正在尝试连接到 AWS 上的 Elasticache Redis 实例,但遇到了一些麻烦...

禁用传输中加密时,我可以正常连接。但是,当它启用时,我遇到了问题。

我正在使用scala-redis,我可以在读取时连接到 redis 客户端。然而,在写入时没有任何反应,我的工作只是挂起并最终超时。elasticache 没有任何身份验证,我按照 scala-redis 中的测试示例建立 SSL 连接。

object RedisClient {
  var writeHost: String = "HOST"
  var readHost: String = "HOST"
  var port: Int = 6379
  var keyTTL: Int = 3600
  var maxIdle: Int = 8
  var database: Int = 0
  var secret: Option[Any] = None
  var timeout: Int = 300000
  var maxConnections: Int = RedisClientPool.UNLIMITED_CONNECTIONS
  var poolWaitTimeout: Int = 3000

  private val sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy(){
    def isTrusted(arg0: Array[X509Certificate], arg1:String) = true
  }).build()

  private lazy val writePool = new RedisClientPool(writeHost, port, maxIdle, database, secret, timeout, maxConnections, poolWaitTimeout, Some(sslContext))
  private lazy val readPool = new RedisClientPool(readHost, port, maxIdle, database, secret, timeout, maxConnections, poolWaitTimeout)
  def getCount(id: String): Option[String] = {
    readPool.withClient(client => {
      client.get(id)
    })
  }

  def setCount(id: String, count: String): Boolean = writePool.withClient(client => {
    client.setex(id, keyTTL, count)
  })

  def close(): Unit = {
    readPool.close()
    writePool.close()
  }
}

标签: scalasslredisamazon-elasticache

解决方案


推荐阅读