#架构#RPC中TCP与HTTP的实现

首先RPC分tcp与http两种

1/ RPC的定义(远程调用> 为什么要远程调用(分布式))

2/tcp与http各自优缺点

3/tcp如何实现(socket实现, client调用server的方法, 拿到返回结果并判断, )

其中的过程为: 问好接口, 接口反射, 接口方法反射, 接口方法socket调用, 拿到结果判断下一步

4/http restful springmvc实现

5/序列化的几种方法(google protocol buf, hession, java writeobject, fastjson, xml)

6/本人的示例代码开源, https://github.com/stevenlii/javautils

7 过程实现如下:

package com.paymoon.demo.web.tcprpc;

public interface SayHeloService {

String sayHello(String helloArg);

}

package com.paymoon.demo.web.tcprpc;

public class SayHeloServiceImpl implements SayHeloService{

@Override

public String sayHello(String helloArg) {

if (helloArg.equals(“hello”)) {

return “hello”;

}else {

return“bye”;

}

}

}

package com.paymoon.demo.web.tcprpc;import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;

import org.apache.log4j.Logger;

public class Provider {
private static transient Logger logger = Logger.getLogger(Provider.class);

public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(1234);
while(true){
Socket socket = server.accept();
ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
String interfacename = input.readUTF();
String methodname = input.readUTF();
Class<?>[] parameterTypes = (Class<?>[])input.readObject();
Object[] argumentsObjects = (Object[])input.readObject();

//execute
Class serviceinterfaceclassClass = Class.forName(interfacename);
// Object service = services.get(interfacename);
//services.get(interfacename); should be overwrite
//查找项目中实现接口的所有类 – i2534 – ITeye技术网站
//http://i2534.iteye.com/blog/811684
//Commons ClassScan – User guide
//http://commons.apache.org/sandbox/commons-classscan/userguide.html
Object service = interfacename;
Method method = serviceinterfaceclassClass.getMethod(methodname, parameterTypes);
Object result = method.invoke(service, argumentsObjects);
ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
output.writeObject(result);

}
} catch (IOException e) {
logger.info(“server error”);
e.printStackTrace();
} catch (ClassNotFoundException e) {
logger.info(“input Class”);
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}


package com.paymoon.demo.web.tcprpc;import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.net.Socket;
import java.net.UnknownHostException;

import org.apache.log4j.Logger;
public class Consumer {
private static transient Logger logger = Logger
.getLogger(Consumer.class);
public static void main(String[] args) {
String interfacename = SayHeloService.class.getName();

try {
Method method = SayHeloService.class.getMethod(“sayHello”, java.lang.String.class);
Object[] arguments = {“hello”};
Socket socket = new Socket(“127.0.0.1”,1234);
//trans method and argument to remote
ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
output.writeUTF(interfacename);
output.writeUnshared(method.getName());
output.writeObject(method.getParameterTypes());
output.writeObject(arguments);
//get data form remote
ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
Object result = input.readObject();
} catch (NoSuchMethodException | SecurityException e) {
logger.info(“method error”);
e.printStackTrace();
} catch (UnknownHostException e) {
logger.info(“output error”);
e.printStackTrace();
} catch (IOException e) {
logger.info(“output error”);
e.printStackTrace();
} catch (ClassNotFoundException e) {
logger.info(“input error”);
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


/**
* @Title: HelloWorld.java
* @Package com.paymoon.demo.web
* @Description: test序列化 fastjason
* @author StevenLii
* @date 2014年11月19日 下午11:01:24
* @version V1.0
*/
package com.paymoon.demo.web.tcprpc;import com.alibaba.fastjson.JSON;
import com.paymoon.demo.vo.Person;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
/**
* @ClassName: FastJasonDemo
* @Description: TODO
* @author StevenLii
* @date 2014年11月19日 下午11:01:24
*
*/
public class FastJasonDemo{
public String seri2json() {
Person person = new Person(“steven”, “25”, “mail”, “175”, “LYU”);
String jsonString = JSON.toJSONString(person);
return jsonString;

}
public Person unSeri2json(String jsonString) {
Person vo = JSON.parseObject(jsonString, Person.class);
return vo;
}
public String seri2xml() {
Person person = new Person(“steven”, “25”, “mail”, “175”, “LYU”);
XStream xStream = new XStream(new DomDriver());
xStream.alias(“person”, Person.class);

String xmlString = xStream.toXML(person);
return xmlString;

}
public Person unSeri2xml(String xmlString) {
XStream xStream = new XStream();
//这句话得有
xStream.alias(“person”, Person.class);

Person vo = (Person)xStream.fromXML(xmlString);
return vo;
}

public static void main(String[] args) {
FastJasonDemo fastJasonDemo = new FastJasonDemo();
String jsonString = fastJasonDemo.seri2json();
System.out.println(“seri resutl”);
System.out.println(jsonString);
Person voPerson = fastJasonDemo.unSeri2json(jsonString);
System.out.println(“unseri result”);
System.out.println(voPerson.toString());
System.err.println(“————————“);
String xmlString = fastJasonDemo.seri2xml();
System.out.println(“seri resutl”);
System.out.println(xmlString);
Person voPersonxml = fastJasonDemo.unSeri2xml(xmlString);
System.out.println(“unseri result”);
System.out.println(voPersonxml.toString());

}
}


发表评论

电子邮件地址不会被公开。