Posted on 2010-01-28 10:50
疯狂 阅读(2388)
评论(1) 编辑 收藏 所属分类:
spring
PHPRPC 是一个轻型的、安全的、跨网际的、跨语言的、跨平台的、跨环境的、跨域的、支持复杂对象传输的、支持引用参数传递的、支持内容输出重定向的、支持分级错误处理的、支持会话的、面向服务的高性能远程过程调用协议。
通过两个例子来简单入门下:
下载:
PHPRPC 3.0.2 for Java , 当然要使用3.0.2 在这之前不支持集合类型,然后双击make.bat生成服务与客户端(当然环境变量要设置的,老在myeclipse里开发,从来不设置,这下又敲半天。),生成phprpc.jar和phprpc_client.jar还有
phprpc_spring.jar 3个jar,在没有集成spring的时候将phprpc.jar和phprpc_client.jar分别放到服务端可客户端的lib下
1,servlet(或者jsp)模式:
首先建立接口:
public interface Hello {
public String sayHi(String name);
public TestPo getPo(TestPo po);
public List<TestPo> getPos(List<TestPo> pos);
实现:
public class HelloImpl implements Hello {
public String sayHi(String name) {
// TODO Auto-generated method stub
return "Hi,"+name;
}
public TestPo getPo(TestPo po) {
System.out.println("client :"+po.toString());
return new TestPo("server name", "server address");
}
public List<TestPo> getPos(List<TestPo> pos) {
List<TestPo> ps = new ArrayList<TestPo>();
for (int i = 0; i < pos.size(); i++) {
ps.add(new TestPo("server name_"+i,"server address_"+i));
System.out.println(pos.get(i).toString());
}
return ps;
}
Testpo.java 需要序列化
public class TestPo implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private String address;
public TestPo(String name, String address) {
super();
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "TestPo [address=" + address + ", name=" + name + "]";
}
}
服务类:
public class PHPrcpServer extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
Hello hello = new HelloImpl();
PHPRPC_Server phprpc_server = new PHPRPC_Server();
phprpc_server.add(hello);
phprpc_server.start(request, response);
}
}
配置:
<servlet>
<servlet-name>PHPrcpServer</servlet-name>
<servlet-class>com.server.phprcp.PHPrcpServer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PHPrcpServer</servlet-name>
<url-pattern>/servlet/PHPrcpServer</url-pattern>
</servlet-mapping>
客户端:
PHPRPC_Client client = new PHPRPC_Client("http://localhost:8080/serverspring/servlet/PHPrcpServer");
Hello m = (Hello) client.useService(Hello.class);
System.out.println(m.sayHi("chenlb"));
TestPo po = m.getPo(new TestPo("clent name","client address"));
System.out.println(po.toString());
List<TestPo> pcs = new ArrayList<TestPo>();
for (int i = 0; i < 20; i++) {
TestPo poc = new TestPo("client_name_"+i,"client_address_"+i);
pcs.add(poc);
}
List<TestPo> pl = m.getPos(pcs);
for (int i = 0; i < pl.size(); i++) {
System.out.println(pl.get(i).toString());
}
2 于spring的集成:
当然需要spring.jar spring-webmvc.jar 还有
phprpc_spring.jar
1建立web项目导入相关的包。
2,配置web.xml 当然如果你以前在spring里配置过rmi,
Hessian,httpinvoker,ws,你也许知道这一步如何配置:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext-*.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>remote</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remote</servlet-name>
<url-pattern>/remoteserver/*</url-pattern>
</servlet-mapping>
上文的接口和实现类不变,当然对list map都是支持的(测试通过) 这里只做了string
在server端发布服务:
当然是在web-inf 下面建立remote-servlet.xml
内容:
<bean name="phps" class="接口实现"/>
<bean name="/phpremote" class="org.phprpc.spring.remoting.PHPRPC_Exporter">//当然可以看出 phprcp实现了spring 对remote的通用接口
<property name="service" ref="phps"/>
<property name="serviceInterface" value="接口"/>
</bean>
配置tomcat 启动后服务就启动了,
3客户端:客户端使用java项目 当然也可以是web项目
添加spring.jar spring-webmvc.jar
配置客户端:
<bean name="phprcp" class="org.phprpc.spring.remoting.PHPRPC_ProxyFactoryBean">
<property name="serviceUrl" value="http://localhost:8080/sall/remoteserver/phpremote"/>
<property name="serviceInterface" value="接口"/>
</bean>
将服务端的接口copy过来,保持包结构不变。
测试:
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-bean.xml");
Hello phpRcpI = (Hello) context.getBean("phprcp");
System.out.println(phpRcpI.sayHi("this is from phpclient..."));
如果对复杂类型 当然 implements Serializable是必须的,如同Hessian,
phphrcp效率还是不错的 ,内部序列化和反序列化机制有了优化,还是值得学习和应用的。