如何部署 Spark Java Web 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19783646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to deploy a spark Java web app?
提问by MichaelP
I used spark web framework to create a webapp, but I don't know how to deploy this webapp. I'm sorry if this is very basic, but I'm new to spark framework and I cannot find any document that guide me how to deploy a spark webapp.:
我使用spark web框架创建了一个webapp,但是我不知道如何部署这个webapp。如果这是非常基本的,我很抱歉,但我是 spark 框架的新手,我找不到任何指导我如何部署 spark webapp 的文档。:
- How to deploy a spark webapp standalone
- How to build a spark webapp (to war file or such a file) and deploy with web server (jetty or Tomcat).
- 如何独立部署 spark webapp
- 如何构建 spark webapp(到 war 文件或此类文件)并使用 web 服务器(jetty 或 Tomcat)进行部署。
采纳答案by dMcNavish
You will first need to create a regular Java project that can be built into a .war file (in Eclipse this would be a Dynamic Web Project)
您首先需要创建一个可以构建到 .war 文件中的常规 Java 项目(在 Eclipse 中,这将是一个动态 Web 项目)
The spark documentation at this link describes what needs to be added to your projects web.xml file. http://sparkjava.com/documentation.html#other-webserver
此链接中的 spark 文档描述了需要添加到项目 web.xml 文件中的内容。http://sparkjava.com/documentation.html#other-webserver
the param-value listed in the documentation within the filter needs to point to the class where you have defined your routes.
过滤器内文档中列出的参数值需要指向您定义路由的类。
Additionally, all the code that was previously in main() needs to be moved to init().
此外,之前在 main() 中的所有代码都需要移到 init() 中。
@Override
public void init() {
get(new Route("/test") {
@Override
public Object handle(Request request, Response response) {
return "response goes here;
}
});
Also, in order for me to deploy it to JBoss, I had to only include the spark libraries and not the Jetty libraries. Once this was done, you should be able to build the war and deploy it to your server the same way you would any other Java project.
此外,为了让我将它部署到 JBoss,我只需要包含 spark 库而不是 Jetty 库。完成此操作后,您应该能够以与任何其他 Java 项目相同的方式构建战争并将其部署到您的服务器。
回答by saaaaaaaaasha
Here you find information about deploy: http://sparkjava.com/documentation.html#embedded-web-server
在这里您可以找到有关部署的信息:http: //sparkjava.com/documentation.html#embedded-web-server
Fist of all, set filter options for web.xml
config:
首先,为web.xml
配置设置过滤器选项:
<web-app>
<!-- some options -->
<filter>
<filter-name>SparkFilter</filter-name>
<filter-class>spark.servlet.SparkFilter</filter-class>
<init-param>
<param-name>applicationClass</param-name>
<param-value>your.package.Application</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SparkFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Application
class should implement of the interface spark.servlet.SparkApplication
and have to initialize the routes in the init()
method.
Application
类应该实现接口,spark.servlet.SparkApplication
并且必须在init()
方法中初始化路由。
This one looks like as (in Java SE 8 you can use Lambda Expressionfor router.):
这看起来像(在 Java SE 8 中,您可以将Lambda 表达式用于路由器。):
package your.package;
import static spark.Spark.*;
public class Application implements SparkApplication {
@Override
public void init() {
get("/", (request, response) -> "Hello World");
get("/hello/:name", (request, response) -> {
return "Hello: " + request.params(":name");
});
}
}
App with this configuration works fine for tomcatand glassfishservers.
具有此配置的应用程序适用于tomcat和glassfish服务器。
回答by DevNG
For the standalone scenario, you can just use Gradle(or Maven) to create fat (meaning has all dependencies including an embedded Jetty server), executable jar file. Here is a simple build.gradle
file that does just that:
对于独立场景,您可以只使用Gradle(或 Maven)来创建 fat(意味着拥有包括嵌入式 Jetty 服务器在内的所有依赖项)、可执行的 jar 文件。这是一个简单的build.gradle
文件,可以做到这一点:
apply plugin: 'java'
apply plugin: 'application'
// TODO Change this to your class with your main method
mainClassName = "my.app.Main"
defaultTasks 'run'
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.sparkjava', name: 'spark-core', version: '2.5.5'
// TODO add more dependencies here...
}
// Create a fat executable jar
jar {
manifest {
attributes "Main-Class": "$mainClassName"
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
archiveName "app.jar"
}
Build the you application on the command line via gradle build
. This will create an app.jar
file in your build/libs
folder then just run:
通过gradle build
. 这将app.jar
在您的build/libs
文件夹中创建一个文件,然后运行:
java -jar build/libs/app.jar
If you want to be really up to date :) then you have to use Dockerto package your JRE and application jar, thus you are not dependent on the software stack installed on the server. To do this we can use a Dockerfile
:
如果您想真正保持最新 :) 那么您必须使用Docker来打包您的 JRE 和应用程序 jar,因此您不依赖于安装在服务器上的软件堆栈。为此,我们可以使用Dockerfile
:
FROM java:8
ADD build/libs/app.jar /
EXPOSE 4567
ENTRYPOINT ["java", "-jar", "app.jar"]
Build the docker image and run it, e.g.:
构建 docker 镜像并运行它,例如:
docker build -t myapp:v1 .
docker run --rm --name myapp -p 4567:4567 myapp:v1
Of course if you want to use the Docker image on a remote web server, you need to push it to Docker Hubor a private docker repository and use docker pull
to pull it to your server, before running it.
当然,如果您想在远程 Web 服务器上使用 Docker 映像,则需要将其推送到Docker Hub或私有Docker存储库,并docker pull
在运行之前将其拉至您的服务器。
回答by Gene
1) Clone this repo: https://github.com/simplesteph/ec2-masterclass-sampleapp
1) 克隆这个 repo: https://github.com/simplesteph/ec2-masterclass-sampleapp
2) Navigate to project pom.xml directory
2) 导航到项目 pom.xml 目录
3) mvn clean install
3) mvn 全新安装
4) go to target folder
4)转到目标文件夹
5) java -jar ec2-masterclass-sample-app-1.0-jar-with-dependencies.jar
5) java -jar ec2-masterclass-sample-app-1.0-jar-with-dependencies.jar
6) In browser, navigate to http://localhost:4567
6) 在浏览器中,导航到http://localhost:4567