File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33namespace Leafpoda \JsonRpcClient \Rpc ;
44
55use Consul \Services \Agent ;
6+ use Consul \Services \Health ;
67use Exception ;
78use 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 }
You can’t perform that action at this time.
0 commit comments