承接 northwestern-sysdev/event-hub-php-sdk 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

northwestern-sysdev/event-hub-php-sdk

Composer 安装命令:

composer require northwestern-sysdev/event-hub-php-sdk

包简介

PHP SDK for the Northwestern EventHub

README 文档

README

This is a set of PHP classes design to give you easy access to the new Northwestern EventHub & AMQ.

As of writing, this PHP SDK implements methods for all EventHub API calls.

Installation

This package is available via composer:

composer require northwestern-sysdev/event-hub-php-sdk

The latest version of this package supports PHP v7.4+. It may not work on older versions of PHP. Please use v1 if you require compatability with older versions!

Examples

Here are some quick examples for using this SDK. For more information about EventHub & its capabilities, please see the documentation on the Service Registry.

Note that all methods from this SDK can throw Northwestern\SysDev\SOA\EventHub\Exception\EventHubDown and Northwestern\SysDev\SOA\EventHub\Exception\EventHubError messages. The former indicates a network problem or outage; the latter is something wrong with your usage of EventHub.

Consumers

You can loop something like the below example code & schedule it in cron to poll the queue for messages.

$message_api = new \Northwestern\SysDev\SOA\EventHub\Message('https://northwestern-dev.apigee.net', 'my api key', new GuzzleHttp\Client);
$topic_name = 'etsysdev.test.queue.name';

try {
    $message = $message_api->readOldest($topic_name); // returns a DeliveredMessage object

    // The ID is useful for moving messages & troubleshooting. The raw message will be a plain text representation, ideal for logging!
    log_stuff_in_my_database($message->getId(), $message->getRawMessage());

    // If you use JSON messages, this will be a PHP associative array. For XML, you'll need to getRawMessage() and parse it yourself.
    $body = $message->getMessage();
    update_my_database($body['some_unique_id_from_the_message'], $body['some_other_info']['a_field']);

    // Should be the last thing you do in your try block
    $message_api->acknowledgeOldest($topic_name);
} catch (\Exception $e) {
    // If we get an error before the acknowledgeOldest call, the message won't be ack'd & removed from the queue.
    // This gives you an opportunity to fix your stuff & try again!
}

Just be aware that EventHub supports webhook delivery; it can do HTTP POSTs to your application when it receives messages in real-time. You should evaluate that option before implementing queue polling.

Publishers

$topic_api = new \Northwestern\SysDev\SOA\EventHub\Topic('https://northwestern-dev.apigee.net', 'my api key', new GuzzleHttp\Client);
$topic_name = 'etsysdev.test.queue.name';

// If you are sending JSON messages, you can build your messages as PHP associative arrays and send those.
$my_message = [
    'id' => 1,
    'important_enterprise_data' => 'Bananas float in water because they are less dense in comparison.',
    'crucial_security_info' => 'Bananas grow on plants that are officially considered an herb.',
];
$message_id = $eh->writeJsonMessage($topic_name, $my_message);

// For XML, you are responsible for building the string & sending the appropriate content type.
$my_message = '<?xml version="1.0" encoding="UTF-8"?><banana><fact>The banana is actually classified as a berry.</fact></banana>'; // but you're using an XML builder -- do whatever to cast to string
$message_id = $eh->writeMessage($topic_name, $my_message, 'application/xml');

Managing Webhooks

EventHub can be configured to deliver messages destined for your application via HTTP POSTs to an API endpoint you've created via webhooks. This is a self-service feature you can configure yourself.

For full details on how this works & the config options, see the EventHub Webhook documentation.

$webhook_api = new \Northwestern\SysDev\SOA\EventHub\Webhook('https://northwestern-dev.apigee.net', 'my api key', new GuzzleHttp\Client);
$topic_name = 'etsysdev.test.queue.name';

// Create a paused webhook
$details = $webhook_api->create($topic_name, [
    'topicName' => $topic_name,
    'endpoint' => 'https://my-app-dev.northwestern.edu/api/webhook/receive', // the URL in your application
    'contentType' => 'application/json', // desired format for delivered messages
    'active' => false, // start off paused, so no deliveries are made to your app
    'securityTypes' => ['NONE'], // what authentication method(s) need to be done to authenticate w/ your endpoint -- see the webhook documentation for more info
    'webhookSecurity' => [
        ['securityType' => 'NONE']
    ]
]);

// When you're ready, turn the webhook on:
$details = $webhook_api->unpause($topic_name);

// You can adjust any of your settings whenever you need to -- see the EventHub docs for more info
$details = $webhook_api->updateConfig($topic_name, [
    'endpoint' => 'https://my-app-dev.northwestern.edu/api/v2/webhooks',
]);

// Or remove the webhook entirely & go back to polling the queue
$webhook_api->delete($topic_name);

FAQs

Why do I have to pass in a GuzzleHttp Client?

I've split this package off from a Laravel-specific one, and having Guzzle in the constructor makes it easy for me to let Laravel's service container inject the dependency.

Guzzle supports some cool middleware stuff, which you may want to set up before giving it to the EventHub SDK.

In fact, this package comes with a re-try middleware for temporary network errors. You can do this instead to get a Guzzle Client that will make three immediate re-try attempts if it's unable to establish a connection to EventHub:

$client = \Northwestern\SysDev\SOA\EventHub\Guzzle\RetryClient::make();
$api = new \Northwestern\SysDev\SOA\EventHub\Webhook('https://northwestern-dev.apigee.net', 'my api key', $client);

This won't auto-retry anything that gets an HTTP error code, e.g. 401 Unauthorized won't trigger a re-try attempt. Feel free to extend the class & adjust createRetryHandler() to better suit your needs.

Do you have more documentation?

Not really. There are PHP docblocks in the code, but they just refer you back to the main EventHub API documentation. This SDK is just a little adapter layer to make using EventHub feel more PHP-y.

I need help!

The I&A team is the primary contact for EventHub -- I'm an end-user too!

But, if you have questions specifically abou the PHP SDK, you can ask in #et-sysdev on the NIT Slack or email nick.evans@northwestern.edu.

Contributing

Submit a pull request!

If you need to include a local copy of the package for development purposes, adjust your consuming apps' composer.json thusly:

{
    // Add this section
    "repositories": [
        {
            "type": "path",
            "url": "/home/vagrant/code/SysDev-EventHub-PHP-SDK"
        }
    ],

    "require": {
        // Any branch that isn't named in a version format can be specified by prefixing
        // it with 'dev-', so this would install the 'my-feature' branch from a local copy of the package.
        "northwestern-sysdev/event-hub-php-sdk": "dev-my-feature"
    },
}

You can test the package by running phpunit && ./vendor/bin/phpstan analyse --level 5 src/.

northwestern-sysdev/event-hub-php-sdk 适用场景与选型建议

northwestern-sysdev/event-hub-php-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 68.86k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 10 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「northwestern」 「northwestern university」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 northwestern-sysdev/event-hub-php-sdk 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-10-31