forked from elviejokike/flask-eureka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheureka.py
87 lines (70 loc) · 2.73 KB
/
eureka.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from flask import Blueprint, Response
from .eurekaclient import EurekaClient
import json
eureka_bp = Blueprint('eureka', __name__)
@eureka_bp.route('/health')
def healthcheck():
"""
Return 200 as default
"""
payback={"status": "UP"}
resp = Response(response=json.dumps(payback),
status=200,
mimetype="application/json;charset=UTF-8")
return resp
# return payback
@eureka_bp.route('/info')
def info():
"""
Return 200 as default
"""
payback={"id":"ks-nlp"}
resp = Response(response=json.dumps(payback),
status=200,
mimetype="application/json;charset=UTF-8")
return resp
class Eureka(object):
def __init__(self, app=None, **kwargs):
"""
Initialize the flask extension
"""
self.kwargs = kwargs if kwargs else {}
self.app = None
if app is not None:
self.init_app(app)
def init_app(self, app):
self.app = app
if not hasattr(app, 'extensions'):
app.extensions = {}
if 'eureka' in app.extensions:
raise RuntimeError('Flask application already initialized')
app.extensions['eureka'] = self
def register_service(self, name=None, **kwargs):
"""
Register service with eureka service
:param name: name of the eureka application
"""
name = self.app.config.get('SERVICE_NAME', name)
eureka_url = self.app.config.get(EurekaClient.EUREKA_SERVICE_URL, None)
data_center = self.app.config.get(EurekaClient.EUREKA_INSTANCE_DATACENTER, None)
host_name = self.app.config.get(EurekaClient.EUREKA_INSTANCE_HOSTNAME, None)
heartbeat_interval = self.app.config.get(EurekaClient.EUREKA_HEARTBEAT_INTERVAL, None)
service_path = self.app.config.get(EurekaClient.EUREKA_SERVICE_PATH, None)
port = self.app.config.get(EurekaClient.EUREKA_INSTANCE_PORT, self._get_service_port())
eureka_client = EurekaClient(name=name, host_name=host_name,
eureka_url=eureka_url,
data_center=data_center,
heartbeat_interval=heartbeat_interval,
service_path=service_path,
port=port,
**kwargs)
eureka_client.star()
def _get_service_port(self):
"""
Retrieve the service port being used by the flask application
"""
port = 5000
server_name = self.app.config.get('SERVER_NAME', None)
if server_name and ':' in server_name:
port = int(server_name.rsplit(':', 1)[1])
return port