简体   繁体   English

REST API托管在多个服务器上

[英]REST API hosted on multiple servers

I have a problem which I need help with (I have never came across this before and I couldn't find any previous related question). 我有一个需要帮助的问题(我以前从未遇到过这个问题,以前也找不到任何相关问题)。

The application in php is hosted on different servers named like 01.example.com, 02.example.com php中的应用程序托管在名为01.example.com, 02.example.com类的不同服务器上

Now, there are many clients that use this application(Software as a Service written in PHP) and each client has its own database on hosted on one particular server - Client A database exists on 01.example.com and Client B database exists on 02.example.com (Which client belongs to which server is stored in a shared database) 现在,有许多使用此应用程序的客户端(用PHP编写的软件即服务),每个客户端都在一个特定的服务器上托管自己的数据库-客户端A数据库位于01.example.com ,客户端B数据库位于02.example.com (哪个客户端属于哪个服务器存储在共享数据库中)

I want to create a REST API with urls like http://HOST/api/resources ( http://01.example.com/api/resources , http://02.example.com/api/resources ) 我想创建一个类似URL的REST API http://HOST/api/resourceshttp://01.example.com/api/resourceshttp://02.example.com/api/resources

How can I make the api URL/URI independent of the host like ( http://example.com/api/resources )? 如何使api URL / URI独立于( http://example.com/api/resources )这样的主机?

I can think of couple of solutions - 我可以想到几种解决方案-

  1. On receiving first request, fetch the server location for the client from shared database and then send a redirect. 收到第一个请求后,从共享数据库中获取客户端的服务器位置,然后发送重定向。
  2. Or ask the clients to user server specific URL/URIs when using REST like http://01.example.com/api/resources . 或在使用REST时(例如http://01.example.com/api/resources要求客户端提供用户服务器特定的URL / URI。 (But then when the client is migrated to different server, their code will break until they change the URLs to the new server) (但是,当客户端迁移到其他服务器时,他们的代码将中断,直到他们将URL更改为新服务器为止)

This might sound confusing (as it did to me as well) and I am finding it hard to design a solution for this situation. 这听起来可能令人困惑(对我也是如此),但我发现很难为这种情况设计解决方案。

You could add the application id (or the client id or whatever is your identifier to choose the DB) to the URL, eg http://example.com/1/api/resources and http://example.com/2/api/resources and redirect all HTTP request through NGINX, haproxy, F5 or any other proxy or router. 您可以在URL中添加应用程序ID(或客户端ID或您要选择数据库的标识符),例如http://example.com/1/api/resourceshttp://example.com/2/ api / resources并通过NGINX,haproxy,F5或任何其他代理或路由器重定向所有HTTP请求。 The problem is that you'll have to reconfigure the proxy each time to migrate an application. 问题在于,每次迁移应用程序时,您都必须重新配置代理。

Generally, if you could merge all these databases into a single database you will save a lot of trouble. 通常,如果您可以将所有这些数据库合并到一个数据库中,则可以节省很多麻烦。 If you separated the databases for performance reasons, consider to use readonly slave for all readonly requests. 如果出于性能原因分离数据库,请考虑对所有只读请求使用只读从属。

More REST API server guidelines: http://example.com/1/api/resources 更多REST API服务器指南: http : //example.com/1/api/resources

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM