keijack / python-eureka-client

A eureka client written in python. Support registering your python component to Eureka Server, as well as calling remote services by pulling the the Eureka registry.
MIT License
184 stars 43 forks source link

How to actually setup the Python Component #26

Closed TheGlobalist closed 4 years ago

TheGlobalist commented 4 years ago

Hi, I have a question regarding the setup of my client as I can't really understand what actions do I need to take from the README.md.

I have an Eureka project with the following application.properties

spring.application.name=eureka-service
server.port= 8761
eureka.server.wait-time-in-ms-when-sync-empty=0

eureka.client.register-with-eureka=true
eureka.client.fetch-registry=false

Next, I wanted to attach Zuul Gateway, in order to have a sort of API Gateway. It gets registered without any problem with the following application.properties.

server:
  port: 8073

spring:
  application:
    name: zuul-gateway

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    serviceUrl:
      defaultZone: http://localhost:8761/eureka

zuul:
  ignoredPatterns:
    - /authenticate
    - /delete
    - /refresh
  routes:
    user-service:
      path: /us/**
      url: http://localhost:5000/
      stripPrefix: true

Now comes my problem. I'm trying to register to Eureka (and, then, make it discoverable and callable also from Zuul) a really simple Flask Component, called user-service, defined with just the following lines

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    import py_eureka_client.eureka_client as eureka_client

    your_rest_server_port = 5000
    # The flowing code will register your server to eureka server and also start to send heartbeat every 30 seconds
    eureka_client.init(eureka_server="http://localhost:8761",
                       app_name="user-service",
                       instance_port=your_rest_server_port)
    app.run()

When I run all the 3 projects, though, Eureka can't manage to find the user-service. Even if I try to use init_discovery_client, I'm still getting the same issue.

Could anyone help me on this?

dinuta commented 4 years ago

Hi @TheGlobalist,

Check an exemple of mine in estuary-discovery or estuary-testrunner.

From what i see the registration link is incorrect.

There are two forms for the eureka server url, from what i recall. One that i am using is something like: http://localhost:8761/eureka/v2

Keep me posted if you succeed. And you will!

dinuta commented 4 years ago

Class: https://github.com/dinuta/estuary-testrunner/blob/master/rest/api/eureka_registrator.py

Register call (from the service):

EurekaRegistrator(os.environ.get('EUREKA_SERVER')).register_app(os.environ["APP_IP_PORT"])

And the service boot options via env vars:

docker run \
-e EUREKA_SERVER=http://10.10.15.28:8080/eureka/v2 
-e APP_IP_PORT=10.10.15.28:8081
-p 8080:8080
dinutac/estuary-testrunner:latest

Also you have a tested and updated dockerized eureka server here: https://github.com/dinuta/netflixoss-eureka

TheGlobalist commented 4 years ago

CHanging the call to

    eureka_client.init(eureka_server="http://192.168.1.10:8761/eureka/",
                       app_name="user-service",
                       instance_port=your_rest_server_port,
                       instance_ip="http://127.0.0.1")

Or

    eureka_client.init(eureka_server="http://localhost:8761/eureka/v2/",
                       app_name="user-service",
                       instance_port=your_rest_server_port,
                       instance_ip="http://127.0.0.1")

Still doesn't help :(

dinuta commented 4 years ago

Change to:

eureka_client.init(eureka_server="http://localhost:8761/eureka/v2/",
                       app_name="user-service",
                       instance_port=5000,
                       instance_ip="127.0.0.1")

or

eureka_client.init(eureka_server="http://localhost:8761/eureka/",
                       app_name="user-service",
                       instance_port=5000, 
                       instance_ip="127.0.0.1")

Instance_ip is either the ip (i recommend to put the network ip, and not localhost) or full hostname.

TheGlobalist commented 4 years ago

Tried both of configurations but still Eureka won't see the Flask Component :(

dinuta commented 4 years ago

This requires complex debugging. You can try 2 scenarios:

Furthermore you can write a small client to fetch Information from eureka to see you are able to retrive the info.

TheGlobalist commented 4 years ago

Apparently, using it from PyCharm is the problem: if I run my snippet through iPython, Eureka sees my component

keijack commented 4 years ago

Hi, @TheGlobalist, After started your flask program, did you find any logs in it? What's the result if you visit http://localhost:8761/eureka/v2/ in your browser?

TheGlobalist commented 4 years ago

Hi, @TheGlobalist, After started your flask program, did you find any logs in it? What's the result if you visit http://localhost:8761/eureka/v2/ in your browser?

Hi, Sorry for being super late in responding! I've swapped configurations: now I'm using a Sanic Web Server to deploy my app. The initialization is done in the following way on my Python Microservice

from helpers.helper import create_app, run_app
from controllers import usercontroller, regtokenutentecontroller, mailcontroller

sanic_app = create_app(
    blueprints=((usercontroller.bp, regtokenutentecontroller.bp, mailcontroller.bp))
)

if __name__ == "__main__":  # pragma: no cover
    run_app(sanic_app)
    import py_eureka_client.eureka_client as eureka_client

    # The flowing code will register your server to eureka server and also start to send heartbeat every 30 seconds
    eureka_client.init(eureka_server="http://localhost:8761/eureka/",
                       app_name="user-service",
                       instance_port=8071)

Which outputs the following

bin/python /Users/gimmi/Desktop/Università/MAGISTRALE/TESI/DataEX/MicroServizi/UserService/user-service/sanic_starter.py
[2020-06-07 14:29:33 +0200] [11133] [INFO] Goin' Fast @ http://localhost:8071
ciao
[2020-06-07 14:29:33 +0200] [11133] [INFO] Starting worker [11133]

But that's it. Nothing more.

Eureka just outputs the following

2020-06-07 14:29:30.646  INFO [,,,] 1048 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 2ms

2020-06-07 14:30:30.648  INFO [,,,] 1048 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 1ms

2020-06-07 14:31:30.648  INFO [,,,] 1048 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2020-06-07 14:32:30.650  INFO [,,,] 1048 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 2ms
2020-06-07 14:33:30.652  INFO [,,,] 1048 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 1ms
2020-06-07 14:34:30.656  INFO [,,,] 1048 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 3ms

and register the microservice only when I shut it down from PyCharm. If I go to http://localhost:8761, which exposes Eureka, I only see the API Gateway that I'm using (which is Zuul)

keijack commented 4 years ago

as well as run_app(sanic_app) is a blocked method, I think you should place it at the end of the method (after eureka_client.init).