From d7d79e436ff0f7f06c796397be978b094db13e0e Mon Sep 17 00:00:00 2001 From: Guillaume Lours Date: Tue, 17 Mar 2020 16:28:17 +0100 Subject: [PATCH] Update Spring Boot and Mysql version for react-java-mysql sample Signed-off-by: Guillaume Lours --- react-java-mysql/README.md | 16 ++--- react-java-mysql/backend/Dockerfile | 27 +++++--- react-java-mysql/backend/pom.xml | 45 ++++++++------ .../project/controllers/HomeController.java | 15 +++-- .../com/company/project/entity/Greeting.java | 61 +++++++++++++++++++ .../repository/GreetingRepository.java | 9 +++ .../src/main/resources/application.properties | 11 ++++ .../backend/src/main/resources/data.sql | 1 + .../backend/src/main/resources/schema.sql | 5 ++ .../src/main/resources/templates/home.ftl | 9 --- react-java-mysql/docker-compose.yaml | 28 +++++++-- 11 files changed, 172 insertions(+), 55 deletions(-) create mode 100644 react-java-mysql/backend/src/main/java/com/company/project/entity/Greeting.java create mode 100644 react-java-mysql/backend/src/main/java/com/company/project/repository/GreetingRepository.java create mode 100644 react-java-mysql/backend/src/main/resources/data.sql create mode 100644 react-java-mysql/backend/src/main/resources/schema.sql delete mode 100755 react-java-mysql/backend/src/main/resources/templates/home.ftl diff --git a/react-java-mysql/README.md b/react-java-mysql/README.md index 7fccc44..2b3bf1d 100644 --- a/react-java-mysql/README.md +++ b/react-java-mysql/README.md @@ -23,17 +23,17 @@ services: build: backend ... db: - image: mysql:5.7 + image: mysql:8.0.19 ... frontend: build: frontend ports: - - 80:9000 + - 9000:9000 ... ``` The compose file defines an application with three services `frontend`, `backend` and `db`. -When deploying the application, docker-compose maps port 80 of the frontend service container to port 9000 of the host as specified in the file. -Make sure port 80 on the host is not already being in use. +When deploying the application, docker-compose maps port 9000 of the frontend service container to port 9000 of the host as specified in the file. +Make sure port 9000 on the host is not already being in use. ## Deploy with docker-compose @@ -41,7 +41,7 @@ Make sure port 80 on the host is not already being in use. $ docker-compose up -d Creating network "react-java-mysql_default" with the default driver Building backend -Step 1/10 : FROM maven:3.5-jdk-9 AS build +Step 1/17 : FROM maven:3.6.3-jdk-11 AS builder ... Successfully tagged react-java-mysql_frontend:latest WARNING: Image for service frontend was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`. @@ -57,11 +57,11 @@ Listing containers must show three containers running and the port mapping as be $ docker ps ONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a63dee74d79e react-java-mysql_backend "java -Djava.securit…" 39 seconds ago Up 37 seconds react-java-mysql_backend_1 -6a7364c0812e react-java-mysql_frontend "docker-entrypoint.s…" 39 seconds ago Up 33 seconds 0.0.0.0:80->9000/tcp react-java-mysql_frontend_1 -b176b18fbec4 mysql:5.7 "docker-entrypoint.s…" 39 seconds ago Up 37 seconds 3306/tcp, 33060/tcp react-java-mysql_db_1 +6a7364c0812e react-java-mysql_frontend "docker-entrypoint.s…" 39 seconds ago Up 33 seconds 0.0.0.0:9000->9000/tcp react-java-mysql_frontend_1 +b176b18fbec4 mysql:8.0.19 "docker-entrypoint.s…" 39 seconds ago Up 37 seconds 3306/tcp, 33060/tcp react-java-mysql_db_1 ``` -After the application starts, navigate to `http://localhost:80` in your web browser to get a colorful message. +After the application starts, navigate to `http://localhost:9000` in your web browser to get a colorful message. ``` My New React App ``` diff --git a/react-java-mysql/backend/Dockerfile b/react-java-mysql/backend/Dockerfile index 265d758..949dd0a 100755 --- a/react-java-mysql/backend/Dockerfile +++ b/react-java-mysql/backend/Dockerfile @@ -1,11 +1,20 @@ -FROM maven:3.5-jdk-9 AS build -COPY pom.xml . -RUN mvn --batch-mode dependency:resolve -COPY . . -RUN mvn --batch-mode package -RUN cp target/*jar target/app.jar +FROM maven:3.6.3-jdk-11 AS builder +WORKDIR /workdir/server +COPY pom.xml /workdir/server/pom.xml +RUN mvn dependency:go-offline -FROM openjdk:9-jre +COPY src /workdir/server/src +RUN mvn install +RUN mkdir -p target/depency +WORKDIR /workdir/server/target/dependency +RUN jar -xf ../*.jar + +FROM openjdk:11-jre-slim + +EXPOSE 8080 VOLUME /tmp -COPY --from=build target/app.jar app.jar -ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] +ARG DEPENDENCY=/workdir/server/target/dependency +COPY --from=builder ${DEPENDENCY}/BOOT-INF/lib /app/lib +COPY --from=builder ${DEPENDENCY}/META-INF /app/META-INF +COPY --from=builder ${DEPENDENCY}/BOOT-INF/classes /app +ENTRYPOINT ["java","-cp","app:app/lib/*","com.company.project.Application"] \ No newline at end of file diff --git a/react-java-mysql/backend/pom.xml b/react-java-mysql/backend/pom.xml index aeef767..edd21fa 100755 --- a/react-java-mysql/backend/pom.xml +++ b/react-java-mysql/backend/pom.xml @@ -14,52 +14,61 @@ org.springframework.boot spring-boot-starter-parent - 2.0.3.RELEASE + 2.2.5.RELEASE UTF-8 UTF-8 - 1.8 + 11 + + org.springframework.boot + spring-boot-starter-web + org.springframework.boot spring-boot-starter-jersey org.springframework.boot - spring-boot-starter-web + spring-boot-starter-jdbc - org.springframework.session - spring-session-core + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-data-jpa + + + mysql + mysql-connector-java + runtime org.springframework.boot spring-boot-devtools - - - org.springframework.boot - spring-boot-starter-freemarker + runtime + true org.springframework.boot spring-boot-starter-test test - - - org.projectlombok - lombok - - - javax.xml.bind - jaxb-api - 2.3.0 + + + org.junit.vintage + junit-vintage-engine + + + diff --git a/react-java-mysql/backend/src/main/java/com/company/project/controllers/HomeController.java b/react-java-mysql/backend/src/main/java/com/company/project/controllers/HomeController.java index 966839d..083e340 100755 --- a/react-java-mysql/backend/src/main/java/com/company/project/controllers/HomeController.java +++ b/react-java-mysql/backend/src/main/java/com/company/project/controllers/HomeController.java @@ -1,16 +1,21 @@ package com.company.project.controllers; -import org.springframework.stereotype.Controller; +import com.company.project.entity.Greeting; +import com.company.project.repository.GreetingRepository; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; -@Controller +@RestController public class HomeController { + @Autowired + private GreetingRepository repository; + @GetMapping("/") - public String showHome(String name, Model model) { - return "home"; + public Greeting showHome(String name, Model model) { + return repository.findById(1).orElse(new Greeting("Not Found 😕")); } } diff --git a/react-java-mysql/backend/src/main/java/com/company/project/entity/Greeting.java b/react-java-mysql/backend/src/main/java/com/company/project/entity/Greeting.java new file mode 100644 index 0000000..1b9fb21 --- /dev/null +++ b/react-java-mysql/backend/src/main/java/com/company/project/entity/Greeting.java @@ -0,0 +1,61 @@ +package com.company.project.entity; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Column; +import javax.persistence.GeneratedValue; + +@Entity +@Table(name = "greetings") +public class Greeting { + + @Id + @GeneratedValue + private int id; + @Column(nullable = false) + private String name; + + public Greeting() { + } + + public Greeting(String name) { + this.name = name; + } + + public Greeting(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Greeting greeting = (Greeting) o; + + return name.equals(greeting.name); + } + + @Override + public int hashCode() { + return name.hashCode(); + } +} diff --git a/react-java-mysql/backend/src/main/java/com/company/project/repository/GreetingRepository.java b/react-java-mysql/backend/src/main/java/com/company/project/repository/GreetingRepository.java new file mode 100644 index 0000000..9c5ab6f --- /dev/null +++ b/react-java-mysql/backend/src/main/java/com/company/project/repository/GreetingRepository.java @@ -0,0 +1,9 @@ +package com.company.project.repository; + +import com.company.project.entity.Greeting; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface GreetingRepository extends CrudRepository { +} \ No newline at end of file diff --git a/react-java-mysql/backend/src/main/resources/application.properties b/react-java-mysql/backend/src/main/resources/application.properties index 8b13789..6aa9b40 100755 --- a/react-java-mysql/backend/src/main/resources/application.properties +++ b/react-java-mysql/backend/src/main/resources/application.properties @@ -1 +1,12 @@ +spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect +spring.jpa.hibernate.ddl-auto=none +spring.jpa.hibernate.show-sql=true + +spring.datasource.url=jdbc:mysql://${MYSQL_HOST:db}:3306/example +spring.datasource.username=root +spring.datasource.password=${MYSQL_PASSWORD:db-57xsl} +spring.datasource.initialization-mode=always +spring.datasource.initialize=true +spring.datasource.schema=classpath:/schema.sql +spring.datasource.continue-on-error=true diff --git a/react-java-mysql/backend/src/main/resources/data.sql b/react-java-mysql/backend/src/main/resources/data.sql new file mode 100644 index 0000000..71a06ba --- /dev/null +++ b/react-java-mysql/backend/src/main/resources/data.sql @@ -0,0 +1 @@ +INSERT INTO example.greetings(name) values ('Docker'); diff --git a/react-java-mysql/backend/src/main/resources/schema.sql b/react-java-mysql/backend/src/main/resources/schema.sql new file mode 100644 index 0000000..6901a3a --- /dev/null +++ b/react-java-mysql/backend/src/main/resources/schema.sql @@ -0,0 +1,5 @@ +CREATE TABLE IF NOT EXISTS example.greetings ( + id INTEGER AUTO_INCREMENT, + name varchar(50) NOT NULL, + PRIMARY KEY (id) +); diff --git a/react-java-mysql/backend/src/main/resources/templates/home.ftl b/react-java-mysql/backend/src/main/resources/templates/home.ftl deleted file mode 100755 index daba182..0000000 --- a/react-java-mysql/backend/src/main/resources/templates/home.ftl +++ /dev/null @@ -1,9 +0,0 @@ - - - - Getting Started: Serving Web Content - - - -

Hello from Docker!

- diff --git a/react-java-mysql/docker-compose.yaml b/react-java-mysql/docker-compose.yaml index f2621d6..33f89ea 100644 --- a/react-java-mysql/docker-compose.yaml +++ b/react-java-mysql/docker-compose.yaml @@ -2,25 +2,41 @@ version: "3.7" services: backend: build: backend + environment: + MYSQL_HOST: db + networks: + - react-spring + - spring-mysql + depends_on: + - db db: environment: MYSQL_DATABASE: example MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db-password - image: mysql:5.7 + image: mysql:8.0.19 restart: always secrets: - - db-password + - db-password volumes: - - db-data:/var/lib/mysql + - db-data:/var/lib/mysql + networks: + - spring-mysql frontend: build: frontend ports: - - 80:9000 + - 9000:9000 volumes: - - ./frontend:/project - - /project/node_modules + - ./frontend:/project + - /project/node_modules + networks: + - react-spring + depends_on: + - backend volumes: db-data: {} secrets: db-password: file: db/password.txt +networks: + react-spring: {} + spring-mysql: {} \ No newline at end of file