Home > Backend Development > PHP Tutorial > PHPRPC 简略示例

PHPRPC 简略示例

WBOY
Release: 2016-06-13 13:09:08
Original
892 people have browsed it

PHPRPC 简单示例
   PHPRPC是一个轻型的、安全的、跨网际的、跨语言的、跨平台的、跨环境的、跨域的、支持复杂对象传输的、支持引用参数传递的、支持内容输出重定向的、支持分级错误处理的、支持会话的、面向服务的高性能远程过程调用协议。
官方主页:http://www.phprpc.org/zh_CN/
   官方网站有比较详尽的各种版本的使用介绍,上手还是比较快的。以下给出我的一个demo示例,从看文档到开发第一demo用了30分钟左右。
我使用的是java版本,前面的安装步骤不在重述了,看官方文档。
1.编写Service接口及实现类

public interface ServiceI {
	public String say(String name);
	public Errors showError(String info);
}
Copy after login

public class FirstService implements ServiceI{
	@Override
	public String say(String name) {
		String res = "Hello, "+name+" from PHPRPC";
		return res;
	}

	public Errors showError(String info){
		Errors er = null;
		er = new Errors();
		er.setId("1111");
		er.setMsg("phprpc make error:"+info);
		return er;
	}
}
Copy after login

public class Errors implements Serializable{
	private String id;
	private String msg;	
	public Errors(){}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}	
}
Copy after login

2.通过Servlet发布服务
public class PhprpcGloabService extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		[b]PHPRPC_Server server = new PHPRPC_Server();
		FirstService first = new FirstService();
		server.add(first);
		server.start(req, res);[/b]	}	
}
Copy after login

配置web.xml文件,将要发布的服务都置于rpcservice空间下
<!-- PHPRPC Web service发布 -->
	<servlet>
		<servlet-name>GloabService</servlet-name>
		<servlet-class>
			com.smartcoms.service.PhprpcGloabService
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>GloabService</servlet-name>
		<url-pattern>/rpcservice/*</url-pattern>
	</servlet-mapping>
Copy after login

服务发布完毕!
3、编写客户端程序
我以jsp页面做客户端访问Service







<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Phprpc 调试</title>





Copy after login

PHPRPC使用起来还是很简单的,可以实现快速Web Service应用。其它方面,还有待在更多的应用环境下实施和评估。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template