Merge pull request #7 from glours/update_spring_example

Update spring-postgresql example
master
Anca Iordache 5 years ago committed by GitHub
commit 0c6fcde001
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,12 +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 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
FROM openjdk:9-jre
EXPOSE 8080 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,24 +14,37 @@
<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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId> <artifactId>spring-boot-starter-jersey</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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.session</groupId> <groupId>org.springframework.session</groupId>
@ -40,26 +53,22 @@
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId> <artifactId>spring-boot-devtools</artifactId>
</dependency> <scope>runtime</scope>
<dependency> <optional>true</optional>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> <exclusions>
<dependency> <exclusion>
<groupId>org.projectlombok</groupId> <groupId>org.junit.vintage</groupId>
<artifactId>lombok</artifactId> <artifactId>junit-vintage-engine</artifactId>
</dependency> </exclusion>
<dependency> </exclusions>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>

@ -1,15 +1,22 @@
package com.company.project.controllers; package com.company.project.controllers;
import com.company.project.entity.Greeting;
import com.company.project.repository.GreetingRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
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;
@Controller @Controller
public class HomeController { public class HomeController {
@Autowired
private GreetingRepository repository;
@GetMapping("/") @GetMapping("/")
public String showHome(String name, Model model) { public String showHome(String name, Model model) {
Greeting dockerGreeting = repository.findById(1).orElse(new Greeting("Not Found 😕"));
model = model.addAttribute("name", dockerGreeting.getName());
return "home"; return "home";
} }

@ -0,0 +1,57 @@
package com.company.project.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "GREETINGS")
public class Greeting {
@Id
private int id;
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,11 @@
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.show-sql=true
spring.datasource.url=jdbc:postgresql://db:5432/${POSTGRES_DB}
spring.datasource.username=postgres
spring.datasource.password=${POSTGRES_PASSWORD:db-wrz2z}
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 GREETINGS(name) values ('Docker');

@ -0,0 +1,4 @@
CREATE TABLE IF NOT EXISTS GREETINGS (
id serial PRIMARY KEY,
name varchar(50) NOT NULL
);

@ -5,5 +5,5 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head> </head>
<body> <body>
<p>Hello from Docker!</p> <p>Hello from ${name}!</p>
</body> </body>

@ -4,6 +4,10 @@ services:
build: backend build: backend
ports: ports:
- 80:8080 - 80:8080
environment:
POSTGRES_DB: example
networks:
- spring-postges
db: db:
environment: environment:
POSTGRES_DB: example POSTGRES_DB: example
@ -14,8 +18,12 @@ services:
- db-password - db-password
volumes: volumes:
- db-data:/var/lib/postgresql/data - db-data:/var/lib/postgresql/data
networks:
- spring-postges
volumes: volumes:
db-data: {} db-data: {}
secrets: secrets:
db-password: db-password:
file: db/password.txt file: db/password.txt
networks:
spring-postges: {}

Loading…
Cancel
Save