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测试可见: 

  • 相关阅读:
    SX知识学习——sources
    SX知识学习——TCGA(转)
    SX知识学习——WES(转)
    SX知识学习——CHIPseq(转+总结)
    编译软件安装——VS2010
    数据库知识学习——mysql
    计算机知识学习——操作系统
    计算机知识学习——计算机组成原理
    Scrapy 中文手册 0.25 文档
    python Requests 库
  • 原文地址:https://www.cnblogs.com/wenbronk/p/9592969.html
Copyright © 2011-2022 走看看