2016-06-07 1 views
0

ELB (Software Load Balancer)를 사용하는 Amazon AWS에 배포 된 Grails 2 애플리케이션을 사용하고 있습니다. 우리가 가지고있는 문제는 애플리케이션이 완전히 초기화되기 전에 grails 애플리케이션 인스턴스가로드 밸런서에 추가된다는 것입니다.Grails에 대한로드 밸런서 healthcheck을 올바르게 구현하는 방법

로드 밸런서가 'healthcheck'URL에 http 요청을합니다. resources 플러그인은 특히 자바 스크립트, CSS, GET '/myapp/lbcheck'

LoadBalancerController.groovy :

package myapp 

class LoadBalancerController { 

    def healthService 

    def healthcheck() { 
     response.contentType = 'text/plain' 
     try { 
      healthService.checkDatabase() 
      render(status: 200, text: "Up") 
     } 
     catch(Exception ex) { 
      log.error("Error with database healthcheck " + ex) 
      render(status: 503, text: "Down") 
     } 
    } 
} 

HealthSerivce.groovy

package myapp 

import groovy.sql.Sql 

class HealthService { 

    def dataSource 

    // Either returns true, or throws an Exception 
    def checkDatabase() { 
     Sql sql = new Sql(dataSource) 
     sql.rows("SELECT 429") 
     sql.close() 
     return true 
    } 
} 

SQL 쿼리는 분명 충분하지 않습니다. 프레임 워크에서 속성이 초기화되었는지 확인하기 위해 다른 종류의 속성을 확인해야 할 필요가있는 것 같습니다.

답변

0

healthService bean의 필드를 BootStrap.groovy 내부의 true로 설정해보십시오. Grails가 완전히 초기화 된 후에 실행되는 것 같습니다. BootStrap.groovy 내부

package myapp 

import groovy.sql.Sql 

class HealthService { 

    def dataSource 

    def initializationComplete = false 

    // Either returns true, or throws an Exception 
    def checkDatabase() { 
     Sql sql = new Sql(dataSource) 
     sql.rows("SELECT 429") 
     sql.close() 
     return true 
    } 
} 

:

class BootStrap { 
    def healthService 

    def init = { servletContext -> 
     healthService.initializationComplete = true 
    } 

} 

당신의 LoadBalancerController.groovy의 :

def healthcheck() { 
    response.contentType = 'text/plain' 
    def healthy = false 
    try { 
     healthy = healthService.with { 
      initializationComplete && checkDatabase() 
     } 
    } 
    catch(Exception ex) { 
     log.error("Error with database healthcheck " + ex) 
    } 

    if (healthy) { 
     render(status: 200, text: "Up") 
    } else { 
     render(status: 503, text: "Down") 
    } 
} 
관련 문제