Quick Start

Nacos SDK for Scala 基于 Java 客户端 nacos-client 做了封装,使得在 Scala 下更易使用。支持使用 HOCON 做为配置文件,支持 Pekko Discovery 和 Play-WS。

依赖

要在项目中使用,请添加以下依赖:

sbt
libraryDependencies += "me.yangbajing.nacos4s" %% "nacos-client-scala" % "2.0.0-SNAPSHOT"
Maven
<dependencies>
  <dependency>
    <groupId>me.yangbajing.nacos4s</groupId>
    <artifactId>nacos-client-scala_3</artifactId>
    <version>2.0.0-SNAPSHOT</version>
  </dependency>
</dependencies>
Gradle
dependencies {
  implementation "me.yangbajing.nacos4s:nacos-client-scala_3:2.0.0-SNAPSHOT"
}

并添加以下依赖源:

resolvers += Resolver.bintrayRepo("helloscala", "maven")

编程使用

Nacos4sNamingService

sourceimport yangbajing.nacos4s.client.naming.Nacos4sNamingService
import yangbajing.nacos4s.client.util.Nacos4s

object NamingDemo extends App {
  val namingService: Nacos4sNamingService = Nacos4s.namingService("127.0.0.0:8848", "")
  val status = namingService.getServerStatus
  assert(status == "UP")
}

Nacos4sConfigService

sourceimport yangbajing.nacos4s.client.config.Nacos4sConfigService
import yangbajing.nacos4s.client.util.Nacos4s

object ConfigDemo extends App {
  val configService: Nacos4sConfigService = Nacos4s.configService("127.0.0.0:8848", "")
  val status = configService.getServerStatus
  assert(status == "UP")
}

HOCON 配置

Nacos4s 支持使用 HOCON 作为配置文件。

application.conf

sourcenacos4s.client {
  naming {
    serverAddr = "127.0.0.1:8848"
    namespace = ""
    autoRegisterInstance = on # true
    serviceName = "me.yangbajing.nacos4s"
    ip = "127.0.0.1"
    port = 6666
  }
  config {
    serverAddr = "127.0.0.1:8848"
    namespace = ""
  }
}

代码

val configService = Nacos4s.configService(ConfigFactory.load().getConfig("nacos4s.client.config"))
val namingService = Nacos4s.namingService(ConfigFactory.load().getConfig("nacos4s.client.config"))

服务自动注册

在使用 ConfigFactory 初始化 Nacos4sNamingService 时设置 autoRegisterInstance = on 可自动将服务注册到 Nacos。

HOCON 完整配置

sourcenacos4s.client.naming {
  // 是否自动注册服务到 Nacos
  autoRegisterInstance = false

  // 注册服务组
  group = "DEFAULT_GROUP"

  // 连接超时
  timeoutMs = 30.seconds

  // Nacos 地址
  serverAddr = "127.0.0.1:8849"

  // Nacos 名字空间
  namespace = "me.yangbajing.nacos"

  // 注册服务名
  serviceName = "161e415e-56a5-4eeb-9897-f84a268caa58"

  // 注册服务IP,默认读取本机第一个外网地址。
  // 若读取失败建议手动设置
  #ip = "127.0.0.1"

  // 注册服务端口
  port = 8080
}

示例

更多 Nacos4sConfigService 使用示例见 测试

sourceprivate val configService = Nacos4s.configService(ConfigFactory.parseString(
  """{
    |  serverAddr = "127.0.0.1:8848"
    |  namespace = ""
    |}""".stripMargin))
private val dataId = "me.yangbajing.nacos4s"
private val group = "DEFAULT_GROUP"
private val timeoutMs = 30000

"ConfigService" should {
  val listener = new AbstractListener {
    override def receiveConfigInfo(configInfo: String): Unit = {
      println(s"[${OffsetDateTime.now()}] Received new config is:\n$configInfo")
    }
  }

  "getServerStatus" in {
    configService.getServerStatus shouldBe "UP"
  }

  "publishConfig" in {
    val content = """nacos4s.client {
                    |  serverAddr = "127.0.0.1:8848"
                    |  namespace = ""
                    |  serviceName = "me.yangbajing.nacos4s"
                    |}""".stripMargin
    configService.publishConfig(dataId, group, content) shouldBe true
    TimeUnit.SECONDS.sleep(1)
  }

  "addListener" in {
    configService.addListener(dataId, group, listener)
  }

  "getConfig" in {
    val content = configService.getConfig(dataId, group, timeoutMs)
    content should include("""serverAddr = "127.0.0.1:8848"""")
  }

  "removeConfig" in {
    TimeUnit.SECONDS.sleep(1)
    configService.removeConfig(dataId, group) shouldBe true
  }

  "removeListener" in {
    TimeUnit.SECONDS.sleep(1)
    configService.removeListener(dataId, group, listener)
  }
}

更多 Nacos4sNamingService 使用示例见 测试

sourceprivate val namingService = Nacos4s.namingService(ConfigFactory.parseString(
  s"""{
     |  serverAddr = "127.0.0.1:8848"
     |  namespace = ""
     |  autoRegisterInstance = true
     |  serviceName = "me-auto-register"
     |  port = 9999
     |}""".stripMargin))
private val serviceName = "me.yangbajing.nacos4s"
private val group = "DEFAULT_GROUP"
private val ip = "127.0.0.1"
private val port = 8888

"Nacos4sNamingService" should {
  "getServerStatus" in {
    namingService.getServerStatus shouldBe "UP"
    TimeUnit.SECONDS.sleep(1)
  }

  "getAllInstances" in {
    val insts = namingService.getAllInstances("me-auto-register")
    insts.foreach(println)
    insts should have size 1
  }

  "registerInstance" in {
    namingService.registerInstance(serviceName, group, ip, port)
    TimeUnit.SECONDS.sleep(1)
    val inst = namingService.selectOneHealthyInstance(serviceName)
    inst should not be null
    inst.isHealthy shouldBe true
    inst.isEnabled shouldBe true
    inst.isEphemeral shouldBe true
    inst.getServiceName shouldBe s"$group@@$serviceName"
    println(inst)
  }
}