Webservice实现跨平台数据交换

什么是Webservice

最近做两个系统之间的数据交换,一直使用的都是ActivityMQ,最近要写一个临时环境的数据交换,没必要增加中间件,为了节省安装mq就直接利用Webservice实现两个系统的数据交换

Webservice 和 Mq的区别

参考其他博客:

个人认为最本质的区别在于 Webservice近乎实时通信,而MQ却通常是延时通信。

因为webservice其实就是本地服务器程序调用远程服务器上的方法,属于两者之间的交互,请求的时候需要等被请求的服务器做出回应后,另一端才会有所动作,也就是说,如果你请求的service服务器关闭了,或者中断了,那么你这边肯定就得不到答复了,你的这次请求就算是打水漂丢失了。

而MQ 则相当于是多了一个中间件
在这里插入图片描述
我所发送的请求 都必须先传达给 这个消息队列组件,然后由这个消息队列组件再去到另一个服务器上去请求,有了响应之后再 返回给当初的请求程序,因为MessageQueue组件会把消息持久化放在本地,所以哪怕突然死机了,请求消息也是不会丢失的。

Message Queue属于比较重量级的应用,在规范化的企业流程中用的比较多。如果企业有很多部门,部门都有自己的系统,那么不同的系统之间的集成通信,Message Queue是很好的选择。MQ一般都做为企业级IT应用的中间件存在,有很多企业是作为标准IT基础结构存在的。在市面上常见的MQ中间件有IBM websphere message queue service,Oracle Advanced Queuing,Microsoft Message Queue(MSMQ),Apache ActiveMQ等

如果使用WebService的话,就要写很多的WebService的代码,去建立这些WebServcie,然后暴露出这些接口,相互之间调用,很费事。但是如果使用Message Queue的话,只要把这个中间件的服务器搭建起来,只要在需要的时候加入不同的Queue Manager就可以了,然后就可以访问了,就可以作为不同系统之间的桥梁了。

长耗时的报表,这个在程序中经常遇见,处理海量数据时,可能生成一个报表需要5分中或是更长的时间,客户不能在线实时等待,报表处理比较耗费资源,不能同时处理很多请求,甚至同时只允许处理一个,这时就可以使用MQ。客户端将报表请求和一些必要的报表条件放到Queue中,报表由另一个服务一个一个的处理,处理好后再给用户发一个消息(MSN消息,或mail等)用户再在浏览器或其他报表浏览器中查看报表。

在线商店,在客户下订单的过程后,系统只需做减库存、记录收货人信息和必要的日志,其他的必须配送处理、交易统计等其他处理可以不同时完成,这时就可以将后续处理消息放入Queue中,让另一台(组)服务器去处理,这样可以加快下订单的过程,提高客户的体验;

WebService通常是实时性要求较高,Client端向Server端发出请求后,这是一个短连接,一个Http请求,这个请求发出后,Client端就会一直等到获取到这个结果。但是使用MQ的话,因为有了中间的这一块区域,当请求发出后,Client端可以继续去干别的事情。等到一段时间以后再去中间件的存储区域上查看一下有结果了么,有了结果就取出来,没有的话就再等会再看。

Webservice 具体实现

一、开发webservice接口的方式

1、使用jdk开发

2、使用第三方工具,如cxf、shiro等

我这里用的是cxf,需要添加cxf的支持

客户端的

    		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>3.0.15</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>3.0.15</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-udp</artifactId>
			<version>3.0.15</version>
		</dependency>
		<dependency>
			<groupId>wsdl4j</groupId>
			<artifactId>wsdl4j</artifactId>
			<version>1.6.2</version>
		</dependency>

我的服务端不是maven项目,jar上传

https://download.csdn.net/download/qq_34187522/12492827
这是我上传的jar包集合

你们可以参考 https://www.cnblogs.com/yxjdragon/p/6030740.html
https://www.cnblogs.com/jedjia/p/cxf.html

1.服务提供者实现

创建一个ProjectWebService 接口

@WebService(targetNamespace = "http://service.cxf.web.com/")
public interface ProjectWebService {
    /**
     * 查询项目信息
     *
     * @param projectId 单个获取项目
     * @return
     */
    @WebMethod(operationName = "getProjectInfoByProjectId")
    public @WebResult(name = "projectXmlStr", targetNamespace = "http://service.cxf.web.com/")
    String getProjectInfoByProjectId(@WebParam(name = "projectId") String projectId);

}

创建一个实现类,并添加配置

@WebService(endpointInterface = "com.XXX.webservice.ProjectWebService",
        targetNamespace = "http://service.cxf.web.com/",
        serviceName = "CXFWebService")
public class ProjectWebServiceImpl implements ProjectWebService {
	    @Override
	    public String getProjectInfoByProjectId(String projectId) {
	    //实现具体业务
	    	return "";
	   	 }
    }

将写好的接口发布出去,可以通过java代码发布或者配置文件
配置文件的方式
此处也可以用jaxws:server,具体可查看jaxws:endpoint与jaxws:server的区别

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd">


    <context:annotation-config />


    <jaxws:endpoint implementor="com.XXX.webservice.ProjectWebServiceImpl" address="/projectWebService" ></jaxws:endpoint>

</beans>

java代码的方式

 public static void main(String[] args) {   
    /***    * 完成 webservice 服务的发布    */ 
      //发布的 webservice 服务的访问地址   
      String address="http://localhost:8090/项目名/webService/projectWebService";   
      //创建 UserService 对象   
      ProjectWebService projectWebService =new ProjectWebServiceImpl ();   
      //发布具体的 webservice 服务  
       Endpoint.publish(address, projectWebService );      
       System.out.println("----------发布 webservice 服务-------------")
       }

发布成功后可以查看发布的内容
http://localhost:8090/项目名/webService/projectWebService?wsdl

1.服务的接收者

配置地址以及处理的接口

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xmlns:http="http://cxf.apache.org/transports/http/configuration"
	xsi:schemaLocation="
	http://cxf.apache.org/transports/http/configuration 
	http://cxf.apache.org/schemas/configuration/http-conf.xsd 
	http://www.springframework.org/schema/beans  
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://cxf.apache.org/jaxws  	
	http://cxf.apache.org/schemas/jaxws.xsd ">
	
	
	<context:property-placeholder location="classpath*:*.properties"/>

	<jaxws:client id="syncIService" address="http://localhost:8090/项目名/webService/projectWebService" serviceClass="com.XXX.util.SyncIService"  />

</beans>

创建接口,CXFWebService与发布的地方保持一致

@Component
@WebService(name = "CXFWebService", targetNamespace = "http://service.cxf.web.com/")
public interface SyncIService {
    /**
     * 查询项目信息
     *
     * @param projectId 单个获取项目
     * @return
     */
    @WebMethod(operationName = "getProjectInfoByProjectId")
    public @WebResult(name = "projectXmlStr", targetNamespace = "http://service.cxf.web.com/")
    String getProjectInfoByProjectId(@WebParam(name = "projectId") String projectId);

}

接下来调用这个方法就好了

  String result = syncIService.getProjectInfoByProjectId(id);
  //输出结果

也可以直接用java代码调用,从别人那里copy来的

public class WebServiceClient {



   public static void main(String[] args) throws Exception {

       //服务的地址

       URL wsUrl = new URL("http://localhost:8090/项目名/webService/projectWebService");

       HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection();

       conn.setDoInput(true);

       conn.setDoOutput(true);

       conn.setRequestMethod("POST");

       conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");

       OutputStream os = conn.getOutputStream();

       //请求体

       String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:q0=\"http://webservice.api.com/\" xmlns:xsd=



\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +

                     "<soapenv:Body> <q0:getValue><arg0>aaa</arg0>  </q0:getValue> </soapenv:Body> </soapenv:Envelope>";

       os.write(soap.getBytes());

       InputStream is = conn.getInputStream();

       byte[] b = new byte[1024];

       int len = 0;

       String s = "";

       while((len = is.read(b)) != -1){

           String ss = new String(b,0,len,"UTF-8");

           s += ss;

       }

       System.out.println(s);

       is.close();

       os.close();

       conn.disconnect();

   }

}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
支持传统C++开发及运行的跨平台的Web网站服务器端环境,建立在Apache平台上,knewcode0.93a版。 测试运行:(测试环境,Windows7) 1、将本目录拷贝到D盘根目录;(如果需要拷贝到其他目录,请自行修改Apache的配置文件“debug\httpd-2.2.25-win32\Apache22\conf\httpd.conf”) 2、运行“D:\knewcode0.93a\StartDebug.bat”启动Apache及knewcode运行环境; 3、在浏览器中,输入“http://127.0.0.1:8090/hello_world.kc”,测试“hello_world_cpp”例子; 4、在浏览器中,输入“http://127.0.0.1:8090/”,测试“exam_blog”例子; 5、“D:\knewcode0.93a\sample\src\webservice_client(c#)”是C#的Webservice客户端例子,可直接运行“webservice_client.exe”测试。 Linux下安装:(测试环境,Ubuntu14.04 32位版) 1、将本目录拷贝到“/home”目录下;(如果需要拷贝到其他目录,请自行修改“kc_apache_mod.conf”和“kc_apache_mod.load”文件) 2、使用超级管理员权限,运行“/home/knewcode0.93a/debug/linux_apache_setup/setup.sh”脚本,拷贝2个配置文件到Apache环境下; 3、修改Apache的相关配置文件,将“/home/knewcode0.93a/sample”目录设置为Apache的主页目录,并设置权限; 4、修改目录权限,设置“/home/knewcode0.93a”目录为可运行和可读写,“/home/knewcode0.93a/sample”目录的所有者改为“www-data”; 5、目前只在Ubuntu14.04 32位版下测试过,其他版本Linux请自行验证,并反馈问题给我,谢谢;目前只支持32位版的Linux。 系统目前尚处于测试阶段,如遇任何问题,请反馈到如下邮箱,谢谢! zogy@163.com ******************************************************************************************** 2015-11-11:knewcode0.93a版 1、增加print的简化关键字“p”; 2、增加“exec”关键字,用于执行自定义脚本; 3、字符串中增加用2个双引号表示1个双引号的转义字符; 4、增加变量定义的“--ref--”选项,用于定义同名外部变量的引用(如无对应的外部变量,则重新定义一个); 5、增加“delay”关键字,用于延迟脚本在特定的位置执行; 6、增加变量定义的“--static--”选项,防止重复定义的变量被覆盖; 7、修复了一些,在负载测试中,因高并发的进程或线程,引起的系统崩溃的bug; 8、修复了db_action和so_session应用中的一些内存泄漏等的bug。 2015-09-21:knewcode0.92a版 增加信号处理,降低因段错误,浮点错误,除0错误等异常,引起的系统崩溃。 2015-09-14:knewcode0.91a版 初始版本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值