PHP code example of shenzhe / swoole-eureka

1. Go to this page and download the library: Download shenzhe/swoole-eureka library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

shenzhe / swoole-eureka example snippets


$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'
]);

$client->getConfig()->setAppName("my-service");

$client->register();

$client->deRegister();

$client->heartbeat();

$client->start();

$client->getConfig()->setHeartbeatInterval(60); // 60 seconds

$instance = $client->fetchInstance("the-service");
$homePageUrl = $instance->homePageUrl;

class RoundRobinStrategy implements DiscoveryStrategy {

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

$client->getConfig()->setDiscoveryStrategy(new RoundRobinStrategy());

class MyProvider implements InstanceProvider {

    public function getInstances($appName) { 
        // return cached instances of the service from the database 
    }
}

$client->getConfig()->setInstanceProvider(new MyProvider());

$instances = $client->fetchInstances("the-service");