首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为python3实现flask-healthz

为python3实现flask-healthz
EN

Stack Overflow用户
提问于 2021-06-02 09:10:19
回答 1查看 457关注 0票数 0

我正在尝试为我的python应用程序实现flask-healthz (https://pypi.org/project/flask-healthz/),以获得活动和冗余探测的回报。但不知何故,它对我不起作用。下面是我的代码片段:

代码语言:javascript
复制
from flask import Flask
from flask_healthz import healthz
from flask_healthz import HealthError

def printok():
    print("Everything is fine")

app = Flask(__name__)
app.register_blueprint(healthz, url_prefix="/healthz")

def liveness():
    try:
        printok()
    except Exception:
        raise HealthError("Can't connect to the file")

def readiness():
    try:
        printok()
    except Exception:
        raise HealthError("Can't connect to the file")

HEALTHZ = {
    "live": "yourapp.checks.liveness",
    "ready": "yourapp.checks.readiness",
}

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000)

代码的输出如下所示:

代码语言:javascript
复制
(app-root) curl http://localhost:5000/ 
127.0.0.1 - - [02/Jun/2021 01:02:56] "GET / HTTP/1.1" 200 -
Hello World! 



(app-root) curl http://localhost/yourapp/healthz/live
curl: (7) Failed to connect to localhost port 80: Connection refused


(app-root) curl http://localhost:5000/yourapp/healthz/live
127.0.0.1 - - [02/Jun/2021 01:03:23] "GET /yourapp/healthz/live HTTP/1.1" 404 -
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>


(app-root) curl http://localhost:5000/yourapp/healthz/ready
127.0.0.1 - - [02/Jun/2021 01:03:37] "GET /yourapp/healthz/ready HTTP/1.1" 404 -
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>


(app-root) curl http://localhost:5000/healthz/readiness
127.0.0.1 - - [02/Jun/2021 01:04:02] "GET /healthz/readiness HTTP/1.1" 404 -
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The readiness check endpoint is not setup</p>


(app-root) curl http://localhost:5000/healthz/liveness
127.0.0.1 - - [02/Jun/2021 01:04:10] "GET /healthz/liveness HTTP/1.1" 404 -
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The liveness check endpoint is not setup</p>
(app-root) 

是不是我做错了什么。我试着在Openshift中运行同样的程序,但是没有成功。

如果有一些有效的例子,我会很有用的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-02 11:00:29

假设这是文档中的复制-粘贴,下面是您可以更改以使其工作的内容。

扁平app.py

代码语言:javascript
复制
from flask import Flask
from flask_healthz import healthz
from flask_healthz import HealthError

app = Flask(__name__)
app.register_blueprint(healthz, url_prefix="/healthz")

def printok():
    print("Everything is fine")

def liveness():
    try:
        printok()
    except Exception:
        raise HealthError("Can't connect to the file")

def readiness():
    try:
        printok()
    except Exception:
        raise HealthError("Can't connect to the file")

app.config.update(
    HEALTHZ = {
        "live": "app.liveness",
        "ready": "app.readiness",
    }
)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000)

然后

代码语言:javascript
复制
curl localhost:5000/healthz/live
OK

curl localhost:5000/healthz/ready
OK
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67797715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档