菜单开关

周梦康 发表于 2017-10-22 1286 次浏览

免费领取阿里云优惠券 我的直播 - 《PHP 进阶之路》

本实验基于http,有待改善,仅用于功能实现的demo

注册中心

define("SERVER_IP", "127.0.0.1");
define("SERVER_PORT", 9505);

$http = new swoole_http_server(SERVER_IP, SERVER_PORT);

$http->on("start", function ($server) {
    echo "http server is started at http://" . SERVER_IP . ":" . SERVER_PORT . "\n";
});

$http->on("request", function ($request, $response) {
    $res = httpRequest($request);
    $response->header("Content-Type", "text/plain");
    $response->end($res);
});

$http->start();

function httpRequest($request)
{
    $argv = $request->rawContent();

    if ($argv) {
        $argv = json_decode($argv, true);
    }

    if (isset($argv['provider'])){
        foreach ($argv['provider'] as $provider){
            // 入库
            file_put_contents(__DIR__."/provider/list",
                sprintf("%s,%s,%s\n",$provider['service'],$provider['ip'],$provider['port']),
                FILE_APPEND);
        }
    }

    $list = file(__DIR__."/provider/list");

    $providers = [];
    foreach ($list as $item){
        $tmp = explode(",",$item);
        $service = array_shift($tmp);
        $providers[$service] = trim(join(":",$tmp));
    }

    $res = [];
    if (isset($argv['consumer'])){
        foreach ($argv['consumer'] as $item){
            if (isset($providers[$item])){
                $res[$item] = $providers[$item];
            }
        }
    }

    return json_encode($res);
}

服务提供者

define("SERVER_IP", "127.0.0.1");
define("SERVER_PORT", 9507);

$http = new swoole_http_server(SERVER_IP, SERVER_PORT);

$http->on("start", function ($server) {
    echo "http server is started at http://" . SERVER_IP . ":" . SERVER_PORT . "\n";
    registerService([['service' => UserService::class,'ip' => SERVER_IP,'port' => SERVER_PORT]], null);
});

$http->on("request", function ($request, $response) {
    $res = httpRequest($request);
    $response->header("Content-Type", "text/plain");
    $response->end($res);
});

$http->start();


function httpRequest($request)
{
    $service = $request->get['service'];
    $action = $request->get['action'];

    if (!isset($service) || !isset($action)) {
        return "";
    }

    $argv = $request->rawContent();

    if ($argv) {
        $argv = json_decode($argv, true);
    }

    $res = call_user_func_array([$service, $action], $argv);

    return json_encode($res);
}

class UserService
{
    public static function getUserInfo($uid)
    {
        // 假设以下内容从数据库取出
        return [
            'id'       => $uid,
            'username' => 'mengkang',
        ];
    }
}


/**
 * @param array $provider 提供服务
 * @param array $consumer 服务消费
 * @return array
 */
function registerService($provider, $consumer)
{
    $content = json_encode(['provider' => $provider, 'consumer' => $consumer]);
    $options['http'] = [
        'timeout' => 5,
        'method'  => 'POST',
        'header'  => 'Content-type:application/x-www-form-urlencoded',
        'content' => $content,
    ];

    $context = stream_context_create($options);

    $url = "http://127.0.0.1:9505";

    $res = file_get_contents($url, false, $context);

    return json_decode($res, true);
}

服务消费者

define("SERVER_IP", "127.0.0.1");
define("SERVER_PORT", 9503);
define("SOA_SERVER_PORT", 9504);

include __DIR__."/RpcClient.php";

$http = new swoole_http_server(SERVER_IP, SERVER_PORT);

$http->on("start", function ($server) {
    echo "http server is started at http://" . SERVER_IP . ":" . SERVER_PORT . "\n";
    registerService(null, ["UserService"]);
});

$http->on("request", function ($request, $response) {
    $userService = new Client('UserService');
    $res = $userService->getUserInfo(103);
    $response->header("Content-Type", "text/plain");
    $response->end(var_export($res,true));
});


$http->start();

/**
 * @param array $provider 提供服务
 * @param array $consumer 服务消费
 * @return array
 */
function registerService($provider, $consumer)
{
    $content = json_encode(['provider' => $provider, 'consumer' => $consumer]);
    $options['http'] = [
        'timeout' => 5,
        'method'  => 'POST',
        'header'  => 'Content-type:application/x-www-form-urlencoded',
        'content' => $content,
    ];

    $context = stream_context_create($options);

    $url = "http://127.0.0.1:9505";

    $res = file_get_contents($url, false, $context);

    $res = json_decode($res, true);

    if ($res){
        foreach ($res as $service => $socket){
            file_put_contents(__DIR__."/provider/".$service,$socket);
        }
    }
}

嗨,老铁,欢迎来到我的博客!

如果觉得我的内容还不错的话,可以关注下我在 segmentfault.com 上的直播。我主要从事 PHP 和 Java 方面的开发,《深入 PHP 内核》作者之一。

[视频直播] PHP 进阶之路 - 亿级 pv 网站架构的技术细节与套路 直播中我将毫无保留的分享我这六年的全部工作经验和踩坑的故事,以及会穿插着一些面试中的 考点难点加分点

评论列表