Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
elp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
崔为之
elp
Commits
b7bab8a3
Commit
b7bab8a3
authored
Nov 21, 2023
by
崔为之
💪🏽
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update project
parent
4360aaed
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
2 deletions
+8
-2
application/libs/flask_consul/consul.py
application/libs/flask_consul/consul.py
+8
-2
No files found.
application/libs/flask_consul/consul.py
View file @
b7bab8a3
...
...
@@ -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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment