Heroku和Java –从新手到初学者,第1部分
最近,我聽(tīng)說(shuō)Heroku允許在Cedar堆棧中部署Java應(yīng)用程序。 由于沒(méi)有真正的軟件構(gòu)想,我決定嘗試一下,僅將SOMETHING配置為可在Heroku上運(yùn)行。
我對(duì)ReST有一些迷戀(我仍然想學(xué)習(xí)并練習(xí)),所以我決定我的第一個(gè)應(yīng)用程序?qū)⑹鞘褂肑ersey (JAX-RS實(shí)現(xiàn))的簡(jiǎn)單問(wèn)候世界。 因此,我在GitHub上啟動(dòng)了一個(gè)項(xiàng)目 ,并開(kāi)始設(shè)置Heroku CLI。
設(shè)置Heroku CLI
Heroku現(xiàn)在很容易設(shè)置。 我記得什么時(shí)候需要Ruby env和我的第一個(gè)
與Ruby的接觸并不是那么好(沒(méi)有任何安裝程序,所以全是手動(dòng)的-而且我很懶),所以我當(dāng)時(shí)放棄了Heroku。 但是現(xiàn)在安裝它變得輕而易舉–只需轉(zhuǎn)到Heroku Toolbelt并為您的平臺(tái)下載版本。 我現(xiàn)在已經(jīng)在Linux Mint和Windows 7上都進(jìn)行了設(shè)置,并且效果很好。
為Heroku設(shè)置項(xiàng)目
我的項(xiàng)目稱為摘要 -應(yīng)該是另一個(gè)票務(wù)管理系統(tǒng)。 但這暫時(shí)不相關(guān)。 最重要的是,為了讓Heroku發(fā)現(xiàn)我們的應(yīng)用程序是Java應(yīng)用程序,必須存在pom.xml文件。 這是因?yàn)镠eroku使用Maven 3來(lái)構(gòu)建Java應(yīng)用程序。
因此,要真正開(kāi)始使用Heroku進(jìn)行任何工作,您需要一個(gè)簡(jiǎn)單的pom.xml文件。 就我而言,我為應(yīng)用程序添加了一個(gè)單獨(dú)的模塊,因此我的主pom如下所示:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.github.pbuda.recaps</groupId><artifactId>recaps</artifactId><packaging>pom</packaging><version>0.0.1-SNAPSHOT</version><inceptionYear>2012</inceptionYear><developers><developer><name>Piotr Buda</name><email>pibuda@gmail.com</email><timezone>+1</timezone></developer></developers><licenses><license><name>Apache License, version 2.0</name><url>http://www.apache.org/licenses/LICENSE-2.0.html</url></license></licenses><modules><module>webmodule</module></modules><build><pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.6</source><target>1.6</target></configuration></plugin></plugins></pluginManagement></build></project>然后是Web模塊,僅用于拆分項(xiàng)目:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.github.pbuda.recaps</groupId><artifactId>recaps</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>webmodule</artifactId><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><version>2.4</version><executions><execution><id>copy-dependencies</id><phase>package</phase><goals><goal>copy-dependencies</goal></goals></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId></plugin></plugins></build><dependencies><dependency><groupId>com.sun.jersey</groupId><artifactId>jersey-server</artifactId><version>1.12</version></dependency><dependency><groupId>com.sun.jersey</groupId><artifactId>jersey-core</artifactId><version>1.12</version></dependency><dependency><groupId>com.sun.jersey</groupId><artifactId>jersey-grizzly2</artifactId><version>1.12</version></dependency></dependencies></project>我運(yùn)行我的示例項(xiàng)目的第一次嘗試集中在設(shè)置Jersey。 在檢查了文檔之后,我決定使用Grizzly2 HTTP服務(wù)器是因?yàn)樗浅H菀自O(shè)置。 我基本上已經(jīng)將文檔教程粘貼到Main類中。 有一些必要的區(qū)別,因?yàn)槔绶?wù)器的端口是由Heroku動(dòng)態(tài)分配的。 因此,經(jīng)過(guò)很少的更改后,生成的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();}} }這將啟動(dòng)服務(wù)器并向其中注冊(cè)一些資源。
一些灰熊的把戲
首先,GrizzlyServerFactory.createHttpServer方法接受一個(gè)必須以女巫模式名稱開(kāi)頭的URI,在本例中為http://。 然后,它必須指定主機(jī)名,首先,我在herokuapp.com上將其設(shè)置為應(yīng)用程序名稱。 這沒(méi)有用,但是Heroku很好地告訴了我:日志中有一條通知,指出服務(wù)器應(yīng)綁定到0.0.0.0,因此我將URI更改為http://0.0.0.0。
其次,Jersey示例等待按鍵以終止服務(wù)器。 不幸的是,Heroku打印了一條消息,然后以某種方式將該消息傳遞給應(yīng)用程序,并且服務(wù)器被終止。 為了解決這個(gè)問(wèn)題,我將System.in.read()包裹在一個(gè)無(wú)限的while循環(huán)中。
這當(dāng)然不是最好的解決方案,但它確實(shí)起作用了,或者看起來(lái)如此。 幾個(gè)小時(shí)后,我檢查了應(yīng)用程序的日志,他們說(shuō)該應(yīng)用程序從上到下運(yùn)行。 因此,我決定從Grizzly切換到Jetty,但這與本文無(wú)關(guān)。
在將所有內(nèi)容推送到Heroku之前,我還添加了一個(gè)Procfile:
web: java -cp webmodule/target/classes:webmodule/target/dependency/* com.github.pbuda.recaps.Main在推送到Heroku之后,該應(yīng)用程序被構(gòu)建并啟動(dòng),并請(qǐng)求http://growing-dawn-9158.herokuapp.com/helloworld產(chǎn)生了一些輸出(在本例中為簡(jiǎn)單的“消息”消息)。 做得好。
我犯的錯(cuò)誤并從中吸取教訓(xùn)
首先,我忘了添加Maven Dependency插件,但是在推送到Heroku之前我解決了這個(gè)問(wèn)題。 沒(méi)有配置它,我就無(wú)法將依賴項(xiàng)添加到classpath中,從而導(dǎo)致ClassNotFound異常。 起初我并沒(méi)有想到它是必需的,但是后來(lái)我看了一下Heroku示例并輕松修復(fù)了它。
其次,我不知道網(wǎng)絡(luò)測(cè)功機(jī)超時(shí)。 成功部署后,我確定該應(yīng)用程序正在運(yùn)行,但是由于超時(shí),日志顯示該應(yīng)用程序已關(guān)閉。 因?yàn)槲也涣私饩W(wǎng)絡(luò)測(cè)功機(jī)超時(shí)的事實(shí),所以我懷疑Grizzly只是以某種方式被打斷了,所以我決定搬到Jetty。 但這也發(fā)生在Jetty的實(shí)現(xiàn)上,因此我開(kāi)始進(jìn)行挖掘并找到了相關(guān)信息。
摘要
我認(rèn)為Heroku很棒。 它是免費(fèi)的Java托管,雖然不受歡迎,但他們做到了,而且效果很好。 我曾經(jīng)嘗試過(guò)Google App Engine,但是體驗(yàn)并不是很好(請(qǐng)注意您已經(jīng)有很長(zhǎng)時(shí)間了),所以我決定給Heroku一個(gè)機(jī)會(huì),因?yàn)樵O(shè)置應(yīng)用程序?qū)嶋H上很簡(jiǎn)單,我想我堅(jiān)持一會(huì)兒并使用該平臺(tái)–查看所有這些插件
翻譯自: https://www.javacodegeeks.com/2013/05/heroku-and-java-from-newbie-to-beginner-part-1.html
總結(jié)
以上是生活随笔為你收集整理的Heroku和Java –从新手到初学者,第1部分的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: (sql linux)
- 下一篇: 具有内部类构造函数参数的Java Ref