Skip to content

Instantly share code, notes, and snippets.

@d10xa
Last active December 20, 2017 14:43
Show Gist options
  • Save d10xa/fbd56fb310a8a0ce0f24a9f2865eaff4 to your computer and use it in GitHub Desktop.
Save d10xa/fbd56fb310a8a0ce0f24a9f2865eaff4 to your computer and use it in GitHub Desktop.
Simple client for netflix eureka
import logging
import signal
import sys
import threading
import requests
class Eureka(object):
started = False
def __init__(
self,
eureka_payload,
app_name,
host_name,
eureka_url="http://localhost:8761",
context="eureka/apps",
instance_id=None,
heart_rate=10
):
super(Eureka, self).__init__()
self.eureka_payload = eureka_payload
self.host_name = host_name
self.app_name = app_name
self.eureka_url = eureka_url
self.context = context
self.instance_id = instance_id
self.heart_rate = heart_rate
self.eureka_app_url = str.join('/', [self.eureka_url, self.context, self.app_name])
instance_path = self.instance_id if self.instance_id else self.host_name
self.eureka_instance_url = str.join('/', [self.eureka_url, self.context, self.app_name, instance_path])
logging.info("Eureka. app url %s", self.eureka_app_url)
logging.info("Eureka. instance url %s", self.eureka_instance_url)
def register(self):
url = self.eureka_app_url
payload = self.eureka_payload()
response = requests.post(url, json=payload)
logging.info("Eureka. register %s %s", url, str(response))
def heartbeat(self):
url = self.eureka_instance_url
response = requests.put(url)
logging.info('Eureka. heartbeat %s %s', response, url)
def deregister(self):
url = self.eureka_instance_url
response = requests.delete(url)
logging.info('Eureka. De-register application instance %s', response)
def start(self):
self.register()
def signal_handler(s, frame):
self.deregister()
logging.info('Eureka. Service de-registered on exit (signal: %s)', s)
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
def set_interval(func, sec):
def func_wrapper():
set_interval(func, sec)
func()
t = threading.Timer(sec, func_wrapper)
t.start()
return t
if not self.started:
set_interval(self.heartbeat, self.heart_rate)
self.started = True
import argparse
import logging
import sys
from eurekasimple import Eureka
def main(argv=sys.argv[1:]):
parser = argparse.ArgumentParser(description="Application params")
parser.add_argument("--eureka-url", type=str, default="http://localhost:8761")
parser.add_argument("--eureka-app", type=str, default="python-application")
parser.add_argument("--eureka-host-name", type=str, default="python-application-host-name")
parser.add_argument("--logging-level", type=str, default="INFO")
parser.add_argument("--eureka-instance-port", type=int, default=8080)
parser.add_argument("--eureka-instance-ip-addr", type=str, default="127.0.0.1")
parser.add_argument("--vip-address", type=str, default="python-application")
parser.add_argument("--renewal-interval-in-secs", type=int, default=30)
parser.add_argument("--duration-in-secs", type=int, default=60)
args = parser.parse_args(argv)
logging.basicConfig(
format=u'%(filename)s[LINE:%(lineno)d]# %(levelname)-8s [%(asctime)s] %(message)s',
level=logging.getLevelName(args.logging_level)
)
logging.info("Arguments: %s", args)
def eureka_payload():
return {
"instance": {
"hostName": args.eureka_host_name,
"app": args.eureka_app,
"ipAddr": args.eureka_instance_ip_addr,
"vipAddress": args.vip_address,
"status": "UP",
"port": {
"$": args.eureka_instance_port,
"@enabled": True
},
"dataCenterInfo": {
"@class": 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo',
"name": 'MyOwn'
},
"leaseInfo": {
"durationInSecs": args.duration_in_secs,
"renewalIntervalInSecs": args.renewal_interval_in_secs
}
}
}
Eureka(
eureka_payload=eureka_payload,
app_name="application",
host_name="application-hostname",
eureka_url="http://localhost:8761",
context="eureka/apps", # spring-boot eureka server
heart_rate=5
).start()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment