/ / एक अभिनेता सिस्टम के अंदर स्प्रे ग्राहकों के साथ रूटिंग कार्यान्वित करना - स्कैला, रूटिंग, अक्का, अभिनेता, स्प्रे

एक अभिनेता सिस्टम के अंदर स्प्रे ग्राहकों के साथ रूटिंग कार्यान्वित करना - स्कैला, रूटिंग, अक्का, अभिनेता, स्प्रे

मेरे पास एक स्प्रे क्लाइंट है जिसका उपयोग एक आरईएसटी वेब सेवा का आह्वान करने के लिए किया जाता है। वर्तमान में मैं इस क्लाइंट का उदाहरण बना रहा हूं (उपयोग कर रहा हूं new GeoSprayWebClient) [नीचे कोड देखें] और इसे मेरे अंदर पुन: उपयोग कर रहा हैआरईएसटी अनुरोध करने के लिए अभिनेता। हालांकि, सेवा का एक उदाहरण सभी भार को संभालने में सक्षम नहीं है। इसलिए, मैं आरईएसटी सेवा की प्रतिकृतियां पेश करना चाहता हूं।

मैं स्प्रे के लिए नया हूं और अभी भी बुनियादी बातों को सीखने की कोशिश कर रहा हूं। मेरे प्रश्न हैं

1) मुझे पता है स्प्रे आंतरिक रूप से अक्का अभिनेताओं का उपयोग करता है। इस विशेष मामले में मुझे एक मिल सकता है ActorRef क्लाइंट के उदाहरणों के लिए ताकि मैं बना सकूंएकाधिक क्लाइंट ActorRefs और एक Akka राउटर बनाने के लिए उनका उपयोग करें। 2) क्या स्प्रे क्लाइंट एपीआई किसी भी तरह की रूटिंग क्षमता प्रदान करता है जो मेरे उपयोग के मामले का समर्थन करेगा?

 import akka.actor.ActorSystem
import spray.client.pipelining._
import spray.http._
import scala.concurrent.Future


trait GeoWebClient {
def get(url: String, params: Map[String, String]): Future[String]
}

class GeoSprayWebClient(implicit system: ActorSystem) extends GeoWebClient {

import system.dispatcher

// create a function from HttpRequest to a Future of HttpResponse
val pipeline: HttpRequest => Future[HttpResponse] = sendReceive

// create a function to send a GET request and receive a string response
def get(path: String, params: Map[String, String]): Future[String] = {
val uri = Uri(path) withQuery params
val request = Get(uri)
val futureResponse = pipeline(request)
futureResponse.map(_.entity.asString)
}
}

उत्तर:

जवाब के लिए 0 № 1
  1. WebClientActor को लागू करें जो आपके काम करने के लिए GeoSprayWebClient को आमंत्रित करता है।

  2. स्प्रे हैंडलर के रूप में राउटर बनाएं:

वैल हैंडलर = context.actorOf ( प्रॉप्स [वेब क्लाइंटएक्टर] .withRouter (RoundRobinRouter (5)), नाम = "हैंडलर राउटर")

  1. हैंडलर को अनुरोध संदेश भेजें

इस तरह, हमारे पास अनुरोध करने के लिए 5 क्लाइंट उदाहरण हैं।

मैं भी स्प्रे करने के लिए नया हूं, यह सुनिश्चित नहीं है कि यह आपके उद्देश्य के लिए है या नहीं। सिर्फ तुम्हारी जानकारी के लिए।

चियर्स ~!


जवाब के लिए 0 № 2

पर आधारित इस मैं पाने में सक्षम था ActorRef

def createHttpRESTClient(host: String, port: Int): ActorRef = {

// execution context for future transformations below
import system.dispatcher
implicit val timeout: Timeout = 10 seconds

val ref: Future[ActorRef] = for {
Http.HostConnectorInfo(hostConnector: ActorRef, _) <- IO(Http) ? Http.HostConnectorSetup(host, port)
}
yield {
hostConnector
}
//FIXME - TODO fix this it"s really bad. However, We are going to create this only once when we create the actor, so I guess it"s okay for now.
Await.result(ref, 10 seconds)
}

और इस तरह मैं एक्टोररफ का उपयोग कर अनुरोध भेज रहा हूं और सेवा से प्रतिक्रिया प्राप्त कर रहा हूं।

  def sendReq(text: String): Future[String] = {
import spray.http._
val params = Map(("key" -> text))
val uri = Uri("/myservice") withQuery params
val request = Get(uri)
//send GET request using the "ask" pattern; the timeout
//TODO - not sure if we can use tell instead of ask here ?
val response: Future[HttpResponse] = restSvrActorRef.ask(request).mapTo[HttpResponse]
log.debug(s"done with sending a request to the REST service")
response.map(_.entity.asString)
}