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 <emart.tigers@gmail.com>
This commit is contained in:
lucaCambi77 2023-07-28 04:51:14 +02:00 committed by GitHub
parent f82330cd1b
commit a8001c3a8e
9 changed files with 222 additions and 16 deletions

View File

@ -79,12 +79,64 @@
<version>${mapstruct.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-docker-compose</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.baeldung.virtualthreads.VirtualThreadsApp</mainClass>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>docker-compose</id>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.baeldung.dockercompose.DockerComposeApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build>
<pluginManagement>
<plugins>
@ -112,19 +164,6 @@
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.baeldung.virtualthreads.VirtualThreadsApp</mainClass>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
@ -149,6 +188,7 @@
<maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
<start-class>com.baeldung.sample.TodoApplication</start-class>
<mockserver.version>5.14.0</mockserver.version>
<spring-boot.version>3.1.0</spring-boot.version>
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
</properties>

View File

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

View File

@ -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<String> getMappingBasePackages() {
return Collections.singleton("com.baeldung.dockercompose");
}
}

View File

@ -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<Item> 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<Item> findByCategory(@RequestParam final String category) {
return itemRepository.findAllByCategory(category);
}
@GetMapping(value = "count", produces = APPLICATION_JSON_VALUE)
public long count() {
return itemRepository.count();
}
}

View File

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

View File

@ -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<Item, String> {
@Query("{name:'?0'}")
Item findItemByName(String name);
@Query(value = "{category:'?0'}")
List<Item> findAllByCategory(String category);
long count();
}

View File

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

View File

@ -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:*}

View File

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