Suppose I have a REST API system that has these requirements:
The API that can be accessed via this URL http://<ipaddress>/api/
I have three computers with three IP addresses: IP A, B and C respectively.
Each of this computer can be accessed via URL API specified earlier.
to access A the URL is http://<IP A>/api.
to access B the URL is http://<IP B>/api.
to access C the URL is http://<IP C>/api.
Each computer may have different information / resources.
A has resource X
B has resource Y
C has resource Z
Client 1 has access in resource X
Client 2 has access in resource Y
Client 3 has access in resource Z
Question:
Can I built those three API access in a single URL using a domain name. For instance, http://example/api for those three computers (A, B and C in single URL http://example/api)?
What I know is the DNS may return multiple IPs and the client can pick 1 IP in round robin fashion. However, in this case, A, B and C have different resources. So the client needs to be mapped to a specific machine. For instance, request from client 1 need to be mapped to computer A where the resource X resides.
Follow-up Question:
If DNS is not suitable, is there another distributed protocol to implement this kind of system?
Thank you.
Best Answer
You have multiple choices, the simplest one:
Use a DNS record with round robin for example api.yourcompany.com so all clients can access the api through http://api.yourcompany.com/api and due to round robin the access will be more or less balanced. Then for each service X, Y, Z you can create a DNS record like servicex.yourcompany.com, servicey.yourcompany.com ... using a CNAME pointing to the right server.
As Ian Bamforth stated you can also have a reverse proxy in front of them (nginx, apache, haproxy ...) and redirect to the required services depending on the URL used.
EDIT:
Example configurations for apache
This way requests coming to your server with URL /api will be routed through the load balancer, URLs /servicex will be routed to serverx and so on.