consul配合swoole注册服务调用服务

创建服务
启动服务端 php artisan ff这里是我用到了laravel的consul
获取服务数据
业务执行流程是:利用consul注册服务-》利用swoole的客户端去连接swoole的服务端调用注册在swoole里的服务代码业务。
注意:本片文章为记录个人demo流程,无参考价值
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2021/9/7
* Time: 18:26
* 控制器
*/
namespace App\Http\Controllers;
use App\Client\UserClient;
use App\Plug\Consul\DestroyService;
use App\Plug\Consul\GetService;
use App\Plug\Consul\RegisterService;
class ConsulController extends Controller
{
public $ip = '172.16.0.100';
/**
* 获取服务
*/
public function getOneConsulService()
{
$a = (new GetService($this->ip,'8510'))->getOne('order');
dd($a->original['data']->Address);
}
/**
* @return \Illuminate\Http\JsonResponse
* @throws \GuzzleHttp\Exception\GuzzleException
* 获取全部服务
*/
public function getAllConsulService()
{
return (new GetService($this->ip,'8510'))->getAll();
}
/**
* 注册服务
*/
public function addConsulService()
{
//id 服务id name 服务名称
$mode = new RegisterService($this->ip,'8510');
return $mode->register('order','OrderService',['order_tag11'],$this->ip,9506);
}
/**
* 删除服务
*/
public function removeConsulService()
{
//id 服务id
$mode = new DestroyService($this->ip,'8510');
return $mode->destroy('order');
}
/**
* 获取服务数据
*/
public function getService()
{
$client = new UserClient();
return $client->service('order')->getUserList(1,'DaoUserService',$this->ip);
}
}<?php
/**
* Created by PhpStorm. 链接Consul
* User: Admin
* Date: 2021/7/29
* Time: 22:51
*/
namespace App\Plug\Consul;
use GuzzleHttp\Client;
class GetService
{
private $client;
private $base_uri;
private $port;
public function __construct($base_uri,$port)
{
$client = new Client([
'base_uri' =>$base_uri.':'.$port,
'timeout' =>3.0,
]);
$this->client = $client;
$this->base_uri = $base_uri;
$this->port = $port;
}
/**
* 获取指定ID的服务
* @param $service_id
* @return \Illuminate\Http\JsonResponse
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getOne($service_id)
{
$response = $this->client->get('/v1/agent/service/'.$service_id);
$reason = $response->getReasonPhrase();
$body = $response->getBody()->getContents();
if($reason == 'OK'){
return response()->json([
'code'=> 0,
'data'=>json_decode($body)
]);
}else{
return response()->json([
'code'=>1000,
'message'=>"获取失败"
]);
}
}
/**
* 获取所有服务
* @return \Illuminate\Http\JsonResponse
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getAll()
{
$response = $this->client->get('/v1/agent/services');
$reason = $response->getReasonPhrase();
$body = $response->getBody()->getContents();
if($reason == 'OK'){
return response()->json([
'code'=> 0,
'data'=>json_decode($body)
]);
}else{
return response()->json([
'code'=>1000,
'message'=>"获取失败"
]);
}
}
}<?php
/**
* Created by PhpStorm. 向Consul内注册服务
* User: Administrator
* Date: 2021/7/28
* Time: 19:04
*/
namespace App\Plug\Consul;
use GuzzleHttp\Client;
class RegisterService
{
private $client;
private $base_uri;
private $port;
public function __construct($base_uri,$port)
{
$client = new Client([
'base_uri' =>$base_uri.':'.$port,
'timeout' =>3.0,
]);
$this->client = $client;
$this->base_uri = $base_uri;
$this->port = $port;
}
/**
* @param $id 服务id
* @param $name 服务名称
* @param array $tags 服务标签
* @param $port服务端口
* @return \Illuminate\Http\JsonResponse
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function register($id,$name,$tags=[],$ip,$port)
{
$data = array(
"ID"=>$id,
"Name"=>$name,
"Tags"=>$tags,
"Address"=>'127.0.0.1',
"Port"=>$port,
"Check"=>array("HTTP"=>"http://".$this->base_uri.':'.$this->port,"Interval"=>"5s")
);
$response = $this->client->put('/v1/agent/service/register', [
'headers' => ['Content-Type' => 'application/json'],
'json'=>$data
]);
$body = $response->getBody()->getContents();
if(!$body){
return response()->json([
'code'=> 0,
'message'=>"注册成功"
]);
}else{
return response()->json([
'code'=>1000,
'message'=>"注册失败"
]);
}
}
}<?php
/**
* Created by PhpStorm. swoole 服务层
* User: Admin
* Date: 2021/7/29
* Time: 23:20
*/
namespace App\Service;
use App\Plug\Consul\RegisterService;
use App\Service\Dao\DaoUserService;
class UserServer
{
protected $server;
protected $class;
public function __construct()
{
$this->server = new \Swoole\Server("0.0.0.0", 9506);
$this->onConnect();
$this->onReceive();
$this->onClose();
$this->onWorkerStart();
$this->onStart();
$this->start();
$this->server->set(array(
'open_length_check' => true, // 开启协议解析
'package_max_length' => 81920,
'package_length_type' => 'N', // 长度字段的类型
'package_length_offset' => 0, //从第几个字节是包长度的值
'package_body_offset' => 4, //从第几个字节开始计算长度
));
}
public function onStart()
{
$this->server->on('start', function ($serv) {
//注册服务
//$res = (new RegisterService('192.168.211.130','8510'))->register('user','UserServer',['user_tag'],9504);
//echo 'consul注册成功'.$res.PHP_EOL;
});
}
public function onWorkerStart()
{
$this->server->on('WorkerStart', function ($serv, $fd) {
$this->class = new DaoUserService();
echo "OrderServer: 启动.\n";
});
}
public function onConnect()
{
$this->server->on('Connect', function ($serv, $fd) {
echo "Client: Connect.\n";
});
}
public function onReceive()
{
$this->server->on('Receive', function ($serv, $fd, $from_id, $data) {
$info = unpack('N', $data);
$len = $info[1];
$body = substr($data, - $len);
$data = json_decode($body, true);
$service = $data['service'];
$action = $data['action'];
$page = $data['param'];
$class = $data['class'];
//$instance = new $class;
$instance = $this->class;
$result = $instance->$action($page);
$result = json_encode($result);
$serv->send($fd, $result);
});
}
public function onClose()
{
$this->server->on('Close', function ($serv, $fd) {
echo "Client: Close.\n";
});
}
public function start()
{
$this->server->start();
}
}
//new UserServer();本文由:xiaoshu168.com 作者:xiaoshu发表,转载请注明来源!