본문 바로가기
Back-end/Spring Boot

SpringBoot 3.1의 ConnectionDetails abstraction

by whatamigonnabe 2023. 8. 1.

https://spring.io/blog/2023/06/19/spring-boot-31-connectiondetails-abstraction를 간단히 요약해보았습니다.

 

spring.datasource.url 와 같이 properties로 remove service와 연결하던 것을 ConnectionDetails 라는 추상화를 통해 표현하게 됐습니다. 이것은 주로 새롭게 추가된 Docker Compose 와 Testcontainers 기능에 쓰입니다. ConnectionDetails Bean이 등록되어 있으면 이것을 사용하고, 그렇지 않으면 기존처럼 properties를 사용합니다.

public interface JdbcConnectionDetails extends ConnectionDetails {

  String getUsername();

  String getPassword();

  String getJdbcUrl();

}

일례로 JdbcConnectionDetails를 보면 각각의 메서드는 spring.datasource.url, spring.datasource.username and spring.datasource.password properties과 같습니다.

이것을 통해서 Connection pool 설정이나 Pool SIze 설정은 못하고, 기존처럼 properties를 사용해야합니다.

이 기능은 VMware Tanzu 클라우드를 사용한다면, 자동으로 ConnectionDetails를 제공하기 때문에 알아서 데이터 베이스 서버와 연결하게 되는 등, 유용하게 사용합니다. 쿠버네티스에서 설정을 고민하는 대신, 자동으로 알아서 연결해주기 때문에 편합니다.

또한, 어플리케이션 밖에서 properties를 사용하게 되면, 이것은 바뀔 수 있지만 여전히 컴파일되고 에러를 냅니다. 대신에 ConnectionDetails를 사용하면 컴파일 에러를 내기 때문에 훨씬 수월하게 오류를 찾을 수 있습니다.

아래와 같이 사용할 수 있습니다.

@Configuration(proxyBeanMethods = false)
class MyConnectionDetailsConfiguration {

  @Bean
  JdbcConnectionDetails myJdbcConnectionDetails() {
    return new JdbcConnectionDetails() {

      @Override
      public String getUsername() {
        return "myuser";
      }

      @Override
      public String getPassword() {
        return "3xtr3mly-s3cr3t";
      }

      @Override
      public String getJdbcUrl() {
        return "jdbc:postgresql://postgres-server.svc.local:5432/mydatabase?ssl=true&sslmode=required";
      }

    };
  }

}

아래와 같은 ConnectionDetails를 제공합니다.

  • CassandraConnectionDetails for connections to a Cassandra server
  • CouchbaseConnectionDetails for connections to a Couchbase server
  • ElasticsearchConnectionDetails for connections to an Elasticsearch server
  • JdbcConnectionDetails for connections to a database server through JDBC
  • KafkaConnectionDetails for connections to a Kafka server
  • MongoConnectionDetails for connections to a MongoDB server
  • Neo4jConnectionDetails for connections to a Neo4J server
  • R2dbcConnectionDetails for connections to a database server through R2DBC
  • RabbitConnectionDetails for connections to a RabbitMQ server
  • RedisConnectionDetails for connections to a Redis server
  • ZipkinConnectionDetails for connections to a Zipkin server

출처

 

Spring Boot 3.1's ConnectionDetails abstraction

If you've used Spring Boot for a while, you're probably familiar with setting up connection details using properties. For example, you may have used spring.datasource.url to configure a JDBC connection. In Spring Boot 3.1 this continues to work as you'd ex

spring.io