bearsunday/thrift 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

bearsunday/thrift

最新稳定版本:0.1.0

Composer 安装命令:

composer require bearsunday/thrift

包简介

README 文档

README

CI

A package that allows high-speed access to BEAR.Sunday resources from multiple languages using Thrift and Swoole.

Features

  • BEAR.Sunday resources can be utilized across various languages and frameworks
  • Seamless access to remote BEAR.Sunday apps from the local BEAR.Sunday app
  • Access resources regardless of language (currently supports PHP, Go, Python, Ruby and BEAR.Sunday)
  • Fast access with Thrift/Swoole, compared to HTTP
  • Can call resources from older versions of BEAR.Sunday apps
  • Operates independently of an HTTP server

Installation

composer require bearsunday/thrift

Usage

Server-side (PHP:BEAR.Sunday)

bin/serve.php

(new ServerBootstrap)(new Config(
    appName: 'MyVendor\MyApp',
    hostname: '127.0.0.1',
    port: 9090,
    appDir: dirname(__DIR__) . '/path/to/app',
    context: 'prod-app',
    server: Engine::Swoole
))

Start Thrift server

> php bin/serve.php
[Sun Mar 17 19:39:20 2024] PHP 8.3.4 Thrift Server (powered by Swoole) started.
Listening on http://127.0.0.1:9090
Document root is /path/to/app
Application context is prod-hal-api-app

Client-side

BEAR.Sunday

Import Thrift App in the module.

protected function configure(): void
{
    // Binding thirft app to a host called "sekai"
    $this->override(new ImportThriftAppModule([
        new ThriftApp('sekai', '127.0.0.1', '9090')
    ]));
}

Thrift apps available just like the 'self' app. See more.

echo $resource->get('page://self/?name=Sekai');  // "greeting": "Konichiwa Sekai" from local app
echo $resource->get('page://sekai/?name=World'); // "greeting": "Hello World" from remote(127.0.0.1:9090) app
// echo $resource->get('/?name=World'); // "greeting": "Hello World" from remote(127.0.0.1:9090) app

PHP

$method = 'get';
$uri = '/user?id=1';

$invoke = new ResourceInvoke($host, $port);
$response = $invoke($method, $uri);

assert($response instanceof ResourceResponse);
printf("Response Code: %s\n", $response->code);
printf("Response Headers: %s\n", json_encode($response->headers));
printf("Raw Response JsonValue: : %s\n", $response->jsonValue);
printf("Response View: %s\n", $response->view);

Go

method := "get"
uri := "/user?id=1

response, err := ResourceInvoke(hostname, port, method, uri)

fmt.Println("Response Code:", response.Code)
fmt.Println("Response Headers:", response.Headers)
fmt.Println("Raw Response JsonValue: ", response.JsonValue)
fmt.Println("Response View:", response.View)

Python

method = "get"
uri = "/user?id=1"

response = ResourceInvoke(hostname, port, method, uri)

print("Response Code:", response.code)
print("Response Headers:", response.headers)
print("Raw Response JsonValue:", response.jsonValue)
print("Response View:", response.view)

Ruby

method = "get"
uri = "/user?id=1"

resource_invoke = ResourceInvoke.new(hostname, port)
response = resource_invoke.call(method, uri)

puts "Response Code: #{response.code}"
puts "Response Headers: #{response.headers}"
puts "Raw Response JsonValue: #{response.jsonValue}"
puts "Response View: #{response.view}"

Note: The URI can be a schema as well as a path. Instead of /user?id=1, you can specify page://self/user?id=1 to access both app and page resources.

As you can see, it's easy to access BEAR.Sunday resources from other languages. Resources become assets that transcend applications and can be accessed quickly.

Demo

Build go & python and start the thrift server.

composer build:all
composer serve

Run the go and pythonclient with another terminal.

composer run:go
composer run:py
composer run:php

Note: Swoole, Thrift, go and python must be installed to run the above scripts. Straight forward if you're installing with brew.

brew install thrift
brew install go
brew install python3

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-18