前期准备
- 准备一个供客户端请求调用的wsdl接口文件:http://proxy.test-a.com:8090/api/services/EasyWS?wsdl
- 两台云服务器,服务器A只有内网ip,只能访问在同一个vpc(私有云网络)下的其他服务器。服务器B内外网ip配置齐全,可进行公网访问。后面就通过服务器B将服务器A对外的请求转发出去。
- Nginx和JDK
一 Java客户端对对端WSDL接口的调用
这里是通过使用AXIS1框架来实现对wdsl文件中webservice接口的调用。springboot项目中引入相关的依赖。
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-saaj</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.4</version>
</dependency>
业务参考代码。
public String process(String messagePrefix, String bussinessMessage) {
String namespace="http://xx.xx.xxx.com";
String methodName = "method";
String endPoint = "http://proxy.test-a.com:8090/api/services/EasyWS";
//String soapAction = "";
String soapActionURI = namespace + methodName;
SOAPHeaderElement header = new SOAPHeaderElement(namespace, methodName);
header.setNamespaceURI(namespace);
org.apache.axis.client.Service service = new org.apache.axis.client.Service();
Call call = null;
try {
call = (Call) service.createCall();
//设置响应超时
call.setTimeout(3000);
//设置地址
call.setTargetEndpointAddress(new java.net.URL(endPoint));
//设置方法名
call.setOperationName(new QName(namespace, methodName));
call.setOperation("method");
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapActionURI);
//设置参数
call.addParameter("methodReq", XMLType.XSD_BASE64, ParameterMode.IN);
call.addParameter("methodReturn", XMLType.XSD_BASE64, ParameterMode.OUT);
//设置返回类型
call.setReturnType(XMLType.XSD_BASE64);
//拼接请求参数
String param = messagePrefix + bussinessMessage;
byte[] ret = (byte[]) call.invoke(new Object[]{param.getBytes(StandardCharsets.UTF_8)});
System.out.println(ret);
String retStr = new String(ret, Charset.forName("GB2312"));
return retStr;
} catch (ServiceException | MalformedURLException | RemoteException e) {
e.printStackTrace();
}
return null;
}
简易代码调用
public List<Goods> getGoodsSysnc(GoodsQueryVo vo) {
String prefix = "0007G0042C03";
if(StringUtils.isEmpty(vo)){
return null;
}
String str = JSON.toJSONString(vo);
log.info("开始请求外部接口数据,请求报文{}:",prefix+str);
String retStr = process(prefix,str);
log.info("结束请求外部接口数据,响应结果:",retStr );
List<Goods> resultList = parseCommonData(retStr, Goods.class);
System.out.println("------------------------");
System.out.println(resultList);
return resultList;
}
二 云服务器的配置
在腾讯云上申请了两台云服务器,做临时测试使用,服务器A中安装JDK并部署项目,服务器B安装Nginx,提供内部服务器向外请求的代理转发服务。
先测试一下不通过服务器B,服务器A中项目在内网环境下直接请求http://proxy.test-a.com:8090/api/services/EasyWS的结果:
服务器B安装并配置Nginx,进行地址转发
server {
listen 8193;
server_name localhost;
client_max_body_size 100M;
location /proxy.test-a.com_8090/{
proxy_pass http://proxy.test-a.com:8090/;
}
}
此时调用服务器A中项目的wsdl接口地址时,改为映射地址。
String endPoint = "http://172.16.0.13:8193/proxy.test-a.com_8090/api/services/EasyWS";
这里的经过就变成了服务器A再次对该地址发起请求时,会将请求地址打到内网地址为172.16.0.13的服务器B上,服务器B中配置了/proxy.test-a.com_8090/为http://proxy.test-a.com:8090/的映射,故当请求到/proxy.test-a.com_8090时,会自动将请求地址转换为http://proxy.test-a.com_8090/api/services/EasyWS,由服务器B转发到公网。