我正在寻求构建我的第一个网络服务,并且希望从一开始就被指出正确的方向。
以下是必须发生的交互:
- 首先,智能手机或计算机将向我的网络服务发送大量数据。
- Web 服务会将信息保存到数据库中。
- 算法将定期访问和修改数据库。
- 该算法会定期捆绑数据并将其发送到智能手机或计算机(如何?)
最大的问题是:为了实现这样的事情,我需要学习哪些基本知识?
现在是那些漫无边际的小问题也在我的脑海中盘旋。如果您愿意,请随意回答这些问题。 (……也许可以认为它们是额外的功劳?)
- 我听过很多关于 RESTful 服务的好消息,我读过 wiki 文章,甚至还尝试过 Twitter 的 RESTful 网络服务。这是显而易见的方法吗?或者我还应该考虑别的事情吗?
- 我使用什么编程语言将内容保存到数据库?我认为 php 将是首选。
- 我使用什么编程语言与数据库交互?我想任何事情都可以接受,对吧?
- 我是否需要担心对数据库的并发访问,或者 MySQL 是否会为我处理这个问题? (我对数据库也相当陌生。)
- 我到底如何发回信息?显然,如果它是对 HTTP 请求的回复,那没有问题,但有时回复可能需要相当长的时间来计算。我应该让 HTTP 请求保持未决状态,直到得到答案吗?
- 有时我需要将信息发送到智能手机,无论信息是否已发送给我。我如何推送信息给我的用户?
其他可以帮助您了解我来自哪里的信息:
- 我非常熟悉 Java、C#、C++ 和 Python。我尝试过 PHP、Javascript 和 Ruby。
- 我对数据库比较陌生,但我了解基本概念。
- 我已经设置了我的服务器并且正在使用基本的 LAMP 堆栈。我对L、A、M和P的理解还相当初级。
I'm looking to build my first web service and I would like to be pointed in the right direction right from the start.
Here are the interactions that must take place:
- First, a smartphone or computer will send a chunk of data to my web service.
- The web service will persist the information to the database.
- Periodically, an algorithm will access and modify the database.
- The algorithm will periodically bundle data and send it out to smartphones or computer (how?)
The big question is: What basic things do I need to learn to in order to implement something like this?
Now here are the little rambling questions that I've also got rolling around in my head. Feel free to answer these if you wish. (...maybe consider them extra credit?)
- I've heard a lot of good things about RESTful services, I've read the wiki article, and I've even played around with the Twitter's webservice which is RESTful. Is this the obvious way to go? Or should I still consider something else?
- What programming language do I use to persist things to the database? I'm thinking php will be the first choice for this.
- What programming language do I use to interact with the database? I'm thinking anything is probably acceptable, right?
- Do I have to worry about concurrent access to the database, or does MySQL handle that for me? (I'm fairly new to databases too.)
- How on earth do I send information back? Obviously, if it's a reply to an HTTP request that's no problem, but there will be times when the reply may take quite a long time to compute. Should I just let the HTTP request remain outstanding until I have the answer?
- There will be times when I need to send information to a smartphone regardless of whether or not information has been sent to me. How can I push information to my users?
Other information that may help you know where I'm coming from:
- I am pretty familiar with Java, C#, C++, and Python. I have played around with PHP, Javascript, and Ruby.
- I am relatively new to databasing, but I get the basic idea.
- I've already set up my server and I'm using a basic LAMP stack. My understanding of L, A, M and P is fairly rudimentary.
发布评论
评论(3)
语言:Python,因为它易于使用,假设 GIL 不是特别关注您的要求(例如多线程)。它具有适用于大多数数据库的驱动程序并支持多种协议。有几个适用于它的 Web 框架 - 最流行的可能是 Django。
协议:如果您专注于 HTTP 学习 SOAP 和 休息。请注意,SOAP 往往很冗长,这会导致移动大量数据时出现问题。另一方面,如果您正在寻找其他选项,请研究套接字编程,也许还有某种二进制格式,例如 Google 的协议缓冲区。 Flash 也是一种可能性(请参阅:Flash 远程处理)。请注意,二进制选项要求用户在其计算机上安装某些内容(例如小程序或独立应用程序)。
回复:如果流程运行时间较长,并且完成后应通知客户,我建议为客户开发一个应用程序。浏览器可以使用 JavaScript 进行编程以定期轮询,或者可以嵌入 Flash 电影以进行实时更新,但这些都是浏览器编程中有些棘手的部分。如果您使用的是无线电话,请查看短信。否则,我只会为客户端提供一种获取状态的方法,但不会发送通知(例如推与拉)。正如 @jcomea_ictx 所写,如果 AJAX 是基于浏览器的解决方案,那么 AJAX 是一种选择 - 研究 jQuery。
并发:了解 ACID 对于数据库的含义。考虑一下如果您收到对同一数据的多次写入会发生什么 - 数据库可能不一定按照您想要的方式解决这个问题。
Language: Python for it's ease of use assuming the GIL is not a particular concern for your requirements (e.g. multi-threading). It has drivers for most databases and supports numerous protocols. There are several web frameworks for it - the most popular probably being Django.
Protocols: if you are HTTP focused study SOAP and REST. Note, SOAP tends to be verbose, which causes problems moving volumes of data. On the other hand, if you are looking at other options study socket programming and perhaps some sort of binary format such as Google's protocol buffers. Flash is also a possibility (see: Flash Remoting). Note, binary options require users install something onto their machine (e.g. applet or standalone app).
Replies: if the process is long running, and the client should be notified when it's done, I would recommend developing an app for the client. Browser's can be programmed with JavaScript to periodically poll, or a Flash movie can be embedded to real time updates, but these are somewhat tricky bits of browser programming. If you're dealing with wireless phones, look at SMS. Otherwise I would just provide a way for clients to get status, but not send out notification (e.g. push vs. pull). As @jcomea_ictx wrote, AJAX is an option if it's a browser based solution - study jQuery.
Concurrency: understand what ACID means with regards to databases. Think about what should happen if you receive multiple writes to the same data - database may not necessarily solve this problem the way you'd want.
如果您已经熟悉 Python,请出于对编程的热爱,不要使用 PHP。后者使得代码更加简洁、更易于维护。并不是说不可能用 PHP 编写出好的代码,但这种情况相对较少。您可以使用 Python 进行所有服务器端的操作,包括使用 MySQLdb 模块与 MySQL 进行交互。使用标准 CGI、FCGI 或 mod_python。
对于数据库来说,使用事务可以消除冲突。但你通常可以以不会发生冲突的方式设计一个系统。例如,在每次插入时使用自动递增主键 ID 将确保每个条目都是唯一的。
您可以使用 Javascript(也许使用 AJAX 方法)“拉”数据,或者使用 SMS 或其他技术“推”数据。
当回复需要一段时间来计算时,您可以使用 AJAX 进行“轮询”。这是一种非常常见的技术。服务器仅通过内置刷新返回“我们正在处理此问题”(或同等内容),直到结果准备就绪。
我不是 REST 方面的专家,但 AJAX,尤其是使用轮询而不是简单地响应用户输入时,可以说违反了 RESTful 原则。但你可以成为一个纯粹主义者,或者你可以做任何有效的事情。由你决定。
我不相信我曾经使用过除 SMS 之外的任何“推送”技术,那是几年前,当时许多公司都有免费的 SMS 网关。因此,如果您想“推送”数据,最好希望其他人加入对话!
Please, for the love of programming, don't use PHP if you're already comfortable with Python. The latter makes for far cleaner, more maintainable code. Not that it's impossible to write good code in PHP, but it's a relative rarity. You can use Python for all the server-end stuff including MySQL interaction, with the MySQLdb module. Either with standard CGI, or FCGI, or mod_python.
As for the database, use of transactions will eliminate conflicts. But you can usually design a system in such a way that conflicts will not happen. For example, use of auto-incrementing primary-key IDs on each insert will make sure that every entry is unique.
You can "pull" data with Javascript, perhaps using AJAX methodology, or "push" using SMS or other technologies.
When replies take a while to compute, you can "poll" using AJAX. This is a very common technique. The server just returns "we are working on this" (or equivalent) with a built-in refresh until the results are ready.
I'm no expert on REST, but AJAX, especially when using polling rather than simply responding to user input, can be said to violate RESTful principles. But you can be a purist, or you can do whatever works. It's up to you.
I don't believe I've ever used any "push" technologies other than SMS, and that was years ago when many companies had free SMS gateways. So if you want to "push" data, better hope someone else joins in the conversation!
使用Java。最新版本的 Java EE 6 使编码 RESTful 和 SOAP 服务成为可能轻而易举,而且它与数据库的互操作也非常容易。
使用真正的语言而不是脚本的好处是:完整的服务器端状态、强类型、多线程以及无数其他可能派上用场或可能不会派上用场的东西,但知道它们可用可以让您的项目面向未来。
Use Java. The latest version of Java EE 6 makes coding RESTful and SOAP services a breeze, and it interoperates with databases very easily as well.
The benefits of using a true language instead of a script are: full server-side state, strong typing, multithreading, and countless other things that may or may not come in handy, but knowing they are available makes your project future proof.