From e95d18bcfd1de21ecc6ea51b9d056d8d701f2a69 Mon Sep 17 00:00:00 2001 From: luyanbo Date: Thu, 10 Mar 2022 20:26:09 +0800 Subject: [PATCH] init commit --- .gitignore | 1 + Dockerfile | 15 +++++ build.sh | 4 ++ deployment.yaml | 24 +++++++ pom.xml | 64 +++++++++++++++++++ .../com/example/springboot/Application.java | 19 ++++++ .../example/springboot/HelloController.java | 19 ++++++ .../com/example/springboot/WebConfig.java | 21 ++++++ src/main/resources/application.yaml | 15 +++++ src/main/resources/static/build/index.html | 1 + .../example/springboot/HelloControllerIT.java | 23 +++++++ .../springboot/HelloControllerTest.java | 29 +++++++++ start.sh | 3 + 13 files changed, 238 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 build.sh create mode 100644 deployment.yaml create mode 100644 pom.xml create mode 100644 src/main/java/com/example/springboot/Application.java create mode 100644 src/main/java/com/example/springboot/HelloController.java create mode 100644 src/main/java/com/example/springboot/WebConfig.java create mode 100644 src/main/resources/application.yaml create mode 100644 src/main/resources/static/build/index.html create mode 100644 src/test/java/com/example/springboot/HelloControllerIT.java create mode 100644 src/test/java/com/example/springboot/HelloControllerTest.java create mode 100755 start.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..81e01e3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM eclipse-temurin:11-jdk-alpine + +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories +RUN apk add curl vim iproute2 tcpdump + +# copy arthas +COPY --from=hengyunabc/arthas:latest /opt/arthas /opt/arthas + +WORKDIR /app +COPY /target/spring-boot-complete-0.0.1-SNAPSHOT.jar /app/opensergo-sca.jar +COPY /start.sh /app + +EXPOSE 20002 +CMD ["/app/start.sh"] + diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..cc7efc8 --- /dev/null +++ b/build.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +mvn clean package -DskipTests +docker build . -t registry.cn-huhehaote.aliyuncs.com/luyanbo-msc/opensergo-sca:0.0.1-SNAPSHOT diff --git a/deployment.yaml b/deployment.yaml new file mode 100644 index 0000000..04070d1 --- /dev/null +++ b/deployment.yaml @@ -0,0 +1,24 @@ +apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1 +kind: Deployment +metadata: + name: luyanbo-opensergo-sca + labels: + app: luyanbo-opensergo-sca +spec: + replicas: 2 + selector: + matchLabels: + app: luyanbo-opensergo-sca + template: + metadata: + labels: + app: luyanbo-opensergo-sca + spec: + containers: + - name: luyanbo-opensergo-sca + image: registry.cn-huhehaote.aliyuncs.com/luyanbo-msc/opensergo-sca:0.0.1-SNAPSHOT + ports: + - containerPort: 9991 + resources: + limits: + cpu: "500m" \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..afc537c --- /dev/null +++ b/pom.xml @@ -0,0 +1,64 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.3 + + + com.example + spring-boot-complete + 0.0.1-SNAPSHOT + spring-boot-complete + Demo project for Spring Boot + + + 1.8 + + + + + io.grpc + grpc-okhttp + 1.44.1 + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-opensergo + 2021.0.1.1-SNAPSHOT + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-actuator + + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/src/main/java/com/example/springboot/Application.java b/src/main/java/com/example/springboot/Application.java new file mode 100644 index 0000000..c85dadd --- /dev/null +++ b/src/main/java/com/example/springboot/Application.java @@ -0,0 +1,19 @@ +package com.example.springboot; + +import java.util.Arrays; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + + +} diff --git a/src/main/java/com/example/springboot/HelloController.java b/src/main/java/com/example/springboot/HelloController.java new file mode 100644 index 0000000..e1a7508 --- /dev/null +++ b/src/main/java/com/example/springboot/HelloController.java @@ -0,0 +1,19 @@ +package com.example.springboot; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HelloController { + + @GetMapping("/index") + public String index() { + return "Greetings from Spring Boot!\n"; + } + + @GetMapping("/hello") + public String hello() { + return "Hello from Spring Boot!\n"; + } + +} diff --git a/src/main/java/com/example/springboot/WebConfig.java b/src/main/java/com/example/springboot/WebConfig.java new file mode 100644 index 0000000..8a42ebd --- /dev/null +++ b/src/main/java/com/example/springboot/WebConfig.java @@ -0,0 +1,21 @@ +package com.example.springboot; + + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebConfig implements WebMvcConfigurer { + + private final Logger logger = LoggerFactory.getLogger(WebConfig.class); + + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addViewController("/").setViewName("forward:/build/index.html"); + } +} diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml new file mode 100644 index 0000000..1b61e12 --- /dev/null +++ b/src/main/resources/application.yaml @@ -0,0 +1,15 @@ +server: + port: 9991 + + +spring: + application: + name: "luyanbo-opensergo" + +management: + endpoint: + info: + enabled: true + web: + exposure: + include: "*" \ No newline at end of file diff --git a/src/main/resources/static/build/index.html b/src/main/resources/static/build/index.html new file mode 100644 index 0000000..05a682b --- /dev/null +++ b/src/main/resources/static/build/index.html @@ -0,0 +1 @@ +Hello! \ No newline at end of file diff --git a/src/test/java/com/example/springboot/HelloControllerIT.java b/src/test/java/com/example/springboot/HelloControllerIT.java new file mode 100644 index 0000000..caee476 --- /dev/null +++ b/src/test/java/com/example/springboot/HelloControllerIT.java @@ -0,0 +1,23 @@ +package com.example.springboot; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.ResponseEntity; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class HelloControllerIT { + + @Autowired + private TestRestTemplate template; + + @Test + public void getHello() throws Exception { + ResponseEntity response = template.getForEntity("/", String.class); + assertThat(response.getBody()).isEqualTo("Greetings from Spring Boot!"); + } +} diff --git a/src/test/java/com/example/springboot/HelloControllerTest.java b/src/test/java/com/example/springboot/HelloControllerTest.java new file mode 100644 index 0000000..81262f4 --- /dev/null +++ b/src/test/java/com/example/springboot/HelloControllerTest.java @@ -0,0 +1,29 @@ +package com.example.springboot; + +import static org.hamcrest.Matchers.equalTo; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +@SpringBootTest +@AutoConfigureMockMvc +public class HelloControllerTest { + + @Autowired + private MockMvc mvc; + + @Test + public void getHello() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string(equalTo("Greetings from Spring Boot!"))); + } +} diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..e75b69e --- /dev/null +++ b/start.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +java -jar /app/opensergo-sca.jar