{{announcement.body}}
{{announcement.title}}

Hello World: Micronaut in the Cloud

DZone 's Guide to

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.

· Java Zone ·
Free Resource

Micronaut 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.

Java
 




x
13


 
1
import io.micronaut.http.MediaType;
2
import io.micronaut.http.annotation.Controller;
3
import io.micronaut.http.annotation.Get;
4
import io.reactivex.Single;
5
 
          
6
@Controller("/")
7
public class HelloController {
8
 
          
9
    @Get(uri = "/", produces = MediaType.TEXT_PLAIN)
10
    public Single<String> hello() {
11
        return Single.just("Hello World! A simple Micronaut template for Platform.sh");
12
    }
13
}


The last step in the code is a start-up application.

Java
 




xxxxxxxxxx
1


1
import io.micronaut.runtime.Micronaut;
2
 
          
3
public class Application {
4
 
          
5
    public static void main(String[] args) {
6
        Micronaut.run(Application.class);
7
    }
8
}



To execute locally, you run the maven package application and then execute the uber-jar.

Shell
 




x


1
mvn clean package
2
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.

YAML
 




xxxxxxxxxx
1


 
1
"https://{default}/":
2
  type: upstream
3
  upstream: "app:http"
4
 
          
5
"https://www.{default}/":
6
  type: redirect
7
  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.
YAML
 




x
8


1
name: app
2
type: "java:8"
3
disk: 1024
4
hooks:
5
    build: mvn clean package
6
web:
7
    commands:
8
        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:

Shell


Done! We have a simple and nice Micronaut application ready to go to the cloud.

Topics:
cloud, java, micronaut, paas, platform.sh

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}