如何在 Openshift 上部署 war 和应用程序

How to deploy a war and an application on Openshift

我正在按照 this 教程在 Openshift 应用程序上安装 Geoserver。问题是对于 Geoserver (a .war) 到 运行,我必须从应用程序中删除 src 文件夹和 pom.xml,但我也需要在那里开发一个应用程序.

那么,如何在拥有 WAR 运行ning 的同时维护 pom.xml 和 src 文件夹(我真的需要删除这些东西才能获得 war运行宁)?有可能吗?

使用 src 文件夹和 pom.xml 我收到了来自 Openshift 的回复:

remote: [INFO] ----------    --------------------------------------------------------------
remote: [INFO] Building test 1.0
remote: [INFO] ------------------------------------------------------------------------
remote: [INFO]
remote: [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ test ---
remote: [INFO]
remote: [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ test ---
remote: [debug] execute contextualize
remote: [INFO] Using 'UTF-8' encoding to copy filtered resources.
remote: [INFO] Copying 1 resource
remote: [INFO]
remote: [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ test ---
remote: [INFO] Nothing to compile - all classes are up to date
remote: [INFO]
remote: [INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ test ---
remote: [debug] execute contextualize
remote: [INFO] Using 'UTF-8' encoding to copy filtered resources.
remote: [INFO] skip non existing resourceDirectory /var/lib/openshift/56f18c457628e1639500003d/app-root/runtime/repo/src/test/resources
remote: [INFO]
remote: [INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ test ---
remote: [INFO] No sources to compile
remote: [INFO]
remote: [INFO] --- maven-surefire-plugin:2.10:test (default-test) @ test ---
remote: [INFO] Tests are skipped.
remote: [INFO]
remote: [INFO] --- maven-war-plugin:2.1.1:war (default-war) @ test ---
remote: [INFO] Packaging webapp
remote: [INFO] Assembling webapp [test] in [/var/lib/openshift/56f18c457628e1639500003d/app-root/runtime/repo/target/test]
remote: [INFO] Processing war project
remote: [INFO] Copying webapp resources [/var/lib/openshift/56f18c457628e1639500003d/app-root/runtime/repo/src/main/webapp]
remote: [INFO] Webapp assembled in [93 msecs]
remote: [INFO] Building war: /var/lib/openshift/56f18c457628e1639500003d/app-root/runtime/repo/webapps/ROOT.war
remote: [INFO] WEB-INF/web.xml already added, skipping
remote: [INFO] ------------------------------------------------------------------------
remote: [INFO] BUILD SUCCESS
remote: [INFO] ------------------------------------------------------------------------
remote: [INFO] Total time: 4.278s
remote: [INFO] Finished at: Tue Mar 22 14:31:40 EDT 2016
remote: [INFO] Final Memory: 8M/111M
remote: [INFO] ------------------------------------------------------------------------
remote: Preparing build for deployment
remote: Deployment id is a3cec3f4
remote: Activating deployment
remote: Starting Postgres cartridge
remote: Postgres started
remote: Emptying tmp dir: /var/lib/openshift/56f18c457628e1639500003d/jbossews//tmp/Geotools
remote: Starting jbossews cartridge
remote: Found 127.3.185.129:8080 listening port
remote: -------------------------
remote: Git Post-Receive Result: success
remote: Activation status: success
remote: Deployment completed with status: success

我的 pom.xml 是 Openshift 提供的默认 pom.xml:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <name>test</name>
    <repositories>
        <repository>
            <id>eap</id>
            <url>http://maven.repository.redhat.com/techpreview/all</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>eap</id>
            <url>http://maven.repository.redhat.com/techpreview/all</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.2-1003-jdbc4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
        </dependency>     
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <profiles>
        <profile>
            <!-- When built in OpenShift the 'openshift' profile will be used when 
                invoking mvn. -->
            <!-- Use this profile for any OpenShift specific customization your app 
                will need. -->
            <!-- By default that is to put the resulting archive into the 'webapps' 
                folder. -->
            <!-- http://maven.apache.org/guides/mini/guide-building-for-different-environments.html -->
            <id>openshift</id>
            <build>
                <finalName>test</finalName>
                <plugins>
                    <plugin>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>2.1.1</version>
                        <configuration>
                            <outputDirectory>webapps</outputDirectory>
                            <warName>ROOT</warName>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

Posdata:我知道如何在其自己的上下文中将 war 变为 运行(例如 http://test-testdomain.rhcloud.com/war/)。

更新:这是我的rhc tail -a geoserver

==> app-root/logs/postgresql.log <==
2016-03-23 12:51:43 GMT LOG:  shutting down
2016-03-23 12:51:43 GMT LOG:  database system is shut down
2016-03-23 12:52:01 GMT LOG:  could not bind socket for statistics collector: Permission denied
2016-03-23 12:52:01 GMT LOG:  trying another address for the statistics collector
2016-03-23 12:52:01 GMT LOG:  could not bind socket for statistics collector: Cannot assign requested address
2016-03-23 12:52:01 GMT LOG:  disabling statistics collector for lack of working socket
2016-03-23 12:52:01 GMT WARNING:  autovacuum not started because of misconfiguration
2016-03-23 12:52:01 GMT HINT:  Enable the "track_counts" option.
2016-03-23 12:52:01 GMT LOG:  database system was shut down at 2016-03-23 12:51:43 GMT
2016-03-23 12:52:01 GMT LOG:  database system is ready to accept connections

==> app-root/logs/jbossews.log <==
Mar 23, 2016 8:52:49 AM org.geoserver.GeoserverInitStartupListener contextDestroyed
INFO: Cleaned up javabean caches
Mar 23, 2016 8:52:50 AM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
SEVERE: The web application [/geoserver] registered the JDBC driver [org.hsqldb.jdbc.JDBCDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
Mar 23, 2016 8:52:50 AM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deployment of web application archive /var/lib/openshift/56f28b760c1e66c08b000209/app-root/runtime/dependencies/jbossews/webapps/geoserver.war has finished in 40,390 ms
Mar 23, 2016 8:52:50 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-127.3.223.1-8080"]
Mar 23, 2016 8:52:50 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 44333 ms

此外,我检查了 ${OPENSHIFT_JBOSSEWS_DIR}webapps,我有 geoserver.warROOT.war.

更新 2: 在 touch 命令之后,我再次 rhc til -a geoserver 并得到:

==> app-root/logs/postgresql.log <==
2016-03-23 14:46:22 GMT LOG:  shutting down
2016-03-23 14:46:22 GMT LOG:  database system is shut down
2016-03-23 14:46:40 GMT LOG:  could not bind socket for statistics collector: Permission denied
2016-03-23 14:46:40 GMT LOG:  trying another address for the statistics collector
2016-03-23 14:46:40 GMT LOG:  could not bind socket for statistics collector: Cannot assign requested address
2016-03-23 14:46:40 GMT LOG:  disabling statistics collector for lack of working socket
2016-03-23 14:46:40 GMT WARNING:  autovacuum not started because of misconfiguration
2016-03-23 14:46:40 GMT HINT:  Enable the "track_counts" option.
2016-03-23 14:46:40 GMT LOG:  database system was shut down at 2016-03-23 14:46:22 GMT
2016-03-23 14:46:40 GMT LOG:  database system is ready to accept connections

==> app-root/logs/jbossews.log <==
Mar 23, 2016 10:47:54 AM org.geoserver.GeoserverInitStartupListener contextDestroyed
INFO: Cleaned up javabean caches
Mar 23, 2016 10:47:57 AM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
SEVERE: The web application [/geoserver] registered the JDBC driver [org.hsqldb.jdbc.JDBCDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
Mar 23, 2016 10:47:57 AM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deployment of web application archive /var/lib/openshift/56f28b760c1e66c08b000209/app-root/runtime/dependencies/jbossews/webapps/geoserver.war has finished in 68,308 ms
Mar 23, 2016 10:47:57 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-127.3.223.1-8080"]
Mar 23, 2016 10:47:57 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 72395 ms

Openshift 应用程序包含一个 git 存储库,用于将源代码存储为您创建的每个应用程序中的文件夹,在您推送到存储库后,如果您 ssh 进入您的应用程序,存储库是位于 $OPENSHIFT_REPO_DIR.

每次您推送到应用程序的存储库时,一系列脚本,在 Openshift 术语中名为 action hooks,是 运行。随附的动作挂钩因您选择的墨盒而异。

推入 java 应用程序服务器盒后发生的一件事是触发了 maven 构建,$OPENSHIFT_REPO_DIR 中的内容(src 文件夹和 pom.xml) 用于创建 WAR 工件,该工件也会自动部署到所选模块中包含的应用程序服务器上。

这基本上意味着您只需将源代码推送到存储库,Openshift 会处理所有事情。您根本不需要担心删除任何内容。事实上,如果删除存储库目录的内容,则没有任何内容可用于构建工件。

如果您选择了自己动手 (DIY) 盒式磁带,则您负责管理所有内容,不包括任何脚本或操作挂钩。


更新:可以将两者放在同一档位,但需要进行一些调整。

首先,您的应用程序,重要的一点是在您的 pom.xml 中,保留 <profile> 部分,至少 openshift 配置文件保持原样,因为在推送事件中,它将触发一个带有 openshift 配置文件的 maven 构建,这将生成一个 WAR 您的应用程序的工件,并在 webapps 目录中生成一个名为 ROOT.war 的文件。此文件将从存储库复制到 tomcat 部署目录。

现在关于地理服务器,在指南的存储库中,它为您提供文件 ROOT.war 作为地理服务器应用程序:

  1. 您需要将其重命名为其他名称,因为触发的 maven 构建将生成您的应用程序的 ROOT.war 文件,除非您修改 maven-war-plugin 的配置。

  2. 您需要将重命名的 war 文件复制到 tomcat 部署目录。为此,您需要使用 动作挂钩 。在这种情况下,deploy 阶段似乎是合适的阶段。

你需要在.openshift/action_hooks目录下创建一个文件,命名为deploy,并赋予它执行权限(chmod +x),并将其包含在版本控制中,这样就可以得到与您的应用程序一起启动。该文件必须包含一个脚本,其中包含将 geoserver WAR 文件复制到部署目录的说明,假设您将 WAR 重命名为 geoserver.war,并且位于 webapps 目录中,这脚本将执行:

#!/bin/bash

echo "Deploying Geoserver application..." 
cp ${OPENSHIFT_REPO_DIR}webapps/geoserver.war ${OPENSHIFT_JBOSSEWS_DIR}/webapps/

Update :我尝试按照指南重现问题并应用我提到的 tweeks,使用 spring 启动应用程序作为应用程序使用根上下文和地理服务器,但我根本没有发现任何问题。

我遵循的步骤是:

  1. 从指南中指定的存储库创建 Openshift 应用程序(tomcat7 + postgresql9.2 cartridges)
  2. 克隆存储库
  3. webapps/ROOT.war 重命名为 webapps/geoserver.war
  4. 创建一个 pom.xml 文件(参见下面的源代码)
  5. 在 src 文件夹中包含我的应用程序的源代码(只是一个 Spring 带有 RestController 的启动应用程序,用于 / 入口点说你好世界。
  6. 如上所述添加 deploy 动作挂钩
  7. 推送代码

pom.xml

    <modelVersion>4.0.0</modelVersion>

    <groupId>es.saljuama</groupId>
    <artifactId>geoserver</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>geoserver</name>
    <description>Geoserver demo project</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.7</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <!-- Using this profile for my local machine, just the app using and embedded tomcat, with devtools, since i'm not sure if you want to make your app interact with the geoserver or not. -->
            <id>local</id>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-devtools</artifactId>
                    <optional>true</optional>
                </dependency>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </dependency>
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>openshift</id>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                    <exclusions>
                        <exclusion>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-starter-tomcat</artifactId>
                        </exclusion>
                    </exclusions>
                </dependency>
                <dependency>
                    <groupId>javax.servlet</groupId>
                    <artifactId>javax.servlet-api</artifactId>
                    <version>3.0.1</version>
                    <scope>provided</scope>
                </dependency>
            </dependencies>
            <build>
                <finalName>geoserver</finalName>
                <plugins>
                    <plugin>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>2.6</version>
                        <configuration>
                            <outputDirectory>webapps</outputDirectory>
                            <warName>ROOT</warName>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>