Scalable Network Service( 高效的网络服务)

Network Service

Web Services ,Distributed Objects etc 

   most have same basic structure;

  •    Read Request
  •    Decode Rquest
  •    Process Service
  •    Encode Reply
  •    Send Reply

当今网络上的各种基于TCP/IP的应用服务,请求,响应的处理过程的本质流程结构均为

  •   从底层IO读取字节请求
  •    把读取后的字节请求进行解码成为自己的业务请求对象
  •   把解码后的业务请求对象进行业务处理
  •   把处理后的响应编码为底层IO可写入的字节响应
  •   利用底层IO返回(发出)编码后的字节响应

But Differ in cost nature performance of each step: Xml Parsing,Json Parsing,File transfer,Web page generation,Computational Service 

每步骤所使用到的一些技术手段不一样:例如解码协议是自定义的还是使用业界流行的?是文本协议还是二进制协议?处理过程就结合具体业务进行处理等

Classic Service Designs 经典的服务模式



class Server implements Runnable {
  public void run() {
    try {
      ServerSocket ss = new ServerSocket(PORT);
      while (!Thread.interrupted())
        new Thread(new Handler(ss.accept())).start();
      // or, single-threaded, or a thread pool
    } catch (IOException ex) { /* ... */ }
  }
  static class Handler implements Runnable {
    final Socket socket;
    Handler(Socket s) { socket = s; }
    public void run() {
      try {
        byte[] input = new byte[MAX_INPUT];
        socket.getInputStream().read(input);
        byte[] output = process(input);
        socket.getOutputStream().write(output);
      } catch (IOException ex) { /* ... */ }
    }
    private byte[] process(byte[] cmd) { /* ... */ }
  }
}

Each Handler May Be Start in its own Thread

Advantage:

   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值