How to Write Python Scripts to Take Eureka Services Offline and Online
This guide shows how to create two Python scripts that use the Eureka REST API to set a service instance's status to OUT_OF_SERVICE for taking it offline and to UP for bringing it back online, handling command‑line arguments, constructing URLs, sending PUT requests, and checking responses.
Eureka Offline Script
Python script that takes a specific Eureka service instance offline by setting its status to OUT_OF_SERVICE via a PUT request.
import sys
import requests
APP_NAME = sys.argv[1]
INSTANCE_ID = sys.argv[2]
eureka_url = "https://eureka_url/eureka/apps"
instance_out_of_service_url = f"{eureka_url}/{APP_NAME}/{INSTANCE_ID}/status?value=OUT_OF_SERVICE"
response = requests.put(instance_out_of_service_url)
print(response.status_code)
if response.status_code == 200:
print('服务实例成功下线')
else:
print('服务实例下线失败,状态码:', response.status_code)Eureka Online Script
Python script that brings a Eureka service instance back online by setting its status to UP via a PUT request.
import sys
import requests
APP_NAME = sys.argv[1]
INSTANCE_ID = sys.argv[2]
eureka_url = "https://eureka_url/eureka/apps"
instance_up_url = f"{eureka_url}/{APP_NAME}/{INSTANCE_ID}/status?value=UP"
response = requests.put(instance_up_url)
print(response.status_code)Practical DevOps Architecture
Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
