yaf && yar微服务/hprose微服务 镜像初始化 —— k8s从入门到高并发系列教程 (四)

        前面的教程已经在docker镜像 软件 层面上初步安装了企业常用的插件,但目前还没有写任何代码。本教程带你初始化yaf框架,并基于yar框架和hprose跨语言微服务框架打包两个微服务代码,在容器间调用。

        yaf是一个用c语言写的,用于php项目mvc分离的框架。yar同样是c语言写的,支持php微服务之间基于tcp协议相互调用。hprose则是php的一个composer包,支持php语言和其他语言之间相互调用。

        用户访问流程图:

我们先把之前那个lnmp文件夹拷贝两份,一份文件夹为 test_api,另一份文件夹为 test_client1 

cp -r lnmp test_api

cp -r lnmp test_client1

yaf

php yaf框架的配置文件内容为

extension = yaf.so
yaf.environ = ${APP_ENV}
yaf.use_namespace = Off
yaf.use_spl_autoload = On

对这两个文件夹下的项目统一做以下操作

修改nginx配置文件

 由于yaf框架要求隐藏入口index.php文件,需要修改conf/default.conf文件,在 location / 下增加 try_files $uri $uri/ /index.php$is_args$args;

    location / {
        root   /src;
        index  index.html index.htm index.php;
        try_files $uri $uri/ /index.php$is_args$args;
    }

使用yaf脚手架初始化项目

克隆yaf项目源码,使用源码中的脚手架工具初始化项目

git clone https://ghproxy.com/https://github.com/laruence/yaf.git /tmp/yaf
/tmp/yaf/tools/cg/yaf_cg -d test_api/www -a test_api

/tmp/yaf/tools/cg/yaf_cg -d test_client1/www -a test_client1

yar

php yar框架的配置文件内容为

extension = yar.so
yar.debug = off
yar.allow_persistent = Off
yar.connect_timeout = 1500
yar.content_type = application/octet-stream 
yar.expose_info = On
yar.packager = php
yar.timeout = 5000
yar.transport = curl

创建yar server

在 test_client1 项目中创建一个yar server

application/models/Tserver.php 文件,定义yar server的服务内容

<?php

class TserverModel {

    public function fun1()
    {
        return "haha";
    }

    protected function __auth($provider, $token) {

        if($provider == 'testprovider'){
            if($token == 'testtoken'){
                return true;
            }
        }

        return false;
    }

}

其中 __auth 函数是用来做微服务接口鉴权的,返回true代码鉴权通过

application/controllers/Tserver.php 文件,定义 yar server 的入口

<?php
  
class TserverController extends Yaf_Controller_Abstract {
  
  public function indexAction() {
    $service = new Yar_Server(new TserverModel());
    $service->handle();
    
    return false;
  }
  
}

使用yar client 调用 test_client1 微服务

我们在 test_api 项目中创建一个yar client,调用在 test_client1 项目中创建的微服务

application/controllers/Tclient.php 文件,作为yar client 客户端调用 yar server中的服务

<?php

class TclientController extends Yaf_Controller_Abstract {

    public function indexAction() {
        $client = new Yar_Client("http://test_client1/tserver/");
        $client->SetOpt(YAR_OPT_CONNECT_TIMEOUT, 1000);
        $client->setOpt(YAR_OPT_PROVIDER, "testprovider");
        $client->setOpt(YAR_OPT_TOKEN, "testtoken");
        $ret = $client->fun1();
        echo $ret;
        return false;
    }

}

 这段代码中使用了微服务调用的token,设置了微服务调用的超时时间

hprose

创建hprose server

在 test_client1 项目中创建一个hprose server

application/controllers/Tserver.php 文件,定义 hprose server 的入口

<?php

class TserverController extends Yaf_Controller_Abstract
{
    public function indexAction()
    {
        $model = new TserverModel();
        $service = new \Hprose\Http\Server();
        $service->addInstanceMethods($model);
        $service->handle();

        return false;
    }
}

使用hprose client 调用 test_client1 微服务

我们在 test_api 项目中创建一个hprose client,调用在 test_client1 项目中创建的微服务

application/controllers/Tclient.php 文件,作为hprose client 客户端调用 hprose server中的服务

<?php

class TclientController extends Yaf_Controller_Abstract
{
    public function indexAction()
    {
        $client = new Hprose\Http\Client('http:/test-client1/tserver/', false);
        $proxy = $client->useService('');
        try {
            $ret = $proxy->fun1();
        } catch (Exception $e) {
            print_r($e);
            exit;
        }

        echo $ret;
        return false;
    }
}

打包镜像

分别进入两个项目文件夹,打包项目代码到镜像中

cd test_api && sudo docker build -t php-fpm:test_api .

cd test_client1 && sudo docker build -t php-fpm:test_client1 .

启动test_client1

sudo docker run --name test_client1 -d php-fpm:test_client1

启动test_api,把test_client1容器关联到test_api容器中

sudo docker run --name test_api \
--link test_client1:test_client1 \
-p 8083:80 -d php-fpm:test_api

浏览器输入 http://127.0.0.1:8083/tclient 测试

 相关链接

手把手教你部署nginx+php

php和nginx镜像合并 && 代码打包到镜像 

nginx-php镜像安装常用软件 

yaf && yar微服务/hprose微服务 镜像初始化 

常用开发工具:php_codesniffer代码规范检查&修复、phpstan语法检查、phpunit单元测试 

.gitlab-ci.yaml自动镜像打包&&互联网企业规范化上线流程(上) 

kustomize/kubectl自动镜像部署&&互联网企业规范化上线流程(下) 

apisix网关、JMeter压测  

prometheus/grafana监控数据收集与展示 

k8s容器网络性能调优 

supervisor进程管理 

安装opcache和apcu 

APM性能监测工具skywalking 

链路跟踪工具zipkin

phpfpm和nginx配置

php整合apollo配置中心

php rdkafka操作kafka消息队列

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Yaf 是一个高效的 PHP 框架,它支持多种缓存机制,其中之一便是 Memcache。下面以一个简单的示例来说明如何在 Yaf 中使用 Memcache。 首先,确保已经安装了 Memcache 扩展,并在 php.ini 文件中启用了该扩展。 接下来,在 Yaf 的配置文件中添加以下代码: ```php // application.ini [product] application.directory = APP_PATH "/application/" ; Memcache 缓存配置 cache.memcache.enable = true cache.memcache.server = "127.0.0.1" cache.memcache.port = 11211 cache.memcache.prefix = "yaf_" ``` 这里定义了一个名为 `cache.memcache` 的缓存配置,启用了 Memcache 缓存,并指定了 Memcache 服务器的地址、端口和缓存前缀。 接着,在 Yaf 的 Bootstrap 文件中添加以下代码: ```php // Bootstrap.php class Bootstrap extends Yaf_Bootstrap_Abstract { public function _initCache() { // 获取缓存配置 $config = Yaf_Application::app()->getConfig()->cache->memcache; // 初始化 Memcache $cache = new Memcache(); $cache->connect($config->server, $config->port); // 将 Memcache 实例注册到 Yaf 的全局注册表中 Yaf_Registry::set("cache", $cache); } } ``` 这里通过 Yaf 的 Bootstrap 机制来初始化 Memcache,将其实例注册到 Yaf 的全局注册表中,方便在整个应用程序中使用。 最后,可以在 Yaf 的控制器中使用 Memcache。例如: ```php // IndexController.php class IndexController extends Yaf_Controller_Abstract { public function indexAction() { // 从 Yaf 的全局注册表中获取 Memcache 实例 $cache = Yaf_Registry::get("cache"); // 尝试从缓存中获取数据 $data = $cache->get("example"); if ($data === false) { // 如果缓存中不存在,则从数据库或其他数据源获取数据 $data = "Hello, world!"; // 将数据存入缓存中 $cache->set("example", $data); } // 输出数据 echo $data; } } ``` 这里通过 Yaf 的全局注册表来获取在 Bootstrap 中注册的 Memcache 实例,尝试从缓存中获取数据,如果缓存中不存在,则从数据库或其他数据源获取数据,并将其存入缓存中。最后输出数据。 这样,就完成了在 Yaf 中使用 Memcache 的示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fanghailiang2016

扔个包子砸我一下吧~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值