Skip to content

Commit

Permalink
merged pr
Browse files Browse the repository at this point in the history
  • Loading branch information
jboner committed Nov 24, 2017
2 parents 95e0228 + 21d9e5c commit b88a881
Show file tree
Hide file tree
Showing 16 changed files with 769 additions and 352 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,6 @@ public class ExampleService {

You need a Consul agent running on your local machine (on its default port) in order to run the tests.

If you are on Mac then you can install Consul through Homebrew using `brew install consul`. Once it is installed you can start up an agent in dev mode by invoking `consul agent -dev -data-dir ~/tmp` which will make an agent available on 127.0.0.1:8500.
If you are on Mac then you can install Consul through Homebrew using `brew install consul` or using docker `docker run -p8500:8500 consul`. Once it is installed you can start up an agent in dev mode by invoking `consul agent -dev -data-dir ~/tmp` which will make an agent available on 127.0.0.1:8500.

Once Consul is running you can run the tests by invoking `sbt test`.
43 changes: 37 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,13 +1,44 @@
import sbt.Keys.version

organization := "com.lightbend.lagom"

name := "lagom-service-locator-consul"

version := "1.0.1-SNAPSHOT"
val lagomVersion = "1.3.5"

val typesafeConfig = "com.typesafe" % "config" % "1.3.1"
val lagomJavadslClient = "com.lightbend.lagom" %% "lagom-javadsl-client" % lagomVersion
val lagomScaladslClient = "com.lightbend.lagom" %% "lagom-scaladsl-client" % lagomVersion
val consulApi = "com.ecwid.consul" % "consul-api" % "1.1.10"
val scalatest = "org.scalatest" %% "scalatest" % "3.0.1" % Test

scalaVersion := "2.11.8"
lazy val `lagom-service-locator-consul` = (project in file("."))
.aggregate(
`lagom-service-locator-javadsl-consul`,
`lagom-service-locator-scaladsl-consul`
)

libraryDependencies ++= Seq(
"com.lightbend.lagom" %% "lagom-javadsl-api" % "1.2.0",
"com.ecwid.consul" % "consul-api" % "1.1.10",
"org.scalatest" %% "scalatest" % "2.2.4" % Test
def common = Seq(
scalaVersion := "2.11.11",
version := "1.4.0-SNAPSHOT"
)

lazy val `lagom-service-locator-javadsl-consul` = (project in file("lagom-service-locator-javadsl-consul"))
.settings(common)
.settings(
libraryDependencies ++= Seq(
lagomJavadslClient,
consulApi,
scalatest
)
)

lazy val `lagom-service-locator-scaladsl-consul` = (project in file("lagom-service-locator-scaladsl-consul"))
.settings(common)
.settings(
libraryDependencies ++= Seq(
lagomScaladslClient,
consulApi,
scalatest
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

# Enables the `ConsulServiceLocatorModule` to register the `ConsulServiceLocator`.
# The `ConsulServiceLocator` implements Lagom's ServiceLocator
play.modules.enabled += "com.lightbend.lagom.discovery.consul.ConsulServiceLocatorModule"
play.modules.enabled += "com.lightbend.lagom.javadsl.discovery.consul.ConsulServiceLocatorModule"
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.lightbend.lagom.discovery.consul
package com.lightbend.lagom.javadsl.discovery.consul

import javax.inject.Inject
import javax.inject.Singleton
import com.typesafe.config.ConfigException.BadValue
import javax.inject.{Inject, Singleton}

import com.typesafe.config.ConfigException.BadValue
import play.api.Configuration

trait ConsulConfig {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,32 @@
package com.lightbend.lagom.discovery.consul
package com.lightbend.lagom.javadsl.discovery.consul

import java.net.InetAddress
import java.net.URI
import java.net.{ InetAddress, URI }
import java.util.Optional
import java.util.concurrent.{CompletionStage, ThreadLocalRandom}
import java.util.function.{Function => JFunction}
import java.util.concurrent.{ CompletionStage, ThreadLocalRandom }
import java.util.function.{ Function => JFunction }
import javax.inject.Inject

import com.ecwid.consul.v1.catalog.model.CatalogService
import com.ecwid.consul.v1.{ ConsulClient, QueryParams }
import com.lightbend.lagom.internal.client.CircuitBreakers
import com.lightbend.lagom.javadsl.api.Descriptor
import com.lightbend.lagom.javadsl.client.CircuitBreakingServiceLocator

import scala.collection.JavaConverters._
import scala.collection.concurrent.Map
import scala.collection.concurrent.TrieMap
import scala.collection.concurrent.{ Map, TrieMap }
import scala.compat.java8.FutureConverters._
import scala.compat.java8.OptionConverters._
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import com.ecwid.consul.v1.ConsulClient
import com.ecwid.consul.v1.QueryParams
import com.ecwid.consul.v1.agent.model.NewService
import com.ecwid.consul.v1.catalog.model.CatalogService
import com.lightbend.lagom.javadsl.api.Descriptor.Call
import com.lightbend.lagom.javadsl.api.ServiceLocator

class ConsulServiceLocator @Inject()(client: ConsulClient, config: ConsulConfig)(implicit ec: ExecutionContext) extends ServiceLocator {

private val roundRobinIndexFor: Map[String, Int] = TrieMap.empty[String, Int]
import scala.concurrent.{ ExecutionContext, Future }

val service = new NewService
service.setId(config.serviceId)
service.setName(config.serviceName)
service.setPort(config.servicePort)
service.setAddress(config.serviceAddress)
new ConsulClient(config.agentHostname).agentServiceRegister(service)
class ConsulServiceLocator @Inject()(client: ConsulClient, config: ConsulConfig, circuitBreakers: CircuitBreakers)(implicit ec: ExecutionContext)
extends CircuitBreakingServiceLocator(circuitBreakers) {

private val roundRobinIndexFor: Map[String, Int] = TrieMap.empty[String, Int]

override def doWithService[T](name: String, serviceCall: Call[_, _], block: JFunction[URI, CompletionStage[T]]): CompletionStage[Optional[T]] =
locateAsScala(name).flatMap { uriOpt =>
uriOpt.fold(Future.successful(Optional.empty[T])) { uri =>
block.apply(uri).toScala.map(Optional.of(_))
}
}.toJava

override def locate(name: String, serviceCall: Call[_, _]): CompletionStage[Optional[URI]] = locateAsScala(name).map(_.asJava).toJava

override def locate(name: String, serviceCall: Descriptor.Call[_, _]): CompletionStage[Optional[URI]] = {
locateAsScala(name).map(_.asJava).toJava
}

private def locateAsScala(name: String): Future[Option[URI]] = Future {
val instances = client.getCatalogService(name, QueryParams.DEFAULT).getValue.asScala.toList
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.lightbend.lagom.discovery.consul
package com.lightbend.lagom.javadsl.discovery.consul

import com.lightbend.lagom.javadsl.api.ServiceLocator
import play.api.{Configuration, Environment, Mode}
import play.api.inject.{Binding, Module}

import javax.inject.Singleton
import javax.inject.Inject
import javax.inject.Provider
import javax.inject.{ Inject, Provider, Singleton }

import com.ecwid.consul.v1.ConsulClient
import com.lightbend.lagom.javadsl.api.ServiceLocator
import play.api.inject.{ Binding, Module }
import play.api.{ Configuration, Environment, Mode }

/**
* This module binds the ServiceLocator interface from Lagom to the `ConsulServiceLocator`.
Expand Down
File renamed without changes.
Loading

0 comments on commit b88a881

Please sign in to comment.