docs/community/samples/serving/helloworld-java-micronaut
RichieEscarez 9a92f43ca2 Fix rendering issues due to nested shortcodes (#1596)
* Community - move frontmatter into README

* Serving - move frontmatter into README

* swap readfile calls from README.md to index.md

* Eventing - move frontmatter into README
2019-08-01 09:38:43 -07:00
..
.mvn Add Micronaut Sample (#1199) 2019-05-20 06:04:29 -07:00
src Add Micronaut Sample (#1199) 2019-05-20 06:04:29 -07:00
.dockerignore Add Micronaut Sample (#1199) 2019-05-20 06:04:29 -07:00
.gitignore Add Micronaut Sample (#1199) 2019-05-20 06:04:29 -07:00
Dockerfile Add Micronaut Sample (#1199) 2019-05-20 06:04:29 -07:00
README.md Fix rendering issues due to nested shortcodes (#1596) 2019-08-01 09:38:43 -07:00
dependency-reduced-pom.xml Add Micronaut Sample (#1199) 2019-05-20 06:04:29 -07:00
micronaut-cli.yml Add Micronaut Sample (#1199) 2019-05-20 06:04:29 -07:00
mvnw Add Micronaut Sample (#1199) 2019-05-20 06:04:29 -07:00
mvnw.cmd Add Micronaut Sample (#1199) 2019-05-20 06:04:29 -07:00
pom.xml Add Micronaut Sample (#1199) 2019-05-20 06:04:29 -07:00
service.yaml Revert the use of Serving v1beta1. (#1545) 2019-06-27 11:06:07 -07:00

README.md

title linkTitle weight type
Hello World - Java (Micronaut) Java (Micronaut) 1 docs

Learn how to deploy a simple web app that is written in Java and uses Micronaut.

This samples uses Docker to build locally. The app reads in a TARGET env variable and then prints "Hello World: ${TARGET}!". If a value for TARGET is not specified, the "NOT SPECIFIED" default value is used.

Use this sample to walk you through the steps of creating and modifying the sample app, building and pushing your container image to a registry, and then deploying your app to your Knative cluster.

Before you begin

You must meet the following requirements to complete this sample:

  • A version of the Knative Serving component installed and running on your Kubernetes cluster. Follow the Knative installation instructions if you need to create a Knative cluster.
  • The following software downloaded and install on your loacal machine:
  • A Docker Hub account where you can push your container image.

Tip: You can clone the Knatve/docs repo and then modify the source files. Alternatively, learn more by manually creating the files youself.

Creating and configuring the sample code

To create and configure the source files in the root of your working directory:

  1. Create the pom.xml file:

      <modelVersion>4.0.0</modelVersion>
         <groupId>com.example.micronaut</groupId>
         <artifactId>helloworld</artifactId>
         <version>1.0.0-SNAPSHOT</version>
         <properties>
             <micronaut.version>1.1.0</micronaut.version>
             <jdk.version>1.8</jdk.version>
             <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
             <exec.mainClass>com.example.helloworld.Application</exec.mainClass>
         </properties>
         <dependencyManagement>
             <dependencies>
             <dependency>
                 <groupId>io.micronaut</groupId>
                 <artifactId>micronaut-bom</artifactId>
                 <version>${micronaut.version}</version>
                 <type>pom</type>
                 <scope>import</scope>
             </dependency>
             </dependencies>
         </dependencyManagement>
         <dependencies>
             <dependency>
                 <groupId>io.micronaut</groupId>
                 <artifactId>micronaut-inject</artifactId>
                 <scope>compile</scope>
             </dependency>
             <dependency>
                 <groupId>io.micronaut</groupId>
                 <artifactId>micronaut-validation</artifactId>
                 <scope>compile</scope>
             </dependency>
             <dependency>
                 <groupId>io.micronaut</groupId>
                 <artifactId>micronaut-runtime</artifactId>
                 <scope>compile</scope>
             </dependency>
             <dependency>
                 <groupId>io.micronaut</groupId>
                 <artifactId>micronaut-http-client</artifactId>
                 <scope>compile</scope>
             </dependency>
             <dependency>
                 <groupId>io.micronaut</groupId>
                 <artifactId>micronaut-http-server-netty</artifactId>
                 <scope>compile</scope>
             </dependency>
                 <dependency>
                 <groupId>ch.qos.logback</groupId>
                 <artifactId>logback-classic</artifactId>
                 <version>1.2.3</version>
                 <scope>runtime</scope>
             </dependency>
         </dependencies>
    
    <build>
        <plugins>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-shade-plugin</artifactId>
                 <version>3.1.0</version>
                 <executions>
                 <execution>
                     <phase>package</phase>
                     <goals>
                     <goal>shade</goal>
                     </goals>
                     <configuration>
                     <transformers>
                         <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                         <mainClass>${exec.mainClass}</mainClass>
                         </transformer>
                         <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                     </transformers>
                     </configuration>
                 </execution>
                 </executions>
             </plugin>
        </plugins>
    </build>
    </project>
    
  2. Create the HelloWorldController.java file in the src/main/java/com/example/helloworld directory. The [ROOT]/src/main/java/com/example/helloworld/HelloWorldController.java file handles requests to the root URI /.

     package com.example.helloworld;
    
     import io.micronaut.http.MediaType;
     import io.micronaut.http.annotation.Controller;
     import io.micronaut.http.annotation.Get;
    
     @Controller("/")
     public class HelloWorldController {
    
         @Get(value = "/", produces = MediaType.TEXT_PLAIN)
         public String index() {
             String target = System.getenv("TARGET");
             if (target == null) {
                 target = "NOT SPECIFIED";
             }
             return "Hello World: " + target;
         }
     }
    
  3. The Micronaut application is configured via src/main/resources/application.yml:

    micronaut:
      application:
        name: helloworld-java-micronaut
      server:
        port: ${PORT:8080}
    
  4. Create the Dockerfile file:

    # Use the official maven/Java 8 image to create a build artifact.
    # https://hub.docker.com/_/maven
    FROM maven:3.5-jdk-8-alpine as builder
    
    # Copy local code to the container image.
    WORKDIR /app
    COPY pom.xml .
    COPY src ./src
    
    # Build a release artifact.
    RUN mvn package -DskipTests
    
    # Use AdoptOpenJDK for base image.
    # It's important to use OpenJDK 8u191 or above that has container support enabled.
    # https://hub.docker.com/r/adoptopenjdk/openjdk8
    # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
    FROM adoptopenjdk/openjdk8:jdk8u202-b08-alpine-slim
    
    # Copy the jar to the production image from the builder stage.
    COPY --from=builder /app/target/helloworld-1.0.0-SNAPSHOT.jar /helloworld.jar
    
    # Run the web service on container startup.
    CMD ["java","-jar","/helloworld.jar"]
    
  5. Create the service.yaml file. You must specify your Docker Hub username in {username}. You can also configure the TARGET, for example you can modify the Micronaut Sample v1 value.

    apiVersion: serving.knative.dev/v1alpha1
    kind: Service
    metadata:
      name: helloworld-java-micronaut
      namespace: default
    spec:
      template:
        spec:
          containers:
            - image: docker.io/{username}/helloworld-java-micronaut
              env:
                - name: TARGET
                  value: "Micronaut Sample v1"
    

Building and deploying the sample

To build a container image, push your image to the registry, and then deploy your sample app to your cluster:

  1. Use Docker to build your container image and then push that image to your Docker Hub registry. You must replace the {username} variables in the following commands with your Docker Hub username.

    # Build the container on your local machine
    docker build -t {username}/helloworld-java-micronaut .
    
    # Push the container to docker registry
    docker push {username}/helloworld-java-micronaut
    
  2. Now that your container image is in the registry, you can deploy it to your Knative cluster by running the kubectl apply command:

    kubectl apply --filename service.yaml
    

    Result: A service name helloworld-java-micronaut is created in your cluster along with the following resources:

    • A new immutable revision for the version of the app that you just deployed.
    • The following networking resources are created for your app:
      • route
      • ingress
      • service
      • load balancer
    • Auto scaling is enable to allow your pods to scale up to meet traffic, and also back down to zero when there is no traffic.

Testing the sample app

To verify that your sample app has been successfully deployed:

  1. View your the ingress IP address of your service by running the following kubectl get command. Note that it may take sometime for the new service to get asssigned an external IP address, especially if your cluster was newly created.

    # In Knative 0.2.x and prior versions, the `knative-ingressgateway` service was used instead of `istio-ingressgateway`.
    INGRESSGATEWAY=knative-ingressgateway
    
    # The use of `knative-ingressgateway` is deprecated in Knative v0.3.x.
    # Use `istio-ingressgateway` instead, since `knative-ingressgateway`
    # will be removed in Knative v0.4.
    if kubectl get configmap config-istio -n knative-serving &> /dev/null; then
        INGRESSGATEWAY=istio-ingressgateway
    fi
    
    kubectl get svc $INGRESSGATEWAY --namespace istio-system
    

    Example result:

    NAME                     TYPE           CLUSTER-IP     EXTERNAL-IP      PORT(S)                                      AGE
    xxxxxxx-ingressgateway   LoadBalancer   10.23.247.74   35.203.155.229   80:32380/TCP,443:32390/TCP,32400:32400/TCP   2d
    
  2. Retrieve the URL for your service, by running the following kubectl get command:

    kubectl get services.serving.knative.dev helloworld-java-micronaut  --output=custom-columns=NAME:.metadata.name,URL:.status.url
    

    Example result:

    NAME                          URL
    helloworld-java-micronaut     http://helloworld-java-micronaut.default.example.com
    
  3. Run the following curl command to test your deployed sample app. You must replace the {IP_ADDRESS} variable the URL that your retrieve in the previous step.

    curl -H "Host: helloworld-java-micronaut.default.example.com" http://{IP_ADDRESS}
    

    Example result:

     Hello World: Micronaut Sample v1
    

Congtratualations on deploying your sample Java app to Knative!

Removing the sample app deployment

To remove the sample app from your cluster, run the following kubectl delete command:

kubectl delete --filename service.yaml