Hello World: Micronaut in the Cloud
Learn how to deploy a plain Micronaut—an open-source, JVM-based framework for building microservices and serverless applications—in the cloud with Platform.sh.
Join the DZone community and get the full member experience.
Join For FreeMicronaut is an open-source, JVM-based framework for building full-stack, modular, easily testable microservice and serverless applications.
Unlike reflection-based IoC frameworks that load and cache reflection data for every single field, method, and constructor in your code, with Micronaut, your application startup time and memory consumption are not bound to the size of your codebase.
Micronaut's cloud support is built right in, including support for common discovery services, distributed tracing tools, and cloud runtimes.
The first step is to create the application itself, and Micronaut has proper documentation. You have the start code link where you can define the dependencies that you need to write your application.
As soon as you generate the application it will create an HelloController
with the Hello world message.
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.reactivex.Single;
"/") (
public class HelloController {
uri = "/", produces = MediaType.TEXT_PLAIN) (
public Single<String> hello() {
return Single.just("Hello World! A simple Micronaut template for Platform.sh");
}
}
The last step in the code is a start-up application.
xxxxxxxxxx
import io.micronaut.runtime.Micronaut;
public class Application {
public static void main(String[] args) {
Micronaut.run(Application.class);
}
}
To execute locally, you run the maven package application and then execute the uber-jar.
x
mvn clean package
java -jar target/micronaut-1.0-SNAPSHOT.jar
Amazing, isn't it? But how about moving this application to the cloud? Let me introduce to you Platform.sh, it is a second-generation Platform-as-a-Service built especially for continuous deployment. It allows you to host web applications on the cloud while making your development and testing workflows more productive.
As a Platform as a Service, or PaaS, Platform.sh automatically manages everything your application needs in order to run. That means you can, and should, view your infrastructure needs as part of your application, and version-control it as part of your application.
To move your application to the cloud, briefly, you need two files:
- One Router (.platform/routes.yaml). Platform.sh allows you to define the routes.
xxxxxxxxxx
"https://{default}/":
type upstream
upstream"app:http"
"https://www.{default}/"
type redirect
to"https://{default}/"
- One or more application containers (.platform.app.yaml). You control your application and the way it will be built and deployed on Platform.sh via a single configuration file.
x
name app
type"java:8"
disk1024
hooks
build mvn clean package
web
commands
start java -Xmx$(jq .info.limits.memory /run/config.json)m -XX +ExitOnOutOfMemoryError -jar target/micronaut-1.0-SNAPSHOT.jar
The application is now ready, so it’s time to move it to the cloud with Platform.sh using the following steps:
- Create a new free trial account.
- Sign up with a new user and password, or login using a current GitHub, Bitbucket, or Google account. If you use a third-party login, you’ll be able to set a password for your Platform.sh account later.
- Select the region of the world where your site should live.
- Select the blank template.
You have the option to either integrate to GitHub, GitLab, or Platform.sh will provide to you. Finally, push to the remote repository:
git remote add platform <platform.sh@gitrepository>
git commit -m "Initial project"
git push -u platform master
Done! We have a simple and nice Micronaut application ready to go to the cloud.
Opinions expressed by DZone contributors are their own.
Trending
-
Revolutionize JSON Parsing in Java With Manifold
-
The Dark Side of DevSecOps and Why We Need Governance Engineering
-
Grow Your Skills With Low-Code Automation Tools
-
Opportunities for Growth: Continuous Delivery and Continuous Deployment for Testers
Comments