NEW
Machine Learning on GPU is now avalaible with Clever Grid. Check it out   
Topics:

Welcome to this new edition of One Framework a Day keeps the Boredom Away. In this series I will show you how to deploy a particular framework on Clever Cloud every day until I want to go back to boredom. Today it's about Vert-x and Golo.

In each post of this series we'll see how to deploy a particular framework on Clever Cloud. Today we are taking a look at Vert-x and Golo.

If you want to tag along, make sure you have git, a Clever Cloud account and that you have installed our CLI Clever-Tools.

What is Vert-x?

Eclipse Vert.x is a tool-kit for building reactive applications on the JVM.

And when they write JVM, they mean it. You can use any JVM language. Beyond the polyglot statement, you will find out that it's also event driven and non blocking. Which means it's ready to scale and will use your hardware as much as possible. It's reactive. I invite you to read the Reactive Manifesto if you have no idea what it is. Vert-x is unopinionated and as such is a good fit for any kind of application or microservice. Here we will use it to build an API.

What is Golo

A lightweight dynamic language for the JVM.

You can install Golo with a standalone binary available on their website. It's important because today we will also look at how to create a Golo project from scrach instead of using an existing one. All of this is happening thanks to our CSO Philippe Charrière who previously blogged on this subject, check out his blog!

Create a Golo Project

In a terminal, type these commands:

golo new vertx.golo.demo --type maven
cd vertx.golo.demo

The Golo CLI has generated a Golo project:

.
├── pom.xml
├── src
│   └── main
│       └── golo
│           └── main.golo

To make sure you can compile the project, you have to update the pom.xml file: Change the Golo version by replacing

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <golo.version>3.2.0-SNAPSHOT</golo.version>
  </properties>

with:

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <golo.version>3.2.0</golo.version>
    <vertx.version>3.4.1</vertx.version>
  </properties>

Add some dependencies and the bintray repository:

  <dependencies>
    <dependency>
      <groupId>org.eclipse.golo</groupId>
      <artifactId>golo</artifactId>
      <version>${golo.version}</version>
    </dependency>

    <dependency>
      <groupId>com.googlecode.json-simple</groupId>
      <artifactId>json-simple</artifactId>
      <version>1.1.1</version>
    </dependency>

    <dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-core</artifactId>
      <version>${vertx.version}</version>
    </dependency>

    <dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-web</artifactId>
      <version>${vertx.version}</version>
    </dependency>

  </dependencies>

  <repositories>
    <repository>
      <id>bintray</id>
      <name>Bintray</name>
      <url>https://jcenter.bintray.com</url>
    </repository>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>bintray</id>
      <name>Bintray</name>
      <url>https://jcenter.bintray.com</url>
    </pluginRepository>
  </pluginRepositories>

We are going to write a very simple Vert-x web application with Golo. For that, replace the content of /src/main/golo/main.golo with this:

module vertx.golo.demo

import io.vertx.core.Vertx
import io.vertx.core.http.HttpServer
import io.vertx.ext.web.Router
import io.vertx.ext.web.handler

import gololang.JSON

let vertx = Vertx.vertx()

function main = |args| {

  let server = vertx: createHttpServer()
  let router = Router.router(vertx)
  router: route(): handler(BodyHandler.create())

  let port =  Integer.parseInt(System.getenv(): get("PORT") orIfNull "8080")

  router: get("/"): handler(|context| {
    context: response(): putHeader("content-type", "text/html;charset=UTF-8")
    context: response(): end("<h1>Hello 🌍</h1>", "UTF-8")
  })

  router: get("/hi"): handler(|context| {
    context: response(): putHeader("content-type", "application/json;charset=UTF-8")
    context: response(): end(JSON.stringify(DynamicObject(): message("Hi 😛")), "UTF-8")
  })

  server: requestHandler(|httpRequest| -> router: accept(httpRequest)): listen(port)

  println("listening on " + port)
}

To test the application:

  • Built it with: mvn package
  • Run it with: mvn exec:java
  • Test your web application by opening http://localhost:8080 and http://localhost:8080/hi

If it works locally then you are ready for the next step. Set it up for Clever Cloud.

Setup

Clever Cloud can run any JVM code packaged as a .war or .jar. You can either checkout those files directly in your Github repository or have them built with Maven or Gradle. In this case it's a Maven build that generates a .jar file from some Golo code. So when we create the application we have to select maven as type: clever create --type maven vertx-golo-sample

The only configurable thing is the port of the server. It defaults to 8080 in the code. It's also the case for each application running on Clever Cloud as it is required. So nothing to change here, we are ready to deploy.

Deploy

To make our Maven buikd work we need to state which build goal is needed and where is the .jar file to run. To do so create in your project directory a clevercloud directory with a jar.json file containing:

{
  "build": {
    "type": "maven",
    "goal": "package"
  },
  "deploy": {
    "jarName": "target/vertx.golo.demo-0.0.1-SNAPSHOT-jar-with-dependencies.jar"
  }
}

It configures your application to run mvn package then java -jar target/vertx.golo.demo-0.0.1-SNAPSHOT-jar-with-dependencies.jar.

The last step to deploy it is to initiate an empty git repository, add and commit your code, then deploy it:

git init
git add src/ pom.xml clevercloud/
git commit -m "first version"
clever deploy

What clever deploy does is pushing your code to a remote git branch that was created when you created the application in the setup step. Right now you should start seeing build logs in the console. If everything worked well run clever open. It will take you straight to your web application and display Hello 🌍!


Profile picture of Philippe Charriere
By Philippe Charriere