Download the PHP package fuermao/eureka_php without Composer
On this page you can find all versions of the php package fuermao/eureka_php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fuermao/eureka_php
More information about fuermao/eureka_php
Files in fuermao/eureka_php
Package eureka_php
Short Description register php service with eureka service
License MIT
Informations about the package eureka_php
PHP Eureka
说明
该版本根据piwvh/php-eureka
稍作配置上的优化而来。
使用说明
安装
使用composer依赖管理工具进行安装即可;
使用
该组件基本未改变原始用法,仅作了添加日志的操作。可参见原作者piwvh/php-eureka
Create Eureka Client
The very first thing you should do is to create an instance of
EurekaClient
using your configuration:
List of all available configuration includes:
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 configuration after creating EurekaClient
instance, using setter methods:
Operations
After creating EurekaClient instance, there will be multiple operations you can do:
-
Registration: register your service instance with Eureka
-
De-registration: de-register your service instance from Eureka
- Heartbeat: send heartbeat to Eureka, to determine the client is up (one-time heartbeat)
You can register your instance and sent periodic heartbeat using start()
method:
With 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
config. This interval time can be changed just like any other
configuration item:
- Service Discovery: fetch an instance of a service from Eureka:
Discovery Strategy
When fetching instances of a service from Eureka, you probably get a list of available instances registered with Eureka. 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:
Then all you have to do is to introduce your custom strategy to EurekaClient
instance:
Local Registry and Caching
As you know, failure is inevitable, specially in cloud-native or distributed applications. So, sometimes Eureka may not be available duo to the failure. In this case, we should have a local registry of services to avoid cascading failures.
In the default behaviour, if Eureka is down, the fetchInstance()
method fails and so
the application throws and exception and can not continue to work. To solve this
problem, you should create a local registry in your application.
There is an interface called InstanceProvider
which you can make use of.
You should implement getInstance()
method of this interface and return instances
of a service based on your ideal logic.
In this example, we have cached the instances of the service in the database and are retrieving them from the database when Eureka is not available.
After creating your custom provider, just make it work by adding it to the configuration:
Your custom provider only gets called when Eureka is down or is not answering properly.
That's it. By adding this functionality, your application continues to work even when Eureka is down.
For caching all available instances of a specific service, you can call fetchInstances()
method
which return all instances of a service, fetched from Eureka: