java 如何构建分布式Java应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11335430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:41:46  来源:igfitidea点击:

How to build a distributed java application?

javamultithreadingweb-servicesclient-serverdistributed-computing

提问by Sami

First of all, I have a conceptual question, Does the word "distributed" only mean that the application is run on multiple machines? or there are other ways where an application can be considered distributed (for example if there are many independent modules interacting togehter but on the same machine, is this distributed?).

首先,我有一个概念性的问题,“分布式”这个词是否仅表示应用程序在多台机器上运行?或者还有其他方式可以将应用程序视为分布式(例如,如果有许多独立的模块在同一台机器上交互在一起,这是分布式的吗?)。

Second, I want to build a system which executes four types of tasks, there will be multiple customers and each one will have many tasks of each type to be run periodically. For example: customer1 will have task_type1 today , task_type2 after two days and so on, there might be customer2 who has task_type1 to be executed at the same time like customer1's task_type1. i.e. there is a need for concurrency. Configuration for executing the tasks will be stored in DB and the outcomes of these tasks are going to be stored in DB as well. the customers will use the system from a web browser (html pages) to interact with system (basically, configure tasks and see the outcomes). I thought about using a rest webservice (using JAX-RS) where the html pages would communicate with and on the backend use threads for concurrent execution. Questions:

其次,我想构建一个执行四种类型任务的系统,会有多个客户,每个客户都会定期运行每种类型的许多任务。例如:customer1今天会有task_type1,两天后会有task_type2等等,可能会有customer2有task_type1要同时执行,就像customer1的task_type1一样。即需要并发。执行任务的配置将存储在 DB 中,这些任务的结果也将存储在 DB 中。客户将通过网络浏览器(html 页面)使用系统与系统交互(基本上,配置任务并查看结果)。我想过使用休息 web 服务(使用 JAX-RS),其中 html 页面将与后端通信并在后端使用线程进行并发执行。问题:

  1. This sounds simple, But am I going in the right direction? or i should be using other technologies or concepts like Java Beans for example?
  1. 这听起来很简单,但我是否朝着正确的方向前进?或者我应该使用其他技术或概念,例如 Java Beans?

2.If my approach is fine, do i need to use a scripting language like JSP or i can submit html forms directly to the rest urls and get the result (using JSON for example)?

2.如果我的方法没问题,我是否需要使用像 JSP 这样的脚本语言,或者我可以直接将 html 表单提交到其余 url 并获得结果(例如使用 JSON)?

  1. If I want to make the application distributed, is it possible with my idea? If not what would i need to use?
  1. 如果我想分发应用程序,我的想法可以吗?如果不是,我需要使用什么?

Sorry for having many questions , but I am really confused about this.

很抱歉有很多问题,但我真的对此感到困惑。

回答by Alex D

I just want to add one point to the already posted answers. Please take my remarks with a grain of salt, since all the web applications I have ever built have run on one server only (aside from applications deployed to Heroku, which may "distribute" your application for you).

我只想在已经发布的答案中添加一点。请持保留态度,因为我曾经构建的所有 Web 应用程序都只在一台服务器上运行(除了部署到 Heroku 的应用程序,它可能会为您“分发”您的应用程序)。

If you feel that you may need to distribute your application for scalability, the first thing you should think about is notweb services and multithreading and message queues and Enterprise JavaBeans and...

如果您觉得可能需要分发应用程序以实现可扩展性,那么您首先应该考虑的不是Web 服务、多线程和消息队列以及 Enterprise JavaBeans 和...

The first thing to think about is your application domain itself and what the application will be doing. Where will the CPU-intensive parts be? What dependencies are there between those parts? Do the parts of the system naturally break down into parallel processes? If not, can you redesign the system to make it so? IMPORTANT: what data needs to be shared between threads/processes (whether they are running on the same or different machines)?

首先要考虑的是您的应用程序域本身以及应用程序将做什么。CPU 密集型部件将在哪里?这些部分之间有什么依赖关系?系统的各个部分是否自然分解为并行进程?如果没有,您能否重新设计系统以使其如此?重要:哪些数据需要在线程/进程之间共享(无论它们是在相同还是不同的机器上运行)?

The ideal situation is where each parallel thread/process/server can get its own chunk of data and work on it without any need for sharing. Even better is if certain parts of the system can be made stateless -- stateless code is infinitely parallelizable (easily and naturally). The more frequent and fine-grained data sharing between parallel processes is, the less scalable the application will be. In extreme cases, you may not even get any performance increase from distributing the application. (You can see this with multithreaded code -- if your threads constantly contend for the same lock(s), your program may even be slower with multiple threads+CPUs than with one thread+CPU.)

理想的情况是每个并行线程/进程/服务器都可以获取自己的数据块并对其进行处理,而无需共享。更好的是,如果系统的某些部分可以成为无状态的——无状态代码是无限可并行化的(简单而自然)。并行进程之间的数据共享越频繁和细粒度,应用程序的可扩展性就越低。在极端情况下,您甚至可能无法通过分发应用程序获得任何性能提升。(您可以在多线程代码中看到这一点——如果您的线程不断争用同一个锁,那么您的程序在使用多个线程 + CPU 时甚至可能比使用一个线程 + CPU 时更慢。)

The conceptualbreakdown of the work to be done is more important than what tools or techniques you actually use to distribute the application. If your conceptual breakdown is good, it will be much easier to distribute the application later if you start with just one server.

要完成的工作的概念分解比您实际用于分发应用程序的工具或技术更重要。如果您的概念分解很好,那么如果您从一台服务器开始,以后分发应用程序会容易得多。

回答by CBass

The term "distributed application" means that parts of the application system will execute on different computational nodes (which may be different CPU/cores on different machines or among multiple CPU/cores on the same machine).

术语“分布式应用程序”是指应用程序系统的各个部分将在不同的计算节点上执行(可能是不同机器上的不同 CPU/内核或同一台机器上的多个 CPU/内核)。

There are many different technological solutions to the question of how the system could be constructed. Since you were asking about Java technologies, you could, for example, build the web application using Google's Web Toolkit, which will give you a rich browser based client user experience. For the server deployed parts of your system, you could start out using simple servlets running in a servlet container such as Tomcat. Your servlets will be called from the browser using HTTP based remote procedure calls.

对于如何构建系统的问题,有许多不同的技术解决方案。由于您询问的是 Java 技术,例如,您可以使用 Google 的 Web Toolkit 构建 Web 应用程序,这将为您提供基于浏览器的丰富客户端用户体验。对于系统的服务器部署部分,您可以开始使用在 servlet 容器(如 Tomcat)中运行的简单 servlet。您的 servlet 将使用基于 HTTP 的远程过程调用从浏览器调用。

Later if you run into scalability problems you can start to migrate parts of the business logic to EJB3 components that themselves can ultimately deployed on many computational nodes within the context of an application server, like Glassfish, for example. I don think you don't need to tackle this problem until you run it to it. It is hard to say whether you will without know more about the nature of the tasks the customer will be performing.

稍后,如果遇到可伸缩性问题,您可以开始将部分业务逻辑迁移到 EJB3 组件,这些组件本身最终可以部署在应用程序服务器上下文中的许多计算节点上,例如 Glassfish。我认为在你解决这个问题之前你不需要解决这个问题。很难说您是否会在不了解更多客户将执行的任务的性质的情况下。

回答by Crollster

To answer your first question - you couldget the form to submit directly to the rest urls. Obviously it depends exactly on your requirements.

要回答您的第一个问题 - 您可以将表单直接提交到其余网址。显然,这完全取决于您的要求。

As @AlexD mentioned in the comments above, you don't always need to distribute an application, however if you wish to do so, you should probably consider looking at JMS, which is a messaging API, which can allow you to run almost any number of worker application machines, readying messages from the message queue and processing them.

正如@AlexD 在上面的评论中提到的,您并不总是需要分发应用程序,但是如果您希望这样做,您可能应该考虑查看JMS,它是一个消息传递 API,它可以让您运行几乎任何工作应用程序机器的数量,从消息队列准备消息并处理它们。

If you wanted to produce a dynamically distributed application, to run on say, multiple low-resourced VMs (such as Amazon EC2 Micro instances) or physical hardware, that can be added and removed at will to cope with demand, then you might wish to consider integrating it with Project Shoal, which is a Java framework that allows for clustering of application nodes, and having them appear/disappear at any time. Project Shoal uses JXTA and JGroups as the underlying communication protocol.

如果您想生成动态分布式应用程序,例如在多个资源不足的 VM(例如 Amazon EC2 Micro 实例)或物理硬件上运行,可以随意添加和删除以满足需求,那么您可能希望考虑将它与Project Shoal集成,这是一个 Java 框架,允许应用程序节点集群,并让它们随时出现/消失。Project Shoal 使用 JXTA 和 JGroups 作为底层通信协议。

Another route could be to distribute your application using EJBs running on an application server.

另一种途径可能是使用在应用程序服务器上运行的EJB来分发您的应用程序。