Skip to content

Basic #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits 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
17 changes: 11 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
FROM openjdk:11.0.1-jre-slim-sid
FROM maven:3.6-jdk-11-slim as BUILD
COPY . /src
WORKDIR /src
RUN mvn install -DskipTests

CMD ["/usr/bin/java", "-jar", "/app/app.jar"]

# Add the service itself
ARG JAR_FILE=demo-0.0.1-SNAPSHOT.jar
COPY target/${JAR_FILE} /app/app.jar
FROM openjdk:11.0.1-jre-slim-stretch
EXPOSE 8080
WORKDIR /app
ARG JAR=hello-0.0.1-BASIC.jar

COPY --from=BUILD /src/target/$JAR /app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
113 changes: 113 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Spring Hello World

This is a very basic Spring Hello World application that serves the value of the application property `message` via the web.

## Quick Start

Clone the repo down locally:

```console
git clone https://github.com/paulczar/spring-helloworld.git
cd spring-hello-world
```

Assuming you have Java and Maven installed you can run it with:

```console
$ mvn spring-boot:run
2019-02-27 15:08:41.186 INFO 29802 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-02-27 15:08:41.420 INFO 29802 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-02-27 15:08:41.426 INFO 29802 --- [ main] net.paulcz.hello.Application : Started Application in 2.217 seconds (JVM running for 5.332)
2019-02-27 15:08:55.500 INFO 29802 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-02-27 15:08:55.500 INFO 29802 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-02-27 15:08:55.506 INFO 29802 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 5 ms
```

Point your web browser or use wget/curl/httpy at `localhost:8080`:

```console
$ http localhost:8080
HTTP/1.1 200
Content-Length: 17
Content-Type: text/plain;charset=UTF-8
Date: Wed, 27 Feb 2019 21:13:35 GMT

hello development
```

## Build and Run

### With Maven

```console
$ mvn clean install
...
...
[INFO] --- maven-jar-plugin:3.1.1:jar (default-jar) @ hello ---
[INFO] Building jar: /home/user/development/spring/hello/target/hello-0.0.1-BASIC.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.238 s
[INFO] Finished at: 2019-02-27T15:15:42-06:00
[INFO] ------------------------------------------------------------------------
```

Then you can run it like so:

> Note: the `MESSAGE` environment variable is not neccesary, but is instead showing that you can change the `message` property to get a different response when accessing the application.

```console
$ MESSAGE="hello world" java -jar target/hello-0.0.1-BASIC.jar

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.3.RELEASE)

2019-02-27 15:16:41.581 INFO 30710 --- [ main] net.paulcz.hello.Application : Starting Application v0.0.1-SNAPSHOT on paulczar with PID 30710 (/home/pczarkowski/development/spring/hello/target/hello-0.0.1-SNAPSHOT.jar started by pczarkowski in /home/pczarkowski/development/spring/hello)
2019-02-27 15:16:41.584 INFO 30710 --- [ main] net.paulcz.hello.Application : The following profiles are active: development
2019-02-27 15:16:42.862 INFO 30710 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-02-27 15:16:42.893 INFO 30710 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-02-27 15:16:42.893 INFO 30710 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.16]
2019-02-27 15:16:42.904 INFO 30710 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib]
2019-02-27 15:16:42.974 INFO 30710 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-02-27 15:16:42.974 INFO 30710 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1297 ms
2019-02-27 15:16:43.266 INFO 30710 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-02-27 15:16:43.556 INFO 30710 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-02-27 15:16:43.561 INFO 30710 --- [ main] net.paulcz.hello.Application : Started Application in 2.546 seconds (JVM running for 2.978)

```

### With Docker

If you have Docker you can skip using Maven and Java and Build a docker image:

```console
$ docker build -t paulczar/spring-hello .
...
...
Step 9/10 : COPY --from=BUILD /src/target/$JAR /app.jar
---> 8dc7d3d7bde4
Step 10/10 : ENTRYPOINT ["java","-jar","/app.jar"]
---> Running in b96dbe5b88c0
Removing intermediate container b96dbe5b88c0
---> 5405805d6f47
Successfully built 5405805d6f47
Successfully tagged paulczar/spring-hello:latest
```

Run it:

```console
$ docker run --name spring-hello -d --rm -p 8080:8080 paulczar/spring-hello
6d47dc1ea4833f1a68c6969d4969a74a4d656b9a85600fd089b3cf0ca9716b9d

$ curl localhost:8080
hello development

$ docker rm -f spring-hello
```
6 changes: 0 additions & 6 deletions deploy/configmap.yaml

This file was deleted.

29 changes: 0 additions & 29 deletions deploy/deployment.yaml

This file was deleted.

29 changes: 0 additions & 29 deletions deploy/rbac.yaml

This file was deleted.

11 changes: 1 addition & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>
<groupId>net.paulcz</groupId>
<artifactId>hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.0.1-BASIC</version>
<name>hello</name>
<description>Hello World App</description>

Expand All @@ -29,15 +29,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- needed for spring kubernetes config reloads -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-config</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/net/paulcz/hello/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.cloud.context.restart.RestartEndpoint;

@SpringBootApplication
@RestController
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/net/paulcz/hello/MyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.cloud.context.restart.RestartEndpoint;

@Configuration
@ConfigurationProperties(prefix = "")
Expand Down
4 changes: 0 additions & 4 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
spring.profiles.active: development
logging.level.org.springframework.cloud.kubernetes: DEBUG
message: hello default
---
spring.profiles: development
message: hello development
---
spring.profiles: kubernetes
message: hello kubernetes
8 changes: 1 addition & 7 deletions src/main/resources/bootstrap.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
spring.application.name: hello
spring.profiles: default,development,kubernetes
spring.cloud.kubernetes.enabled: true
spring.cloud.kubernetes.config.enabled: true
spring.cloud.kubernetes.reload.enabled: true
spring.cloud.kubernetes.reload.mode: event
spring.cloud.kubernetes.reload.strategy: refresh
management.endpoint.restart.enabled: true
spring.profiles: default,development