It is common to have a Java application that you want to migrate to Upsun. Upsun supports several styles of Java application, such as monolith, microservices, stateful, and stateless.
Minimum Requirement
To run a Java application at Upsun you need:
- A supported Java version
- A build management tool
- A Git Repository:
Monolith/Single Application
To start a Java application, you need to understand the Upsun structure. You will need to configure your application, routes, and services.
Application
- A Java version, e,g.:
java:<VERSION>. - Hooks define what happens when building the application. This build process typically generates an executable file such as a uber-jar. For example,
mvn clean package. - The commands key defines the command to launch the application. For example,
java -jar file.jar. - In the start’s command needs to receive the port where the application will execute thought the
PORTenvironment. That’s best when your app follows the port bind principle. For example,java -jar jar --port=$PORT.
Route
- It defines the application will link in the route. For example,
"myapp:http".
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:
Upsun supports multiple applications and there are two options:
- One application YAML file to each application
- Aggregate all applications in a single file with an
.upsun/config.yamlfile
| Article | Content |
|---|---|
| Microservices in the cloud, part two | Source |
| Microservices in the cloud, part one | Source |
| Multiple Applications | Source |
Configure multi-applications with .upsun/config.yaml | Source |
Access to managed services
Upsun provides managed services such as databases, cache, and search engines.
However, you can use an external database or other services during a transition process. To maintain security, you must ensure that your external service (e.g., your external database or a third-party API)
is configured to accept connections from Upsun’s outbound IP addresses.
When applications need to access a managed service, it is important to include the relationships key.
By default an application may not talk to any other container without a relationship explicitly allowing access.
The most common mechanisms for connecting to a managed service are listed below.
Overwrite
If you are using a framework that follows the Twelve-Factor App methodology, particularly the third point, you can configure the application directly from environment variables.
Examples of such frameworks include Spring, Eclipse MicroProfile Config, Quarkus, and Micronauts.
Service credentials are available within the service environment variables, or the PLATFORM_RELATIONSHIPS environment variable.
Service environment variables
`PLATFORM_RELATIONSHIPS` environment variable
Assuming the relationship postgresql is configured to grant access to a PostgreSQL service container, you can map the automatically generated environment variable (POSTGRESQL_HOST) to whatever your application expects to use:
.environment
This sets environment variables with the names your app needs, and the values from service environment variables.
This variable is a base64-encoded JSON object with keys of the relationship name and values of arrays of relationship endpoint definitions.Upsun supports the jq tool, which allows to extract information from this JSON.
.environment
This sets environment variables with names your app needs and the values from PLATFORM_RELATIONSHIPS environment variable.
| 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.
Example:
You can obtain relationship information through the service environment variables themselves,
or through the PLATFORM_RELATIONSHIPS environment variable.
Say your application has a relationship named postgresql to a database service named postgresql:
Service environment variables
`PLATFORM_RELATIONSHIPS` environment variable
.environment
This sets environment variables with the names your app needs, and the values from service environment variables.
This PLATFORM_RELATIONSHIPS variable is a base64-encoded JSON object with keys of the relationship name and values of arrays of relationship endpoint definitions.Upsun supports the jq tool, which allows to extract information from this JSON.
.environment
This sets environment variables with names your app needs and the values from PLATFORM_RELATIONSHIPS environment variable.
This .environment file can interact to each application file.
Example: