txf-server
2.x版本已将txf-admin合并到txf-server中,不再使用json-rpctxf-server
基于 hyperf 开发,使用其 jsonrpc
功能。
txf-server
不做任何请求参数验证,所以最好配置成只能内网请求且前置服务器做好充足的参数校验、过滤工作。
新建数据库
我不太喜欢使用 migration
,所以直接使用数据库创建了
CREATE TABLE `txf_content` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL COMMENT '标题',
`content` text NOT NULL COMMENT '内容',
`user_id` int(10) unsigned NOT NULL COMMENT '发布人ID',
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
创建model
php bin/hyperf.php gen:model txf_content
# 配置 App\Model\Content
1. 删除引用,让它继承于当前目录的 Model
use Hyperf\DbConnection\Model\Model;
2. 增加属性,让mysql自己管理 created_at updated_at
public $timestamps = false;
开发接口
namespace App\Service;
use App\Model\Content;
use App\Service\Interfaces\ContentServiceInterface;
use Hyperf\RpcServer\Annotation\RpcService;
/**
* Class ContentService
* @package App\Service
* @RpcService(name="ContentService", protocol="jsonrpc", server="jsonrpc")
*/
class ContentService implements ContentServiceInterface
{
public function addContent(array $data)
{
return Content::query()->insert($data);
}
}
jsonrpc-server配置
默认配置在 config/autoload/server.php
中
环境配置
默认配置如下,根据你的实际环境进行调整
APP_NAME=txf-server
APP_ENV=dev
DB_DRIVER=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=txf
DB_USERNAME=root
DB_PASSWORD=123456
DB_CHARSET=utf8mb4
DB_COLLATION=utf8mb4_unicode_ci
DB_PREFIX=txf_
REDIS_HOST=127.0.0.1
REDIS_AUTH=(null)
REDIS_PORT=6379
REDIS_DB=1
REDIS_MAX_IDLE_TIME=60
# JSONRPC-SERVER地址
SERVER_HOST=127.0.0.1
SERVER_PORT=9505