美文网首页
ScalaTra(一)简单搭建以及jetty部署

ScalaTra(一)简单搭建以及jetty部署

作者: 一条湫刀鱼 | 来源:发表于2017-12-26 15:55 被阅读234次
    • scala 2.12.4
    • sbt 0.14.6
    • scalatra 2.6.2
    • 开发工具 vim

    参考自:
    http://scalatra.org/getting-started/first-project.html
    http://scalatra.org/guides/2.4/deployment/standalone.html

    • ./build.sbt
    val ScalatraVersion = "2.6.2"
    
    organization := "com.example"
    
    name := "My Scalatra Web App"
    
    version := "0.1.0-SNAPSHOT"
    
    scalaVersion := "2.12.4"
    
    resolvers += Classpaths.typesafeReleases
    
    libraryDependencies ++= Seq(
      "org.scalatra" %% "scalatra" % ScalatraVersion,
      "org.scalatra" %% "scalatra-scalatest" % ScalatraVersion % "test",
      "ch.qos.logback" % "logback-classic" % "1.1.5" % "runtime",
      "javax.servlet" % "javax.servlet-api" % "3.1.0" % "provided",                                                                              
      "org.eclipse.jetty" % "jetty-webapp" % "9.2.15.v20160210" % "container;compile"
    )
    enablePlugins(SbtTwirl)
    enablePlugins(ScalatraPlugin)
    
    • ./project/plugins.sbt
    addSbtPlugin("com.typesafe.sbt" % "sbt-twirl" % "1.3.12")
    addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.6")//sbt的assembly打包插件支持
    addSbtPlugin("org.scalatra.sbt" % "sbt-scalatra" % "1.0.1") 
    
    • 创建jetty启动器
      1 package com.example.app    
      2                            
      3 import org.eclipse.jetty.server.Server 
      4 import org.eclipse.jetty.servlet.{DefaultServlet, ServletContextHandler}
      5 import org.eclipse.jetty.webapp.WebAppContext
      6 import org.scalatra.servlet.ScalatraListener
      7 
      8 object JettyLauncher { // this is my entry object as specified in sbt project definition                                                 
      9   def main(args: Array[String]) { 
     10     val port = if(System.getenv("PORT") != null) System.getenv("PORT").toInt else 8080
     11 
     12     val server = new Server(port)   
     13     val context = new WebAppContext()
     14     context setContextPath "/"
     15     context.setResourceBase("src/main/webapp")
     16     context.addEventListener(new ScalatraListener)
     17     context.addServlet(classOf[MyScalatraServlet], "/*")
     18     context.addServlet(classOf[LoginServlet], "/login")
     19     server.setHandler(context)
     20       
     21     server.start
     22     server.join            
     23   }
     24 }
    
    • LoginServlet.scala
      1 package com.example.app    
      2                            
      3 import org.scalatra._      
      4 
      5 class LoginServlet extends ScalatraServlet {
      6 
      7   get("/") {               
      8     println("Test")        
      9     "Hi LoginServlet:GET!\n"                                                                                                             
     10   } 
     11       
     12 }
    
    • MyScalatraServlet.scala
      1 package com.example.app    
      2                                                                                                                                          
      3 import org.scalatra._
      4 import java.util.concurrent.TimeUnit
      5 
      6 class MyScalatraServlet extends ScalatraServlet {
      7 
      8   get("/") {
      9     TimeUnit.SECONDS.sleep(3)
     10     "Hi MyScalatraServlet:GET!\n"
     11   }
     12 
     13 }
    

    ScalatraBootstrap.scala

    import com.example.app._
    import org.scalatra._
    import javax.servlet.ServletContext
    import com.example.app.MyScalatraServlet
    import com.example.app.LoginServlet
    
    class ScalatraBootstrap extends LifeCycle {
      override def init(context: ServletContext) {
        context.mount(new MyScalatraServlet, "/*")
        context.mount(new LoginServlet, "/login")
      }
    }
    
    • 运行
    [root@xxy-srv-test-01 my-scalatra-web-app]# sbt
    [info] Loading settings from plugins.sbt ...
    [info] Loading project definition from /data1/taohui/hi_scala/my-scalatra-web-app/project
    [info] Loading settings from build.sbt ...
    [info] Set current project to My Scalatra Web App (in build file:/data1/taohui/hi_scala/my-scalatra-web-app/)
    [info] sbt server started at 127.0.0.1:5741
    sbt:My Scalatra Web App> 
    sbt:My Scalatra Web App> jetty:start
    [info] Updating {file:/data1/taohui/hi_scala/my-scalatra-web-app/}my-scalatra-web-app...
    [info] Done updating.
    [info] starting server ...
    2017-12-26 15:54:39.445:INFO::main: Logging initialized @185ms to org.eclipse.jetty.util.log.StdErrLog
    [success] Total time: 3 s, completed Dec 26, 2017 3:54:39 PM
    2017-12-26 15:54:39.456:INFO:oejr.Runner:main: Runner
    sbt:My Scalatra Web App> 2017-12-26 15:54:39.679:INFO:oejs.Server:main: jetty-9.4.6.v20170531
    2017-12-26 15:54:41.777:INFO:oeja.AnnotationConfiguration:main: Scanning elapsed time=1806ms
    2017-12-26 15:54:42.174:INFO:oejs.session:main: DefaultSessionIdManager workerName=node0
    2017-12-26 15:54:42.174:INFO:oejs.session:main: No SessionScavenger set, using defaults
    2017-12-26 15:54:42.178:INFO:oejs.session:main: Scavenging every 600000ms
    15:54:42.247 [main] INFO  o.scalatra.servlet.ScalatraListener - The cycle class name from the config: ScalatraBootstrap
    15:54:42.600 [main] INFO  o.scalatra.servlet.ScalatraListener - Initializing life cycle class: ScalatraBootstrap
    2017-12-26 15:54:42.804:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@5d3411d{/,file:///data1/taohui/hi_scala/my-scalatra-web-app/target/webapp/,AVAILABLE}{file:///data1/taohui/hi_scala/my-scalatra-web-app/target/webapp/}
    2017-12-26 15:54:42.824:INFO:oejs.AbstractConnector:main: Started ServerConnector@306cf3ea{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
    2017-12-26 15:54:42.825:INFO:oejs.Server:main: Started @3568ms
    jetty:start
    
    • jetty容器独立部署 [sbt-shell窗口下]
      使用sbt的assembly进行项目打包
    [info] SHA-1: b2ce657adc3335fe018621166ef763419031cb9c
    [info] Packaging /data1/taohui/hi_scala/my-scalatra-web-app/target/scala-2.12/My Scalatra Web App-assembly-0.1.0-SNAPSHOT.jar ...
    [info] Done packaging.
    [success] Total time: 9 s, completed Dec 26, 2017 3:55:24 PM
    sbt:My Scalatra Web App> 
    
    • 备注
      jetty容器上运行,需要添加对应的jetty依赖和sbt的assembly插件支持,配置明细见上,输出jar包后直接java -jar <jar_name>.jar,即可运行。

    相关文章

      网友评论

          本文标题:ScalaTra(一)简单搭建以及jetty部署

          本文链接:https://www.haomeiwen.com/subject/bqwlgxtx.html