general java
[spring boot] l7check 막코드 + nginx 설정
'김용환'
2015. 10. 5. 17:29
spring boot 애플리케이션으로 개발하는 웹 서버를 다음과 같이 구성할 수 있다.
nginx 설정시 l7check url을 명세하고, 이를 web application에서 처리하도록 설정한다.
server {
location /l7check.html {
access_log off;
allow all;
proxy_pass http://localhost:8080/health;
proxy_connect_timeout 1s;
proxy_read_timeout 1s;
proxy_send_timeout 1s;
}
}
HeathChecker라는 Controller를 만들어서 배포시 start,stop할 때마다 호출하여 l7check를 제어할 수 있도록 만들었다.
/l7check/true, /l7check/false url을 주어 health check 결과를 build하도록 하였다. 참고로, spring boot는 health check down시는 503 에러를 발생하도록 되어 있어 l7check를 아주 손쉽게 처리할 수 있다.
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HealthChecker implements HealthIndicator {
private Boolean isActivating = true;
@Override
public Health health() {
Boolean isOk = check();
if (!isOk) {
return Health.down().withDetail("Error Code", 10000).build();
}
return Health.up().build();
}
public Boolean check() {
return isActivating;
}
@RequestMapping(value="/l7check/{value}", method = RequestMethod.GET)
@ResponseBody
public Boolean setActivating(@PathVariable Boolean value) {
this.isActivating = value;
return this.isActivating;
}
}