定制 insidestyles/json-rpc-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

insidestyles/json-rpc-bundle

Composer 安装命令:

composer require insidestyles/json-rpc-bundle

包简介

A simple and fast way to create json rpc server api as microservice. Json Rpc 2.0 Bundle

README 文档

README

Symfony Json Rpc Bundle Fast way to create json rpc microservice, using symfony messenger and laminas json server

  • Json Rpc 2.0
  • Batch Support

Requirements

    "php": ">=8.0",
    "ext-json": "*",
    "symfony/framework-bundle": "^5.3" || ^6.0,
    "laminas/laminas-json-server": "^3.1"

Installation

This package is installable and autoloadable via Composer

composer require insidestyles/json-rpc-bundle

Add package config: json_rpc_api.yaml

json_rpc_api:
    handlers:
        main:
            path: /api
            host: ~
            serializer: ~
            context: ~
            logger: ~
            error_handler: ~

Add Bundle to App

    Insidestyles\JsonRpcBundle\JsonRpcBundle::class => ['all' => true],

Add route config: json_rpc_api_routing.yaml

_json_rpc_api:
  resource: .
  type: json_rpc_api

Usage

  • Add Example HelloWorldApi

Update api interface with JsonRpcApi Annotation

@JsonRpcApi(namespace = "main")

<?php
    /**
     * @JsonRpcApi(namespace = "main")
     *
     * @author Fuong <insidestyles@gmail.com>
     */
    interface HelloWordJsonRpcApiInterface extends JsonRpcApiInterface
    {
        public function helloWorld(string $name);
    }
    
  • HelloWorld Api with Symfony Messenger:

Add requirement:

composer require symfony/messenger

Config Symfony Messenger https://symfony.com/doc/current/messenger.html

services:

    hello_world_api:
        class: Insidestyles\JsonRpcBundle\Api\MessageBus\HelloWorldApi
        arguments:
            - "@messenger.bus.default"
        tags:
            - {name: json_rpc_api, handler: main}

    hello_world_api_handler:
        class: Insidestyles\JsonRpcBundle\Handler\HelloWorldHandler
        tags:
            - {name: messenger.message_handler}
              
  • Add extra api endpoint with jms serializer and default symfony logger
json_rpc_api:
    handlers:
        main:
            path: /api
            host: ~
            serializer: ~
            context: ~
            logger: ~
            error_handler: ~
        auth:
            path: /auth
            host: ~
            serializer: json_rpc_api.serialzier.jms
            context: ~
            logger: monolog.logger
            error_handler: ~
  • Add custom serializer adapter
composer require jms/serializer-bundle
services:
    json_rpc_api.serialzier.custom:
        class: Insidestyles\JsonRpcBundle\Server\Adapter\Serializer\CustomSerializer
        arguments:
            - "@serializer"
  • Add custom serializer adapter
services:
  Insidestyles\\Core\\Exceptions\\ApiErrorHandler:
    class: 'Insidestyles\Core\Exceptions\ApiErrorHandler'
    tags:
      - { name: 'json_rpc_api.error_handler' }
<?php

namespace Insidestyles\Core\Exceptions;

use Doctrine\ORM\EntityNotFoundException;
use Insidestyles\JsonRpcBundle\Server\ErrorHandler\Handler\ErrorHandlerInterface;
use Throwable;

class ApiErrorHandler implements ErrorHandlerInterface
{

    private const SYSTEM_ERRORS = [
        EntityNotFoundException::class,
    ];

    public function parse(Throwable $e): array
    {
        if (!$this->isSupported($e)) {
            return [];
        }
        return [
            'code' => $e->getCode(),
            'message' => $e->getMessage(),
            'data' => [],
        ];
    }

    public function isSupported(Throwable $e): bool
    {
        return $e instanceof IsExceptionInterface || in_array(get_class($e), static::SYSTEM_ERRORS);
    }
}
  • Enable jms serializer
json_rpc_api:
    handlers:
        main:
            path: /api
            host: ~
            serializer: json_rpc_api.serializer.jms
            context: json_rpc_api.serializer.default_context            
            logger: ~
            error_handler: ~

Tips

  • Install Symfony Validator and validation middle ware from messenger bus for message validation
framework:
    messenger:
        buses:
            command_bus:
                middleware:
                    - messenger.middleware.validation
<?php

namespace Insidestyles\JsonRpcBundle\Message;

use Symfony\Component\Validator\Constraints as Assert;

/**
 * @author Fuong <insidestyles@gmail.com>
 */
class HelloWorldMessage
{
    /**
     * @Assert\NotBlank()
     * @var string
     */
    private $message;

    public function __construct(string $message)
    {
        $this->message = $message;
    }

    /**
     * @return string
     */
    public function getMessage(): string
    {
        return $this->message;
    }
}
  • You don't have to extend class AbstractApi if you don't want to use Symfony Messenger. Just implement your own interface that extends JsonRpcApiInterface. See example HelloWorldApi

  • Go to api endpoint http:://localhost/api to see json-rpc methods

  • simple request content

{
    "id": 1,
	"method": "main.helloWorld",
	"params": {
		"name": "test"
	}
}
  • batch request content
[
    {
        "id": 1,
        "method": "main.helloWorld",
        "params": {
            "name": "hello1"
        }
    },
    {
        "id": 2,
    	"method": "main.helloWorld",
    	"params": {
    		"name": "hello2"
    	}
    }
]
  • using remote service with other api endpoints

Add requirement:

composer ocramius/proxy-manager

Update service

    hello.remote_services.hello:
        tags:
            -   name: 'json_rpc_remote_service'
                url: '%hello.api_server_url%'
                class: Insidestyles\JsonRpcBundle\Sdk\Contract\HelloWordJsonRpcApiInterface

Now you can call api

    $container->get('hello.remote_services.hello')->helloWorld('Hi');

insidestyles/json-rpc-bundle 适用场景与选型建议

insidestyles/json-rpc-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 497 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 insidestyles/json-rpc-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 insidestyles/json-rpc-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 insidestyles/json-rpc-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 497
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-04-14