压缩数据,提升Web service性能

项目中碰到一个问题,为客户做系统的另一家厂商居然用我们提供的Web service来初始化应用系统数据库,通过web service从源数据库中取一次取三百万行的表,处理后去填充另外一张表。调用接口的那哥们居然使用一个“select *"就想把所有数据都取过去,结果肯定是报内存溢出的错误。经过沟通后让他分批进行调用,但感觉效率还是不够高。在.net与java客户端自己暂时实在试验不出其它方法,所以决定使用压缩数据的方法。
.net与java交互的压缩方式较为简单的实现方式是用gzip格式,java本身有gzip的支持,.net没有相应的类库,我使用了开源项目sharpziplib来实现.net端的压缩功能。
经过试验,传输相同的字串,起码能够让长度减少三分之二,并且有效减低CPU的利用率。
代码如下:
1、C#端代码

 

public   static   string  Compress( string  uncompressedString)
        
{
            
byte[] byteData=System.Text.Encoding.UTF8.GetBytes(uncompressedString);
            MemoryStream ms
=new MemoryStream();
            Stream s
=new GZipOutputStream(ms);
            s.Write(byteData,
0,byteData.Length);
            s.Close();
            
byte[] compressData=(byte[])ms.ToArray();
            ms.Flush();
            ms.Close();
            
return System.Convert.ToBase64String(compressData,0,compressData.Length);
        }


        
public   static   string  DeCompress( string  compressedString)
        
{
//            string uncompressedString=string.Empty;
            StringBuilder sb=new StringBuilder(40960);
            
int totalLength=0;
            
byte[] byteInput=System.Convert.FromBase64String(compressedString);
            
byte[] writeData=new byte[4096];
            Stream s
=new GZipInputStream(new MemoryStream(byteInput));
            
while(true)
            
{
                
int size=s.Read(writeData,0,writeData.Length);
                
if(size>0)
                
{
                    totalLength
+=size;
                    sb.Append(System.Text.Encoding.UTF8.GetString(writeData,
0,size));
                }

                
else
                
{
                    
break;
                }

            }

            s.Flush();
            s.Close();
            
return sb.ToString();
        }

2、Java端代码
 public static String compress(String s) throws IOException{
        ByteArrayInputStream input 
=   new  ByteArrayInputStream(s.getBytes( " UTF-8 " )); 
        ByteArrayOutputStream output 
=   new  ByteArrayOutputStream( 1024 );
        GZIPOutputStream gzout 
=   new  GZIPOutputStream(output); 

        
byte [] buf = new   byte [ 1024 ];
        
int  number; 

        
        
while  ((number  =  input.read(buf))  !=   - 1 ){ 
            gzout.write(buf,
0 ,number); 
        }
        
        gzout.close(); 
        input.close();
        
        String result 
= new  BASE64Encoder().encode(output.toByteArray());
        
        output.close();
        
        
return  result;
    }
    
    public static String decompress(String data) throws IOException{
        ByteArrayOutputStream output 
=   new  ByteArrayOutputStream( 1024 );
        ByteArrayInputStream input 
=   new  ByteArrayInputStream( new  BASE64Decoder().decodeBuffer(data)); 
        GZIPInputStream gzinpt 
=   new  GZIPInputStream(input);
        
byte [] buf  =   new   byte [ 1024 ];
        
int  number  =   0 ;
        
        
while ((number  =  gzinpt.read(buf))  !=   - 1 ){
            output.write(buf,
0 ,number);
        }
         
        gzinpt.close();
        input.close();
        
        String result 
=   new  String(output.toString( " UTF-8 " ));
        
        output.close();
        
        
return  result; 
        
    }

在web service服务器端加压,java客户端的调用解压,使用web service的性能得到了提升。
但觉得这样还是不能彻底的解决问题,下一步准备做的改进主要有两点,一是把服务器把返回的大数据量压缩成zip文件以attachment的格式返回给java客户端,另一个是在内部网络以tcp soap的方式进行调用,辅以多线程的技术,更有效地利用服务器的多CPU与并实现服务的分流,使web service不必依赖于IIS(当然啦,这些都需要WSE的支持)。 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值