Heroku和Java –从新手到初学者,第1部分

最近,我听说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 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>

我运行我的示例项目的第一次尝试集中在设置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.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/369076.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【谈谈IO】BIO、NIO和AIO

BIO: BIO是阻塞IO&#xff0c;体现在一个线程调用IO的时候&#xff0c;会挂起等待&#xff0c;然后Thread会进入blocked状态&#xff1b;这样线程资源就会被闲置&#xff0c;造成资源浪费&#xff0c;通常一个系统线程数是有限的&#xff0c;而且&#xff0c;Thread进入内核态也…

css动画-模拟正余弦曲线

今天就写一个css3抛物线的动画吧 从左到右的抛物线动画&#xff0c;我们就暂且把动作分为匀速向右运动和变速的上下运动。 水平匀速运动我们可以利用 translateX(x)&#xff1a;定义 2D 转换&#xff0c;沿着 X 轴移动元素&#xff1b;以及linear&#xff1a;动画从头到尾的速…

UVA-11549 Calculator Conundrum

InputThe first line of the input contains an integer t (1 ≤ t ≤ 200), the number of test cases. Each test casecontains two integers n (1 ≤ n ≤ 9) and k (0 ≤ k < 10 n ) where n is the number of digits this calculatorcan display k is the starting num…

java 当前类_Java获取当前类名的两种方法

适用于非静态方法&#xff1a;this.getClass().getName()适用于静态方法&#xff1a;Thread.currentThread().getStackTrace()[1].getClassName()获取类名&#xff1a;1、在类的实例中可使用this.getClass().getName();但在static method中不能使用该方法&#xff1b;2、在stat…

具有内部类构造函数参数的Java Reflection奇数

关于Java内部类 Java允许成员类&#xff08;在其他类内定义的类&#xff09;&#xff0c;局部类&#xff08;在语句块内定义的类&#xff09;和匿名类&#xff08;无名称的类&#xff09;&#xff1a; class Outer {Object anonymous new Object(){}; // this is an anonymou…

HDOJ 1012-1020

最近感冒了&#xff0c;有点小咳嗽&#xff0c;做题速度比较慢&#xff0c;本以为这周会做的比较少&#xff0c;没想到全是水题。。。我做的也蛮开心的....对自己无语HDOJ 1012这个题目蛮简单&#xff0c;就是输出格式比较烦&#xff0c;处理好格式基本就没问题了HDOJ 1013这个…

静态页面如何实现 include 引入公用代码

一直以来&#xff0c;我司的前端都是用 php 的 include 函数来实现引入 header 、footer 这些公用代码的&#xff0c;就像下面这样&#xff1a; <!-- index.php --><!DOCTYPE html><html lang"en"><head><meta charset"UTF-8"&…

java list 循环赋值_Java List集合的坑(add方法报空指针,循环赋值时list已保存的值会改变)...

先看空指针异常&#xff1a;ListmovieInfos null;这样创建时&#xff0c;list指向为空&#xff0c;修改方法&#xff1a;ListmovieInfos new ArrayList();再看list循环赋值的问题&#xff1a;问题描述&#xff1a;for (i0;i<10;i){movieInfoSum.movieId (int)recommendatio…

ros使用时的注意事项技巧

1.rosrun package-name executable-name 比如 rosrun turtlesim turtlesim_node 2.一旦启动roscore后,便可以运行ROS程序了。ROS程序的运行实例被称为节点(node)&#xff0c;roscore叫做节点管理器 3.查看节点列表rosnode list 4.需要注意节点名并不一定与对应可执行文件名称相…

分享几道经典的javascript面试题

这几道题目还是有一点意思的&#xff0c;大家可以研究一番&#xff0c;对自己的技能提升绝对有帮助。 1、调用过程中输出的内容是什么 function fun(n, o) {console.log(o);return {fun : function(m) {return fun(m, n);}} }var a fun(0);a.fun(1);a.fun(2);a.fun(3);var…

Rete之外的生活– RIP Rete 2013 :)

我只是对我的新算法进行最后的修改。 它合并了Leaps &#xff0c; 面向集合的Match和Left / Right取消链接的概念 &#xff0c;以及我自己的一些想法。 该代码已提交&#xff0c;但我正在积累工作并编写更多测试。 我将在一周左右的时间内写一个完整的博客&#xff0c;详细介绍…

Ubuntu+vscode打不开

前沿: vscode链接参考链接 问题: 之前在Ubuntu上安装chrome, 结果chrome没装成功, 还把vscode给qiu坏了, 貌似是当时安装chrome时提示要升级一个包. 后来发现当时是修改了libnss这个包的版本: 解决方法: # 将libnss给downgrade一下就OK了. sudo apt install libnss32:3.21-1ubu…

java线程死锁代码_java线程死锁代码示例

死锁是操作系统层面的一个错误&#xff0c;是进程死锁的简称&#xff0c;最早在 1965 年由 Dijkstra 在研究银行家算法时提出的&#xff0c;它是计算机操作系统乃至整个并发程序设计领域最难处理的问题之一。事实上&#xff0c;计算机世界有很多事情需要多线程方式去解决&#…

【2018】判定三角形

Time Limit: 3 second Memory Limit: 2 MB 输入三个正整数&#xff0c;若能用这三个数作为边长组成三角形&#xff0c;则判断三角形的形状&#xff0c;如果是等边三角形就输出“DB”&#xff0c;如果是等腰三角形就输出“DY”&#xff0c;否则就输出“YB”&#xff1b;如果不能…

纯css隐藏移动端滚动条解决方案(ios上流畅滑动)

html代码展示&#xff08;直接复制代码保存至本地文件运行即可&#xff09;&#xff1a; <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, init…

Spring Data Solr教程:配置

在我的Spring Data Solr教程的上一部分中&#xff0c;我们了解到Solr提供了一个类似REST的HTTP API&#xff0c;该API可用于向Solr索引添加信息并针对索引数据执行查询。 问题在于&#xff0c;在开发环境中运行单独的Solr实例有点麻烦。 但是&#xff0c;并非所有希望都因此而…

JavaAppArguments.java

思路&#xff1a;定义一个String类型的变量&#xff0c;用来接收每次args读取到的值&#xff0c;然后把String转换为int,然后计算输出sum。 流程图&#xff1a; 源代码&#xff1a; 截图&#xff1a; 转载于:https://www.cnblogs.com/xiaohaigege666/p/7635907.html

webpack 引入jquery和第三方jquery插件

1、引入jquery jQuery 直接在 html 中引入&#xff0c;然后在 webpack 中把它配置为全局即可。 index.html: <!DOCTYPE html><html><head><meta charset"UTF-8"><title><% htmlWebpackPlugin.options.title %></title>&…

Spring Data Solr教程:CRUD(差不多)

在我的Spring Data Solr教程的上一部分中&#xff0c;我们学习了如何配置Spring Data Solr。 现在该迈出一步&#xff0c;了解我们如何管理Solr实例中存储的信息。 此博客文章描述了我们如何向Solr索引添加新文档&#xff0c;如何更新现有文档的信息以及如何从索引中删除文档。…

数独项目--关键代码展示:

关键代码展示&#xff1a; //判断该数字在当前数独是否符合要求 int judge(int num, int ple){ int x ple / 9; //x表示数字的纵坐标 int y ple % 9; //y表示数字的横坐标 int qulx x / 3; int quey y / 3; //que表示9宫格的区域 for (int i 0; i < 9; i){ if (…