CXF 教程 (二)
将 Service 布署在远端
1 Overview
2 Server
3 Client
1 Overview
上例中我们的 Server 和 Client 都是在本地。下面演示如果布署在远端需如何修改。
2 Server
因为在启动 Server 时指定了地址,所以需进行如下修改,启动 Server 时使用参数指定地址
public class HelloServer {
public static void main(String[] args) {
// Create our service implementation
HelloService helloWorldImpl = new HelloServiceImpl();
String defaultUrl = "http://localhost:9000/Hello";
String url = defaultUrl;
if (args.length > 0) {
url = args[0];
}
// Create our Server
ServerFactoryBean svrFactory = new ServerFactoryBean();
svrFactory.setServiceClass(HelloService.class);
svrFactory.setAddress(url);
svrFactory.setServiceBean(helloWorldImpl);
svrFactory.create();
}
}
值得注意的是,我在 server 上使用 localhost 或 127.0.0.1 后,在浏览器中并不能使用真实的 IP 地址进行访问!
小技巧:将测试程序复制到远程测试时,可以用如下命令导出所有依赖包。
mvn clean package dependency:copy-dependencies -DoutputDirectory=target/lib
3 Client
如果是象上例在代码中指定 URL 地址的话,只需要把 URL 改成实际 IP 地址即可
String wsdlUrl = "http://[ip_address]:[port]/Hello?wsdl";
URL wsdlURL = new URL(wsdlUrl);
但如果是使用的 WSDL,则需修改 WSDL 文件中的如下部分:
name="HelloService">
binding="tns:HelloServiceSoapBinding" name="HelloServicePort">
location="http://[ip_address]:[port]/Hello" />