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
187 stars 44 forks source link

How to set preferIpAddress field in python client #39

Closed sid8491 closed 3 years ago

sid8491 commented 3 years ago

We need to register a service developed in python. When I am registering, it is showing 127.0.0.1:app-name:90 in the eureka dashboard. Other applications which were registered by spring-boot application are showing their ec2 instance ip instead of 127.0.0.1. Upon enquiring for the same, teams told me they set preferIpAddress=true field in their config.

What would be the way to set this in python client? Is there any alternative parameter for this?

There is instance_ip parameter but we won't be knowing the ip address.

Eureka config:

eureka_client.init(
    eureka_server="localhost:8761",
    app_name="app-name",
    instance_port=PORT,
    eureka_context="eureka"
)

Versions: Python 3.6.9 py-eureka-client==0.8.6

keijack commented 3 years ago

In the component, the IP was find by the try to connecting the component to one of the eureka server and try to get the IP address. So if your python component register to the eureka server in the same machine, the IP may be 127.0.0.1.

    s = socket.socket(socket_family, socket.SOCK_DGRAM)
    s.connect((target_ip, target_port))
    ip = s.getsockname()[0]
sid8491 commented 3 years ago

ok, so it does all that automatically. but how can we force it to show the ip-address like all other springboot applications are doing using preferIpAddress field?

keijack commented 3 years ago

instance_ip could be set when initializing the client. And do you use the private ip or public ip in EC2?

sid8491 commented 3 years ago

private ip.

sid8491 commented 3 years ago

it could be set when initializing the client, but that would be hard-coding the IP. whereas, the application which is registering itself on eureka won't be knowing the private IP of EC2.

all the other applications (all of which are spring-boot applications) are not sending instance_ip, all of them are just setting preferIpAddress=true. Is there any similar parameter in py_eureka_client?

keijack commented 3 years ago

OK, I get it. In Amazon Data centre, all IPs and hostnames should also send to the Eureka, this is not quite well supported here, I'll figure it out.

sid8491 commented 3 years ago

oh thanks, that's great.

keijack commented 3 years ago

The component now will use the first none-loopback ip as the default instance ip, please check it in version 0.8.8. @sid8491

sid8491 commented 3 years ago

@keijack is there any property added to change the default behavior as well?

keijack commented 3 years ago

@keijack is there any property added to change the default behavior as well?

If you want to change the behavior, you can get your own ip/host via code, and set it to instance_ip and instance_host parameter.

sid8491 commented 3 years ago

@keijack thanks a ton. my issue is resolved.

artpoplsh commented 3 years ago

it doesn't work,and my version is 0.9.3, eureka_client_init( instance_ip=)

keijack commented 3 years ago

@artpoplsh Could you please give more detail?

artpoplsh commented 3 years ago

@artpoplsh Could you please give more detail?

I have solved this problem by call eureka_client_init(* instance_ip=ip,instance_host**=ip), could there be some sample paramter like preferIpAddress=true in springcloud

keijack commented 3 years ago

@artpoplsh you can find all arguments from code. they are

def __init__(self,
             eureka_server: str = _DEFAULT_EUREKA_SERVER_URL,
             eureka_domain: str = "",
             region: str = "",
             zone: str = "",
             eureka_availability_zones: Dict[str, str] = {},
             eureka_protocol: str = "http",
             eureka_basic_auth_user: str = "",
             eureka_basic_auth_password: str = "",
             eureka_context: str = "/eureka",
             prefer_same_zone: bool = True,
             should_register: bool = True,
             should_discover: bool = True,
             app_name: str = "",
             instance_id: str = "",
             instance_host: str = "",
             instance_ip: str = "",
             instance_port: int = _DEFAULT_INSTNACE_PORT,
             instance_unsecure_port_enabled: bool = True,
             instance_secure_port: int = _DEFAULT_INSTNACE_SECURE_PORT,
             instance_secure_port_enabled: bool = False,
             data_center_name: str = _DEFAULT_DATA_CENTER_INFO,  # Netflix, Amazon, MyOwn
             renewal_interval_in_secs: int = _RENEWAL_INTERVAL_IN_SECS,
             duration_in_secs: int = _DURATION_IN_SECS,
             home_page_url: str = "",
             status_page_url: str = "",
             health_check_url: str = "",
             secure_health_check_url: str = "",
             vip_adr: str = "",
             secure_vip_addr: str = "",
             is_coordinating_discovery_server: bool = False,
             metadata: Dict = {},
             remote_regions: List[str] = [],
             ha_strategy: int = HA_STRATEGY_RANDOM):
    ...
artpoplsh commented 3 years ago

HA_STRATEGY_RANDOM

I see,but there is no argument like preferIpAddress