Skip to content
This repository has been archived by the owner on Jun 7, 2021. It is now read-only.

Add MongoDB example that works with prestarted MongoDB #196

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions microprofile-nosql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# MicroProfile plus MongoDB in a .war Example

This example takes a normal [MicroProfile](http://microprofile.io) WAR, and wraps it into
a `-thorntail` runnable jar, with a little added MongoDB access for more fun!

> Please raise any issues found with this example in our JIRA:
> https://issues.jboss.org/browse/THORN

## Project `pom.xml`

This project is a traditional WAR project, with maven packaging
of `war` in the `pom.xml`

<packaging>war</packaging>

The project adds a `<plugin>` to configure `thorntail-maven-plugin` to
create the runnable `.jar`.

<plugin>
<groupid>io.thorntail</groupId>
<artifactId>thorntail-maven-plugin</artifactId>
<version>${version.thorntail}</version>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>

To define the needed parts of Thorntail, a dependency is added

<dependency>
<groupid>io.thorntail</groupId>
<artifactId>microprofile</artifactId>
<version>${version.thorntail}</version>
</dependency>

This dependency provides the APIs that are part of the MicroProfile
to your application, so the project does *not* need to specify those.

Additional application dependencies (in this case `joda-time`) can be
specified and will be included in the normal `.war` construction and available
within the Thorntail application `.jar`.

## Start MongoDB

First start the MondoDB server, which may take some time to launch in the background (which is why we hacked a sleep of 5 seconds below).
Then, create 'devuser' account and set the password to 'tTeX%4#MpN0~sQl':

* docker run -d --name some-mongo -p 27017:27017 -e MONGO_INITDB_DATABASE=mongotestdb mongo
* sleep 5
* docker exec some-mongo /bin/sh -c "mongo mongotestdb --eval \"db.createUser({ user: 'devuser', pwd: 'tTeX%4#MpN0~sQl', roles: ['dbAdmin'] }); db.auth('devuser', 'tTeX%4#MpN0~sQl'); \" "

## Stop MongoDB

After you done playing with this example, you can stop the MongoDB database via:
* docker stop some-mongo
* docker rm some-mongo

## Run

* mvn thorntail:run

For build and deployment to OpenShift, will deploy to active OpenShift project.
* oc project
* mvn clean package fabric8:build fabric8:deploy

Read about the maven fabric8 plugin at https://maven.fabric8.io/

## Use

Since Thorntail apps tend to support one deployment per executable, it
automatically adds a `jboss-web.xml` to the deployment if it doesn't already
exist. This is used to bind the deployment to the root of the web-server,
instead of using the `.war`'s own name as the application context.

http://localhost:8080/Hello to show that the application prints a 'Howdy' message, then
http://localhost:8080/Hello/createDiaryEntry to show that the application can write and read the MongoDB database.

159 changes: 159 additions & 0 deletions microprofile-nosql/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2018 Red Hat, Inc. and/or its affiliates.
~
~ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.thorntail.examples</groupId>
<artifactId>examples-parent</artifactId>
<version>2.2.0.Final-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>

<artifactId>example-microprofile-nosql</artifactId>

<name>Thorntail Examples: Microprofile NoSQL</name>
<description>Thorntail Examples: Microprofile NoSQL</description>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Microprofile -> MicroProfile


<packaging>war</packaging>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>io.thorntail</groupId>
<artifactId>thorntail-maven-plugin</artifactId>
<executions>
<execution>
<id>package</id>
</execution>
<execution>
<id>start</id>
</execution>
<execution>
<id>stop</id>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin</artifactId>
<version>3.5.41</version>
<executions>
<execution>
<goals>
<goal>resource</goal>
</goals>
</execution>
<execution>
<id>execution1</id>
<configuration>
<!-- Standard d-m-p configuration which defines how images are build, i.e. how the docker.tar is created -->
<images>
<image>
<name>mongo</name>
<!-- "alias" is used to correlate to the containers in the pod spec -->
<alias>mymongo</alias>
</image>
</images>
<!-- resources to be created -->
<resources>
<!-- Labels that are applied to all created objects -->
<labels>
<group>quickstarts</group>
</labels>
<!-- Definition of the ReplicationController / ReplicaSet. Any better name than "containers" ? -->
<deployment>
<!-- Name of the replication controller, which will have a sane default (container alisa, mvn coords, ..) -->
<name>${project.artifactId}</name>
<!-- Replica count-->
<replicas>1</replicas>
<!-- Container to include in the POD template. By default all with a "build" section in the
"images" configuration will be add to the POD. However, they can be configured separately, too.
-->
<containers>
<container>
<!-- Alias name correlating with the same named "image" configuration above. Can be ommitted
if there is only a single image added -->
<alias>camel-service</alias>
<ports>
<!-- Ports to expose in the pod specs -->
<port>8778</port>
</ports>
<mounts>
<scratch>/var/scratch</scratch>
</mounts>
</container>
</containers>
<!-- Volumes used in the replicaSet -->
<volumes>
<volume>
<name>scratch</name>
<type>emptyDir</type>
</volume>
</volumes>
</deployment>
<!-- Dedicated section for (multiple) services to define -->
<services>
<service>
<name>${project.artifactId}</name>
<headless>true</headless>
</service>
</services>
</resources>
</configuration>
</execution>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand why this entire <execution> is needed. Seems to me your usage of FMP is unnecessarily complicated.

</executions>
<configuration>
<generator>
<includes>
<include>thorntail-v2</include>
</includes>
<config>
<thorntail>
<from>registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift</from>
</thorntail>
</config>
</generator>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>io.thorntail.examples</groupId>
<artifactId>examples-base</artifactId>
</dependency>
<dependency>
<groupId>io.thorntail</groupId>
<artifactId>microprofile</artifactId>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>io.thorntail</groupId>
<artifactId>mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.2.2</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.wildfly.swarm.it.jaxrs;

import org.jboss.arquillian.drone.api.annotation.Drone;
import org.jboss.arquillian.junit.Arquillian;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.WebDriver;
import org.wildfly.swarm.it.AbstractIntegrationTest;

import static org.fest.assertions.Assertions.assertThat;

/**
* @author Bob McWhirter
* @author Ken Finnigan
*/
@RunWith(Arquillian.class)
public class JAXRSApplicationIT extends AbstractIntegrationTest {

@Drone
WebDriver browser;

@Test
public void testIt() {
browser.navigate().to("http://localhost:8080/");
assertThat(browser.getPageSource()).contains("Howdy at ");
}

@Test
public void testExceptionMapping() {
browser.navigate().to("http://localhost:8080/bad");
assertThat(browser.getPageSource()).contains("This is our exception page!");
}

@Test
public void testCreateMongoDBDocument() {
browser.navigate().to("http://localhost:8080/createDiaryEntry");
assertThat(browser.getPageSource()).contains("MongoDB example");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
apiVersion: v1
kind: DeploymentConfig
metadata:
name: example-microprofile
spec:
replicas: 1
strategy:
rollingParams:
timeoutSeconds: 10800
type: Rolling
template:
metadata:
labels:
app: example-microprofile
deploymentconfig: example-microprofile
spec:
containers:
- env:
- name: KUBERNETES_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: JAVA_APP_DIR
value: /deployments
- name: JAVA_OPTIONS
value: -Dswarm.project.stage=production -Djava.net.preferIPv4Stack=true
name: example-microprofile
image: example-microprofile:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
name: http
protocol: TCP
securityContext:
privileged: false
triggers:
- type: ConfigChange
- imageChangeParams:
automatic: true
containerNames:
- example-microprofile
from:
kind: ImageStreamTag
name: example-microprofile:latest
type: ImageChange
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the only thing you do here is specifying -Dswarm.project.stage. You don't need to add the full YAML, just what you need to add, FMP will generate the rest.

10 changes: 10 additions & 0 deletions microprofile-nosql/src/main/fabric8/example-microprofile-route.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
kind: Route
metadata:
name: example-microprofile
spec:
port:
targetPort: 8080
to:
kind: Service
name: example-microprofile
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think FMP would generate the same thing.

14 changes: 14 additions & 0 deletions microprofile-nosql/src/main/fabric8/example-microprofile-svc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Service
metadata:
name: example-microprofile
annotations:
description: Inventory Service based on WF Swarm
spec:
ports:
- protocol: TCP
port: 8080
targetPort: 8080
selector:
deploymentconfig: example-microprofile
type: LoadBalancer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think FMP would generate the same thing.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.wildfly.examples.swarm.jaxrs;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

/**
* @author Bob McWhirter
*/
@ApplicationPath("/")
public class MyApplication extends Application {

public MyApplication() {
}
}
Loading