Skip to content

A PHP client for Spring Cloud Netflix Eureka service registration and discovery.

License

Notifications You must be signed in to change notification settings

linderun/php-eureka-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Netflix Eureka Client

A PHP client for Spring Cloud Netflix Eureka service registration and discovery.

Installation

Run

composer require linderun/php-eureka-client

or add dependency to your composer.json file

"require": {
    ...
    "linderun/php-eureka-client": "^1.0"
}

Usage example

1. Use needed packages

use EurekaClient\EurekaClient;
use EurekaClient\Models\Instance;
use EurekaClient\Models\Metadata;
use EurekaClient\Models\DataCenterInfo;
use GuzzleHttp\Client;

2. Create Eureka app instance

// We will use app name and instance id for making requests below.
$appId = 'my_app';
$instanceId = 'app_instance_id';

// Create app instance metadata.
$metadata = new Metadata();
$metadata->set('instanceKey', 'instanceValue');

// Create data center metadata.
$dataCenterMetadata = new Metadata();
$dataCenterMetadata->set('dataCenterKey', 'dataCenterValue');

// Create data center info (Amazon example).
$dataCenterInfo = new DataCenterInfo();
$dataCenterInfo
  ->setName('Amazon')
  ->setClass('com.netflix.appinfo.AmazonInfo')
  ->setMetadata($dataCenterMetadata);

// Create Eureka app instance.
$instance = new Instance();
$instance
  ->setInstanceId($instanceId)
  ->setHostName('hostName')
  ->setApp($appName)
  ->setIpAddr('127.0.0.1')
  ->setPort(80)
  ->setSecurePort(433)
  ->setHomePageUrl('http://localhost')
  ->setStatusPageUrl('http://localhost/status')
  ->setHealthCheckUrl('http://localhost/health')
  ->setSecureHealthCheckUrl('https://localhost/health')
  ->setVipAddress('vipAddress')
  ->setSecureVipAddress('secureVipAddress')
  ->setMetadata($metadata)
  ->setDataCenterInfo($dataCenterInfo);

3. Create Eureka client

// Create eureka client.
$eurekaClient = new EurekaClient('localhost', 8080);

4. Make requests

  // Register new application instance.
  $response = $eurekaClient->register($appId, $instance);

  // De-register application instance.
  $response = $eurekaClient->deRegister($appId, $instanceId);

  // Query for all instances.
  $allApps = $eurekaClient->getAllApps();

  // Query for all appID instances.
  $app = $eurekaClient->getApp($appId);

  // Query for a specific appID/instanceID.
  $appInstance = $eurekaClient->getAppInstance($appId, $instanceId);

  // Query for a specific instanceID.
  $instance = $eurekaClient->getInstance($instanceId);

  // Send application instance heartbeat.
  $response = $eurekaClient->heartBeat($appId, $instanceId);

  // Take instance out of service.
  $response = $eurekaClient->takeInstanceOut($appId, $instanceId);

  // Move instance back into service (remove override).
  $response = $eurekaClient->putInstanceBack($appId, $instanceId);

  // Update metadata.
  $response = $eurekaClient->updateAppInstanceMetadata($appId, $instanceId, $metadata);

  // Query for all instances under a particular vip address.
  $instances = $eurekaClient->getInstancesByVipAddress('vipAddress');

  // Query for all instances under a particular secure vip address.
  $instances = $eurekaClient->getInstancesBySecureVipAddress('secureVipAddress');
    
  // Fetch an instance of a service from Eureka.
  $instance = $eurekaClient->fetchInstance($appId);

5. Discovery Strategy

When fetching instances of a service from Eureka, you probably get a list of available instances. You can choose one of them based on your desired strategy of load balancing. For example, a Round-robin or a Random strategy might be your choice. Currently this library only supports RandomStrategy, but you can create your custom strategy by implementing getInstance() method of DiscoveryStrategy interface:

class RoundRobinStrategy implements DiscoveryStrategy {

    public function getInstance($instances) {
        // return an instance
    }
    
}

Then all you have to do, is to introduce your custom strategy to EurekaClient instance:

$eurekaClient->getContainer()->setDiscoveryStrategy(new RoundRobinStrategy());

About

A PHP client for Spring Cloud Netflix Eureka service registration and discovery.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages