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

Micronaut Quickstart - Deployment for The Hello-World App by Gradle Tasks

DZone 's Guide to

Micronaut Quickstart - Deployment for The Hello-World App by Gradle Tasks

Here's how to deploy the Hello-World app by Gradle Tasks to a variety of plugins.

· Microservices Zone ·
Free Resource

To facilitate deployment across platforms, you can use Gradle tasks and plugins. Gradle supports different plugins to deploy to Kubernetes, OpenShift, GCP, and deploy as AWS Lambda. 

Plugins

In this tutorial, we will consider the Micronaut Framework by using Kotlin language. 

Micronaut positioned as a modern, JVM-based, full-stack framework for building modular, easily testable microservice and serverless applications. Let's create a simple "hello-world" app with multiple platform deployment(OpenShift/Kubernetes/Amazon/Google Cloud Platform) by using simple Gradle tasks.

Source: micronaut project

 

Application

Create your first Kotlin application:

Shell
 




x


1
mn create-app com.micronaut.complete --lang=kotlin


With a simple controller:

Kotlin
 




xxxxxxxxxx
1


 
1
@Controller
2
class GreetingController {
3
 
          
4
    @Get(uris = ["/hello", "/greeting"])
5
    fun greeting(): String {
6
        return "Hello from Micronaut!"
7
    }
8
}



Docker Support

Setup jib for Docker support in project build.gradle:

Groovy
 




xxxxxxxxxx
1
13


1
plugins {
2
    id 'com.google.cloud.tools.jib' version '2.3.0'
3
}
4
 
          
5
jib {
6
    to {
7
        image = 'elvaliev/micronaut-quickstart'
8
        auth {
9
            username = DOCKERHUB_USERNAME
10
            password = DOCKERHUB_PASSWORD
11
        }
12
    }
13
}


Run in command line: .gradlew jib

Google Cloud Platform (GCP) 

Prerequisites: Google Cloud Platform account.

Create a project

Create a project directly in your GCP or by using a command-line tool:

Shell
 




xxxxxxxxxx
1


1
gcloud projects create PROJECT_ID --set-as-default 
2
gcloud app create --project=PROJECT_ID


Plugin setup

For deploying to GCP used appengine-gradle-plugin.

Setup plugin configuration in project build.gradle:

Groovy
 




xxxxxxxxxx
1
10


 
1
buildscript {
2
    repositories {
3
        jcenter()
4
        mavenLocal()
5
    }
6
 
          
7
    dependencies {
8
        classpath 'com.google.cloud.tools:appengine-gradle-plugin:2.2.0'
9
    }
10
}
11
 
          
12
apply plugin: "com.google.cloud.tools.appengine-appyaml"
13
 
          
14
appengine {
15
    stage.artifact =
16
            "${buildDir}/libs/${project.name}-${project.version}.jar"
17
 
          
18
    deploy {
19
        stopPreviousVersion = true
20
        promote = true
21
        projectId = GCLOUD_CONFIG
22
        version = GCLOUD_CONFIG
23
    }
24
 
          
25
}


Parameter GCLOUD_CONFIG (it's PROJECT_ID from the previous step) you could set in build.gradle directly or in gradle.properties.

Appengine Setup

In your project create file: src/main/appengine/app.yaml:

YAML
 




xxxxxxxxxx
1


 
1
runtime: java11
2
instance_class: F4
3
entrypoint: 'java -agentpath:/opt/cdbg/cdbg_java_agent.so=--log_dir=/var/log
4
                  -jar <JAR_FILE_NAME>.jar'


Deploy task

Execute the Gradle commands:

Shell
 




xxxxxxxxxx
1


 
1
./gradlew appengineDeploy


Browse your application:

Shell
 




xxxxxxxxxx
1


1
gcloud app browse


Kubernetes/OpenShift 

Prerequisites: OC/Kubernetes Client - already logged in.

For deploying to Kubernetes/OpenShift used my plugin k8s_aws_plugin, which used templates or file configuration list (supported .json and .yaml formats) and image-streams for deployment.

Template Setup

Define in your project file for OpenShift and/or Kubernetes deployment as a "configuration list":

YAML
 




xxxxxxxxxx
1
13


 
1
 ...
2
---
3
apiVersion: "v1"
4
kind: "Service"
5
metadata:
6
  ...
7
  name: "micronaut-quickstart"
8
spec:
9
  ports:
10
  - name: "http"
11
    port: 8090
12
    targetPort: 8090
13
    ...


Or "template":

YAML
 




xxxxxxxxxx
1
10


 
1
apiVersion: v1
2
kind: Template
3
metadata:
4
  name: my-template
5
objects:
6
- kind: DeploymentConfig
7
  apiVersion: v1
8
  metadata:
9
      labels:
10
      ...


Plugin Setup

Setup plugin configuration in the project build.gradle:

Groovy
 




xxxxxxxxxx
1
16


 
1
plugins {
2
                    ...
3
    id "com.elvaliev.k8s_aws_plugin" version "1.0.4"
4
}
5
 
          
6
 apply plugin: "com.elvaliev.k8s_aws_plugin"
7
 
          
8
 kubernetes {
9
    template = 'k8s/kubernetes.yml'
10
    image = 'elvaliev/micronaut-quickstart'
11
 }
12
 
          
13
 openshift {
14
    template = 'k8s/openshift.yml'
15
    image = 'elvaliev/micronaut-quickstart'
16
 }


Note: If you named template as "openshift" and "kubernetes" in project root - don't need to specify templates. The plugin will recognize it.

Note: Plugin support command options, it's not necessary to specify extensions "openshift" and "kubernetes"

Deploy tasks

For deploying to Openshift by using extensions: ./gradlew openshiftDeploy or by using command-line options:

Shell
 




xxxxxxxxxx
1


1
./gradlew openshiftDeploy --template="k8s/openshift.yml" --image="elvaliev/micronaut-quickstart"


For deploying to Kubernetes by using extensions: ./gradlew kubernetesDeploy or by using command-line options:

Shell
 




xxxxxxxxxx
1


 
1
./gradlew kubernetesDeploy --template="k8s/kubernetes.yml" --image="elvaliev/micronaut-quickstart"


AWS Lambda

Prerequisites: AWS Client - already logged in.

Dependency

Setup aws dependency in the project build.gradle:

Groovy
 




xxxxxxxxxx
1


 
1
dependencies {
2
    implementation "io.micronaut:micronaut-function-aws"
3
}


Serverless Function

For deploying as AWS Lambda setup serverless-function:

Kotlin
 




xxxxxxxxxx
1


 
1
@FunctionBean("hello-micronaut")
2
class HelloKotlinFunction : Supplier<String> {
3
 
          
4
    override fun get(): String {
5
        return "Hello from Serverless Micronaut Functions!"
6
    }
7
}


Plugin Setup

For deploying serverless functions used gradle aws plugin. Define AWS_ROLE and AWS_REGION in gradle.properties.

Setup plugin configuration in the project build.gradle:

Groovy
 




xxxxxxxxxx
1
45


 
1
import com.amazonaws.services.lambda.model.InvocationType
2
import com.amazonaws.services.lambda.model.Runtime
3
import jp.classmethod.aws.gradle.lambda.*
4
 
          
5
 
          
6
plugins {
7
    ...
8
    id "jp.classmethod.aws.lambda" version "0.41"
9
}
10
apply plugin: 'jp.classmethod.aws.lambda'
11
 
          
12
aws {
13
    profileName = "default"
14
    region = AWS_REGION
15
}
16
 
          
17
lambda {
18
    region = AWS_REGION
19
}
20
 
          
21
task zip(type: Zip) {
22
    from "function/"
23
    destinationDir file("build")
24
}
25
 
          
26
mainClassName = "io.micronaut.function.executor.FunctionApplication"
27
 
          
28
task deploy(type: AWSLambdaMigrateFunctionTask, dependsOn: shadowJar) {
29
    functionName = "hello-micronaut"
30
    handler = "io.micronaut.function.aws.MicronautRequestStreamHandler"
31
    role = AWS_ROLE
32
    runtime = Runtime.Java8
33
    zipFile = shadowJar.archivePath
34
    memorySize = 256
35
    timeout = 60
36
}
37
 
          
38
task invoke(type: AWSLambdaInvokeTask) {
39
    functionName = "hello-micronaut"
40
    invocationType = InvocationType.RequestResponse
41
    payload = ''
42
    doLast {
43
        println "Lambda function result: " + new String(invokeResult.payload.array())
44
    }
45
}


Deploy Tasks

Deploy to AWS Lambda: ./gradlew deploy and invoke Lambda: ./gradlew invoke.


Github: https://github.com/ElinaValieva/micronaut-quarkus-quickstarts 

Topics:
aws lambda, deployment, deployment automation, google cloud platform, gradle, gradle plugins, kubernates, micronaut, openshift

Published at DZone with permission of Elina Valieva . See the original article here.

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}