java heroku_Heroku和Java –从新手到初学者,第1部分
java heroku
最近,我聽說Heroku允許在Cedar堆棧中部署Java應(yīng)用程序。 由于沒有真正的軟件構(gòu)想,我決定嘗試一下,僅配置SOMETHING以在Heroku上運行。
我對ReST有所了解(我仍然想學習并練習),所以我決定我的第一個應(yīng)用程序?qū)⑹鞘褂肑ersey (JAX-RS實現(xiàn))的簡單問候世界。 因此,我在GitHub上啟動了一個項目 ,并開始設(shè)置Heroku CLI。
設(shè)置Heroku CLI
Heroku現(xiàn)在很容易設(shè)置。 我記得什么時候需要Ruby env和我的第一個
與Ruby的接觸并不是那么好(沒有任何安裝程序,所以都是手動的-而且我很懶),所以我當時放棄了Heroku。 但是現(xiàn)在安裝它變得輕而易舉–只需轉(zhuǎn)到Heroku Toolbelt并為您的平臺下載版本。 我現(xiàn)在已經(jīng)在Linux Mint和Windows 7上都進行了設(shè)置,并且效果很好。
為Heroku設(shè)置項目
我的項目稱為摘要 -應(yīng)該是另一個票務(wù)管理系統(tǒng)。 但這暫時不相關(guān)。 最重要的是,為了讓Heroku發(fā)現(xiàn)我們的應(yīng)用程序是Java應(yīng)用程序,必須存在pom.xml文件。 這是因為Heroku使用Maven 3來構(gòu)建Java應(yīng)用程序。
因此,要真正開始使用Heroku進行任何工作,您需要一個簡單的pom.xml文件。 就我而言,我為該應(yīng)用程序添加了一個單獨的模塊,因此我的主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模塊,僅用于拆分項目:
<?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>我最初運行示例項目的嘗試集中在設(shè)置Jersey。 在檢查了文檔之后,我決定使用Grizzly2 HTTP服務(wù)器,只是因為它很容易設(shè)置。 我基本上已經(jīng)將文檔教程粘貼到Main類中。 有一些必要的區(qū)別,因為例如服務(wù)器的端口是由Heroku動態(tài)分配的。 因此,經(jīng)過很少的更改后,生成的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();}} }這將啟動服務(wù)器并向其中注冊一些資源。
一些灰熊的把戲
首先,GrizzlyServerFactory.createHttpServer方法接受一個必須以女巫模式名稱開頭的URI,在本例中為http://。 然后,它必須指定主機名,首先我將其設(shè)置為herokuapp.com上的應(yīng)用程序名稱。 這沒有用,但是Heroku很好地告訴了我:日志中有一條通知,指出服務(wù)器應(yīng)綁定到0.0.0.0,因此我將URI更改為http://0.0.0.0。
其次,Jersey示例等待按鍵以終止服務(wù)器。 不幸的是,Heroku打印了一條消息,然后以某種方式將該消息傳遞給應(yīng)用程序,并且服務(wù)器被終止。 為了解決這個問題,我將System.in.read()包裹在一個無限的while循環(huán)中。
這當然不是最好的解決方案,但是它起作用了,或者看起來如此。 幾個小時后,我檢查了應(yīng)用程序的日志,他們說應(yīng)用程序從上到下運行。 因此,我決定從Grizzly切換到Jetty,但這與本文無關(guān)。
在將所有內(nèi)容推送到Heroku之前,我還添加了一個Procfile:
web: java -cp webmodule/target/classes:webmodule/target/dependency/* com.github.pbuda.recaps.Main在推送到Heroku之后,該應(yīng)用程序被構(gòu)建并啟動,并請求http://growing-dawn-9158.herokuapp.com/helloworld產(chǎn)生了一些輸出(在這種情況下為簡單的“消息”消息)。 做得好。
我犯的錯誤并從中吸取教訓(xùn)
首先,我忘了添加Maven Dependency插件,但是在推送到Heroku之前我解決了這個問題。 沒有配置它,我就無法將依賴項添加到classpath中,從而導(dǎo)致ClassNotFound異常。 起初我并沒有想到它是必需的,但是隨后我查看了Heroku的示例并輕松地對其進行了修復(fù)。
其次,我不知道網(wǎng)絡(luò)測功機超時。 成功部署后,我確定該應(yīng)用程序正在運行,但是由于超時,日志顯示該應(yīng)用程序已關(guān)閉。 因為我不知道網(wǎng)絡(luò)測功機超時的事實,所以我懷疑Grizzly只是被某種方式打斷了,所以我決定搬到Jetty。 但這也發(fā)生在Jetty的實現(xiàn)上,因此我開始進行挖掘并找到了相關(guān)信息。
摘要
我認為Heroku很棒。 它是免費的Java托管,雖然并不流行,但他們做到了,而且效果很好。 我曾經(jīng)嘗試過Google App Engine,但是體驗并不是很好(請注意您已經(jīng)有很長時間了),所以我決定給Heroku一個機會,因為設(shè)置應(yīng)用程序?qū)嶋H上非常簡單,我想堅持一會兒并使用該平臺–查看所有這些插件
翻譯自: https://www.javacodegeeks.com/2013/05/heroku-and-java-from-newbie-to-beginner-part-1.html
java heroku
總結(jié)
以上是生活随笔為你收集整理的java heroku_Heroku和Java –从新手到初学者,第1部分的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: flac安卓车载能播放吗(flac安卓)
- 下一篇: ddos攻击的过程(ddos过程描述故事