dtja73027 2013-05-28 19:53
浏览 25
已采纳

PHP中的内部API,包含的替代方案? [关闭]

I've got 1 server with more than ten different standalone applications, all written in PHP. These applications communicate with each other through an API. There is one application on top to let them communicate with clients from outside the server, globally it looks like this:

http://i.stack.imgur.com/VPsiW.png

The app who has to handle the hit responds in JSON to App 1, App 1 generates an appropriate answer to the hit, such as a webpage, image or just plain JSON, based on the information in the JSON.

If an application needs to communicate with another application, he will send a HTTP-request with cURL. Basically he becomes a client on his own server. I know this method consumes more resources than using a direct function, like Include or Require. But when I use these functions, I lose a lot of the standalone-principles.

The reason I like to stick with my standalone-principles is that I like to work with external junior programmers. I don’t want them to be obliged to use things like OOP, Classes etc. I want to say to them: “This is the input, I expect the output to be this, create a PHP-application to achieve it”.

My questions are:

  • Does the HTTP-method use many more resources compared to direct functions like Include or Require?
  • Are there better ways, besides Include or Require, to build a internal API with PHP?!
  • 写回答

1条回答 默认 最新

  • douzhimao8656 2013-05-28 20:06
    关注

    The answers to your questions:

    1. It depends on a lot of things, including, but not limited to:

      • The location of your server
      • The number of nodes, and the quality of the nodes, between your servers
      • The latency of your servers
      • The format you use to convey the information

      To give you an idea of how this all pans out, consider the simple case where all your apps are virtual hosts on the same box. You are using curl to hit myserver.com/app1/ from app2. What you may not be aware of is that this:

      • Is forcing a DNS lookup to myserver.com
      • Will travel all the way out to the public interface of your router before looping back in

      Instead of the earlier scenario, consider this one: you are using curl to hit myserver2.com from myserver.com. The two are on a VPN together, but both also have a public interface where myserver.com and myserver2.com are bound to (respectively). The result? You're going over WAN when you could have tunneled through VPN.

    In all cases, you will incur anywhere between ~10ms and ~50ms of latency per call, if the calls go to WAN.

    "Direct functions" like include solve a completely different problem, but yes, they cut down on the entire HTTP request overhead. Direct instantiation should always be preferred, and your second best option should be caching the HTTP calls using something like redis.

    Question two has a simple answer: Yes, not building one. If by an "internal API", you are building nothing more than a load-balancer, consider just using a load balancer. If you are building content mediation, consider abstracting it away and letting your devs actually do what they're good at.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000