Skip to content

Commit 58f62fc

Browse files
committed
健康状态检测
1 parent ee227f3 commit 58f62fc

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

src/Rpc/Consul.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Leafpoda\JsonRpcClient\Rpc;
44

55
use Consul\Services\Agent;
6+
use Consul\Services\Health;
67
use Exception;
78
use Leafpoda\JsonRpcClient\Exception\JsonrpcException;
89

@@ -17,12 +18,18 @@ class Consul
1718
public function getNode($serviceName): string
1819
{
1920
try {
20-
$kv = new Agent();
21-
$services = $kv->services()->getBody();
21+
//获取健康的services的id
22+
$agent = new Health();
23+
$checks = $agent->state('passing')->getBody();
24+
$checks = json_decode($checks,true);
25+
$passServiceId = array_column($checks,'ServiceID');
26+
//获取services详细信息
27+
$agent = new Agent();
28+
$services = $agent->services()->getBody();
2229
$services = json_decode($services,true);
2330
$nodes = [];
24-
foreach ($services as $service){
25-
if ($service['Service']==$serviceName){
31+
foreach ($services as $serviceId => $service){
32+
if ($service['Service']==$serviceName && in_array($serviceId,$passServiceId)){
2633
$nodes[] = $service;
2734
}
2835
}

0 commit comments

Comments
 (0)