湖南网站建设服务,企业自助建站策划方案,企业在哪些网站发布,动态域名做网站java heroku最近#xff0c;我听说Heroku允许在Cedar堆栈中部署Java应用程序。 由于没有真正的软件构想#xff0c;我决定尝试一下#xff0c;仅配置SOMETHING以在Heroku上运行。 我对ReST有所了解#xff08;我仍然想学习并练习#xff09;#xff0c;所以我决定我的第… java heroku 最近我听说Heroku允许在Cedar堆栈中部署Java应用程序。 由于没有真正的软件构想我决定尝试一下仅配置SOMETHING以在Heroku上运行。 我对ReST有所了解我仍然想学习并练习所以我决定我的第一个应用程序将是使用Jersey JAX-RS实现的简单问候世界。 因此我在GitHub上启动了一个项目 并开始设置Heroku CLI。 设置Heroku CLI Heroku现在很容易设置。 我记得什么时候需要Ruby env和我的第一个 与Ruby的接触并不是那么好没有任何安装程序所以都是手动的-而且我很懒所以我当时放弃了Heroku。 但是现在安装它变得轻而易举–只需转到Heroku Toolbelt并为您的平台下载版本。 我现在已经在Linux Mint和Windows 7上都进行了设置并且效果很好。 为Heroku设置项目 我的项目称为摘要 -应该是另一个票务管理系统。 但这暂时不相关。 最重要的是为了让Heroku发现我们的应用程序是Java应用程序必须存在pom.xml文件。 这是因为Heroku使用Maven 3来构建Java应用程序。 因此要真正开始使用Heroku进行任何工作您需要一个简单的pom.xml文件。 就我而言我为该应用程序添加了一个单独的模块因此我的主pom如下所示 ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.github.pbuda.recaps/groupIdartifactIdrecaps/artifactIdpackagingpom/packagingversion0.0.1-SNAPSHOT/versioninceptionYear2012/inceptionYeardevelopersdevelopernamePiotr Buda/nameemailpibudagmail.com/emailtimezone1/timezone/developer/developerslicenseslicensenameApache License, version 2.0/nameurlhttp://www.apache.org/licenses/LICENSE-2.0.html/url/license/licensesmodulesmodulewebmodule/module/modulesbuildpluginManagementpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdconfigurationsource1.6/sourcetarget1.6/target/configuration/plugin/plugins/pluginManagement/build/project 然后是Web模块仅用于拆分项目 ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdcom.github.pbuda.recaps/groupIdartifactIdrecaps/artifactIdversion0.0.1-SNAPSHOT/version/parentartifactIdwebmodule/artifactIdbuildpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-dependency-plugin/artifactIdversion2.4/versionexecutionsexecutionidcopy-dependencies/idphasepackage/phasegoalsgoalcopy-dependencies/goal/goals/execution/executions/pluginplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactId/plugin/plugins/builddependenciesdependencygroupIdcom.sun.jersey/groupIdartifactIdjersey-server/artifactIdversion1.12/version/dependencydependencygroupIdcom.sun.jersey/groupIdartifactIdjersey-core/artifactIdversion1.12/version/dependencydependencygroupIdcom.sun.jersey/groupIdartifactIdjersey-grizzly2/artifactIdversion1.12/version/dependency/dependencies/project 我最初运行示例项目的尝试集中在设置Jersey。 在检查了文档之后我决定使用Grizzly2 HTTP服务器只是因为它很容易设置。 我基本上已经将文档教程粘贴到Main类中。 有一些必要的区别因为例如服务器的端口是由Heroku动态分配的。 因此经过很少的更改后生成的Main类如下所示 /*** Copyright 2012 Piotr Buda** Licensed under the Apache License, Version 2.0 (the License);* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an AS IS BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.github.pbuda.recaps;import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;
import org.glassfish.grizzly.http.server.HttpServer;import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.net.URI;/*** Created by IntelliJ IDEA.* User: pbu* Date: 28.02.12* Time: 21:01* To change this template use File | Settings | File Templates.*/
public class Main {private static URI getBaseURI(String hostname, int port) {return UriBuilder.fromUri(http://0.0.0.0/).port(port).build();}protected static HttpServer startServer(URI uri) throws IOException {System.out.println(Starting grizzly...);ResourceConfig rc new PackagesResourceConfig(com.github.pbuda.recaps);return GrizzlyServerFactory.createHttpServer(uri, rc);}public static void main(String[] args) throws IOException {URI uri getBaseURI(System.getenv(HOSTNAME), Integer.valueOf(System.getenv(PORT)));HttpServer httpServer startServer(uri);System.out.println(String.format(Jersey app started with WADL available at %sapplication.wadl\nTry out %shelloworld\nHit enter to stop it...,uri, uri));while(true) {System.in.read();}}
} 这将启动服务器并向其中注册一些资源。 一些灰熊的把戏 首先GrizzlyServerFactory.createHttpServer方法接受一个必须以女巫模式名称开头的URI在本例中为http//。 然后它必须指定主机名首先我将其设置为herokuapp.com上的应用程序名称。 这没有用但是Heroku很好地告诉了我日志中有一条通知指出服务器应绑定到0.0.0.0因此我将URI更改为http://0.0.0.0。 其次Jersey示例等待按键以终止服务器。 不幸的是Heroku打印了一条消息然后以某种方式将该消息传递给应用程序并且服务器被终止。 为了解决这个问题我将System.in.read包裹在一个无限的while循环中。 这当然不是最好的解决方案但是它起作用了或者看起来如此。 几个小时后我检查了应用程序的日志他们说应用程序从上到下运行。 因此我决定从Grizzly切换到Jetty但这与本文无关。 在将所有内容推送到Heroku之前我还添加了一个Procfile web: java -cp webmodule/target/classes:webmodule/target/dependency/* com.github.pbuda.recaps.Main 在推送到Heroku之后该应用程序被构建并启动并请求http://growing-dawn-9158.herokuapp.com/helloworld产生了一些输出在这种情况下为简单的“消息”消息。 做得好。 我犯的错误并从中吸取教训 首先我忘了添加Maven Dependency插件但是在推送到Heroku之前我解决了这个问题。 没有配置它我就无法将依赖项添加到classpath中从而导致ClassNotFound异常。 起初我并没有想到它是必需的但是随后我查看了Heroku的示例并轻松地对其进行了修复。 其次我不知道网络测功机超时。 成功部署后我确定该应用程序正在运行但是由于超时日志显示该应用程序已关闭。 因为我不知道网络测功机超时的事实所以我怀疑Grizzly只是被某种方式打断了所以我决定搬到Jetty。 但这也发生在Jetty的实现上因此我开始进行挖掘并找到了相关信息。 摘要 我认为Heroku很棒。 它是免费的Java托管虽然并不流行但他们做到了而且效果很好。 我曾经尝试过Google App Engine但是体验并不是很好请注意您已经有很长时间了所以我决定给Heroku一个机会因为设置应用程序实际上非常简单我想坚持一会儿并使用该平台–查看所有这些插件 参考 Heroku和Java –从新手到初学者我们的JCG合作伙伴 Piotr Buda的第1部分在Software Ramblings博客上。 翻译自: https://www.javacodegeeks.com/2013/05/heroku-and-java-from-newbie-to-beginner-part-1.htmljava heroku