Motan Learning notes a micro-blogging lightweight RPC Framework Motan
Motan Learning notes two Motan architecture analysis
Motan Learning notes three Motan Demo analysis
The Yar analysis of Motan learning notes four Motan Demo
Motan Learning notes Five opentracing Learning basics
Motan Learning notes six opentracing Brave+zipkin implementation
Found Yar this thing, Motan new Yarrpcprotocol This protocol, mainly support PHP serialization.
To learn the specific implementation of the YAR protocol
As you can see from the code, support for exporter does not support refer because of its HTTP protocol, so PHP direct rest method calls on the line
@SpiMeta (name = "Yar") public class Yarrpcprotocol extends Abstractprotocol {private concurrenthashmap<string, Pro
vidermessagerouter> ipport2requestrouter = new concurrenthashmap<string, providermessagerouter> ();
@Override protected <T> exporter<t> createexporter (provider<t> Provider, url url) {
Return to new yarexporter<t> (URL, provider, this);
@Override protected <T> referer<t> createreferer (class<t> clz, url url, url serviceurl) {
TODO throw new Motanframeworkexception ("not yet implemented!"); Public providermessagerouter initrequestrouter (url url, provider<?> Provider) {String Ipport = URL.G
Etserverportstr ();
Providermessagerouter requestrouter = Ipport2requestrouter.get (Ipport);
if (Requestrouter = = null) {ipport2requestrouter.putifabsent (Ipport, New Yarmessagerouter ()); Requestrouter = Ipport2requestrOuter.get (Ipport);
} requestrouter.addprovider (provider);
return requestrouter; public void unexport (URL url, provider<?> Provider) {String Protocolkey = Motanframeworkutil.getp
Rotocolkey (URL);
String Ipport = Url.getserverportstr ();
exporter<?> exporter = (exporter<?>) exportermap.remove (Protocolkey);
if (exporter!= null) {Exporter.destroy (); } synchronized (Ipport2requestrouter) {Providermessagerouter Requestrouter = ipport2requestrouter.ge
T (Ipport);
if (requestrouter!= null) {Requestrouter.removeprovider (provider);
} loggerutil.info ("Yarrpcexporter unexport success:url={}", URL); }
}
Then look at the endpointfactory, the specific implementation of the class is Netty4yar
@SpiMeta (name = "Netty4yar") public
class Netty4yarendpointfactory extends Abstractendpointfactory {
@ Override
protected Server innercreateserver (url url, messagehandler messagehandler) {return
new Netty4httpserver (URL, new Yarmessagehandlerwarpper (MessageHandler));
}
@Override
protected Client innercreateclient (url url) {
//TODO
throw new Motanframeworkexception ("not yet Implemented! ");
}
}
Look at the Initchannel of Netty's server
B.group (Bossgroup, Workergroup). Channel (Nioserversocketchannel.class). Childhandler (New channelinitializer< Socketchannel> () {
@Override public
void Initchannel (Socketchannel ch) throws Exception {
Ch.pipeline ( ). AddLast ("Http-decoder", New Httprequestdecoder ());
Ch.pipeline (). AddLast ("Http-aggregator", New Httpobjectaggregator (Maxcontentlength));
Ch.pipeline (). AddLast ("Http-encoder", New Httpresponseencoder ());
Ch.pipeline (). AddLast ("http-chunked", New Chunkedwritehandler ());
Ch.pipeline (). AddLast ("Serverhandler", handler);
}
). Option (Channeloption.so_backlog, 1024). Childoption (channeloption.so_keepalive, false);
Then look at handler.
public class Yarmessagehandlerwarpper implements MessageHandler {private Yarmessagerouter orghandler; Public Yarmessagehandlerwarpper (MessageHandler orghandler) {if (Orghandler = = null) {throw new Motan
Frameworkexception ("MessageHandler is null!");
} if (Orghandler instanceof yarmessagerouter) {This.orghandler = (yarmessagerouter) Orghandler; else {throw new Motanframeworkexception ("Yarmessagehandlerwarper can not wrapper" + Orghandler.getclas
S (). Getsimplename ()); @Override public Object Handle (Channel Channel, Object message) {Fullhttprequest HttpRequest
= (fullhttprequest) message;
String uri = Httprequest.geturi (); int index = Uri.indexof ("?");
/should not being null String Requestpath = URI;
map<string, string> attachments = null;
if (Index >-1) {Requestpath = uri.substring (0, index); if (iNdex!= uri.length ()-1) {attachments = Getattachments (uri.substring (index + 1, uri.length ()));
} yarresponse yarresponse = null;
String packagername = "JSON";
try {bytebuf buf = httprequest.content ();
Final byte[] Contentbytes = new byte[buf.readablebytes ()];
Buf.getbytes (0, contentbytes);
Yarrequest yarrequest = new Attachmentrequest (Yarprotocol.buildrequest (contentbytes), attachments);
Yarrequest.setrequestpath (Requestpath);
Yarresponse = (yarresponse) orghandler.handle (channel, yarrequest);
catch (Exception e) {loggerutil.error ("Yarmessagehandlerwarpper handle yar request fail.", e);
Yarresponse = Yarprotocolutil.builddefaulterrorresponse (E.getmessage (), packagername);
} byte[] ResponseBytes;
try {responsebytes = yarprotocol.toprotocolbytes (yarresponse); catch (IoexCeption e) {throw new Motanframeworkexception ("Convert Yar response to bytes fail.", e); } fullhttpresponse HttpResponse = new Defaultfullhttpresponse (httpversion.http_1_1, Httpresponsest ATUs.
OK, Unpooled.wrappedbuffer (responsebytes));
Httpresponse.headers (). Set (HttpHeaders.Names.CONTENT_TYPE, "application/x-www-form-urlencoded");
Httpresponse.headers (). Set (HttpHeaders.Names.CONTENT_LENGTH, Httpresponse.content (). Readablebytes ()); if (httpheaders.iskeepalive (HttpRequest)) {httpresponse.headers (). Set (HttpHeaders.Names.CONNECTION, values.ke
ep_alive);
else {httpresponse.headers (). Set (HttpHeaders.Names.CONNECTION, values.close);
return httpresponse; Private map<string, string> getattachments (String params) {map<string, string> Map = new Hashm
Ap<string, string> ();
string[] ParamArray = Params.split ("&"); for (STring Param:paramarray) {string[] kv = param.split ("=");
if (kv.length = = 2) {map.put (kv[0], kv[1]); else {loggerutil.warn ("Yar attachment parse fail.
URI param: "+ param);
} return map; }
}