Scala Zookeeper 访问 Demo

灰太狼 2022-03-26 02:22 218阅读 0赞
  1. import scala.collection.mutable.ArrayBuffer
  2. import scala.collection.JavaConverters._
  3. import com.google.common.base.{ Objects, Strings}
  4. import org.apache.commons.compress.utils.Charsets
  5. import org.apache.curator.framework.{ CuratorFramework, CuratorFrameworkFactory}
  6. import org.apache.curator.retry.ExponentialBackoffRetry
  7. import org.apache.curator.utils.ZKPaths
  8. import org.slf4j.LoggerFactory
  9. object ZkTest {
  10. private[this] val logger = LoggerFactory.getLogger(classOf[ZkTest])
  11. private[this] val zkAddress = "localhost:2181"
  12. private[this] var client: CuratorFramework = _
  13. def init(zkAddr: String = zkAddress): Unit = {
  14. client = CuratorFrameworkFactory.newClient(
  15. zkAddr,
  16. new ExponentialBackoffRetry(1000, 3)
  17. )
  18. client.start()
  19. }
  20. def destroy(): Unit = {
  21. if (client != null) {
  22. client.close()
  23. }
  24. }
  25. /** * 给定一个ZK的路径,返回路径下所有< 目录, 值 > 的对象数组 * @param path * @return */
  26. def findProperties(path: String): Array[PropertyItem] = {
  27. val properties = new ArrayBuffer[PropertyItem]()
  28. val stat = client.checkExists().forPath(path)
  29. if (null != stat) {
  30. val children = client.getChildren.forPath(path).asScala
  31. val dataBuilder = client.getData
  32. if (children != null) for (child <- children) {
  33. val propPath = ZKPaths.makePath(path, child)
  34. val item = PropertyItem(child, new String(dataBuilder.forPath(propPath), Charsets.UTF_8))
  35. properties += item
  36. }
  37. }
  38. properties.toArray
  39. }
  40. /** * 给定一个ZK路径,返回路径下所有子目录名字的数组 * @param path * @return */
  41. def listChildren(path: String): Array[String] = {
  42. var children = Array.empty[String]
  43. val stat = client.checkExists.forPath(path)
  44. if (stat != null) {
  45. val childrenBuilder = client.getChildren
  46. children = childrenBuilder.forPath(path).asScala.toArray
  47. }
  48. children
  49. }
  50. /** * 创建ZK节点,返回创建状态 * @param path * @param value * @return */
  51. def createProperty(path: String, value: String = null): Boolean = {
  52. var result = false
  53. try {
  54. val stat = client.checkExists.forPath(path)
  55. if (stat == null) {
  56. val data = if (Strings.isNullOrEmpty(value)) {
  57. Array.empty[Byte]
  58. } else {
  59. value.getBytes(Charsets.UTF_8)
  60. }
  61. val opResult = client.create.creatingParentsIfNeeded.forPath(path, data)
  62. result = Objects.equal(path, opResult)
  63. }
  64. } catch {
  65. case e: Exception =>
  66. logger.error(e.getMessage, e)
  67. }
  68. result
  69. }
  70. /** * 更新节点数据 * @param path * @param value * @return */
  71. def updateProperty(path: String, value: String): Boolean = {
  72. var result = false
  73. try {
  74. val stat = client.checkExists.forPath(path)
  75. if (stat != null) {
  76. val opResult = client.setData().forPath(path, value.getBytes(Charsets.UTF_8))
  77. result = opResult != null
  78. }
  79. else {
  80. val opResult = client.create.creatingParentsIfNeeded.forPath(path, value.getBytes(Charsets.UTF_8))
  81. result = Objects.equal(path, opResult)
  82. }
  83. } catch {
  84. case e: Exception =>
  85. logger.error(e.getMessage, e)
  86. }
  87. result
  88. }
  89. /** * 删除节点 * @param path */
  90. def deleteProperty(path: String): Unit = {
  91. try {
  92. val stat = client.checkExists.forPath(path)
  93. if (stat != null) {
  94. client.delete.deletingChildrenIfNeeded.forPath(path)
  95. }
  96. } catch {
  97. case e: Exception =>
  98. logger.error(e.getMessage, e)
  99. }
  100. }
  101. /** * 获取节点数据 * @param path * @return */
  102. def getValue(path: String): String = {
  103. var result: String = null
  104. try {
  105. val stat = client.checkExists.forPath(path)
  106. if (stat != null) {
  107. val data = client.getData.forPath(path)
  108. result = new String(data)
  109. }
  110. } catch {
  111. case e: Exception =>
  112. logger.error(e.getMessage, e)
  113. }
  114. result
  115. }
  116. }
  117. class ZkTest
  118. case class PropertyItem(name: String, value: String)

发表评论

表情:
评论列表 (有 0 条评论,218人围观)

还没有评论,来说两句吧...

相关阅读