亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 Laravel 與微服務一起使用:是否可以在沒有數據庫的情況下使用 Eloquent?

將 Laravel 與微服務一起使用:是否可以在沒有數據庫的情況下使用 Eloquent?

PHP
holdtom 2022-12-11 08:58:52
我們正在使用微服務架構。laravel服務有兩組數據:容納管理員的數據庫。以及管理員可以訪問的所有其他數據,這些數據來自對其他服務的 GRPC 調用。我想要像 eloquent(也許是API 資源?)這樣的東西來構建數據/關系,但不是進行數據庫查詢來加載數據,而是需要對其他服務進行 GRPC 調用。我正在考慮制作一個自定義類,該類擴展了 Eloquent 并重載了調用數據庫的受保護函數,但這聽起來像是一個糟糕時期的秘訣。如果有人有做我所描述的事情的經驗,你去了哪個方向?什么起作用了?什么沒有?
查看完整描述

1 回答

?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

所以,我最終根本沒有使用 eloquent。我繼續使用文檔中解釋的協議設置。但是我使用路由綁定在控制器中啟用類型提示:

<?php


namespace App\Providers;


use OurNamespace\GrpcClient;

use Illuminate\Support\Facades\Route;

use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

use OurNamespace\Customer;

use OurNamespace\CustomerIdInput;


class RouteServiceProvider extends ServiceProvider

{

    /**

     * This namespace is applied to your controller routes.

     *

     * In addition, it is set as the URL generator's root namespace.

     *

     * @var string

     */

    protected $namespace = 'App\Http\Controllers';


    /**

     * Define your route model bindings, pattern filters, etc.

     *

     * @return void

     */

    public function boot()

    {

        //


        parent::boot();

        Route::bind('customer', function ($customerId) {

            $grpcClient = app(GrpcClient::class);

            $customerIdInput = new CustomerIdInput();

            $customerIdInput->setCustomerId($customerId);

            list($customer, $status) = $grpcClient->GetCustomerDetails($customerIdInput)->wait();

            if ($status->code != 0) {

                error_log('ERROR - ' . print_r($status, 1));

                return redirect()->back()->withErrors(['There was an error retrieving that customer', $status->details]);

            }

            return $customer;

        });

來自GrpcClientAppServiceProvider。這樣,如果我們想進行查詢,就不必手動實例化它。


<?php


namespace App\Providers;


use Illuminate\Support\ServiceProvider;

use OurNamespace\GrpcClient;

use Grpc\ChannelCredentials;


class AppServiceProvider extends ServiceProvider

{

    /**

     * Register any application services.

     *

     * @return void

     */

    public function register()

    {

        $this->app->singleton('OurNamespace\GrpcClient', function ($app) {

            return new GrpcClient(env('GRPC_HOST'), [

                'credentials' => ChannelCredentials::createInsecure(),

            ]);

        });


查看完整回答
反對 回復 2022-12-11
  • 1 回答
  • 0 關注
  • 110 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號