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:
parent
f82330cd1b
commit
a8001c3a8e
|
@ -1,7 +1,7 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
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">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>spring-boot-3</artifactId>
|
<artifactId>spring-boot-3</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
@ -79,12 +79,64 @@
|
||||||
<version>${mapstruct.version}</version>
|
<version>${mapstruct.version}</version>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</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>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</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>
|
<build>
|
||||||
<pluginManagement>
|
<pluginManagement>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
@ -112,19 +164,6 @@
|
||||||
</annotationProcessorPaths>
|
</annotationProcessorPaths>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</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>
|
</plugins>
|
||||||
</pluginManagement>
|
</pluginManagement>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
@ -149,6 +188,7 @@
|
||||||
<maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
|
<maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
|
||||||
<start-class>com.baeldung.sample.TodoApplication</start-class>
|
<start-class>com.baeldung.sample.TodoApplication</start-class>
|
||||||
<mockserver.version>5.14.0</mockserver.version>
|
<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>
|
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
|
@ -15,8 +15,12 @@ spring:
|
||||||
hibernate:
|
hibernate:
|
||||||
dialect: org.hibernate.dialect.H2Dialect
|
dialect: org.hibernate.dialect.H2Dialect
|
||||||
thread-executor: standard
|
thread-executor: standard
|
||||||
|
docker:
|
||||||
|
compose:
|
||||||
|
enabled: false
|
||||||
|
autoconfigure:
|
||||||
|
exclude: org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
|
||||||
|
|
||||||
# Custom Properties
|
|
||||||
cors:
|
cors:
|
||||||
allow:
|
allow:
|
||||||
origins: ${CORS_ALLOWED_ORIGINS:*}
|
origins: ${CORS_ALLOWED_ORIGINS:*}
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue