zoukankan      html  css  js  c++  java
  • redis-scala链接redis集群

    代码: 

    package com.wenbronk.sparkstreaming.scala.commons
    
    import java.time.Duration
    
    import io.lettuce.core.RedisURI
    import io.lettuce.core.cluster.api.StatefulRedisClusterConnection
    import io.lettuce.core.cluster.{ClusterClientOptions, ClusterTopologyRefreshOptions, RedisClusterClient}
    
    import scala.collection.immutable
    
    
    object RedisUtils {
    
      val ip = "10.110.122.172"
      val ports = Array(7000, 7001, 7002, 7003, 7004, 7005)
    
      def getRediceConnect: StatefulRedisClusterConnection[String, String] = {
        val uris: immutable.Seq[RedisURI] = ports.map(port => {
          RedisURI.builder().withHost(ip).withPort(port).build()
        }).toList
        import scala.collection.JavaConverters._
        val redisClusterClient = RedisClusterClient.create(uris.asJava)
        val topologyRefreshOptions = ClusterTopologyRefreshOptions.builder.enablePeriodicRefresh(Duration.ofMinutes(10)).enableAllAdaptiveRefreshTriggers.build
        redisClusterClient.setOptions(ClusterClientOptions.builder.autoReconnect(true).pingBeforeActivateConnection(true).topologyRefreshOptions(topologyRefreshOptions).build)
        redisClusterClient.connect()
      }
    
    
    }

    测试: 

    package com.wenbronk.sparkstreaming.test
    
    import com.wenbronk.sparkstreaming.scala.commons.RedisUtils
    import org.scalatest.FunSuite
    
    class RedisTests extends FunSuite{
    
      test("redisSet") {
        val utils = RedisUtils
        val connect = utils.getRediceConnect
        connect.async().set("abc", "1234")
        println("hello")
      }
    
      test("redisGet") {
        val utils = RedisUtils
        val connect = utils.getRediceConnect
        val value = connect.async().get("abc")
        println(value.get())
      }
    
      test("flushdb") {
        val utils = RedisUtils
        val connect = utils.getRediceConnect
        val value = connect.async().flushdb()
        println(value.get())
      }
    
    
    }

    scala测试可见: 

  • 相关阅读:
    golang的并行快速排序
    深信服面试
    一道算法题-勇敢的牛牛
    一道算法题-打车
    一道算法题-最少需要换掉的瓷砖数量
    《C++ Primer Plus》读书笔记之二—复合类型
    图解:TCP协议中的三次握手和四次挥手
    一道算法题-不用加减乘除做加法
    OSI七层模型与TCP/IP五层模型
    Socket编程
  • 原文地址:https://www.cnblogs.com/wenbronk/p/9592969.html
Copyright © 2011-2022 走看看