简体   繁体   中英

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

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)

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 )

How can I make the api URL/URI independent of the host like ( 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 . (But then when the client is migrated to different server, their code will break until they change the URLs to the new server)

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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