开发网络服务器有哪些不同的方法?

发布于 2025-01-02 15:53:47 字数 89 浏览 3 评论 0原文

开发网络服务器有哪些不同的方法? 所以我猜有(1)多线程(2)事件循环,还有其他吗?每种方法的优点/缺点是什么?你什么时候会使用每一个?你能列出每种方法的具体实现吗

What are the different approaches in developing a web-server?
So I guess there are (1) multi-thread (2) event-loop, is there anything else? What would be the pros/cons of each approach? when would you use each? can you list specific impl' for each approache

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半仙 2025-01-09 15:53:47

不同的方法可以是:

  1. 单线程:所有连接均由单个线程处理,该线程
    “监听”并等待连接并处理请求。
    实现起来很简单,但它是最无用的服务器
    一次仅服务请求

  2. 多线程:服务器监听请求和每个传入的请求
    请求被分配给一个新的线程来处理它。所以每个客户端
    连接由其专用线程处理。这种方法(不同于
    1)支持并发处理客户端请求但不支持
    可以很好地扩展,因为每个新请求都会在服务器上创建一个新线程
    这需要大量资源。最终服务器会遇到
    limit

  3. 多线程池:与(2)相同的想法,但不是创建一个新的
    处理每个传入请求的线程来自线程池的线程
    使用。即创建线程并将其放置在池中以供稍后使用
    重用。这可以很好地支持多个客户端请求,并且
    这是标准方法。例如,Tomcat 的工作方式如下。

  4. Event-Queue:每个传入的请求都被放入一个队列中并被
    由接受队列请求的后台线程处理。这是
    非阻塞并且这种类型的异步处理也可以扩展
    老实说,我不确定它是否比(3)更好
    性能。我认为可以使用 tomcat 为此配置
    NIO架构

Different approach can be:

  1. Single threaded: All connections are handled by a single thread that
    "listens" for and awaits for connections and processes requests.It
    is simple to implement but it is the most useless server as it can
    only serve request at a time

  2. Multithreaded:The server listens for requests and each incoming
    request is allocated to a new thread to handle it.So each client
    connection is handled by its dedicated thread. This approach(unlike
    1) supports concurrent processing of client requests but does not
    scale well since each new request creates a new thread at the server
    and this takes a lot of resources.Eventually the server will hit a
    limit

  3. Multithreaded-Pools:Same idea as (2) but instead of creating a new
    thread to handle each incoming request a thread from a thread-pool
    is used.I.e. threads are created and placed on a pool for later
    reuse.This scales very well supporting multiple client requests and
    it is the standard approach.E.g. Tomcat works like this.

  4. Event-Queue:Each incoming request is placed into a queue and is
    processed by a background thread taking requests of the queue. It is
    non-blocking and this type of asynchronous processing also scales
    well.To be honest I am not sure if it is better than (3) in
    performance.I think that tomcat can be configured for this using the
    NIO architecture

御弟哥哥 2025-01-09 15:53:47

您应该添加非阻塞 I/O。看看 Netty

You should add non-blocking I/O. Have a look at Netty.

油饼 2025-01-09 15:53:47

一些服务器(例如 G-WAN)混合了多线程池事件队列,让服务器的 CPU 核心饱和,每个线程处理许多连接。

免责声明:我参与了这个项目的开发。

Some servers like G-WAN mix Multithreaded-Pools and Event-Queues, letting the server saturate CPU Cores with each thread processing many connections.

Disclamer: I am involved in the development of this project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文