-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add discovery implementation and test
- Loading branch information
Showing
10 changed files
with
237 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
discovery-eureka/src/main/scala/org/apache/pekko/discovery/eureka/EurekaResponse.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.pekko.discovery.eureka | ||
|
||
object EurekaResponse { | ||
case class Application(name: String, instance: Seq[Instance]) | ||
case class Instance(hostName: String, app: String, vipAddress: String, secureVipAddress: String, ipAddr: String, status: String, port: PortWrapper, securePort: PortWrapper, healthCheckUrl: String, statusPageUrl: String, homePageUrl: String, appGroupName: String, dataCenterInfo: DataCenterInfo, lastDirtyTimestamp: String) | ||
case class Status() | ||
case class PortWrapper(port: Int, enabled: Boolean) | ||
case class DataCenterInfo(name: String = "MyOwn", clz : String = "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo") | ||
} | ||
|
||
import EurekaResponse._ | ||
|
||
case class EurekaResponse(application: Application, errorCode: Option[String]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
discovery-eureka/src/main/scala/org/apache/pekko/discovery/eureka/JsonFormat.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* license agreements; and to You under the Apache License, version 2.0: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* This file is part of the Apache Pekko project, which was derived from Akka. | ||
*/ | ||
|
||
/* | ||
* Copyright (C) 2017-2021 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package org.apache.pekko.discovery.eureka | ||
|
||
import org.apache.pekko.discovery.eureka.EurekaResponse.{Application, DataCenterInfo, Instance, PortWrapper} | ||
import org.apache.pekko.http.scaladsl.marshallers.sprayjson.SprayJsonSupport | ||
import spray.json._ | ||
|
||
object JsonFormat extends SprayJsonSupport with DefaultJsonProtocol { | ||
implicit val portFormat: JsonFormat[PortWrapper] = new JsonFormat[PortWrapper] { | ||
|
||
override def read(json: JsValue): PortWrapper = { | ||
json.asJsObject.getFields("$", "@enabled") match { | ||
case Seq(JsNumber(port), JsString(enabled)) => PortWrapper(port.toInt, enabled.toBoolean) | ||
case _ => throw DeserializationException("PortWrapper expected") | ||
} | ||
} | ||
|
||
override def write(obj: PortWrapper): JsValue = JsObject( | ||
"$" -> JsNumber(obj.port), | ||
"@enabled" -> JsString(obj.enabled.toString)) | ||
} | ||
implicit val dataCenterInfoFormat: JsonFormat[DataCenterInfo] = new JsonFormat[DataCenterInfo] { | ||
|
||
override def read(json: JsValue): DataCenterInfo = { | ||
json.asJsObject.getFields("name","@class") match { | ||
case Seq(JsString(name), JsString(clz)) => DataCenterInfo(name, clz) | ||
case _ => throw DeserializationException("DataCenterInfo expected") | ||
} | ||
} | ||
|
||
override def write(obj: DataCenterInfo): JsValue = JsObject( | ||
"name" -> JsString(obj.name), | ||
"@class" -> JsString(obj.clz)) | ||
} | ||
implicit val instanceFormat: JsonFormat[Instance] = jsonFormat14(Instance.apply) | ||
implicit val applicationFormat: JsonFormat[Application] = jsonFormat2(Application.apply) | ||
implicit val rootFormat: RootJsonFormat[EurekaResponse] = jsonFormat2(EurekaResponse.apply) | ||
} |
85 changes: 85 additions & 0 deletions
85
discovery-eureka/src/test/scala/EurekaServiceDiscoverySpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import org.apache.pekko.actor.ActorSystem | ||
import org.apache.pekko.discovery.ServiceDiscovery.ResolvedTarget | ||
import org.apache.pekko.discovery.eureka.EurekaServiceDiscovery | ||
import org.apache.pekko.testkit.TestKitBase | ||
import org.scalatest.BeforeAndAfterAll | ||
import org.scalatest.concurrent.ScalaFutures | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.time.{Millis, Seconds, Span} | ||
import org.scalatest.wordspec.AnyWordSpecLike | ||
|
||
import java.net.InetAddress | ||
import scala.concurrent.duration.DurationInt | ||
import scala.io.Source | ||
import scala.util.Try | ||
|
||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
|
||
class EurekaServiceDiscoverySpec | ||
extends AnyWordSpecLike | ||
with Matchers | ||
with BeforeAndAfterAll | ||
with TestKitBase | ||
with ScalaFutures { | ||
"Eureka Discovery" should { | ||
"work for defaults" in { | ||
|
||
val lookupService = new EurekaServiceDiscovery() | ||
val resolved = lookupService.lookup("BANK-ACCOUNT", 10.seconds).futureValue | ||
resolved.addresses should contain( | ||
ResolvedTarget( | ||
host = "127.0.0.1", | ||
port = Some(8558), | ||
address = Try(InetAddress.getByName("127.0.0.1")).toOption)) | ||
|
||
} | ||
} | ||
|
||
private def resourceAsString(name: String): String = | ||
Source.fromInputStream(getClass.getClassLoader.getResourceAsStream(name)).mkString | ||
|
||
override def afterAll(): Unit = { | ||
super.afterAll() | ||
print("clean up \n") | ||
} | ||
|
||
override implicit lazy val system: ActorSystem = ActorSystem("test") | ||
|
||
implicit override val patienceConfig: PatienceConfig = | ||
PatienceConfig(timeout = scaled(Span(30, Seconds)), interval = scaled(Span(50, Millis))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters