使用 webservice 实现 RPC 调用
WebService 介绍
Web service 是一个平台独立的,低耦合的 web 的应用程序用于开发分布式的互操作的应用程序。Web Service 技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。
SOAP(Simple Object Access Protocol) 简单对象访问协议:
SOAP=http+xml

WSDL(Web Services Description Language) Web Service 的描述语言:
一个 webservice 服务的说明书,通过该说明书可以完成 webservice 服务的调用
服务提供者实现
创建 ws-provider 项目

创建 UserService 接口
package com.bjsxt.service;
public interface UserService {
public String syaHello(String name);
}
创建 UserServiceImpl 实现类
package com.bjsxt.service.impl;
import com.bjsxt.service.UserService;
import javax.jws.WebService;
@WebService
public class UserServiceImpl implements UserService {
@Override
public String syaHello(String name) {
return "name: "+name;
}
}
发布 webservice 服务
package com.bjsxt.app;
import com.bjsxt.service.UserService;
import com.bjsxt.service.impl.UserServiceImpl;
import javax.xml.ws.Endpoint;
public class WsProviderApp {
public static void main(String[] args) {
//发布的webService服务访问地址
String address="http://localhost:9999/ws";
//创建UserService对象
UserService userService=new UserServiceImpl();
Endpoint.publish(address,userService);
System.out.println("============发布WebService服务=============");
}
}
获取 wdsl 文档

服务消费者实现
创建 ws-consumer 项目

wsimport 生成消费者代码

消费远程服务
package com.bjsxt.app;
import com.bjsxt.client.UserServiceImpl;
import com.bjsxt.client.UserServiceImplService;
public class WsConsumerApp {
public static void main(String[] args) {
/***
* 完成 webservice 服务的消费
*/
//创建服务类对象
UserServiceImplService service=new UserServiceImplService();
//获得远程服务的代理对象
UserServiceImpl userService=service.getUserServiceImplPort();
System.out.println(userService.getClass().getName());
//进行远程服务调用
String result = userService.sayHello("张三");
System.out.println("result="+result);
}
}
WEBSERVICE 相关 API 总结


浙公网安备 33010602011771号