Merge pull request #13 from glours/update_react-java-mysql_sample

Update Spring Boot and Mysql version for react-java-mysql sample
master
Guillaume LOURS 5 years ago committed by GitHub
commit 6e866bda0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -23,17 +23,17 @@ services:
build: backend build: backend
... ...
db: db:
image: mysql:5.7 image: mysql:8.0.19
... ...
frontend: frontend:
build: frontend build: frontend
ports: ports:
- 80:9000 - 9000:9000
... ...
``` ```
The compose file defines an application with three services `frontend`, `backend` and `db`. 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. 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 80 on the host is not already being in use. Make sure port 9000 on the host is not already being in use.
## Deploy with docker-compose ## 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 $ docker-compose up -d
Creating network "react-java-mysql_default" with the default driver Creating network "react-java-mysql_default" with the default driver
Building backend 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 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`. 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 $ docker ps
ONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 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 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 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:5.7 "docker-entrypoint.s…" 39 seconds ago Up 37 seconds 3306/tcp, 33060/tcp react-java-mysql_db_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 My New React App
``` ```

@ -1,11 +1,20 @@
FROM maven:3.5-jdk-9 AS build FROM maven:3.6.3-jdk-11 AS builder
COPY pom.xml . WORKDIR /workdir/server
RUN mvn --batch-mode dependency:resolve COPY pom.xml /workdir/server/pom.xml
COPY . . RUN mvn dependency:go-offline
RUN mvn --batch-mode package
RUN cp target/*jar target/app.jar
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 VOLUME /tmp
COPY --from=build target/app.jar app.jar ARG DEPENDENCY=/workdir/server/target/dependency
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] 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"]

@ -14,52 +14,61 @@
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version> <version>2.2.5.RELEASE</version>
<relativePath/> <relativePath/>
</parent> </parent>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version> <java.version>11</java.version>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-jersey</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.session</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-session-core</artifactId> <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>mysql</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>mysql-connector-java</artifactId>
<scope>test</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>lombok</artifactId> <artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.xml.bind</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>jaxb-api</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<version>2.3.0</version> <scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>

@ -1,16 +1,21 @@
package com.company.project.controllers; 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.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; 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 { public class HomeController {
@Autowired
private GreetingRepository repository;
@GetMapping("/") @GetMapping("/")
public String showHome(String name, Model model) { public Greeting showHome(String name, Model model) {
return "home"; return repository.findById(1).orElse(new Greeting("Not Found 😕"));
} }
} }

@ -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();
}
}

@ -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<Greeting, Integer> {
}

@ -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

@ -0,0 +1 @@
INSERT INTO example.greetings(name) values ('Docker');

@ -0,0 +1,5 @@
CREATE TABLE IF NOT EXISTS example.greetings (
id INTEGER AUTO_INCREMENT,
name varchar(50) NOT NULL,
PRIMARY KEY (id)
);

@ -1,9 +0,0 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>Hello from Docker!</p>
</body>

@ -2,25 +2,41 @@ version: "3.7"
services: services:
backend: backend:
build: backend build: backend
environment:
MYSQL_HOST: db
networks:
- react-spring
- spring-mysql
depends_on:
- db
db: db:
environment: environment:
MYSQL_DATABASE: example MYSQL_DATABASE: example
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db-password MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db-password
image: mysql:5.7 image: mysql:8.0.19
restart: always restart: always
secrets: secrets:
- db-password - db-password
volumes: volumes:
- db-data:/var/lib/mysql - db-data:/var/lib/mysql
networks:
- spring-mysql
frontend: frontend:
build: frontend build: frontend
ports: ports:
- 80:9000 - 9000:9000
volumes: volumes:
- ./frontend:/project - ./frontend:/project
- /project/node_modules - /project/node_modules
networks:
- react-spring
depends_on:
- backend
volumes: volumes:
db-data: {} db-data: {}
secrets: secrets:
db-password: db-password:
file: db/password.txt file: db/password.txt
networks:
react-spring: {}
spring-mysql: {}
Loading…
Cancel
Save