Commit b7bab8a3 authored by 崔为之's avatar 崔为之 💪🏽

Update project

parent 4360aaed
......@@ -7,7 +7,7 @@
# @Email : StudentCWZ@outlook.com
# @Date : 2023/11/21 09:10
# @File : consul.py
# @Description :
# @Description : This module provides an interface for registering and deregistering Flask services with Consul.
"""
......@@ -22,21 +22,25 @@ from application.libs.helper import EnvVarHelper
class FlaskConsulService:
"""Flask service class for Consul registration and deregistration."""
def __init__(self, app=None):
"""Initialize with optional Flask application."""
self.app = app
self.client = None
self.service_id = None
if app is not None:
self.init_app(app)
def init_app(self, app: Flask):
def init_app(self, app: Flask) -> None:
"""Initialize service with Flask app, register it with Consul and setup deregistration at exit."""
env_vars, server_cfg = self.set_server_cfg(app)
self.register_service(env_vars, server_cfg)
atexit.register(self.deregister_service)
@staticmethod
def set_server_cfg(app: Flask) -> tuple:
"""Return environment variables and server configuration."""
# 实例化对象
eh = EnvVarHelper()
# 从环境变量获取 consul 参数
......@@ -52,6 +56,7 @@ class FlaskConsulService:
return env_vars, cfg
def register_service(self, env_vars: dict, cfg: ConfigHelper):
"""Register the service with Consul using the given environment variables and server configuration."""
service_name = cfg.get('Name', 'elasticsearch-log-parse')
service_address = cfg.get('IP', 'localhost')
service_port = cfg.get('Port', '5000')
......@@ -77,4 +82,5 @@ class FlaskConsulService:
ServerError('Failed to register service')
def deregister_service(self):
"""Deregister the service from Consul."""
self.client.agent.service.deregister(self.service_id)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment