【问题标题】:React App - Include /health - Health Endpoint for Load BalancerReact App - 包括 /health - 负载均衡器的健康端点
【发布时间】:2020-05-09 01:35:12
【问题描述】:

我有一个 React 应用程序,我希望将它部署在负载均衡器后面,负载均衡器会定期 ping 应用程序以查看它是否运行良好。我的要求是提供一个/health 端点,可用于健康检查。

实现健康端点的理想方式是什么?

  • 这是任何计划以自动修复方式部署 React 应用程序的人所必需的
  • 这个App的主页是/dashboard,是一个重磅页面。因此,它不能用作健康端点。

即:我见过有 /hello 类型的端点的反应应用程序返回一个简单的消息,如 I am healthy

【问题讨论】:

    标签: reactjs react-router


    【解决方案1】:

    我将回答我自己的问题。经过大量研究并询问有经验的 React 开发人员,以下是在 React 应用程序中包含健康端点的使用方法。

    在容器化 React 应用以在 Kubernetes 环境中使用时提出了这个要求。

    永远不要尝试使用现有页面作为您的健康检查端点。因为,您的常规页面很繁重,而运行状况检查端点需要很简单。

    因此,使用/health(或更好的路径)创建一个新路由并返回一个简单的 HTML 元素。下面给出的是一个简单的Route 组件。

    <Route path="/health">
        <h3>Hey There!!! The App is Healthy</h3>
    </Route>
    

    这在Routes.js 文件中使用,如下所示。

    import React from 'react';
    import { Switch, Redirect, Route } from 'react-router-dom';
    
    const Routes = () => {
        return (
            <Switch>    
                {/* This endpoint will just return you to a dummy HTML with a simple heading tag */}
                <Route path="/health">
                    <h3>Hey There!!! The App is Healthy</h3>
                </Route>
    
                {/* All other routes will be defined here */}
    
                {/* Finally you will be redirected to a not found page */}
                <Redirect to="/not-found" />
            </Switch>
        );
    };
    
    export default Routes;
    

    【讨论】:

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