1、第2章 分布式通信,2,主要内容,2.1 通信基础2.2 客户/服务器模型2.3 套接字socket 2.4 远程过程调用2.5 Web Service 2.6 消息队列 2.7 分布式对象2.8 组通信,3,主要内容,2.1 通信基础2.2 客户/服务器模型2.3 套接字socket 2.4 远程过程调用2.5 Web Service 2.6 消息队列 2.7 分布式对象2.8 组通信,4,2.1 通信基础,开放式系统:使用标准化规则与其它开放系统通信的系统 协议:进程间通信所必须遵循的规则,消息的格式、内容和意义协议的种类面向连接、非面向连接分层协议ISO OSI参考模型Internet,
2、5,OSI参考模型分层协议,进程A,进程B,6,The Internet 协议,Network,IP,TCP,UDP,Application,Transport,Datagrams,Messages (UDP)Streams (TCP),7,典型消息结构,8,网络技术的发展(1),分组交换协议Dr.L.Kleinrock(MIT),1962TCP/IP协议R.Kahn(MIT,ARPA),V,Cerf(UCB,Stanford), IEEE通信974CDMA协议与EthernetDr.B.Metcalfe(HU,MIT,PARC),19733COM,以太网适配器,9,网络技术的发展(2),AR
3、PANET与InternetIPTO,Dr.J.Licklider(1960),Dr.B.Taylor(1966)Dr.L.Roberts(MIT),BBN公司ARPANET(1969,UCB,SRI)Stanford校园网与LANL.Bosack,S.Lerner(Stanford),1980-1983Cisco(1984),Cisco路由器,10,11,ATM网络技术,分组交换、线路交换和信元交换信元(cell):小的、定长的单元信元交换:同时支持点到点(point-point)和多播(multicast)功能,12,ATM参考模型,物理层:SONETOC-1(51.8M),OC-3c(1
4、55M)ATM层:信元传输和路由适配层:VBR,CBR,13,信元头格式,14,ATM交换网络及交换机内部构造,15,主要内容,2.1 通信基础2.2 客户/服务器模型2.3 套接字socket 2.4 远程过程调用2.5 Web Service 2.6 消息队列 2.7 分布式对象2.8 组通信,16,2.2 客户/服务器模型,服务器:为用户提供服务的一组协同进程 客户:代理用户,17,客户/服务器(1-1),18,客户-服务器交互 (I),Remote procedure call,19,客户-服务器交互(II),多层结构,20,客户-服务器交互(III),异步 remote proced
5、ure call,21,客户/服务器(1-N),22,Example: Web proxy server,23,客户-服务器交互(IV),24,Peer-to-Peer 合作,25,Mobile Code Example: Applet,26,瘦客户,27,消息格式,struct message long source; /* 发送者标识*/ long dest; /* 接受者标识 */ long opcode; /* 操作码:读、写、创建、删除 */ long result; /* 返回结果代码 :成功、失败*/ long offset; /* 读写位置 */ long count; /*
6、读写计数 */ char filenameMAX_PATH; /* 文件名*/ char dataBUF_SIZE; /* 数据区*/,28,服务器程序,void main(void) struct message m1,m2; /* 输入、输出的消息 */ int r; /* 返回的执行结果 */ while (1) receive(FILE_SERVER, /* 返回结果 */ ,29,客户程序,int read(char *file, int position , int nbytes, char *buf) struct message m1; /* 消息缓冲区 */ m1.opcod
7、e = READ; /* 设置参数 */ m1.offset = position; /* 读位置 */ m1.count = nbytes; /* 读长度 */ strcpy( /* 返回结果 */ ,30,31,寻址方式(Addressing),1、机器号+本地标识号例:UNIX机器IP地址端口号(port)/* Socket address, internet style. */struct sockaddr_in short sin_family; /*AF_INET*/ u_short sin_port; struct in_addr sin_addr; char sin_zero8
8、;,32,2、广播定位进程全局唯一标识号,33,3、ASCII码进程名名字服务器,34,阻塞与非阻塞原语,(1)阻塞发送原语 同步原语(2)非阻塞发送原语 异步原语在消息被发送之前,发送者不能修改消息缓冲区,35,有缓冲与无缓冲型接收,无缓冲区发送比接收先发生 ?直接丢弃消息暂存“意外”消息有缓冲区邮箱(mailbox):缓存所有的输入消息“溢出”问题,36,可靠的和非可靠的发送和接收,非可靠的收发可能丢失消息由用户负责确认独立的确认消息内核-内核确认应答用作请求的确认消息客户内核确认折衷方法服务器端设置计时器,超时后,发确认消息,37,38,实现技术小结,39,客户/服务器协议中的包类型,4
9、0,包交换举例,41,主要内容,2.1 通信基础2.2 客户/服务器模型2.3 套接字socket 2.4 远程过程调用2.5 Web Service 2.6 消息队列 2.7 分布式对象2.8 组通信,42,2.3 套接字socket, socket通信的基本原理 socket通信的工作过程,43,一个socket在逻辑上有三个特征(三要素):网域类型规程,44,网域:表明一个socket用于哪一种网络或说哪一族网络规程。各种网络对节点地址的命名方法不同,又称“地址族”或“规程族”。AF_INET表示Internet网插口,故各节点使用IP地址。AF_IPX为Novell的IPX网插口,AF
10、_X25为X.25网插口,等等。其中的特殊,不走网络而在同一台计算机上用于进程通信,BSD称为AF_UNIX,在POSIX标准里定义了一种AF_LOCAL域名。,45,类型:表明网络中通信所遵循的模式“面向连接”:又叫“虚电路”模式,通信双方先要在互相之间建立起一种虚拟连接(线路),再通过虚拟线路进行通信。在通信过程中,所有报文传递保持原来次序,所以,报文之间是有关联的,每个报文都不是孤立的。在这种模式中,所有报文的传递都是可靠的,由网络中物理线路引入差错控制来保证。 “无连接”:事先不必建立虚线路,直接可发送和接收报文。但报文是孤立的,正确性没有保证,甚至可能丢失。由于网络延迟或路径不同,接
11、收端收到报文的次序可能与发送端发送报文的次序不一样。这种模式传递的报文是不可靠的,无保证的。也没有“流量控制”手段,进程从系统调用返回时仅表明该socket会把报文发送去,并不表明报文已到达了接收方的socket。,46,规程:表明具体网络规程。一般来说,网域与类型结合作一起就大体上决定了规程。如网域为AF_INET,而类型为“无连接”,则规程大致为UDP了。但有时,还会有其他选择。,47,Berkeley Sockets API,Socket primitives for TCP/IP.,48,基本的socket调用,49,50,51,Berkeley Sockets调用,Connectio
12、n-oriented communication pattern using sockets.,52,数据流 (1),Setting up a stream between two processes across a network.,53,数据流(2),Setting up a stream directly between two devices.,54,数据流(3),An example of multicasting a stream to several receivers.,55,定义 QoS (1),A flow specification.,56,定义 QoS (2),The
13、 principle of a token bucket algorithm.,57,配置流,The basic organization of RSVP for resource reservation in a distributed system.,58,同步机制 (1),The principle of explicit synchronization on the level data units.,59,同步机制 (2),The principle of synchronization as supported by high-level interfaces.,2-41,60,主
14、要内容,2.1 通信基础2.2 客户/服务器模型2.3 套接字socket 2.4 远程过程调用2.5 Web Service 2.6 消息队列 2.7 分布式对象2.8 组通信,61,2.4 远程过程调用,允许程序去调用位于其它机器上的过程 调用者和被调者都不用考虑消息通信举例:int read(int fd, char * buf, int nbytes);count = read(fd, buf, nbytes);参数传递call-by-valuecall-by-reference,62,传统的过程调用,本地过程调用中的参数传递:调用读前的堆栈调用过程激活时的堆栈,63,软件层,RPC
15、is more than a (transport) protocol: a structuring mechanism for distributed systems,64,RPC中的调用与消息,1,2,3,4,5,6,7,8,9,10,65,RPC执行的主要步骤,1.客户过程以普通方式调用相应的客户存根。2.客户存根建立消息并激活内核陷阱。3.内核将消息发送到远程内核。4.远程内核将消息送到服务器存根。5.服务器存根取出消息中的参数后调用服务器的过程。6.服务器完成工作后将结果返回至服务器存根。7.服务器存根将它打包并激活内核陷阱。8.远程内核将消息发送至客户内核。9.客户内核将消息交给客
16、户存根。10.客户存根从消息中取出结果返回给客户。,66,客户和服务Stubs,Principle of RPC between a client & server program.,67,异步RPC (I),68,异步RPC (II),69,编写Client和Server的步骤,70,RPC编程举例,/* 生成RPC程序 */rpcgen FILE_RPC.x=编译结果 /* 参数定义 */ FILE_RPC_xdr.c /* 服务器端程序 */ FILE_RPC_svc.c /* server存根 */ FILE_RPC_server.c /* 服务器模版程序 */ /* 客户端程序 */
17、 FILE_RPC_clnt.c /* client存根 */ FILE_RPC_client.c /* 客户模版程序 */,71,RPC申明- File_RPC.x,#define RPC_ARGSIZE 256#define RPC_RETSIZE 256/* the interface version number */#define uSERVERVERS1/* program number range established by ONC */#define uSERVERPROG 0x20000051struct CallArg_t char bodyRPC_ARGSIZE; /*
18、 RPCs arg data form */,struct Return_t char bodyRPC_RETSIZE; /* RPCs arg data form */* program definition*/program SERVERPROG version SERVERVERS Return_t FILE_CREATE(CallArg_t) = 1;Return_t FILE_READ(CallArg_t) = 2; Return_t FILE_WRITE(CallArg_t) = 3; Return_t FILE_DEL(CallArg_t) = 2; = uSERVERVERS;
19、 = uSERVERPROG;,72,73,RPC服务端程序 - FileRPC_svc.c,.void main2() SVCXPRT *transp; transp = svcudp_create(RPC_ANYSOCK); svc_register(transp, SERVERPROG, SERVERVERS, serverprog_1, IPPROTO_UDP); /* 注册 */ transp = svctcp_create(RPC_ANYSOCK, 0, 0); svc_register(transp, SERVERPROG, SERVERVERS, serverprog_1, I
20、PPROTO_TCP) ; /* 注册 */ svc_run(); / 启动RPC exit(1);static void serverprog_1(struct svc_req * rqstp, SVCXPRT * transp) ,union CallArg_t file_create_1_arg;CallArg_t file_read_1_arg; : argument; switch (rqstp-rq_proc) : case FILE_READ: xdr_argument = xdr_CallArg_t; xdr_result = xdr_Return_t; local = (ch
21、ar *(*)(char *,struct svc_req *) file_read_1_svc; break; : default:svcerr_noproc(transp);return; ,74,RPC客户端程序 - FileRPC_clnt.c,./* Default timeout can be changed using clnt_control() */static struct timeval TIMEOUT = 25, 0 ;Return_t * file_read_1(CallArg_t *argp, CLIENT *clnt) static Return_t res; b
22、zero(char *) .,75,基本RPC操作,(a)调用read之前的栈; b)调用过程处于激活状态时的栈; (c)返回调用者之后的栈,值参调用和变参调用 call-by-copy/restore,76,参数定义和产生Stub,77,参数传递,参数编组(marshalling):将参数装入消息例:add(i,j)远程计算,78,参数传递问题,编码表示:ASCII, EDBIC数字表示:小末端、大末端规范形式:整形、字符型、布尔型、浮点型等指针参数:copy/restoredereference,79,参数传递问题,(a)486中的原始消息;(b)在SPARC上接受到的消息;(c)经过翻译
23、之后的消息;(框中的小数字表明了每一个字节的地址),80,建立表示基本数据类型的标准,81,参数传递问题,如何传递 指针 ?只有在特定地址空间才有意义! 已知长度的数组和结构Copy/restore 语义IN/OUT/INOUT 标签到任意数据结构的指针 ?没有一般的解决办法 通常: 把指针传递给它的 “源”,82,客户如何定位服务器呢?,将服务器的网络地址固化到客户机中 动态捆绑,83,动态捆绑,客户,绑定器,服务器,84,绑定(SUN RPC),Port Mapper (rpcbind) listens at UDP port 111Server registers program ID
24、& versionrpcinfo -p - display all registered RPC serversWhen client issues clnt_create, the port mapper is contacted:program-to-port number mappingarguments: (program ID, version, protocol)response: servers port number,85,绑定(DCE),86,绑定接口,句柄(handle): 如IP地址唯一标识(ID):程序号认证:身份标识,87,RPC故障处理,客户不能定位服务器返回错误号
25、errno产生例外:SIG_NOSERVER丢失请求信息超时重发丢失应答信息等幂性请求序号,88,RPC故障处理,4. 服务器崩溃解决至少一次语义 (重发请求 )至多一次语义 (立即放弃并报告失败)不做任何保证恰好一次?,(a)正常状态;(b)执行后崩溃;(c)执行前崩溃,89,RPC故障处理,5. 客户崩溃孤儿问题:资源浪费、资源封锁解决方案:根除(日志文件 )再生(epochs,所有远程计算被终止 )温和再生(试图去找到该远程计算的调用者 )过期(标准时间段T ),90,RPC实现技术,RPC可以使用哪些协议?TCP 提供可靠传输ACKs, timeouts, retransmission
26、sUDP 不提供可靠传输,但RPC执行RETRANSMIT timeout因此在total timeout后,无论使用TCP或UDP都重复请求TCP需要建立连接 对于一些短的RPCs,有额外开销专门协议信包和报文的长度,91,RPC实现技术,确认停等(stop-and-waitt)协议(b)爆发 (blast)协议差错控制流量控制:数据溢出(overrun),92,RPC实现技术,关键路径,客户机调用存根过程准备消息缓冲区将参数编排进缓冲区填写消息头陷入内核将上下文切换到内核将消息复制到内核确定目的地址将地址写入消息头设置网络接口启动计时器,服务器执行服务调用服务器在堆栈上设置参数解析参数将上
27、下文切换到存根将消息复制到存根检查存根是否在等待确定使用哪个服务器存根检查消息包的有效性处理中断,93,RPC实现技术,拷贝DMA分散-聚集(scatter-gather)虚拟内存映射,客户存根,网络,内核,内核,服务器存根,94,RPC实现技术,计时器管理计时器列表扫描算法(sweep algorithms) (a)排序表中的时间片耗尽;(b)进程表中的时间片耗尽,95,RPC实现技术,6. 存在的问题全局变量的共享弱数据类型的传递,数组?指针传递管道:读驱动、写驱动(a)管道(b)读驱动方法(c)写驱动方法,96,97,主要内容,2.1 通信基础2.2 客户/服务器模型2.3 套接字soc
28、ket 2.4 远程过程调用2.5 Web Service 2.6 消息队列 2.7 分布式对象2.8 组通信,98,2.5 Web Services,“Web services” is an effort to build a distributed computing platform for the Web.,99,Web Services 框架,可以用术语描述为:What goes “on the wire”: Formats and protocols.What describes what goes on the wire:Description languages.What al
29、lows us to find these descriptions:Discovery of services.,100,Web-Services 体系结构,SOAP消息,服务提供方,服务请求方,服务代理方,WSDL docs,使用 e-services的应用,2,创建业务/服务,1,发现业务/服务,4,3,7,调用服务,8,UDDIRegistry,XSD docs,Servlets, CGI, Perl,6,5,检索 WSDL文档,101,XML Messaging: SOAP,SOAP 1.1 defined:An XML envelope for XML messaging,Head
30、ers + bodyAn HTTP binding for SOAP messaging.SOAP is “transport independent”.A convention for doing RPC.An XML serialization format for structured dataSOAP Attachments addsHow to carry and reference data attachments using in a MIME envelope and a SOAP envelope.,102,The SOAP Envelope, . . .,103,What
31、goes on the wire,Internet-scale integration needs a lingua-francaXML messaging protocol over HTTP: SOAPIntra-enterprise integration needs to allow alternates:CORBA, RMIMessagingIn-memory method calls,SOAP,Security,Attachments,Reliability,Routing,Transactions,Context,W3C,104,Descriptions: Meta-data,I
32、ntegration requires interoperable machine-understandable descriptionsEnables dynamic, delayed binding of components. Language extensibility provides support for different levels of application integration.,Interface,Service QoS,Service,Public Flows,Flows and Composition,Agreements,WSDL,WSFL,XML Sche
33、ma,105,Web Services Description Language,Provides functional description of network services: IDL descriptionProtocol and deployment detailsPlatform independent description. Extensible language.A short history:WSDL v1.0, 9/2000WSDL v1.1 submitted to W3C 3/2001.A de facto industry standard.,106,WSDL
34、Structure,portTypeAbstract definition of a service (set of operations)Multiple bindings per portType:How to access itSOAP, JMS, direct callPortsWhere to access it,Service,Port(e.g. http:/host/svc),Binding(e.g. SOAP),Abstract interface,portType,operation(s),inMesage,outMessage,Port,Binding,107,Using
35、WSDL,As extended IDL: WSDL allows tools to generate compatible client and server stubs.Tool support for top-down, bottom-up and “meet in the middle” development.Allows industries to define standardized service interfaces.Allows advertisement of service descriptions, enables dynamic discovery and bin
36、ding of compatible services. Used in conjunction with UDDI registryProvides a normalized description of heterogeneous applications.,108,Client Proxy object,RMI-IIOP,JMS/MQ,SOAP/HTTP,Client invocation,Single stub can invoke services over different bindingsDepends only on abstract interface.Are indepe
37、ndent of binding (but pluggable).Add new bindings without recompiling/redeploying stubAllows optimisations based on the bindings of service.Will support extended services models if describedIn WSDL,109,WSFL Overview,WSFL describes Web Service compositions.Usage patterns of Web Services: describes wo
38、rkflow or business processes.Interaction patterns: describes overall partner interactions., WS, WS,110,WSFL Flow Models,Activities represent units of processing.,Flow of data is modeled through data links., WS,Activities can be mapped to the flow interface,Control links define execution flow as a di
39、rected acyclic graph,Activities are associated with specific typed service providers,111,Using Flow Models,“Public flows” provide a representation of the service behavior as required by its users.Typically, an abstraction of the actual flow begin executedDefines a “behavioral contract” for the servi
40、ce.Internal implementation need not be flow-based.Flows are reusable: specify components types, but not what specific services should be used!“Private flows” are the flows executed in practice. WSFL serves as a “portable flow implementation language”Same language is used in WSFL to represent both ty
41、pes of processes.,112,Global Models,Global models describe how the composed Web Services interact. RosettaNet automated.Like an ADL.Interactions are modeled as links between endpoints of two service interfaces (WSDL operations).An essentially distributed description of the interaction.,A,B,C,113,Dis
42、covery: Finding Meta-data,Static binding requires service “libraries”.Dynamic binding requires runtime discovery of meta-data,Inspection,Directory,ADS,DISCO,UDDI,114,UDDI Overview,UDDI defines the operation of a service registry:Data structures for registeringBusinessesTechnical specifications: tMod
43、el is a keyed reference to a technical specification.Service and service endpoints: referencing the supported tModelsSOAP Access APIRules for the operation of a global registry“private” UDDI nodes are likely to appear, though.,115,UDDI Relationships,Web Service,116,主要内容,2.1 通信基础2.2 客户/服务器模型2.3 套接字so
44、cket 2.4 远程过程调用2.5 Web Service 2.6 消息队列 2.7 分布式对象2.8 组通信,117,2.6 消息队列,四种松散组合的通信使用队列,118,消息队列原语,119,消息队列的一般结构 (I),队列层地址和网络层地址间的关系,120,消息队列的一般结构(II),2-29,121,消息代理,The general organization of a message broker in a message-queuing system.,122,IBM MQSeries,123,Channels,Some attributes associated with mes
45、sage channel agents.,124,消息传输 (I),使用路由表和别名表,125,消息传输 (II),IBM MQSeries MQI原语,126,主要内容,2.1 通信基础2.2 客户/服务器模型2.3 套接字socket 2.4 远程过程调用2.5 Web Service 2.6 消息队列 2.7 分布式对象2.8 组通信,127,2.7 分布式对象,128,Binding a Client to an Object,使用global references不明确绑定使用global and local references明确绑定,Distr_object* obj_ref;
46、/Declare a systemwide object referenceobj_ref = ;/ Initialize the reference to a distributed objectobj_ref- do_something();/ Implicitly bind and invoke a method(a)Distr_object objPref;/Declare a systemwide object referenceLocal_object* obj_ptr;/Declare a pointer to local objectsobj_ref = ;/Initialize the reference to a distributed objectobj_ptr = bind(obj_ref);/Explicitly bind and obtain a pointer to the local proxyobj_ptr - do_something();/Invoke a method on the local proxy(b),