- 浏览: 2552809 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Spray(13)REST API Project - Integrate with Spray Client
10. Integrate with Spray Client
10.1. Add the dependencies
in the build.sbt
"io.spray" % "spray-client" % "1.1-M7",
10.2. Initiate the Actor when Our system start
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props
import akka.actor.actorRef2Scala
import spray.can.server.HttpServer
import spray.io.IOExtension
import spray.io.SingletonHandler
import com.typesafe.config.ConfigFactory
import spray.routing.directives.PathMatcher
import shapeless._
import spray.routing.HttpServiceActor
import spray.can.client.DefaultHttpClient
import spray.client.HttpConduit
implicit val system = ActorSystem("on-spray-can")
val config = ConfigFactory.load()
//initiate the spray client actor
val httpClient = DefaultHttpClient(system)
val analyticsServerAddress: String = config.getString("analytics.server.address")
val analyticsServerPort: Int = config.getInt("analytics.server.port")
//actor which holds the connection pool to analytics remote server
val analyticsConduit = system.actorOf(
props = Props(new HttpConduit(httpClient, analyticsServerAddress, analyticsServerPort)),
name = "sillycat_conduit"
)
So we will name the spray client as "sillycat_conduit".
When we wants to use this actor.
Get the actorRefFActory in the BaseService
val sillycatRESTTemplate: SillycatRESTTemplate = SillycatRESTTemplate.apply(actorRefFactory)
And in the Template Class, I will get the actorRefFactory like this>
import akka.actor.{ Props, Actor }
import spray.routing._
import spray.routing.directives._
import spray.util.LoggingContext
import spray.http.StatusCodes._
import spray.httpx.SprayJsonSupport._
import shapeless._
import akka.actor._
import spray.routing.authentication._
import spray.client.HttpConduit
import spray.http.HttpRequest
import spray.http.HttpMethods
import scala.util.{ Success, Failure }
import akka.actor.{ Props, ActorSystem }
import spray.can.client.DefaultHttpClient
import spray.client.HttpConduit
import spray.httpx.SprayJsonSupport
import spray.http._
import spray.util._
import scala.concurrent.Future
import scala.concurrent.Await
import scala.concurrent.duration._
import org.joda.time.Period
import scala.slick.session.Database.threadLocalSession
import spray.json._
import scala.Predef._
class SillycatRESTTemplate(actorRefFactory: ActorRefFactory) {
val sillycat_conduit = actorRefFactory.actorFor("/user/sillycat_conduit") //actor name
val default_timeout = 30 second
…snip...
val pipeline = HttpConduit.sendReceive(analytics_conduit)
//post the data, prettyRequest is the JSON format data
val responseFuture = pipeline(HttpRequest(method = HttpMethods.POST, entity = prettyRequest, uri = "/dashboard/sillycat"))
//block result
val result = Await.result(responseFuture, default_timeout)
//I need to use block result here, because I need to put the response to client right now
val result_str = result.entity.toOption.get.asString
return result_str
}
11. Validation
I can add the validation everywhere if I want
require(List(7, 30, 90).contains(periodDays), "PeriodDays should be in List(7,30,90), but system gets periodDays = " + periodDays)
require(!stores.isEmpty, "There is no stores related to this tag, Tag = " + tag)
Because there is this kind of default handler there
implicit def myExceptionHandler(implicit log: LoggingContext) =
ExceptionHandler.fromPF {
case e: java.lang.IllegalArgumentException => ctx =>
log.warning("Request {} could not be handled normally", ctx.request)
ctx.complete(BadRequest, e.getMessage)
}
Actually when I dive into the source codes of scala, I saw the require is defined like this>
@inline final def require(requirement: Boolean, message: => Any) {
if (!requirement)
throw new IllegalArgumentException("requirement failed: "+ message)
}
12. How to Work with Actor
come soon...
13. Plain SQL for click
come soon…
Tips
1. How to delete branch in github
delete local branch
>git branch -d :branchName
delete remote branch
>git push origin :branchName
or
>git push origin --delete :branchName
But since I delete the remote branch from the GUI of github, I got these error message:
Carls-MacBook-Pro:winner-seller-server carl$ git push origin --delete another_auth
error: unable to delete 'another_auth': remote ref does not exist
error: failed to push some refs to 'https://github.com/luohuazju/magic.git'
But actually, when I check the all branch on my local, I saw the remote branch
>git branch -a
* master
remotes/origin/another_auth
remotes/origin/master
Solution:
Then only thing I need to do is to prune my local
>git remote prune origin
prune remotes any branch that does not exist in the remote origin anymore.
2. Permission denied while sbt command
[warn] exception occurred while writing properties file /Users/carl/.ivy2/cache/commons-codec/commons-codec/ivydata-1.4.properties: /Users/carl/.ivy2/cache/commons-codec/commons-codec/ivydata-1.4.properties (Permission denied)
Solution:
I do not know why, some of the directory are root under the .ivy. I am going to change them.
>sudo chown -R carl .ivy2/cache
References:
Spray 1 ~ 9
http://sillycat.iteye.com/blog/1766558
http://sillycat.iteye.com/blog/1766568
http://sillycat.iteye.com/blog/1857105
http://sillycat.iteye.com/blog/1858282
http://sillycat.iteye.com/blog/1858298
http://sillycat.iteye.com/blog/1859025
spray(10)
http://sillycat.iteye.com/blog/1872476
spray(11 12)
http://sillycat.iteye.com/blog/1882919
http://sillycat.iteye.com/blog/1882963
10. Integrate with Spray Client
10.1. Add the dependencies
in the build.sbt
"io.spray" % "spray-client" % "1.1-M7",
10.2. Initiate the Actor when Our system start
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props
import akka.actor.actorRef2Scala
import spray.can.server.HttpServer
import spray.io.IOExtension
import spray.io.SingletonHandler
import com.typesafe.config.ConfigFactory
import spray.routing.directives.PathMatcher
import shapeless._
import spray.routing.HttpServiceActor
import spray.can.client.DefaultHttpClient
import spray.client.HttpConduit
implicit val system = ActorSystem("on-spray-can")
val config = ConfigFactory.load()
//initiate the spray client actor
val httpClient = DefaultHttpClient(system)
val analyticsServerAddress: String = config.getString("analytics.server.address")
val analyticsServerPort: Int = config.getInt("analytics.server.port")
//actor which holds the connection pool to analytics remote server
val analyticsConduit = system.actorOf(
props = Props(new HttpConduit(httpClient, analyticsServerAddress, analyticsServerPort)),
name = "sillycat_conduit"
)
So we will name the spray client as "sillycat_conduit".
When we wants to use this actor.
Get the actorRefFActory in the BaseService
val sillycatRESTTemplate: SillycatRESTTemplate = SillycatRESTTemplate.apply(actorRefFactory)
And in the Template Class, I will get the actorRefFactory like this>
import akka.actor.{ Props, Actor }
import spray.routing._
import spray.routing.directives._
import spray.util.LoggingContext
import spray.http.StatusCodes._
import spray.httpx.SprayJsonSupport._
import shapeless._
import akka.actor._
import spray.routing.authentication._
import spray.client.HttpConduit
import spray.http.HttpRequest
import spray.http.HttpMethods
import scala.util.{ Success, Failure }
import akka.actor.{ Props, ActorSystem }
import spray.can.client.DefaultHttpClient
import spray.client.HttpConduit
import spray.httpx.SprayJsonSupport
import spray.http._
import spray.util._
import scala.concurrent.Future
import scala.concurrent.Await
import scala.concurrent.duration._
import org.joda.time.Period
import scala.slick.session.Database.threadLocalSession
import spray.json._
import scala.Predef._
class SillycatRESTTemplate(actorRefFactory: ActorRefFactory) {
val sillycat_conduit = actorRefFactory.actorFor("/user/sillycat_conduit") //actor name
val default_timeout = 30 second
…snip...
val pipeline = HttpConduit.sendReceive(analytics_conduit)
//post the data, prettyRequest is the JSON format data
val responseFuture = pipeline(HttpRequest(method = HttpMethods.POST, entity = prettyRequest, uri = "/dashboard/sillycat"))
//block result
val result = Await.result(responseFuture, default_timeout)
//I need to use block result here, because I need to put the response to client right now
val result_str = result.entity.toOption.get.asString
return result_str
}
11. Validation
I can add the validation everywhere if I want
require(List(7, 30, 90).contains(periodDays), "PeriodDays should be in List(7,30,90), but system gets periodDays = " + periodDays)
require(!stores.isEmpty, "There is no stores related to this tag, Tag = " + tag)
Because there is this kind of default handler there
implicit def myExceptionHandler(implicit log: LoggingContext) =
ExceptionHandler.fromPF {
case e: java.lang.IllegalArgumentException => ctx =>
log.warning("Request {} could not be handled normally", ctx.request)
ctx.complete(BadRequest, e.getMessage)
}
Actually when I dive into the source codes of scala, I saw the require is defined like this>
@inline final def require(requirement: Boolean, message: => Any) {
if (!requirement)
throw new IllegalArgumentException("requirement failed: "+ message)
}
12. How to Work with Actor
come soon...
13. Plain SQL for click
come soon…
Tips
1. How to delete branch in github
delete local branch
>git branch -d :branchName
delete remote branch
>git push origin :branchName
or
>git push origin --delete :branchName
But since I delete the remote branch from the GUI of github, I got these error message:
Carls-MacBook-Pro:winner-seller-server carl$ git push origin --delete another_auth
error: unable to delete 'another_auth': remote ref does not exist
error: failed to push some refs to 'https://github.com/luohuazju/magic.git'
But actually, when I check the all branch on my local, I saw the remote branch
>git branch -a
* master
remotes/origin/another_auth
remotes/origin/master
Solution:
Then only thing I need to do is to prune my local
>git remote prune origin
prune remotes any branch that does not exist in the remote origin anymore.
2. Permission denied while sbt command
[warn] exception occurred while writing properties file /Users/carl/.ivy2/cache/commons-codec/commons-codec/ivydata-1.4.properties: /Users/carl/.ivy2/cache/commons-codec/commons-codec/ivydata-1.4.properties (Permission denied)
Solution:
I do not know why, some of the directory are root under the .ivy. I am going to change them.
>sudo chown -R carl .ivy2/cache
References:
Spray 1 ~ 9
http://sillycat.iteye.com/blog/1766558
http://sillycat.iteye.com/blog/1766568
http://sillycat.iteye.com/blog/1857105
http://sillycat.iteye.com/blog/1858282
http://sillycat.iteye.com/blog/1858298
http://sillycat.iteye.com/blog/1859025
spray(10)
http://sillycat.iteye.com/blog/1872476
spray(11 12)
http://sillycat.iteye.com/blog/1882919
http://sillycat.iteye.com/blog/1882963
发表评论
-
Stop Update Here
2020-04-28 09:00 316I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 476NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 369Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 370Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 337Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 431Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 436Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 374Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 456VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 385Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 479NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 424Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 337Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 248GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 452GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 328GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 314Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 319Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 294Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 312Serverless with NodeJS and Tenc ...
相关推荐
AndroidAsync.zip,用于Android的异步套接字、HTTP(客户端 服务器)、WebSocket和socket.io库。基于nio,而不是threads.asynchronous socket、http(client server)和android的websocket库。基于nio,而不是线程。
spray-actor-per-request, 使用每个请求模型中的参与者的示例 Spray 应用程序 每个请求的 Spray这个项目提供了一个示例 Spray 应用程序,它使用每个请求模型中的参与者。为什么要为每个HTTP请求启动一个参与者?轻松...
"slick-pg_spray-json_2.10-0.5.2.2.zip"中的Slick-PG版本,结合了Scala的spray-json库,使得JSON数据可以无缝地在数据库和代码之间转换。spray-json是Scala的一个轻量级、快速且易于使用的JSON库,它可以方便地解析...
spray-template使得开发RESTful API和服务时,能够快速、灵活地生成动态HTML或其他文本格式的响应。 在spray-can中,spray-template扮演了重要的角色,它允许开发者使用简洁、可读性强的模板语言来构建HTTP响应的...
描述 "spray-cache-spymemcached.zip" 涉及的是一个针对 Spray 框架的缓存扩展,名为 SpyMemcached 后端。Spray 是一个基于 Scala 的高性能、轻量级的 HTTP 和 RESTful 服务构建工具,常用于构建异步、非阻塞的服务...
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
$ git clone git://github.com/spray/spray-template.git my-project 将目录更改为您的克隆: $ cd 我的项目 启动 SBT: $ sbt 编译一切并运行所有测试: 测试 启动应用程序: 重新开始 浏览到以查看 angular...
"scala-rest-client-evaluation"项目旨在评估这些Scala REST客户端框架的性能、易用性和功能。 该项目主要关注以下几点关键知识点: 1. **RESTful API**: REST(Representational State Transfer)是一种网络应用...
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
概要该项目解释了如何使用具有 CORS 支持的 Spray 实现 REST API假设我假设你有经验和你的工作环境准备好使用以下技术/框架: Akka、SBT、CORS、Spray、cURL、Scala、Git要求这篇文章面向那些有 Scala 工作经验并...