From 412374ed4813972df6f1c1780f21c2110d9f95ce Mon Sep 17 00:00:00 2001 From: lucaCambi77 Date: Fri, 28 Jul 2023 04:51:14 +0200 Subject: [PATCH] Docker Compose Support in Spring Boot 3 [ BAEL-6648 ] (#14394) * feat: spring boot docker compose support * fix: add actuator * fix: indent * fix: remove optional * fix: remove mongodb config * fix: add non docker-compose profile --------- Co-authored-by: Eric Martin --- spring-boot-modules/spring-boot-3/pom.xml | 70 +++++++++++++++---- .../DockerComposeApplication.java | 11 +++ .../dockercompose/config/MongoConfig.java | 42 +++++++++++ .../controller/ItemController.java | 46 ++++++++++++ .../baeldung/dockercompose/model/Item.java | 22 ++++++ .../repository/ItemRepository.java | 20 ++++++ .../resources/application-docker-compose.yml | 7 ++ .../src/main/resources/application.yml | 6 +- .../src/main/resources/docker-compose.yml | 14 ++++ 9 files changed, 222 insertions(+), 16 deletions(-) create mode 100644 spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/dockercompose/DockerComposeApplication.java create mode 100644 spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/dockercompose/config/MongoConfig.java create mode 100644 spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/dockercompose/controller/ItemController.java create mode 100644 spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/dockercompose/model/Item.java create mode 100644 spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/dockercompose/repository/ItemRepository.java create mode 100644 spring-boot-modules/spring-boot-3/src/main/resources/application-docker-compose.yml create mode 100644 spring-boot-modules/spring-boot-3/src/main/resources/docker-compose.yml diff --git a/spring-boot-modules/spring-boot-3/pom.xml b/spring-boot-modules/spring-boot-3/pom.xml index 369ba30b5a..32c69801ca 100644 --- a/spring-boot-modules/spring-boot-3/pom.xml +++ b/spring-boot-modules/spring-boot-3/pom.xml @@ -1,7 +1,7 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 spring-boot-3 0.0.1-SNAPSHOT @@ -79,12 +79,64 @@ ${mapstruct.version} true + + org.springframework.boot + spring-boot-docker-compose + + + org.springframework.boot + spring-boot-starter-data-mongodb + + + org.springframework.boot + spring-boot-starter-actuator + org.springframework.boot spring-boot-starter-test + + + default + + true + + + + + org.springframework.boot + spring-boot-maven-plugin + + com.baeldung.virtualthreads.VirtualThreadsApp + + + org.projectlombok + lombok + + + + + + + + + docker-compose + + + + org.springframework.boot + spring-boot-maven-plugin + + com.baeldung.dockercompose.DockerComposeApplication + + + + + + + @@ -112,19 +164,6 @@ - - org.springframework.boot - spring-boot-maven-plugin - - com.baeldung.virtualthreads.VirtualThreadsApp - - - org.projectlombok - lombok - - - - @@ -149,6 +188,7 @@ 3.0.0-M7 com.baeldung.sample.TodoApplication 5.14.0 + 3.1.0 0.2.0 diff --git a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/dockercompose/DockerComposeApplication.java b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/dockercompose/DockerComposeApplication.java new file mode 100644 index 0000000000..005226dedc --- /dev/null +++ b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/dockercompose/DockerComposeApplication.java @@ -0,0 +1,11 @@ +package com.baeldung.dockercompose; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DockerComposeApplication { + public static void main(String[] args) { + SpringApplication.run(DockerComposeApplication.class, args); + } +} diff --git a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/dockercompose/config/MongoConfig.java b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/dockercompose/config/MongoConfig.java new file mode 100644 index 0000000000..9ebeb840b8 --- /dev/null +++ b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/dockercompose/config/MongoConfig.java @@ -0,0 +1,42 @@ +package com.baeldung.dockercompose.config; + +import java.util.Collection; +import java.util.Collections; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration; + +import com.mongodb.ConnectionString; +import com.mongodb.MongoClientSettings; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; + +/** + * This profile is active for non docker-compose profile and will set up a MongoClient. + * When docker-compose profile is active, the application will boot with application-docker-compose.yml and the Docker Compose support will start a default configuration + */ +@Configuration +@Profile("!docker-compose") +public class MongoConfig extends AbstractMongoClientConfiguration { + + @Override + protected String getDatabaseName() { + return "test"; + } + + @Override + public MongoClient mongoClient() { + ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/test"); + MongoClientSettings mongoClientSettings = MongoClientSettings.builder() + .applyConnectionString(connectionString) + .build(); + + return MongoClients.create(mongoClientSettings); + } + + @Override + public Collection getMappingBasePackages() { + return Collections.singleton("com.baeldung.dockercompose"); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/dockercompose/controller/ItemController.java b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/dockercompose/controller/ItemController.java new file mode 100644 index 0000000000..0b2b323131 --- /dev/null +++ b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/dockercompose/controller/ItemController.java @@ -0,0 +1,46 @@ +package com.baeldung.dockercompose.controller; + +import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; + +import java.util.List; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.dockercompose.model.Item; +import com.baeldung.dockercompose.repository.ItemRepository; + +import lombok.RequiredArgsConstructor; + +@RestController +@RequestMapping("/item") +@RequiredArgsConstructor +public class ItemController { + + private final ItemRepository itemRepository; + + @PostMapping(consumes = APPLICATION_JSON_VALUE) + public ResponseEntity save(final @RequestBody Item item) { + return ResponseEntity.ok(itemRepository.save(item)); + } + + @GetMapping(produces = APPLICATION_JSON_VALUE) + public Item findByName(@RequestParam final String name) { + return itemRepository.findItemByName(name); + } + + @GetMapping(value = "category", produces = APPLICATION_JSON_VALUE) + public List findByCategory(@RequestParam final String category) { + return itemRepository.findAllByCategory(category); + } + + @GetMapping(value = "count", produces = APPLICATION_JSON_VALUE) + public long count() { + return itemRepository.count(); + } +} diff --git a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/dockercompose/model/Item.java b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/dockercompose/model/Item.java new file mode 100644 index 0000000000..7c898bc2dc --- /dev/null +++ b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/dockercompose/model/Item.java @@ -0,0 +1,22 @@ +package com.baeldung.dockercompose.model; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Document("item") +@Data +@NoArgsConstructor +@AllArgsConstructor +public class Item { + + @Id + private String id; + private String name; + private int quantity; + private String category; +} + diff --git a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/dockercompose/repository/ItemRepository.java b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/dockercompose/repository/ItemRepository.java new file mode 100644 index 0000000000..c608942e03 --- /dev/null +++ b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/dockercompose/repository/ItemRepository.java @@ -0,0 +1,20 @@ +package com.baeldung.dockercompose.repository; + +import java.util.List; + +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.mongodb.repository.Query; + +import com.baeldung.dockercompose.model.Item; + +public interface ItemRepository extends MongoRepository { + + @Query("{name:'?0'}") + Item findItemByName(String name); + + @Query(value = "{category:'?0'}") + List findAllByCategory(String category); + + long count(); +} + diff --git a/spring-boot-modules/spring-boot-3/src/main/resources/application-docker-compose.yml b/spring-boot-modules/spring-boot-3/src/main/resources/application-docker-compose.yml new file mode 100644 index 0000000000..bce67fa400 --- /dev/null +++ b/spring-boot-modules/spring-boot-3/src/main/resources/application-docker-compose.yml @@ -0,0 +1,7 @@ +spring: + docker: + compose: + enabled: true + file: docker-compose.yml + autoconfigure: + exclude: org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-3/src/main/resources/application.yml b/spring-boot-modules/spring-boot-3/src/main/resources/application.yml index 5f9031bc9e..3885e59a61 100644 --- a/spring-boot-modules/spring-boot-3/src/main/resources/application.yml +++ b/spring-boot-modules/spring-boot-3/src/main/resources/application.yml @@ -15,8 +15,12 @@ spring: hibernate: dialect: org.hibernate.dialect.H2Dialect thread-executor: standard + docker: + compose: + enabled: false + autoconfigure: + exclude: org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration -# Custom Properties cors: allow: origins: ${CORS_ALLOWED_ORIGINS:*} diff --git a/spring-boot-modules/spring-boot-3/src/main/resources/docker-compose.yml b/spring-boot-modules/spring-boot-3/src/main/resources/docker-compose.yml new file mode 100644 index 0000000000..4bfc2349bd --- /dev/null +++ b/spring-boot-modules/spring-boot-3/src/main/resources/docker-compose.yml @@ -0,0 +1,14 @@ +version: '3.8' +services: + db: + image: mongo:latest + ports: + - '27017:27017' + volumes: + - db:/data/db + labels: + org.springframework.boot.service-connection: mongo +volumes: + db: + driver: + local \ No newline at end of file