파워노트

[ skeleton ] springboot 프로젝트 - hikariCP 설정 본문

spring boot

[ skeleton ] springboot 프로젝트 - hikariCP 설정

파워킴 2021. 8. 10. 22:54
반응형

 

[ skeleton ] springboot 프로젝트 - mysql 연동 

  • mysql 연동하는 프로젝트 설정.
  • 데이터 처리 적용. 
  • hikari 설정. 

 

 자주 마주치는 오류 

  • spring-boot-starter-data-jdbc, spring-boot-starter-data-jpa 와 같은 의존성을 추가하고 springboot 를 실행하면. 아래와 같은 경고가 나타난다.
***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).


Process finished with exit code 0

* 이것은 datasource 관련한 의존성이 존재 하는데 url이 입력되지않아 db 설정에 오류가 발생한것이다.

 

 

 설정하기 

 

  • build.gradle dependency 설정  
dependencies {
..
    implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
    runtimeOnly 'mysql:mysql-connector-java'
..
}
  • 아주 간단한 설정.
    spring:
      datasource:
        url: mysql://192.168.0.250:3306/powerdatabase?useSSL=false&autoReconnect=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Seoul
        username: root
        password: xxxxxx
        driver-class-name: com.mysql.jdbc.Driver​

    - 이렇게만 해주면 spring이 알아서 jdbc 설정하고 데이터 처리를 할수 있게 자동설정된다. 
    - HikariPool 도 spring 2.x에서는 기본이다. 
    [ datasource 설정 ]

 HikariPool Custom config 1

  • Hikari Pool 을 나중에 원할하게 쓰기위해 또는 이중 database 와 같이 다양한 mysqlTemplate을 사용할때를 대비해서 번거롭지만 HikariConfig를 Custom 설정해주자. 
  • configure class 생성
    @Configuration(proxyBeanMethods = false)
    public class MyDataSourceConfiguration {
        @Bean
        @ConfigurationProperties("spring.datasource")
        public DataSource dataSource() {
            return DataSourceBuilder.create().build();
        }
    }​
     
  • application.yml property 설정.
    - jdbc-url 설정이 없어 오류가 난다. 추가해 준다.
    spring:
      datasource:
        jdbc-url: jdbc:mysql://192.168.25.250:3306/powerdatabase?useSSL=false&autoReconnect=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Seoul
        username: root
        password: xxxxxx
        driver-class-name: com.mysql.jdbc.Driver

 

 HikariPool Custom config 2

 

  • spring.datasource의 property를 이용하여 Type을 HikariDataSource Type 으로 처리 
    @Configuration(proxyBeanMethods = false)
    public class MyDataSourceConfiguration  {
    
        @Bean
        @Primary
        @ConfigurationProperties("spring.datasource")
        public DataSourceProperties dataSourceProperties() {
            return new DataSourceProperties();
        }
    
        @Bean
        @ConfigurationProperties("spring.datasource.hikari")
        public HikariDataSource dataSource(DataSourceProperties properties) {
            return properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
        }
    }
  • application.yml property 설정.
    spring:
      datasource:
        url: jdbc:mysql://192.168.25.250:3306/powerdatabase?useSSL=false&autoReconnect=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Seoul
        username: root
        password: xxxxxx
        driver-class-name: com.mysql.jdbc.Driver
        hikari:
          connection-timeout: 3000
          validation-timeout: 3000
          minimum-idle: 5
          max-lifetime: 240000
          maximum-pool-size: 20
    * 이렇게 하면 spring.datasource 의 url 속성을 가지고 hikari datasource type의 pool 설정을 할수 있다. 

 

** complete 설정이 완료되면 아래와 같이 정상 실행됨을 확인할수 있다.

 

 

* gitHub 소스:

https://github.com/powerkkim/skeleton/releases/tag/DatasourceSetting_MySql

 

 

마무리 

  • 그냥 간당하게 yml 에서 url만 설정하고 써도 되지만 나중에 꼭 뭔가가 추가적으로 설정되곤한다.
  • 이렇게 만들어 놓으면 나중에 그냥 편하게 이대로만 쓰면 된다. 

 

참고

 

반응형
Comments