hyperf從零開始構(gòu)建微服務(wù)(一)——構(gòu)建服務(wù)提供者

閱讀目錄

上一篇文章我們了解了如何使用hyperf對(duì)項(xiàng)目進(jìn)行垂直拆分,這是我們整個(gè)微服務(wù)模塊的基礎(chǔ)。

hyperf支持JSON-RPC和gRPC,我們?cè)?a target="_blank">分布式服務(wù)架構(gòu)一文中介紹過什么是JSON-RPC以及JSON-RPC請(qǐng)求響應(yīng)的案例(后來做的補(bǔ)充),后面我們會(huì)著重以JSON-RPC為例來解決整個(gè)微服務(wù)系統(tǒng)出現(xiàn)的各種問題。

首先我們加以明確什么是服務(wù)。

服務(wù)有兩種角色,一種是 服務(wù)提供者(ServiceProvider),即為其它服務(wù)提供服務(wù)的服務(wù),另一種是 服務(wù)消費(fèi)者(ServiceConsumer),即依賴其它服務(wù)的服務(wù),一個(gè)服務(wù)既可能是 服務(wù)提供者(ServiceProvider),同時(shí)又是 服務(wù)消費(fèi)者(ServiceConsumer)。而兩者直接可以通過 服務(wù)契約 來定義和約束接口的調(diào)用,在 Hyperf 里,可直接理解為就是一個(gè) 接口類(Interface),通常來說這個(gè)接口類會(huì)同時(shí)出現(xiàn)在提供者和消費(fèi)者下?!怨倬W(wǎng)。

簡(jiǎn)單的說,我們的項(xiàng)目模塊將會(huì)被分為服務(wù)提供者模塊和服務(wù)消費(fèi)者模塊。 服務(wù)提供者模塊指的是提供各種服務(wù)的模塊,它需要與數(shù)據(jù)庫進(jìn)行交互。 服務(wù)消費(fèi)者模塊指的是消費(fèi)服務(wù)的模塊。它需要遠(yuǎn)程訪問服務(wù)提供者。

本節(jié)課的源碼已上傳至github,https://github.com/bailangzhan/hyperf-rpc

下面我們按照步驟,看看如何構(gòu)建服務(wù)提供者。

1、創(chuàng)建數(shù)據(jù)表

CREATE DATABASE hyperf;
USE hyperf;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL DEFAULT '' COMMENT '姓名',
  `gender` tinyint(1) NOT NULL DEFAULT '0' COMMENT '性別 1男 2女 0未知',
  `created_at` int(11) NOT NULL DEFAULT '0' COMMENT '創(chuàng)建時(shí)間',
  `updated_at` int(11) NOT NULL DEFAULT '0' COMMENT '更新時(shí)間',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用戶基礎(chǔ)表';

2、構(gòu)建服務(wù)提供者

composer create-project hyperf/hyperf-skeleton shop_provider_user

安裝的時(shí)候會(huì)讓我們選擇默認(rèn)的組件,除了時(shí)區(qū)和數(shù)據(jù)庫外,其他一律選擇“n”,選擇如下

What time zone do you want to setup ?
  [n] Default time zone for php.ini
Make your selection or type a time zone name, like Asia/Shanghai (n):
Asia/Shanghai
  Do you want to use Database (MySQL Client) ?
  [y] yes
  [n] None of the above
  Make your selection or type a composer package name and version (yes): y
  - Adding package hyperf/database (~2.2.0)
  - Adding package hyperf/db-connection (~2.2.0)
  Do you want to use Redis Client ?
  [y] yes
  [n] None of the above
  Make your selection or type a composer package name and version (yes): n
  Which RPC protocol do you want to use ?
  [1] JSON RPC with Service Governance
  [2] JSON RPC
  [3] gRPC
  [n] None of the above
  Make your selection or type a composer package name and version (n): n
  Which config center do you want to use ?
  [1] Apollo
  [2] Aliyun ACM
  [3] ETCD
  [4] Nacos
  [n] None of the above
  Make your selection or type a composer package name and version (n): n
  Do you want to use hyperf/constants component ?
  [y] yes
  [n] None of the above
  Make your selection (n): n
  Do you want to use hyperf/async-queue component ? (A simple redis queue component)
  [y] yes
  [n] None of the above
  Make your selection or type a composer package name and version (n): n
  Do you want to use hyperf/amqp component ?
  [y] yes
  [n] None of the above
  Make your selection or type a composer package name and version (n): n
  Do you want to use hyperf/model-cache component ?
  [y] yes
  [n] None of the above
  Make your selection or type a composer package name and version (n): n
  Do you want to use hyperf/elasticsearch component ?
  [y] yes
  [n] None of the above
  Make your selection or type a composer package name and version (n): n
  Do you want to use hyperf/tracer component ? (An open tracing protocol component, adapte with Zipkin etc.)
  [y] yes
  [n] None of the above
  Make your selection or type a composer package name and version (n): n

需要什么組件后面可以我們?cè)僮孕刑砑印?/p>

3、安裝json rpc依賴

cd shop_provider_user
composer require hyperf/json-rpc

4、安裝rpc server組件

我們準(zhǔn)備讓 shop_provider_user 應(yīng)用對(duì)外提供服務(wù),所以需要安裝 rpc server組件

composer require hyperf/rpc-server

5、修改server配置

shop_provider_user 提供的是 jsonrpc服務(wù),不需要提供http服務(wù),所以屏蔽 http 服務(wù)配置,新加 jsonrpc-http 服務(wù)。

hyperf支持 jsonrpc-http 協(xié)議、jsonrpc 協(xié)議以及jsonrpc-tcp-length-check 協(xié)議,我們后續(xù)都將以 jsonrpc-http為例。

以下配置在 config/autoload/server.php 文件內(nèi),注意 jsonrpc-http服務(wù)配置的端口號(hào)是9600

'servers' => [
//        [
//            'name' => 'http',
//            'type' => Server::SERVER_HTTP,
//            'host' => '0.0.0.0',
//            'port' => 9501,
//            'sock_type' => SWOOLE_SOCK_TCP,
//            'callbacks' => [
//                Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
//            ],
//        ],
    [
        'name' => 'jsonrpc-http',
        'type' => Server::SERVER_HTTP,
        'host' => '0.0.0.0',
        'port' => 9600,
        'sock_type' => SWOOLE_SOCK_TCP,
        'callbacks' => [
            Event::ON_REQUEST => [\Hyperf\JsonRpc\HttpServer::class, 'onRequest'],
        ],
    ],
],

6、配置數(shù)據(jù)庫

修改.env文件,修改 APP_NAME以及數(shù)據(jù)庫配置

APP_NAME=shop_provider_user
DB_DRIVER=mysql
DB_HOST=192.168.33.20
DB_PORT=3306
DB_DATABASE=hyperf
DB_USERNAME=www
DB_PASSWORD=123456
DB_CHARSET=utf8mb4
DB_COLLATION=utf8mb4_unicode_ci
DB_PREFIX=

7、編寫基礎(chǔ)代碼

7-1、編寫model代碼

生成model并修改如下:

php bin/hyperf.php gen:model User
【app/Model/User.php】
<?php
declare (strict_types=1);
namespace App\Model;
use Hyperf\DbConnection\Model\Model;
/**
 */
class User extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'gender'];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'gender' => 'integer'];
    // 自定義時(shí)間戳的格式 U表示int
    protected $dateFormat = 'U';
}

7-2、編寫service代碼

app下新建JsonRpc目錄,編寫UserService.php和UserServiceInterface.php文件。

UserServiceInterface 對(duì)外提供兩個(gè)接口,一個(gè)用于創(chuàng)建用戶,一個(gè)用于獲取用戶信息。

【UserServiceInterface】
<?php
namespace App\JsonRpc;
interface UserServiceInterface
{
    public function createUser(string $name, int $gender);
    public function getUserInfo(int $id);
}
【UserService】
<?php
namespace App\JsonRpc;
use App\Model\User;
use Hyperf\RpcServer\Annotation\RpcService;
/**
 * @RpcService(name="UserService", protocol="jsonrpc-http", server="jsonrpc-http")
 */
class UserService implements UserServiceInterface
{
    /**
     * @param string $name
     * @param string $gender
     * @return string
     */
    public function createUser(string $name, int $gender)
    {
        if (empty($name)) {
            throw new \RuntimeException("name不能為空");
        }
        $result = User::query()->create([
            'name' => $name,
            'gender' => $gender,
        ]);
        return $result ? "success" : "fail";
    }
    /**
     * @param int $id
     * @return array
     */
    public function getUserInfo(int $id)
    {
        $user = User::query()->find($id);
        if (empty($user)) {
            throw new \RuntimeException("user not found");
        }
        return $user->toArray();
    }
}

注意,在 UserService 類中,我們使用了 @RpcService 注解,記得 use Hyperf\RpcServer\Annotation\RpcService;

@RpcService 共有 4 個(gè)參數(shù),也就是 Hyperf\RpcServer\Annotation\RpcService 的4個(gè)屬性:

  1. name 屬性為定義該服務(wù)的名稱,注意不同的service不要用相同的名字,該名字唯一,Hyperf 會(huì)根據(jù)該屬性生成對(duì)應(yīng)的 ID 注冊(cè)到服務(wù)中心去,后面我們會(huì)詳細(xì)介紹,這里有個(gè)印象就好;
  2. protocol 屬性為定義該服務(wù)暴露的協(xié)議,目前僅支持 jsonrpc-http, jsonrpc, jsonrpc-tcp-length-check ,分別對(duì)應(yīng)于 HTTP 協(xié)議和 TCP 協(xié)議下的兩種協(xié)議,默認(rèn)值為 jsonrpc-http,這里的值對(duì)應(yīng)在 Hyperf\Rpc\ProtocolManager 里面注冊(cè)的協(xié)議的 key,它們本質(zhì)上都是 JSON RPC 協(xié)議,區(qū)別在于數(shù)據(jù)格式化、數(shù)據(jù)打包、數(shù)據(jù)傳輸器等不同;
  3. server 屬性為綁定該服務(wù)類發(fā)布所要承載的 Server,默認(rèn)值為 jsonrpc-http,該屬性對(duì)應(yīng) config/autoload/server.php 文件內(nèi) servers 下所對(duì)應(yīng)的 name;
  4. publishTo 屬性為定義該服務(wù)所要發(fā)布的服務(wù)中心,目前僅支持 consul、nacos 或?yàn)榭?,我們這里先不做設(shè)置,留空

到這里我們就構(gòu)建好一個(gè)基本的服務(wù)提供者了。

postman測(cè)試

下面我們測(cè)試下這兩個(gè)接口是否正常。執(zhí)行命令 php bin/hyperf.php start 啟動(dòng)服務(wù),利用postman發(fā)送請(qǐng)求。

請(qǐng)求地址:http://127.0.0.1:9600
請(qǐng)求方法:POST
請(qǐng)求參數(shù)
{
    "jsonrpc": "2.0",
    "method": "/user/createUser",
    "params": {
        "name": "zhangsan",
        "gender": 3
    },
    "id": "61025bc35e07d",
    "context": []
}
header頭
Content-Type: application/json
響應(yīng)結(jié)果
{
    "jsonrpc": "2.0",
    "id": "61025bc35e07d",
    "result": "success",
    "context": []
}
image

看下數(shù)據(jù)表

image

created_at 和 update_at 這兩個(gè)字段被自動(dòng)填充。

再利用 postman訪問 /user/getUserInfo 方獲試試。

請(qǐng)求地址:http://127.0.0.1:9600
請(qǐng)求方法:POST
請(qǐng)求參數(shù)
{
    "jsonrpc": "2.0",
    "method": "/user/getUserInfo",
    "params": {
        "id": 1
    },
    "id": "61025bc35e07d",
    "context": []
}
header頭
Content-Type: application/json
響應(yīng)結(jié)果
{
    "jsonrpc": "2.0",
    "id": "61025bc35e07d",
    "result": {
        "id": 1,
        "name": "zhangsan",
        "gender": 3,
        "created_at": "1630101991",
        "updated_at": "1630101991"
    },
    "context": []
}

到這里,我們的服務(wù)提供者基本上就構(gòu)建好了,有同學(xué)可能看其他文章介紹還有consul的內(nèi)容,我們后續(xù)還會(huì)介紹更多內(nèi)容,稍安勿躁。

下一節(jié),我們繼續(xù)構(gòu)建服務(wù)消費(fèi)者。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容