Spring
H2 Console in WebFlux
§무명소졸§
2020. 7. 28. 10:23
개발 단계에서 H2 database 는 유용하게 사용된다. Spring Boot 에 embded 돼있기 때문에 별도의 설치 없이 사용 가능하다. 또한 웹에서 접근 가능한 UI를 제공한다. 해당 설정 정보는 application.yml 파일에 설정으로 관리할 수 있다. 아래는 해당 설정이다.
spring:
h2:
console:
path: /h2-web
enabled: true
하지만 web 계층에서 web-flux를 사용하고 있다면 해당 설정은 동작하지 않고 H2 web Ui 를 사용하기 위해서 아래와 같은 자바 Configuration 파일을 작성해야된다.
@Profile("local") 은 로컬에서만 사용한다는 의미이다. deploy 배포 환경을 따른다.
package info.m2sj.jpatutorial.support.h2;
import lombok.extern.slf4j.Slf4j;
import org.h2.tools.Server;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@Profile("local")
public class H2Console {
private Server webServer;
@EventListener(ContextRefreshedEvent.class)
public void start() throws java.sql.SQLException {
log.info("starting h2 console at port 8078");
this.webServer = org.h2.tools.Server.createWebServer("-webPort", "8078", "-tcpAllowOthers").start();
}
@EventListener(ContextClosedEvent.class)
public void stop() {
log.info("stopping h2 console at port 8078");
this.webServer.stop();
}
}
그리고 추가로 pom.xml 이나 build.gradle 파일에 dependency 의존성의 라이프를 compile 로 변경해야 된다. 아래는 gradle 일 경우 예제이다.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
compile 'com.h2database:h2' //required runtime -> compile for run h2 server in the webflux
.
.
.