Micronaut in the Cloud: Micronaut Data
In the third part of this series on deploying Micronaut to the cloud, we take a look at building the Micronaut app with Micronaut Data.
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 tutorial of the second tutorial about Micronaut in the cloud we'll deploy an application with Micronaut Data. Micronaut Data is a database access toolkit that uses Ahead of Time (AoT) compilation to pre-compute queries for repository interfaces that are then executed by a thin, lightweight runtime layer.
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. At this point, we need Postgresql and Micronaut Data dependency.
If you want to run a PostgreSQL locally, a good option might be Docker, which you can run with the command below:
xxxxxxxxxx
docker run --ulimit memlock=-1:-1 -it --rm=true --memory-swappiness=0 --name micronaut_test -e POSTGRES_USER=micronaut -e POSTGRES_PASSWORD=micronaut -e POSTGRES_DB=micronaut -p 5432:5432 postgres:10.5
The first step is to set up the database configuration. We need to configure the application to run locally and enable it to be overwritten to the production environment.
xxxxxxxxxx
micronaut
application
name jpa
datasources
default
url $ JDBC_URL `jdbc postgresql //localhost 5432/micronaut`
driverClassName org.postgresql.Driver
username $ JDBC_USER micronaut
password $ JDBC_PASSWORD micronaut
jpa.default.properties.hibernate.hbm2ddl.auto update
The infrastructure code is ready; the next step is to create the application itself. In this sample, we'll create a small rest-application to store books. Therefore, we'll create a Book entity.
x
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import java.util.Objects;
name = "book") (
public class Book {
strategy = GenerationType.AUTO) (
private Long id;
name = "name", nullable = false) (
private String name;
name = "isbn", nullable = false) (
private String isbn;
//getter and setter
}
In a database integration, we're happy to have a CrudRepository
. It is an interface for performing CRUD (Create, Read, Update, Delete). This a blocking variant and is based mainly on the same interface in Spring Data; however, it includes integrated validation support.
xxxxxxxxxx
import io.micronaut.data.annotation.Repository;
import io.micronaut.data.repository.CrudRepository;
public interface BookRepository extends CrudRepository<Book, Long> {
}
The last step is to create a resource where the client can do the request and then the CRUD.
x
import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Delete;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Post;
import io.micronaut.scheduling.TaskExecutors;
import io.micronaut.scheduling.annotation.ExecuteOn;
import javax.validation.Valid;
TaskExecutors.IO) (
"/books") (
public class BookController {
private final BookRepository repository;
public BookController(BookRepository repository) {
this.repository = repository;
}
"/{id}") (
public Book show(Long id) {
return repository.findById(id).orElse(null);
}
public Iterable<Book> findAll() {
return repository.findAll();
}
public HttpResponse<Book> save( Book book) {
return HttpResponse.created(repository.save(book));
}
"/{id}") (
public HttpResponse delete(Long id) {
repository.deleteById(id);
return HttpResponse.noContent();
}
}
The application is ready to go, and you can run the test the application. The next step is to move to the cloud with Platform.sh.
- To move your application to the cloud, briefly, you need three files:
xxxxxxxxxx
"https://{default}/":
type upstream
upstream"app:http"
"https://www.{default}/"
type redirect
to"https://{default}/"
Platform.sh allows you to completely define and configure the topology and services you want to use on your project.
xxxxxxxxxx
db
type postgresql11
disk512
One 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. On this application, we allow the relationship between the PostgreSQL database and the application. So, our application container will have access to see the PostgreSQL container. Also, we'll overwrite the local configuration to use in the cloud to Platform.sh.
xxxxxxxxxx
name app
type"java:11"
disk1024
hooks
build mvn clean package -DskipTests
relationships
database"db:postgresql"
web
commands
start java -jar $JAVA_OPTS target/micronaut-data-0.1.jar
To simplify the application file, we'll use Shell variables int the .environment
file.
xxxxxxxxxx
export JDBC_HOST=`echo $PLATFORM_RELATIONSHIPS|base64 -d|jq -r ".database[0].host"`
export JDBC_PORT=`echo $PLATFORM_RELATIONSHIPS|base64 -d|jq -r ".database[0].port"`
export JDBC_PASSWORD=`echo $PLATFORM_RELATIONSHIPS|base64 -d|jq -r ".database[0].password"`
export JDBC_USER=`echo $PLATFORM_RELATIONSHIPS|base64 -d|jq -r ".database[0].username"`
export DATABASE=`echo $PLATFORM_RELATIONSHIPS|base64 -d|jq -r ".database[0].path"`
export JDBC_URL=jdbc:postgresql://${JDBC_HOST}:${JDBC_PORT}/${DATABASE}
export JAVA_MEMORY=-Xmx$(jq .info.limits.memory /run/config.json)m
export JAVA_OPTS="$JAVA_MEMORY -XX:+ExitOnOutOfMemoryError"
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.
Done! We have a simple and nice Micronaut application ready to go to the cloud.
Opinions expressed by DZone contributors are their own.
Comments