将 Laravel 与微服务一起使用:是否可以在没有数据库的情况下使用 Eloquent?

我们正在使用微服务架构。laravel服务有两组数据:

  • 容纳管理员的数据库。

  • 以及管理员可以访问的所有其他数据,这些数据来自对其他服务的 GRPC 调用。

我想要像 eloquent(也许是API 资源?)这样的东西来构建数据/关系,但不是进行数据库查询来加载数据,而是需要对其他服务进行 GRPC 调用。我正在考虑制作一个自定义类,该类扩展了 Eloquent 并重载了调用数据库的受保护函数,但这听起来像是一个糟糕时期的秘诀。如果有人有做我所描述的事情的经验,你去了哪个方向?什么起作用了?什么没有?


holdtom
浏览 128回答 1
1回答

慕村9548890

所以,我最终根本没有使用 eloquent。我继续使用文档中解释的协议设置。但是我使用路由绑定在控制器中启用类型提示:<?phpnamespace 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{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* This namespace is applied to your controller routes.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* In addition, it is set as the URL generator's root namespace.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @var string&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected $namespace = 'App\Http\Controllers';&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Define your route model bindings, pattern filters, etc.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return void&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function boot()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; parent::boot();&nbsp; &nbsp; &nbsp; &nbsp; Route::bind('customer', function ($customerId) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $grpcClient = app(GrpcClient::class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $customerIdInput = new CustomerIdInput();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $customerIdInput->setCustomerId($customerId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list($customer, $status) = $grpcClient->GetCustomerDetails($customerIdInput)->wait();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($status->code != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error_log('ERROR - ' . print_r($status, 1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return redirect()->back()->withErrors(['There was an error retrieving that customer', $status->details]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $customer;&nbsp; &nbsp; &nbsp; &nbsp; });来自GrpcClientAppServiceProvider。这样,如果我们想进行查询,就不必手动实例化它。<?phpnamespace App\Providers;use Illuminate\Support\ServiceProvider;use OurNamespace\GrpcClient;use Grpc\ChannelCredentials;class AppServiceProvider extends ServiceProvider{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Register any application services.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return void&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function register()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->app->singleton('OurNamespace\GrpcClient', function ($app) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new GrpcClient(env('GRPC_HOST'), [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'credentials' => ChannelCredentials::createInsecure(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP