16 binary Format Data Transfer Service Framework--xxl-hex

Source: Internet
Author: User

GitHub:Https://github.com/xuxueli/xxl-hex

Git.osc:http://git.oschina.net/xuxueli0323/xxl-hex

Blog address (with tutorial):http://www.cnblogs.com/xuxueli/p/5003305.html

Tips: This hex-based communication program, and other online, or what you know at the moment is not the same thing, puzzled to see the code;

Summary:Xxl-hex is a lightweight remoting onhttp tool that provides RPC functionality using a simple method. The use of the 16 encoding protocol, has the following features: 1, data encryption, 2, cross-language;

cross-language, data transfer Scenarios :

JSON: A lightweight data Interchange format that is completely independent of the language (Bean object) JSON string;

XML: Extensible Markup Language, designed to transmit data (Bean object) XML string;

Xxl-hex (This scenario): A Hex data transfer scheme (Bean object) that requires a predetermined rule (hex string);

Communication Solutions :HttpClient + Servlet

Before designing the Xxl-hex, the optional communication scheme was Netty, MINA, jetty and so on (refer to my other wheel: http://www.cnblogs.com/xuxueli/p/4845111.html), but for the sake of kiss, Use the Httpclient+servlet method that is most convenient for system integration;

implementation principle, part of the logic point introduction :

1, the total call logic is: client-side package Irequest encoded as Hex post to the server side, server-side decoding after matching handler execution handle logic after the Iresponse response to the client side, after the client received, One request to finish.

2, IREQUEST message structure: Irequest message header holds the classname of the message, the server can instantiate irequest based on classname reflection, and match handler to execute handle logic;

3, irequest matching handler: Server maintenance a message hash table, according to the request message classname can be located to the processing handler, for example: Handlermap.put (" Com.xxl.demo.msg.request.DemoRequest "," Com.xxl.service.impl.DemoHandler ");

4. Server-Side Registration handler: The business handler must be a subclass of Ihandler and needs to be initialized when it is started (can be done by declaring a service for spring), Super.registry is called in the parameterless construct ( Xrequest.class); Can complete the relationship with Xrequest;

Use the tutorial :

1, the introduction of dependency (self-hit jar package, if necessary to provide jar package, can give me mail):

<dependency> <groupId>com.xxl</groupId> <artifactId>xxl-hex-core</artifactId> < Version>0.0.1-snapshot</version></dependency>

2. API development: Request, Response

Package Com.xxl.demo.msg.request;import Com.xxl.hex.core.codec.impl.irequest;public Class Demorequest extends irequest {private String Param;public string GetParam () {return param;} public void SetParam (String param) {this.param = param;}}

  

Package Com.xxl.demo.msg.response;import Com.xxl.hex.core.codec.impl.iresponse;public Class DemoResponse extends Iresponse {private String Result;public string GetResult () {return result;} public void Setresult (String result) {This.result = result;}}

3. Server-Side development:

Configuration Hexservlet:

<servlet><servlet-name>hexServlet</servlet-name><servlet-class> com.xxl.hex.http.servlet.hexservlet</servlet-class></servlet><servlet-mapping>< servlet-name>hexservlet</servlet-name><url-pattern>/hexservlet</url-pattern></ Servlet-mapping>

Development handle:

Package Com.xxl.service.impl;import Org.springframework.stereotype.service;import Com.xxl.demo.msg.request.demorequest;import Com.xxl.demo.msg.response.demoresponse;import Com.xxl.hex.core.codec.impl.irequest;import Com.xxl.hex.core.codec.impl.iresponse;import Com.xxl.hex.core.handler.IHandler, @Servicepublic class Demohandler extends Ihandler{public Demohandler () { Super.registry (Demorequest.class);} @Overridepublic Iresponse handle (irequest request) {demorequest msg = (demorequest) request;demoresponse res = new Demores Ponse (Res.setcode); Res.setmsg ("Success"); Res.setresult ("Hi, Jack. ("+ msg.getparam () +") "); return res;}}

4. Client side:

Demorequest request = new Demorequest (); Request.setparam ("Hi, Lucy."); String Request_hex = Bytehexconverter.byte2hex (Request.tohexbyte ()); System.out.println (Request_hex); String url = "Http://localhost:8080/xxl-hex-demo-server/hexServlet"; String Response_hex = Httputil.sendhex (URL, request_hex); System.out.println (Response_hex);D emoresponse msg = (demoresponse) DemoResponse.class.newInstance (). Fillhexbyte ( Bytehexconverter.hex2byte (Response_hex)); System.out.println (Beanutils.describe (msg));

  

Run:

Server side: Registering handler

Client side: Request log, log request data string;

16 binary Format Data Transfer Service Framework--xxl-hex

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.