本文整理汇总了Scala中org.apache.curator.framework.CuratorFrameworkFactory类的典型用法代码示例。如果您正苦于以下问题:Scala CuratorFrameworkFactory类的具体用法?Scala CuratorFrameworkFactory怎么用?Scala CuratorFrameworkFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CuratorFrameworkFactory类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。
示例1: CuratorFactory
//设置package包名称以及导入依赖的类
package com.box.castle.core
import com.box.castle.core.common.{CastleFatalException}
import com.box.castle.core.config.CastleConfig
import org.slf4s.Logging
import org.apache.curator.framework.imps.CuratorFrameworkState
import org.apache.curator.framework.{CuratorFrameworkFactory, CuratorFramework}
import scala.concurrent.duration._
// $COVERAGE-OFF$
class CuratorFactory(castleConfig: CastleConfig) extends Logging {
def create(): CuratorFramework = {
log.info("Begin creating Curator...")
val curator: CuratorFramework = CuratorFrameworkFactory.builder
.namespace(util.join(castleConfig.castleZookeeperConfig.root, castleConfig.namespace))
.connectString(castleConfig.castleZookeeperConfig.connect)
.sessionTimeoutMs(castleConfig.castleZookeeperConfig.sessionTimeout.toMillis.toInt)
.connectionTimeoutMs(castleConfig.castleZookeeperConfig.connectionTimeout.toMillis.toInt)
.retryPolicy(castleConfig.castleZookeeperConfig.retryPolicy)
.build
curator.start()
curator.blockUntilConnected(castleConfig.castleZookeeperConfig.initialConnectTimeout.toMillis.toInt, MILLISECONDS)
if (curator.getState != CuratorFrameworkState.STARTED) {
throw new CastleFatalException(s"Could not connect to ZK within the " +
s"configured timeout of ${castleConfig.castleZookeeperConfig.initialConnectTimeout}")
}
log.info("Successfully created a CuratorFramework instance")
curator
}
}
// $COVERAGE-ON$
开发者ID:Box-Castle,项目名称:core,代码行数:38,代码来源:CuratorFactory.scala
示例2: CuratorTests
//设置package包名称以及导入依赖的类
package com.bwsw.cloudstack.common.curator
import com.bwsw.cloudstack.imp.dao.zookeeper.ZookeeperTestServer
import com.google.common.io.Files
import org.apache.curator.framework.{CuratorFramework, CuratorFrameworkFactory}
import org.apache.curator.retry.ExponentialBackoffRetry
import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers}
class CuratorTests extends FlatSpec with Matchers with BeforeAndAfterAll {
System.getProperty("java.io.tmpdir", "./target/")
val ZOOKEEPER_PORT = 21810
var zk: ZookeeperTestServer = null
implicit var curator: CuratorFramework = null
override def beforeAll() = {
zk = new ZookeeperTestServer(ZOOKEEPER_PORT, Files.createTempDir().toString)
curator = CuratorFrameworkFactory.builder()
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
.namespace("tests")
.connectString(s"127.0.0.1:$ZOOKEEPER_PORT").build()
curator.start()
}
override def afterAll() = {
curator.close()
zk.stop
}
}
开发者ID:bwsw,项目名称:cs-imp,代码行数:31,代码来源:CuratorTests.scala
示例3: ZKDrlManager
//设置package包名称以及导入依赖的类
package com.muziyuchen.drule.zk
import org.apache.curator.framework.{CuratorFramework, CuratorFrameworkFactory}
import org.apache.curator.framework.recipes.cache.{PathChildrenCache, PathChildrenCacheEvent, PathChildrenCacheListener}
import org.apache.curator.retry.ExponentialBackoffRetry
import scala.collection.mutable
import scala.collection.JavaConversions._
private[zk] class ZKDrlManager(connectString: String, path: String) {
private var client: CuratorFramework = _
private var cache: PathChildrenCache = _
def start: Unit = {
client = CuratorFrameworkFactory.newClient(connectString, new ExponentialBackoffRetry(1000, 3))
client.start
client.blockUntilConnected
cache = new PathChildrenCache(client, path, true)
cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE)
}
def getDrls: mutable.MutableList[String] = {
var drls = new mutable.MutableList[String]()
for (childData <- cache.getCurrentData) {
val drl = new String(childData.getData(), "UTF-8")
drls += drl
}
drls
}
def drlChanged(callback: () => Unit): Unit = {
cache.getListenable.addListener(new PathChildrenCacheListener {
override def childEvent(client: CuratorFramework, event: PathChildrenCacheEvent): Unit = {
callback
}
})
}
def close:Unit = {
cache.close
client.close
}
}
开发者ID:gspandy,项目名称:d-rule,代码行数:51,代码来源:ZKDrlManager.scala
示例4: MetastoreFactory
//设置package包名称以及导入依赖的类
package yumi.metastore
import com.typesafe.config.Config
import org.apache.curator.framework.CuratorFrameworkFactory
import org.apache.curator.retry.RetryNTimes
import yumi.{Logging, YumiUtils}
object MetastoreFactory extends Logging {
private[this] val configPrefix = "yumi.metastore"
def create(config: Config): Metastore = {
config.getString(s"$configPrefix.type") match {
case "zookeeper" => createZkMetastore(config)
case "in-memory" => createInMemoryMetastore
case other => throw new IllegalArgumentException(s"Invalid metastore `$other` type.")
}
}
private[this] def createZkMetastore(config: Config): ZkMetastore = {
log.info("Creating zookeeper metastore.")
val zkConfPrefix = s"$configPrefix.zookeeper"
val headNode = config.getString(s"$zkConfPrefix.head-node")
val quorum = config.getString(s"$zkConfPrefix.quorum")
val retryTimes = config.getInt(s"$zkConfPrefix.retry-times")
val sleepBetween = config.getInt(s"$zkConfPrefix.sleep-between-tries")
val curatorFramework = CuratorFrameworkFactory
.newClient(quorum, new RetryNTimes(retryTimes, sleepBetween))
new ZkMetastore(curatorFramework, headNode, YumiUtils)
}
private[this] def createInMemoryMetastore: Metastore = {
log.warn("In memory metastore is for development only.")
new FlashMetastore
}
}
开发者ID:coderdiaries,项目名称:yumi,代码行数:42,代码来源:MetastoreFactory.scala
示例5: ZookeeperServerStartup
//设置package包名称以及导入依赖的类
import java.util
import java.util.concurrent.TimeUnit
import org.apache.curator.framework.{CuratorFramework, CuratorFrameworkFactory}
import org.apache.curator.retry.ExponentialBackoffRetry
import org.apache.curator.test.TestingServer
import org.apache.zookeeper.CreateMode
import org.apache.zookeeper.ZooDefs.Ids
import org.apache.zookeeper.data.ACL
object ZookeeperServerStartup {
val port = 2181
val endpoints = s"127.0.0.1:$port"
def main(args: Array[String]): Unit = {
val zkServer = new TestingServer(port, true)
createNodes()
addShutdownHook(zkServer)
}
private def createNodes() = {
val zkClient: CuratorFramework = {
val connection = CuratorFrameworkFactory.builder()
.connectString(endpoints)
.sessionTimeoutMs(2000)
.connectionTimeoutMs(5000)
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
.build()
connection.start()
connection.blockUntilConnected(5000, TimeUnit.MILLISECONDS)
connection
}
zkClient.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.PERSISTENT)
.withACL(new util.ArrayList[ACL](Ids.OPEN_ACL_UNSAFE))
.forPath(BookieServerStartup.zkLedgersRootPath)
zkClient.create()
.withMode(CreateMode.PERSISTENT)
.withACL(new util.ArrayList[ACL](Ids.OPEN_ACL_UNSAFE))
.forPath(BookieServerStartup.zkBookiesAvailablePath)
zkClient.close()
}
private def addShutdownHook(zkServer: TestingServer) = {
Runtime.getRuntime.addShutdownHook(new Thread() {
override def run(): Unit = {
zkServer.close()
}
})
}
}
开发者ID:bwsw,项目名称:bookkeeper-tutorial-scala,代码行数:59,代码来源:ZookeeperServerStartup.scala
示例6: CuratorTests
//设置package包名称以及导入依赖的类
package com.bwsw.imp.curator
import org.apache.curator.framework.{CuratorFramework, CuratorFrameworkFactory}
import org.apache.curator.retry.ExponentialBackoffRetry
import org.apache.curator.test.TestingServer
import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers}
class CuratorTests extends FlatSpec with Matchers with BeforeAndAfterAll {
val ZOOKEEPER_PORT = 21810
var testingServer = new TestingServer(ZOOKEEPER_PORT)
implicit var curator: CuratorFramework = _
override def beforeAll() = {
testingServer.start()
curator = CuratorFrameworkFactory.builder()
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
.namespace("tests")
.connectString(s"127.0.0.1:$ZOOKEEPER_PORT").build()
curator.start()
}
it must "do nothing" in {}
override def afterAll() = {
curator.close()
testingServer.stop()
}
}
开发者ID:bwsw,项目名称:imp,代码行数:32,代码来源:CuratorTests.scala
示例7: ZookeeperServiceDiscovery
//设置package包名称以及导入依赖的类
package com.knoldus
import com.google.inject.Singleton
import org.apache.curator.framework.CuratorFrameworkFactory
import org.apache.curator.retry.RetryNTimes
import org.apache.curator.x.discovery.ServiceDiscoveryBuilder
import scala.collection.JavaConversions._
@Singleton
class ZookeeperServiceDiscovery {
val curatorFramework = CuratorFrameworkFactory.newClient("localhost:2181", new RetryNTimes(5, 1000))
curatorFramework.start()
val serviceDiscovery = ServiceDiscoveryBuilder
.builder(Void.TYPE)
.basePath("scala-zookeeper-kit")
.client(curatorFramework).build()
serviceDiscovery.start()
def getServiceBaseUrl(serviceName: String): String = {
val serviceProvider = serviceDiscovery.serviceProviderBuilder().serviceName(serviceName).build()
serviceProvider.start()
lazy val allInstances = serviceProvider.getAllInstances.toList
val instance = serviceProvider.getInstance()
val url = instance.buildUriSpec()
println(s"URL:::::$url")
url
}
}
开发者ID:shiv4nsh,项目名称:scala-zookeeper-starter-kit,代码行数:31,代码来源:ZookeeperServiceDiscovery.scala
示例8: DLock
//设置package包名称以及导入依赖的类
package yamrcraft.etlite.utils
import java.util.concurrent.TimeUnit
import org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex
import org.apache.curator.framework.{CuratorFramework, CuratorFrameworkFactory}
import org.apache.curator.retry.ExponentialBackoffRetry
import org.slf4j.LoggerFactory
class DLock(zkConnect: String, lockFile: String, waitForLockSeconds: Int) {
val logger = LoggerFactory.getLogger(this.getClass)
private var zkClient: Option[CuratorFramework] = None
private var lock: Option[InterProcessSemaphoreMutex] = None
def tryLock(): Boolean = {
require(lock.isEmpty, "lock can't be reused")
logger.info("acquiring lock...")
zkClient = Some(CuratorFrameworkFactory.newClient(zkConnect, new ExponentialBackoffRetry(1000, 3)))
zkClient.get.start()
lock = Some(new InterProcessSemaphoreMutex(zkClient.get, lockFile))
lock.get.acquire(waitForLockSeconds, TimeUnit.SECONDS)
}
def release() = {
require(lock.nonEmpty, "lock wasn't acquired")
logger.info("releasing lock")
lock.foreach(_.release())
zkClient.foreach(_.close())
}
}
class FakeLock extends DLock("", "", 0) {
override def tryLock() = true
override def release() = {}
}
开发者ID:yamrcraft,项目名称:etl-light,代码行数:41,代码来源:DLock.scala
注:本文中的org.apache.curator.framework.CuratorFrameworkFactory类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论