Apache cxf rpc demo

来源:互联网 发布:淘宝优惠券活动报名 编辑:程序博客网 时间:2024/06/04 19:28

      RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。RPC协议假定某些传输协议的存在,如TCP或UDP,为通信程序之间携带信息数据。在OSI网络通信模型中,RPC跨越了传输层和应用层。RPC使得开发包括网络分布式多程序在内的应用程序更加容易。

下面是一个cxf rpc的demo

服务端:

1、创建HelloWorld接口类

package com.mms.webservice;import javax.jws.WebService;import javax.jws.soap.SOAPBinding;import javax.jws.soap.SOAPBinding.Style;import javax.jws.soap.SOAPBinding.Use;@WebService@SOAPBinding(style=Style.RPC, use=Use.LITERAL)public interface HelloWorld {public String sayHi(String name);}

2、创建HelloWorld实现类

package com.mms.webservice;import javax.jws.WebService;@WebService(endpointInterface="com.mms.webservice.HelloWorld",serviceName="HelloWorld")public class HelloWorldImpl implements HelloWorld {public String sayHi(String name) {return "Hello " + name;}}

3、创建service

package com.server;import javax.xml.ws.Endpoint;import com.mms.webservice.HelloWorld;import com.mms.webservice.HelloWorldImpl;public class RPCService {public static void main(String[] args) {System.out.println("web service start");HelloWorld helloWorld=new HelloWorldImpl();String address = "http://localhost:8080/service/helloWorld";Endpoint.publish(address, helloWorld);System.out.println("web service end");}

启动RPCService 之后在浏览器中输入http://localhost:8080/service/helloWorld?wsdl


这个代表rcp的服务已经启动成功

客户端:

4、创建客户端服务类

package com.client;import javax.xml.namespace.QName;import javax.xml.ws.Service;import javax.xml.ws.soap.SOAPBinding;import com.mms.webservice.HelloWorld;public class RPCClient {private static final QName SERVICE_NAME = new QName("http://webservice.mms.com/", "HelloWorld");private static final QName PORT_NAME = new QName("http://webservice.mms.com/", "HelloWorldPort");private RPCClient() {}public static void main(String args[]) throws Exception {Service service = Service.create(SERVICE_NAME);// Endpoint AddressString endpointAddress = "http://localhost:8080/service/helloWorld";// Add a port to the Serviceservice.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);HelloWorld hw = service.getPort(HelloWorld.class);System.out.println(hw.sayHi("Ares"));}}



0 0
原创粉丝点击