Forwarding HTTP/REST Request to another REST server in Spray

2,690 views
Skip to first unread message

Soumya Simanta

unread,
Jan 16, 2015, 7:16:10 PM1/16/15
to spray...@googlegroups.com

I've a bunch of existing REST services (#1 and #2 below) that are running on different endpoints that are used internally only. Now I want to expose some of these REST APIs (API-1 and API-2) externally using Spray because this external endpoint will also provide some additional APIs (API-3, API-4).

Is there a simple/recommended way to forward the external REST requests to my new endpoint to existing REST endpoints?


PS: I'm crossposting this for a better coverage. http://stackoverflow.com/questions/27994748/forwarding-http-rest-request-to-another-rest-server-in-spray



John Smith

unread,
Oct 19, 2015, 2:51:38 PM10/19/15
to spray.io User List
Any updates on this?

Josh Hesketh

unread,
Oct 20, 2015, 7:26:13 AM10/20/15
to spray.io User List
The stackoverflow post mentioned has responses

John Smith

unread,
Oct 20, 2015, 3:04:04 PM10/20/15
to spray.io User List
I saw them, but the solution does not seem to fit well the case where the proxy needs to forward the requests to different nodes based on some logic. For example, given a field in the request body determine what's the specific node to handle that request. 
I'm starting to think that Spray might not be a good fit for this problem.

Thanks!

Age Mooij

unread,
Oct 21, 2015, 4:28:51 AM10/21/15
to spray...@googlegroups.com
I have built several proxies and API gateways using Spray. The core concepts are very much like the Cake Solutions blog post, so I use the normal spray-routing dsl to select/filter/route requests to the right proxy. If you need to choose the proxy target based on the incoming request than spray-routing is perfect for that. Here's an example for the core routing logic

(decompressRequest() & compressResponseIfRequested(())) {
  matchesConfiguredTarget(configuration) { target =>
    methodIsAllowedFor(target) {
      recordMetricsFor(target) {
        proxyTo(proxyFor(target))
      }
    }
  }
}

Unlike the blog post I create a dedicated actor per configured remote target. This actor load balances across multiple instances of the same target service and it manages its own dedicated HttpHostConnector to prevent every request having to go through the IO(Http) manager actor.

Writing high-performance proxies and gateways is not a trivial job but Spray makes it pretty easy.

Hope this helps
Age


-- 
You received this message because you are subscribed to the Google Groups "spray.io User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
Visit this group at http://groups.google.com/group/spray-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/spray-user/be5c9156-ae5a-46cc-9997-4f0e5b940d8f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Lily

unread,
Apr 8, 2016, 5:14:19 AM4/8/16
to spray.io User List
Hi, I want to use proxyTo of ProxyDirectives,but I get an error below,
    error: object ProxyDirectives is not a member of package spray.routing.directives
    [ant:scalac] import spray.routing.directives.{CachingDirectives, ProxyDirectives}
    [ant:scalac]        ^
    [ant:scalac] one error found

the config of  build.gradle is :
    ext.sprayVersion = '1.3.3'
    compile "io.spray:spray-can_2.11:$sprayVersion"
    compile "io.spray:spray-routing_2.11:$sprayVersion"
    compile "io.spray:spray-servlet_2.11:$sprayVersion"
    compile "io.spray:spray-caching_2.11:$sprayVersion"
    compile 'io.spray:spray-json_2.11:1.3.2'

How can i access the ProxyDirectives? Thanks very much!

在 2015年1月17日星期六 UTC+8上午8:16:10,Soumya Simanta写道:
Reply all
Reply to author
Forward
0 new messages