PHP Netflix Eureka Client
A PHP client for (Spring Cloud) Netflix Eureka service registration and discovery., (*1)
Installation
You can install this package using Composer:, (*2)
composer require "piwvh/php-eureka", (*3)
Documentation
Create Eureka Client
The very first thing you need to do is to create an instance of EurekaClient using your own configuration:, (*4)
$client = new EurekaClient([
'eurekaDefaultUrl' => 'http://localhost:8761/eureka',
'hostName' => 'service.hamid.work',
'appName' => 'service',
'ip' => '127.0.0.1',
'port' => ['8080', true],
'homePageUrl' => 'http://localhost:8080',
'statusPageUrl' => 'http://localhost:8080/info',
'healthCheckUrl' => 'http://localhost:8080/health'
]);
List of all available configuration are as follows:, (*5)
-
eurekaDefaultUrl (default: http://localhost:8761);
hostName
appName
ip
-
status (default: UP)
-
overriddenStatus (default: UNKNOWN)
port
-
securePort (default: ['443', false])
-
countryId (default: 1)
-
dataCenterInfo (default: ['com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo', 'MyOwn'])
homePageUrl
statusPageUrl
healthCheckUrl
vipAddress
secureVipAddress
-
heartbeatInterval (default: 30)
-
discoveryStrategy (default: RandomStrategy)
instanceProvider
You can also change the configuration after creating EurekaClient instance, using setter methods:, (*6)
$client->getConfig()->setAppName("my-service");
Operations
After creating EurekaClient instance, there will be multiple operations to perform:
- Registration: register your service instance with Eureka, (*7)
$client->register();
-
De-registration: de-register your service instance from Eureka
$client->deRegister();
-
Heartbeat: send heartbeat to Eureka, to show the client is up (one-time heartbeat)
$client->heartbeat();
You can register your instance and send periodic heartbeat using start() method:, (*8)
$client->start();
Using this method, first your service gets registered with Eureka using the
configuration you have provided. Then, a heartbeat will be sent to the Eureka periodically, based
on heartbeatInterval configuration value. This interval time can be changed just like any other
configuration item:, (*9)
$client->getConfig()->setHeartbeatInterval(60); // 60 seconds
It's apparent that this method should be used in CLI.
- Service Discovery: fetch an instance of a service from Eureka:, (*10)
$instance = $client->fetchInstance("the-service");
$homePageUrl = $instance->homePageUrl;
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., (*11)
Currently, this library only supports RandomStrategy, however, you can create your custom
strategy by implementing getInstance() method of DiscoveryStrategy interface:, (*12)
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:, (*13)
$client->getConfig()->setDiscoveryStrategy(new RoundRobinStrategy());
Local Registry and Caching
Failure is inevitable, specially in cloud-native applications. Thus, sometimes Eureka may not be available because of failure.
In these cases, we should have a local registry of services to avoid cascading failures., (*14)
By default, if Eureka is down, the fetchInstance() method fails, so an
exception would be thrown and the application cannot continue to work. To solve this
problem, you should create a local registry of services., (*15)
There is an interface called InstanceProvider which you can make use of, by
implementing getInstances() method of this interface and returning instances
of a service based on your ideal logic., (*16)
class MyProvider implements InstanceProvider {
public function getInstances($appName) {
// return cached instances of the service from the Redis
}
}
In this example, we have cached the instances of the service in the Redis and
are loading them when Eureka is down., (*17)
After creating your custom provider, just make it work by adding it to the configuration:, (*18)
$client->getConfig()->setInstanceProvider(new MyProvider());
Your custom provider only gets called when Eureka is down or is not answering properly., (*19)
That's all you need to do. By adding this functionality, your application keeps working even
when Eureka is down., (*20)
For caching all available instances of a specific service, you can call fetchInstances() method
which fetches all of the instances of the service from Eureka:, (*21)
$instances = $client->fetchInstances("the-service");