In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to achieve a XML-RPC server/client". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1 starting from big data's deployment system Minos
Minos is a set of large-scale distributed deployment and monitoring system newly explored and developed by Xiaomi in 2013, aiming at the pain point of inefficient operation and unified configuration management of massive service nodes. The system implements distributed computing based on supervisor XML-RPC and supports cross-regional and multi-node deployment services.
The above figure is the Minos architecture diagram, and the operation of the client to the task node is done through the XML-RPC of supervisor. Next let's talk about what XML-RPC is and how it works.
2 what is XML-RPC?
XML-RPC is a distributed computing protocol for remote procedure calls (remote program calls) (remote procedure call,RPC), which encapsulates the calling function through XML and uses the HTTP protocol as the transmission mechanism.
[1] it can be implemented in Perl, Java, Python, C, Cellular languages, PHP and many programming languages. The implementation is suitable for Unix,Windows and Macintosh.
XML-RPC was inspired by two early protocols. One is the anonymous RPC protocol designed by Dave Winer. This is why the XML-RPC server is usually installed under / RPC2. Another, more important, is the first draft of the SOAP protocol. Dave Winer provides a rich history of XML-RPC, and those interested in the relationship between XML-RPC and SOAP can consult the data on their own.
3 how does XML-RPC work?
A simple example.
Server code example: (Python2.7 runtime environment)
#-*-coding:utf-8-*-from SimpleXMLRPCServer import SimpleXMLRPCServerfrom SimpleXMLRPCServer import SimpleXMLRPCRequestHandler# specified path class RequestHandler (SimpleXMLRPCRequestHandler): rpc_paths = ('/ RPC2',) # build serverserver = SimpleXMLRPCServer (("localhost", 8000), requestHandler=RequestHandler) server.register_introspection_functions () # register pow function # pow.__name__ is the name of pow function server.register_function (pow) # register custom function def adder_function (x Y): return x + yserver.register_function (adder_function, 'add') # registered instance # the method of the instance is published as the XML-RPC method class MyFuncs: def div (self, x, y): return x / / yserver.register_instance (MyFuncs ()) # launch serverserver.serve_forever ()
Next, the client will call the method on the server side:
#-*-coding:utf-8-*-import xmlrpclibs = xmlrpclib.ServerProxy ('http://localhost:8000')print s.pow (2Magne3) # Returns 2Mab 3 = 8print s.add (2L3) # Returns 5print s.div (5L2) # Returns 5 print s.system.listMethods 2 = print all server methods print s.system.listMethods ()
Therefore, in the deployment system, the server of supervisor XML-RPC is constructed, the method of node operation is encapsulated, and the method is called remotely by the client to realize distributed deployment.
However, SimpleXMLRPCServer is a single-threaded server. This means that the server receives multiple requests from multiple clients at the same time and must wait for the first request to complete before continuing. There is the most direct way to solve this problem:
From SimpleXMLRPCServer import SimpleXMLRPCServerfrom SocketServer import ThreadingMixIn# added ThreadXMLRPCServer class class ThreadXMLRPCServer (ThreadingMixIn, SimpleXMLRPCServer): pass# rebuilt serverserver = ThreadXMLRPCServer (("localhost", 8000), requestHandler=RequestHandler,allow_none=True) # Forking and threading versions of each type of server can be created using the ForkingMixIn and ThreadingMixIn mix-in classes. For instance, a threading UDP server class is created as follows: [2] class ThreadingUDPServer (ThreadingMixIn, UDPServer): pass
Reading the source code of ThreadingMixIn shows that ThreadingMixIn overrides the process_request method called in the server class. Therefore, multithreading on the server side is realized.
Problems encountered in the use of 4-chain XML-RPC and their solutions
In our practical application, when we build multiple client of XML-RPC server and make chain calls, we often encounter the alarm of httplib.CannotSendRequest.
Calling procedure:
Client-calls-SimpleXMLRPCServer's functions-calls-SimpleXMLRPCServer's functions-do finish task
Remember that troubleshooting CannotSendRequest occurs when a httplib.HTTPConnection is interrupted by another new "request" operation. Therefore, each httplib.HTTPConnection.request must be paired with a .getresponse () call. If the pairing is interrupted by another request operation, the second request will generate a CannotSendRequest error. Therefore, a request similar to the following will generate an error:
Connection = httplib.HTTPConnection (...) connection.request (...) connection.request (...)
Problem analysis:
Both calls are built by serverproxy. This problem occurs only when there are multiple requests at the same time, and there should be conditions for competition for resources. Serverproxy is shared in the middle tier.
The solution is as follows:
Is to let each thread create its own serverproxy.
Solution:
The link for the next call is built when the method of the first SimpleXMLRPCServer is called. Ensure that each thread maintains its own serverproxy.
"how to achieve a XML-RPC server/client" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.