【问题标题】:How to properly implement load balancer healthcheck for Grails如何为 Grails 正确实施负载均衡器健康检查
【发布时间】:2016-06-08 04:36:23
【问题描述】:

我正在使用部署到 Amazon AWS 的 Grails 2 应用程序,该应用程序使用软件负载均衡器 (ELB)。我们遇到的问题是 grails 应用程序实例在应用程序完全初始化之前被添加到负载均衡器中。它是 resources 插件,专门提供静态资源,如 javascript、css、图像等。

负载平衡器向“健康检查”URL 发出 http 请求。 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")
        }
    }
}

HealthService.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 查询显然是不够的。似乎我们需要检查框架中的其他类型的属性以确定它已被初始化。

【问题讨论】:

    标签: grails load-balancing


    【解决方案1】:

    您可以尝试在BootStrap.groovy 内部将healthService bean 上的字段设置为true。我认为这是在 Grails 完全初始化之后运行的。

    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
        }
    }
    

    BootStrap.groovy内:

    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")
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-20
      • 2020-01-12
      • 2013-04-11
      • 1970-01-01
      • 2016-08-05
      • 2021-08-14
      • 2019-08-27
      • 2015-08-04
      • 2017-06-04
      相关资源
      最近更新 更多