Moving a Java application to Web PaaS

Last updated 2nd June 2022

Objective

It is common to have a Java application that you want to migrate to Web PaaS. Web PaaS supports several styles of Java application, such as monolith, microservices, stateful, and stateless.

Minimum Requirement

To run a Java application at Web PaaS you will need:

A container application can't be bigger than 8 GB of memory. For more details, see tuning.

Monolith/Single Application

To start a Java application, you need to understand the Web PaaS structure. At minimum, you to configure your application and have two YAML files (though they can be blank if you don't need them):

Application

# .platform.app.yaml

name: app
type: "java:<version>" [1]
disk: 1024
hooks:
    build: [2]
web:
    commands:
        start: [3]

1. A Java version, e,g.: java:11

2. Hooks define what happens when building the application. This build process typically generates an executable file such as a uber-jar e.g.: mvn clean package

3. The commands key defines the command to launch the application. E.g.: java -jar file.jar

4. In the start's command needs to receive the port where the application will execute thought the PORT environment. That's best when your app follows the port bind principle. E.g.: java -jar jar --port=$PORT

Be aware that after the build, it creates a read-only system. You have the mount option to create a writable folder.

Route

# .platform/routes.yaml

"https://{default}/":
    type: upstream
    upstream: [1]
"https://www.{default}/":
    type: redirect
    to: "https://{default}/"

1. It defines the application will link in the route, e.g.: "app:http"

Application instances have a limited amount of memory, the size of which depends on your plan and configuration. The largest available is 8 GB. A stateless application can be scaled horizontally to multiple application instances using Varnish in a load balancer configuration.

Microservices

You have the option to use several languages in microservices. If you're using Java there are several options to aggregate these services into a microservices:

Web PaaS supports multiple applications and there are two options:

  • One application YAML file to each application
  • Aggregate all applications in a single file with an applications.yaml file
Article Content
Microservices in the cloud, part two Source
Microservices in the cloud, part one Source
Multiple Applications Source
Configure multi-applications with applications.yaml Source

As a single application, in the multi-app, you have the option to set load balancer to some or all applications in the project cluster.

Access to services included at Web PaaS

Web PaaS has services managed by Web PaaS itself such as database, cache and search engine. However, you can use a database or any services such as a transition process, just be aware of the firewall.

When applications need to access a service, it is important to include the Relationships key, because. by default an application may not talk to any other container within a project it includes others projects as a microservices architecture.

To connect to a service from your deployed application, you will need to pass the relationships information into your application's configuration. The way to do so varies with the application. The most common mechanisms are listed below.

Overwrite

If you are using a framework that follows the Twelve-Factor App methodology, particularly the third point, you will be able to configure the application directly from environment variables. Examples of such frameworks include Spring, Eclipse MicroProfile Config, Quarkus, and Micronauts.

The services information is available in the PLATFORM_RELATIONSHIPS environment variable. This variable is a base64-encoded JSON object with keys of the relationship name and values of arrays of relationship endpoint definitions.

Web PaaS supports the jq tool, which allows to extract information from this JSON.

export DB_HOST=`echo $PLATFORM_RELATIONSHIPS | base64 --decode | jq -r ".database[0].host"`
Article Source
Spring Data MongoDB Source
Jakarta EE/MicroProfile Config Source
Spring Data JPA Source
Payara JPA Source

To reduce the number of lines in the application file and to make it cleaner, you have the option to move the variable environment to another file: a .environment file.

E.g.:

export DB_HOST=`echo $PLATFORM_RELATIONSHIPS | base64 --decode | jq -r ".database[0].host"`
export DB_PASSWORD=`echo $PLATFORM_RELATIONSHIPS | base64 --decode | jq -r ".database[0].password"`
export DB_USER=`echo $PLATFORM_RELATIONSHIPS | base64 --decode | jq -r ".database[0].username"`
export DB_DATABASE=`echo $PLATFORM_RELATIONSHIPS | base64 --decode | jq -r ".database[0].path"`
export JDBC=jdbc:postgresql://${HOST}/${DATABASE}
export JAVA_MEMORY=-Xmx$(jq .info.limits.memory /run/config.json)m
export JAVA_OPTS="$JAVA_MEMORY -XX:+ExitOnOutOfMemoryError"

This .environment can interact to each application file. E.g.:

name: app
type: "java:11"
disk: 1024
hooks:
    build: ./mvnw package -DskipTests -Dquarkus.package.uber-jar=true
relationships:
    database: "db:postgresql"
web:
    commands:
        start: java -jar $JAVA_OPTS $CREDENTIAL -Dquarkus.http.port=$PORT jarfile.jar

Using Java Config Reader

If your framework does not support configuration via environment variables, there is the Web PaaS Config Reader.This library provides a streamlined way to interact with a Web PaaS environment. It offers utility methods to access routes and relationships more cleanly than reading the raw environment variables yourself. See the maven dependency.

import Config;

Config config = new Config();
MySQL database = config.getCredential("database", MySQL::new);
DataSource dataSource = database.get();

Did you find this guide useful?

Please feel free to give any suggestions in order to improve this documentation.

Whether your feedback is about images, content, or structure, please share it, so that we can improve it together.

Your support requests will not be processed via this form. To do this, please use the "Create a ticket" form.

Thank you. Your feedback has been received.


These guides might also interest you...

OVHcloud Community

Access your community space. Ask questions, search for information, post content, and interact with other OVHcloud Community members.

Discuss with the OVHcloud community

In accordance with the 2006/112/CE Directive, modified on 01/01/2015, prices incl. VAT may vary according to the customer's country of residence
(by default, the prices displayed are inclusive of the UK VAT in force).