天天看点

在OpenShift上运行锡兰

今年,我们发布了三种在OpenShift上运行Ceylon代码的方式:

  • 使用锡兰弹药筒的准骨,
  • 使用Vert.x墨盒在Ceylon中写一个顶点,或者
  • 将您的Ceylon应用程序打包为

    .war

    文件,然后在WildFly墨盒上运行它。

在本文中,我们将介绍如何使用OpenShift Ceylon墨盒在OpenShift Online上编写和发布准系统应用程序。 Vert.x和WildFly方法将在以后的博客文章中介绍。

OpenShift Ceylon墨盒用于OpenShift V2。 是的,我知道它很旧,因为现在是V3,但是OpenShift的在线版本仍然是V2,因此它仍然很重要。 我们也在研究V3碳粉盒,应该很快就淘汰了。

用Ceylon编写准系统的Web应用程序

让我们从创建一个新的Ceylon项目开始:

$ ceylon new hello-world ceylon-blog-openshift
Enter module name [com.example.helloworld]: openshift.bare  
Enter module version [1.0.0]: 1
Would you like to generate Eclipse project files? (y/n) [y]: n
Would you like to generate an ant build.xml? (y/n) [y]: n
$ cd ceylon-blog-openshift
           

现在编译并运行它以检查所有内容是否都在控制之下:

$ ceylon compile
Note: Created module openshift.bare/1
$ ceylon run openshift.bare/1
Hello, World!
           

现在,通过使用

ceylon.net

模块并调整其文档代码示例 ,使其启动HTTP服务器。

首先将该模块导入

source/openshift/bare/module.ceylon

native("jvm")
module openshift.bare "1" {
  import ceylon.net "1.2.0";
}
           

然后在

source/openshift/bare/run.ceylon

使用它:

import ceylon.io { SocketAddress }
import ceylon.net.http.server { ... }

shared void start(String host, Integer port){
    //create a HTTP server
    value server = newServer {
        //an endpoint, on the path /hello
        Endpoint {
            path = startsWith("/");
            //handle requests to this path
            service(Request request, Response response)
                    => response.writeString("hello world");
        }
    };
    //start the server
    server.start(SocketAddress(host, port));
}

shared void run(){
    start("127.0.0.1", 8080);
}
           

让我们运行它:

$ ceylon compile
Note: Created module openshift.bare/1
$ ceylon run openshift.bare/1
Starting on 127.0.0.1:8080
Debug: XNIO version 3.3.0.Final 
Debug: XNIO NIO Implementation Version 3.3.0.Final 
Httpd started.
           

并在

http://localhost:8080

本地尝试,它应该显示一个带有

hello world

的网页。

调整我们的应用程序以在OpenShift上运行

现在,通过使用

ceylon.openshift

模块查看我们是否在OpenShift上运行,并使其绑定到正确的地址,使它适应于在OpenShift上由OpenShift指定主机名和端口的情况。

首先将OpenShift模块导入

source/openshift/bare/module.ceylon

native("jvm")
module openshift.bare "1" {
  import ceylon.net "1.2.0";
  import ceylon.openshift "1.2.0";
}
           

并在

source/openshift/bare/run.ceylon

使用它:

import ceylon.openshift { openshift }
import ceylon.io { SocketAddress }
import ceylon.net.http.server { ... }

shared void start(String host, Integer port){
    //create a HTTP server
    value server = newServer {
        //an endpoint, on the path /hello
        Endpoint {
            path = startsWith("/");
            //handle requests to this path
            service(Request request, Response response)
                    => response.writeString("hello world");
        }
    };
    //start the server
    server.start(SocketAddress(host, port));
}

shared void run(){
    if(openshift.running){
        start(openshift.ceylon.ip, openshift.ceylon.port);
    }else{
        start("127.0.0.1", 8080);
    }
}
           

因此,它现在可以像以前一样在本地运行,也可以在OpenShift中运行。

配置我们的OpenShift Ceylon墨盒应用程序

让我们创建所需的OpenShift结构,以告诉OpenShift Ceylon墨盒如何运行我们的模块。 我们通过安装OpenShift Ceylon命令行插件来做到这一点:

$ ceylon plugin install ceylon.openshift/1.2.0
Scripts for ceylon.openshift installed in /home/stephane/.ceylon/bin/ceylon.openshift
           

现在我们运行它:

$ ceylon openshift init openshift.bare/1
Installing file .openshift/config/ceylon.properties: Generated
...
           

对于那些想要了解更多信息或调整OpenShift Ceylon墨盒如何部署应用程序的人, 该文档提供了更多信息 。

我们的应用程序现在可以在OpenShift上运行了。

将我们的应用程序部署到OpenShift Online

现在,假设您已经有一个OpenShift Online帐户并安装了

rhc

命令 ,则可以继续使用Ceylon墨盒创建一个OpenShift应用程序:

$ rhc create-app --no-git -a test https://raw.github.com/ceylon/openshift-cartridge/master/metadata/manifest.yml
The cartridge 'https://raw.github.com/ceylon/openshift-cartridge/master/metadata/manifest.yml' will be downloaded and installed

Application Options
-------------------
Domain:     fromage
Cartridges: https://raw.github.com/ceylon/openshift-cartridge/master/metadata/manifest.yml
Gear Size:  default
Scaling:    no

Creating application 'test' ... done


Waiting for your DNS name to be available ... done

Your application 'test' is now available.

  URL:        http://test-fromage.rhcloud.com/
  SSH to:     [email protected]
  Git remote: ssh://[email protected]/~/git/test.git/

Run 'rhc show-app test' for more details about your app.
           

这在OpenShift Online上创建了我们的应用程序,并为我们提供了可以访问它的URL(

http://test-fromage.rhcloud.com/

),以及可以在其中推送应用程序的Git存储库(

ssh://[email protected]/~/git/test.git/

)。

现在,我们只需要将我们的应用程序转换为一个Git存储库,并添加

rhc

给我们的

openshift

远程Url,就在上面:

$ git init
Initialised empty Git repository in /home/stephane/src/java-eclipse/ceylon-blog-openshift/.git/
$ git remote add openshift ssh://[email protected]/~/git/test.git/
           

Ceylon OpenShift弹药筒包含一个演示示例应用程序,我们可以通过将当前应用程序推送到OpenShift来摆脱它:

$ git add source .openshift
$ git commit -m "Initial commit"
...
$ git push -f openshift master
Counting objects: 23, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (18/18), done.
Writing objects: 100% (23/23), 3.79 KiB | 0 bytes/s, done.
Total 23 (delta 1), reused 0 (delta 0)
remote: Stopping Ceylon cart
remote: Application is already stopped
remote: Repairing links for 1 deployments
remote: Building git ref 'master', commit 58ab35c
remote: 
remote: Building Ceylon app...
remote: Compiling every module in /var/lib/openshift/../app-root/runtime/repo//source for the JVM:
remote: Note: Created module openshift.bare/1
remote: Ceylon build done.
remote: Preparing build for deployment
remote: Deployment id is ...
remote: Activating deployment
remote: TODO
remote: Starting Ceylon cart
remote: Executing /var/lib/openshift/.../ceylon/usr/ceylon-1.2.0/bin/ceylon
remote: With params: run   --rep=/var/lib/openshift/.../app-root/runtime/repo/.openshift/config/modules --cacherep=/var/lib/openshift/.../app-root/runtime/repo//cache --rep=http://modules.ceylon-lang.org/repo/1/ --rep=/var/lib/openshift/.../app-root/runtime/repo//modules openshift.bare/1 
remote: With JAVA_OPTS:  -Dcom.redhat.ceylon.common.tool.terminal.width=9999 -Dceylon.cache.repo=/var/lib/openshift/.../app-root/runtime/repo//cache
remote: Ceylon started with pid: 350715
remote: Waiting for http server to boot on 127.5.184.1:8080 ... (1/30)
remote: Waiting for http server to boot on 127.5.184.1:8080 ... (2/30)
remote: Waiting for http server to boot on 127.5.184.1:8080 ... (3/30)
remote: Waiting for http server to boot on 127.5.184.1:8080 ... (4/30)
remote: Found 127.5.184.1:8080 listening port
remote: 
remote: -------------------------
remote: Git Post-Receive Result: success
remote: Activation status: success
remote: Deployment completed with status: success
To ssh://[email protected]/~/git/test.git/
   2a29bdf..58ab35c  master -> master
           

就是这样,您现在可以在

http://test-fromage.rhcloud.com/

在线检查您的应用程序。

恭喜你!

现在,您还可以在GitHub或其他地方在线发布代码,并且每次将您的修改推送到

openshift

遥控器时,您的应用程序都会随着

openshift

的更改重新启动。

请继续关注此博客上的Vert.x和WildFly Ceylon OpenShift部署指南。

翻译自: https://www.javacodegeeks.com/2015/11/running-ceylon-on-openshift.html