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

Deploy Quarkus Faster in the Cloud With Platform.sh Part 6

DZone 's Guide to

Deploy Quarkus Faster in the Cloud With Platform.sh Part 6

How to deploy a Panache/MongoDB Quarkus application in the cloud with Platform.sh

· Java Zone ·
Free Resource

Quarkus is, in its own words, a Cloud Native, (Linux) Container First framework for writing Java applications. It has become popular because of the amazingly fast boot time and incredibly low RSS memory. In this series of articles about Quarkus, we'll teach how to deploy Quarkus with Panache and MongoDB.

MongoDB is a well known NoSQL Database that is widely used, but using its raw API can be cumbersome as you need to express your entities and your queries as a MongoDB Document.

MongoDB with Panache provides active record style entities (and repositories) like you have in Hibernate ORM with Panache and focuses on making your entities trivial and fun to write in Quarkus.

The first step is to create the application itself, and Quarkus has proper documentation. You have either a Maven Archetype or the start code link where you can define the dependencies that you need to write your application.

XML
 




x
12


 
1
<dependency>
2
    <groupId>io.quarkus</groupId>
3
    <artifactId>quarkus-resteasy</artifactId>
4
</dependency>
5
<dependency>
6
    <groupId>io.quarkus</groupId>
7
    <artifactId>quarkus-resteasy-jsonb</artifactId>
8
</dependency>
9
<dependency>
10
    <groupId>io.quarkus</groupId>
11
    <artifactId>quarkus-mongodb-panache</artifactId>
12
</dependency>



We need to configure the application to run locally to test it.

Properties files
 




xxxxxxxxxx
1


 
1
quarkus.mongodb.database=gods
2
quarkus.mongodb.hosts=127.0.0.1:27017
3
quarkus.mongodb.credentials.username=
4
quarkus.mongodb.credentials.password=
5
quarkus.mongodb.credentials.auth-mechanism=SCRAM-SHA-1



If you want to run a MongoDB locally, a good option might be a Docker, that you can run with the command below:

Shell
 




xxxxxxxxxx
1


 
1
docker run -d --name mongodb-instance -p 27017:27017 mongo



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 Mythological gods where we have a name and their powers. Therefore, we'll create a God entity.

Java
 




xxxxxxxxxx
1
12


 
1
import io.quarkus.mongodb.panache.PanacheMongoEntity;
2
 
          
3
import java.util.Set;
4
 
          
5
public class God extends PanacheMongoEntity {
6
 
          
7
    public String name;
8
 
          
9
    public Set<String> powers;
10
 
          
11
}
12
 
          



The last step is to create a resource where the client can do request and then the CRUD. As we can see, with Panache, we can reduce a lot of complexity of the code.

Java
 




x


1
import io.quarkus.panache.common.Sort;
2
import org.bson.types.ObjectId;
3
import org.jboss.resteasy.annotations.jaxrs.PathParam;
4
 
          
5
import javax.enterprise.context.ApplicationScoped;
6
import javax.transaction.Transactional;
7
import javax.ws.rs.Consumes;
8
import javax.ws.rs.DELETE;
9
import javax.ws.rs.GET;
10
import javax.ws.rs.POST;
11
import javax.ws.rs.PUT;
12
import javax.ws.rs.Path;
13
import javax.ws.rs.Produces;
14
import javax.ws.rs.WebApplicationException;
15
import javax.ws.rs.core.MediaType;
16
import javax.ws.rs.core.Response;
17
import java.util.List;
18
 
          
19
@Path("gods")
20
@ApplicationScoped
21
@Produces(MediaType.APPLICATION_JSON)
22
@Consumes(MediaType.APPLICATION_JSON)
23
public class GodResource {
24
 
          
25
    @GET
26
    public List<God> get() {
27
        return God.listAll(Sort.ascending("name"));
28
    }
29
 
          
30
    @GET
31
    @Path("{id}")
32
    public God getSingle(@PathParam String id) {
33
 
          
34
        God entity = God.findById(new ObjectId(id));
35
        if (entity == null) {
36
            throw new WebApplicationException("God with id of " + id + " does not exist.", Response.Status.NOT_FOUND);
37
        }
38
        return entity;
39
    }
40
 
          
41
    @POST
42
    @Transactional
43
    public Response create(God god) {
44
        god.persist();
45
        return Response.ok(god).status( Response.Status.CREATED).build();
46
    }
47
 
          
48
    @PUT
49
    @Path("{id}")
50
    @Transactional
51
    public God update(@PathParam String id, God god) {
52
 
          
53
        God entity = God.findById(new ObjectId(id));
54
        if (entity == null) {
55
            throw new WebApplicationException("God with id of " + id + " does not exist.", Response.Status.NOT_FOUND);
56
        }
57
 
          
58
        entity.name = god.name;
59
        entity.powers = god.powers;
60
        entity.persistOrUpdate();
61
        return entity;
62
    }
63
 
          
64
    @DELETE
65
    @Path("{id}")
66
    @Transactional
67
    public Response delete(@PathParam String id) {
68
        boolean deleted = God.deleteById(new ObjectId(id));
69
        if (deleted) {
70
            return Response.status(Response.Status.NO_CONTENT).build();
71
        }
72
        throw new WebApplicationException("God with id of " + id + " does not exist.", Response.Status.NOT_FOUND);
73
    }
74
 
          
75
}
76
 
          



The application is ready to go, 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:

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}/"


Platform.sh allows you to completely define and configure the topology and services you want to use on your project.

YAML
 




xxxxxxxxxx
1


 
1
mongodb:
2
  type: mongodb:3.6
3
  disk: 1024



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. This application will grand permission to access MongoDB.

YAML
 




xxxxxxxxxx
1
15


 
1
name: app
2
 
          
3
type: "java:11"
4
disk: 1024
5
 
          
6
hooks:
7
    build: ./mvnw package -DskipTests -Dquarkus.package.uber-jar=true
8
 
          
9
relationships:
10
    mongodb: 'mongodb:mongodb'
11
 
          
12
web:
13
    commands:
14
        start: java -jar $JAVA_OPTS $CREDENTIAL -Dquarkus.http.port=$PORT target/panache-mongodb-1.0-SNAPSHOT-runner.jar
15
 
          



To simplify the application file, we'll use Shell variables int the  .environment  file.

Shell
 




xxxxxxxxxx
1


 
1
export MONGO_PORT=`echo $PLATFORM_RELATIONSHIPS|base64 -d|json_pp|jq -r ".mongodb[0].port"`
2
export MONGO_HOST=`echo $PLATFORM_RELATIONSHIPS|base64 -d|json_pp|jq -r ".mongodb[0].host"`
3
export MONGO_ADDRESS="${MONGO_HOST}:${MONGO_PORT}"
4
export MONGO_PASSWORD=`echo $PLATFORM_RELATIONSHIPS|base64 -d|json_pp|jq -r ".mongodb[0].password"`
5
export MONGO_USER=`echo $PLATFORM_RELATIONSHIPS|base64 -d|json_pp|jq -r ".mongodb[0].username"`
6
export MONGO_DATABASE=`echo $PLATFORM_RELATIONSHIPS|base64 -d|json_pp|jq -r ".mongodb[0].path"`
7
export JAVA_MEMORY=-Xmx$(jq .info.limits.memory /run/config.json)m
8
export JAVA_OPTS="$JAVA_MEMORY -XX:+ExitOnOutOfMemoryError"
9
export CREDENTIAL="-Dquarkus.mongodb.hosts=$MONGO_ADDRESS -Dquarkus.mongodb.credentials.username=$MONGO_USER -Dquarkus.mongodb.credentials.password=$MONGO_PASSWORD -Dquarkus.mongodb.database=$MONGO_DATABASE"



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 Quarkus application ready to go to the cloud.


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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}