Skip to content

Commit

Permalink
Adding Spring demo application
Browse files Browse the repository at this point in the history
  • Loading branch information
mikouaj committed Dec 7, 2023
1 parent 9e27322 commit 73b2e75
Show file tree
Hide file tree
Showing 18 changed files with 728 additions and 0 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/build-demo-app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: Build Demo App
on:
push:
branches:
- main
paths:
- 'demo-app/**'
pull_request:
branches:
- main
paths:
- 'demo-app/**'

jobs:
build:
name: Build
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./demo-app
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'zulu'
- name: Build
run: mvn --batch-mode --update-snapshots package

4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ on:
push:
branches:
- main
paths-ignore:
- 'demo-app/**'
pull_request:
branches:
- main
paths-ignore:
- 'demo-app/**'

jobs:
build:
Expand Down
61 changes: 61 additions & 0 deletions .github/workflows/release-demo-app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: Spring Rest JPA Release
on:
push:
tags:
- "demo-app/v*"

jobs:
build:
name: Build
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./demo-app
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'zulu'
- name: Build
run: mvn --batch-mode --update-snapshots package
- name: Docker login
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Version from tag
id: version-from-tag
run: echo version=${GITHUB_REF_NAME//demo-app\/} >> $GITHUB_OUTPUT
- name: Docker Extract metadata
id: meta
uses: docker/metadata-action@v4
with:
images: ghcr.io/google/spring-demo-app
tags: |
type=semver,pattern={{major}}.{{minor}}.{{patch}},value=${{ steps.version-from-tag.outputs.version }}
- name: Docker build and push
uses: docker/build-push-action@v4
with:
context: ./demo-app
file: ./demo-app/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
19 changes: 19 additions & 0 deletions demo-app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM azul/zulu-openjdk-alpine:17-latest
VOLUME /tmp
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["sh", "-c", "java ${JAVA_OPTS} -jar /app.jar ${0} ${@}"]
49 changes: 49 additions & 0 deletions demo-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# spring-demo-app

The folder contains source code of a simple Java application that uses Spring Boot framework
and exposes the REST endpoint. The data is fetched from the relational database with
Java Persistence API.

## Usage

The service exposes below endpoints:

* `/books` returns collection of books

In the addition to above, Spring Boot actuator endpoints are exposed per configuration.

### Source code

Prerequisites:

* [JDK](https://openjdk.org/projects/jdk/17/) 17 or newer
* [Maven](https://maven.apache.org/download.cgi)

Build application from the source code.

```sh
mvn package
java -jar target/spring-demo-app-0.0.1-SNAPSHOT.jar
```

### Container image

1. Adjust `application.yaml` according to your needs
2. Run the container

```sh
docker run -d --name spring-demo-app \
-p 8080:8080 \
-v "`pwd`/application.yaml:/application.yaml" \
ghcr.io/google/spring-demo-app:latest
```

### Kubernetes

1. Adjust `application.yaml` according to your needs
2. Adjust `kustomization.yaml` according to your needs
3. Customize and apply Kubernetes manifests

```sh
kubectl kustomize | kubectl apply -f
```
32 changes: 32 additions & 0 deletions demo-app/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

spring:
datasource:
url: jdbc:h2:mem:mydb
username: sa
password: password
driverClassName: org.h2.Driver
jpa:
hibernate:
ddl-auto: create
database-platform: org.hibernate.dialect.H2Dialect
defer-datasource-initialization: true
management:
endpoints:
web:
exposure:
include:
- health
- prometheus
26 changes: 26 additions & 0 deletions demo-app/examples/spring-demo-app-gmp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: monitoring.googleapis.com/v1
kind: PodMonitoring
metadata:
name: spring-demo-app
spec:
selector:
matchLabels:
app.kubernetes.io/name: spring-demo-app
endpoints:
- port: http
path: /actuator/prometheus
interval: 5s
82 changes: 82 additions & 0 deletions demo-app/examples/spring-demo-app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: ServiceAccount
metadata:
name: spring-demo-app
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: spring-demo-app
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: spring-demo-app
template:
metadata:
labels:
app.kubernetes.io/name: spring-demo-app
spec:
serviceAccountName: spring-demo-app
containers:
- name: spring-demo-app
image: ghcr.io/google/spring-demo-app:latest
args:
- --spring.config.location=file:/config/application.yaml
env:
- name: JAVA_OPTS
value: -XX:MaxRAMPercentage=75
volumeMounts:
- name: spring-demo-app-config
mountPath: /config
ports:
- name: http
containerPort: 8080
startupProbe:
periodSeconds: 2
failureThreshold: 60
httpGet:
path: /actuator/health
port: http
scheme: HTTP
livenessProbe:
httpGet:
path: /actuator/health
port: http
scheme: HTTP
resources:
limits:
cpu: "1"
memory: 512Mi
requests:
cpu: 500m
memory: 512Mi
volumes:
- name: spring-demo-app-config
configMap:
name: spring-demo-app-config
---
apiVersion: v1
kind: Service
metadata:
name: spring-demo-app
spec:
selector:
app.kubernetes.io/name: spring-demo-app
ports:
- port: 8080
targetPort: http
28 changes: 28 additions & 0 deletions demo-app/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- examples/spring-demo-app.yaml
# Uncomment for Google Managed Prometheus metrics scraping
#- examples/spring-app-demo-gmp.yaml

namespace: demo

configMapGenerator:
- name: spring-demo-app-config
files:
- application.yaml
Loading

0 comments on commit 73b2e75

Please sign in to comment.