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

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 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一个机会,因为设置应用程序实际上非常简单,我想坚持一会儿并使用该平台–查看所有这些插件


翻译自: https://www.javacodegeeks.com/2013/05/heroku-and-java-from-newbie-to-beginner-part-1.html

java heroku

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值