first commit
This commit is contained in:
commit
1b5e68eea9
23
Dockerfile
Normal file
23
Dockerfile
Normal file
@ -0,0 +1,23 @@
|
||||
FROM harbor.hzbb.top/tools/maven:v3.8.8
|
||||
|
||||
MAINTAINER hzbb2221
|
||||
|
||||
ENV LANG=C.UTF-8 JVM_OPTS_ENV=$JVM_OPTS AGENT_NAME_ENV=$AGENT_NAME SW_ADDR_ENV=$SW_ADDR
|
||||
|
||||
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&\
|
||||
echo 'Asia/Shanghai' >/etc/timezone
|
||||
|
||||
COPY . /data/springboot-helloworld
|
||||
WORKDIR /data/springboot-helloworld
|
||||
|
||||
RUN mvn clean install
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
CMD java -jar \
|
||||
$JVM_OPTS_ENV \
|
||||
-XX:+UnlockExperimentalVMOptions -XX:MaxRAMFraction=1 \
|
||||
#-javaagent:/tmp/agent/skywalking-agent.jar \
|
||||
#-Dskywalking.agent.service_name=$AGENT_NAME_ENV \
|
||||
#-Dskywalking.collector.backend_service=$SW_ADDR_ENV \
|
||||
target/helloworld-0.0.1-SNAPSHOT.jar
|
39
README.md
Normal file
39
README.md
Normal file
@ -0,0 +1,39 @@
|
||||
# Spring Boot Hello World
|
||||
|
||||
A spring boot enabled hello world application
|
||||
|
||||
[](https://travis-ci.org/gazgeek/springboot-helloworld)
|
||||
|
||||
[](https://coveralls.io/r/gazgeek/springboot-helloworld)
|
||||
|
||||
- Travis CI build and test
|
||||
- Continuous deployment to Heroku on success
|
||||
|
||||
## Usage
|
||||
|
||||
- Directly using maven
|
||||
```
|
||||
mvn spring-boot:run
|
||||
```
|
||||
|
||||
- From within your IDE right click run
|
||||
```
|
||||
Application.java
|
||||
```
|
||||
|
||||
- From executable jar file
|
||||
```
|
||||
mvn clean install
|
||||
java -jar target/helloworld-0.0.1-SNAPSHOT.jar
|
||||
```
|
||||
|
||||
- To run this as a docker application (assumption docker is installed on your machine)
|
||||
```
|
||||
docker pull gazgeek/springboot-helloworld
|
||||
docker container run -p 8080:8080 -d gazgeek/springboot-helloworld
|
||||
|
||||
Go to Browser and type http://localhost:8080/ or do curl http://localhost:8080/ on command prompt
|
||||
```
|
||||
|
||||
|
||||
|
12
docker-compose.yml
Normal file
12
docker-compose.yml
Normal file
@ -0,0 +1,12 @@
|
||||
version: '3'
|
||||
services:
|
||||
springboot-helloworld:
|
||||
build:
|
||||
context: ./
|
||||
dockerfile: Dockerfile
|
||||
container_name: springboot-helloworld
|
||||
restart: always
|
||||
environment:
|
||||
- JVM_OPTS_ENV=-Xms1024m -Xmx1024m -Xmn512m -XX:+UseConcMarkSweepGC
|
||||
ports:
|
||||
- 8080:8080
|
64
pom.xml
Normal file
64
pom.xml
Normal file
@ -0,0 +1,64 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
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>
|
||||
|
||||
<groupId>com.gazgeek</groupId>
|
||||
<artifactId>helloworld</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.2.2.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.jacoco</groupId>
|
||||
<artifactId>jacoco-maven-plugin</artifactId>
|
||||
<version>0.7.2.201409121644</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>prepare-agent</id>
|
||||
<goals>
|
||||
<goal>prepare-agent</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.eluder.coveralls</groupId>
|
||||
<artifactId>coveralls-maven-plugin</artifactId>
|
||||
<version>3.0.1</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
17
src/main/java/com/gazgeek/helloworld/Application.java
Normal file
17
src/main/java/com/gazgeek/helloworld/Application.java
Normal file
@ -0,0 +1,17 @@
|
||||
package com.gazgeek.helloworld;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan
|
||||
public class Application {
|
||||
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.gazgeek.helloworld.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class HomeController {
|
||||
|
||||
@RequestMapping("/")
|
||||
String home() {
|
||||
return "Hello from GazGeek!";
|
||||
}
|
||||
|
||||
}
|
1
src/main/resources/application.properties
Normal file
1
src/main/resources/application.properties
Normal file
@ -0,0 +1 @@
|
||||
server.port: ${port:8080}
|
75
src/test/java/com/gazgeek/helloworld/HealthTest.java
Normal file
75
src/test/java/com/gazgeek/helloworld/HealthTest.java
Normal file
@ -0,0 +1,75 @@
|
||||
package com.gazgeek.helloworld;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.boot.test.TestRestTemplate;
|
||||
import org.springframework.boot.test.WebIntegrationTest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.springframework.http.HttpStatus.OK;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = Application.class)
|
||||
@WebIntegrationTest(randomPort = true)
|
||||
public class HealthTest {
|
||||
|
||||
@Value("${local.server.port}")
|
||||
private Integer port;
|
||||
|
||||
private RestTemplate restTemplate = new TestRestTemplate();
|
||||
|
||||
|
||||
@Test
|
||||
public void checkHealth() {
|
||||
getRequest("/health")
|
||||
.assertStatusCode(OK)
|
||||
.assertResponseBody("{\"status\":\"UP\"}");
|
||||
}
|
||||
|
||||
|
||||
private HealthResponse getRequest(String uri) {
|
||||
return new HealthResponse(restTemplate.getForEntity(getUri(uri), String.class));
|
||||
}
|
||||
|
||||
|
||||
protected URI getUri(String uri) {
|
||||
return UriComponentsBuilder
|
||||
.newInstance()
|
||||
.scheme("http")
|
||||
.host("localhost")
|
||||
.port(port)
|
||||
.path(uri)
|
||||
.build()
|
||||
.toUri();
|
||||
}
|
||||
|
||||
|
||||
private static class HealthResponse {
|
||||
|
||||
private ResponseEntity<String> responseEntity;
|
||||
|
||||
public HealthResponse(ResponseEntity<String> responseEntity) {
|
||||
this.responseEntity = responseEntity;
|
||||
}
|
||||
|
||||
public HealthResponse assertStatusCode(HttpStatus expected) {
|
||||
assertThat(responseEntity.getStatusCode(), is(expected));
|
||||
return this;
|
||||
}
|
||||
|
||||
public HealthResponse assertResponseBody(String expected) {
|
||||
assertThat(responseEntity.getBody(), is(expected));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
26
src/test/java/com/gazgeek/helloworld/HelloWorldResponse.java
Normal file
26
src/test/java/com/gazgeek/helloworld/HelloWorldResponse.java
Normal file
@ -0,0 +1,26 @@
|
||||
package com.gazgeek.helloworld;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class HelloWorldResponse {
|
||||
|
||||
private ResponseEntity<String> responseEntity;
|
||||
|
||||
public HelloWorldResponse(ResponseEntity<String> entity) {
|
||||
this.responseEntity = entity;
|
||||
}
|
||||
|
||||
public HelloWorldResponse assertStatusCode(HttpStatus expected) {
|
||||
assertThat(responseEntity.getStatusCode(), is(expected));
|
||||
return this;
|
||||
}
|
||||
|
||||
public HelloWorldResponse assertResponseBody(String expected) {
|
||||
assertThat(responseEntity.getBody(), is(expected));
|
||||
return this;
|
||||
}
|
||||
}
|
50
src/test/java/com/gazgeek/helloworld/HomeControllerTest.java
Normal file
50
src/test/java/com/gazgeek/helloworld/HomeControllerTest.java
Normal file
@ -0,0 +1,50 @@
|
||||
package com.gazgeek.helloworld;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.boot.test.TestRestTemplate;
|
||||
import org.springframework.boot.test.WebIntegrationTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import static org.springframework.http.HttpStatus.OK;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = Application.class)
|
||||
@WebIntegrationTest(randomPort = true)
|
||||
public class HomeControllerTest {
|
||||
|
||||
@Value("${local.server.port}")
|
||||
private Integer port;
|
||||
|
||||
private RestTemplate restTemplate = new TestRestTemplate();
|
||||
|
||||
@Test
|
||||
public void helloWorld() {
|
||||
getRequest("/")
|
||||
.assertStatusCode(OK)
|
||||
.assertResponseBody("Hello from GazGeek!");
|
||||
}
|
||||
|
||||
private HelloWorldResponse getRequest(String uri) {
|
||||
return new HelloWorldResponse(restTemplate.getForEntity(getUri(uri), String.class));
|
||||
}
|
||||
|
||||
|
||||
protected URI getUri(String uri) {
|
||||
return UriComponentsBuilder
|
||||
.newInstance()
|
||||
.scheme("http")
|
||||
.host("localhost")
|
||||
.port(port)
|
||||
.path(uri)
|
||||
.build()
|
||||
.toUri();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user