yaangvu / php-eureka-client
PHP client for the Netflix Eureka server.
Installs: 5 052
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^7.4|^8.0
- ext-json: *
- guzzlehttp/guzzle: ^7.3
- illuminate/support: ^8.47
Requires (Dev)
- phpunit/phpunit: ^9.5
README
PHP Eureka Client
PHP client for the Netflix Eureka server. Supports all Eureka REST operations.
Installation
Run
composer require yaangvu/php-eureka-client
or add dependency to your composer.json file
"require": {
...
"yaangvu/php-eureka-client": "^1.0"
}
Usage example
1. Use needed packages
2. Create Eureka app instance
// We will use app name and instance id for making requests below. $appName = 'new_app'; $instanceId = 'test_instance_id'; // Create app instance metadata. $metadata = new Metadata(); $metadata->set('test_key', 'test_value'); // Create data center metadata (required for Amazon only). $dataCenterMetadata = new Metadata(); $dataCenterMetadata->set('data_center_test_key', 'data_center_test_value'); // Create data center info (Amazon example). $dataCenterInfo = new DataCenterInfo(); $dataCenterInfo ->setName('Amazon') ->setClass('com.netflix.appinfo.AmazonInfo') ->setMetadata($dataCenterMetadata); // Create data center info (Own Data Center). $dataCenterInfo = new DataCenterInfo(); $dataCenterInfo ->setName('MyOwn') ->setClass('com.netflix.appinfo.MyDataCenterInfo'); // Create Eureka app instance. $instance = new Instance(); $instance ->setInstanceId($instanceId) ->setHostName('test_host_name') ->setApp($appName) ->setIpAddr('127.0.0.1') ->setPort(80) ->setSecurePort(433) ->setHomePageUrl('http://localhost') ->setStatusPageUrl('http://localhost/status') ->setHealthCheckUrl('http://localhost/health-check') ->setSecureHealthCheckUrl('https://localhost/health-check') ->setVipAddress('test_vip_address') ->setSecureVipAddress('test_secure_vip_address') ->setMetadata($metadata) ->setDataCenterInfo($dataCenterInfo);
3. Create Eureka client
// Eureka client usage example. // Create guzzle client. $guzzle = new Client(); // Create eureka v2 client. $eurekaClient = new EurekaClient('localhost', 8080, $guzzle); // Create eureka v1 client. $eurekaClient = new EurekaClient('localhost', 8080, $guzzle, 'eureka');
4. Make requests
try { // Register new application instance. $response = $eurekaClient->register($appName, $instance); // Query for all instances. $allApps = $eurekaClient->getAllApps(); // Query for all application instances. $app = $eurekaClient->getApp($appName); // Query for a specific application instance. $appInstance = $eurekaClient->getAppInstance($appName, $instanceId); // Query for a specific instance. $instance = $eurekaClient->getInstance($instanceId); // Send application instance heartbeat. $response = $eurekaClient->heartBeat($appName, $instanceId); // Take instance out of service. $response = $eurekaClient->takeInstanceOut($appName, $instanceId); // Put instance back into service. $response = $eurekaClient->putInstanceBack($appName, $instanceId); // Update metadata. $response = $eurekaClient->updateAppInstanceMetadata($appName, $instanceId, [ 'new_key' => 'new_value', ]); // Query for all instances under a particular vip address/ $instances = $eurekaClient->getInstancesByVipAddress('test_vip_address'); // Query for all instances under a particular secure vip address. $instances = $eurekaClient->getInstancesBySecureVipAddress('test_secure_vip_address'); // De-register application instance. $response = $eurekaClient->deRegister($appName, $instanceId); } catch (Exception $e) { echo $e->getMessage() . PHP_EOL; }