免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3744 | 回复: 0

remoting架构探讨 [复制链接]

论坛徽章:
0
发表于 2011-03-02 13:33 |显示全部楼层
转: HorsonJin


remoting架构探讨




1.remoting技术的特点
  remoting技术可以为分布式应用提供强有力的支持,我们可以根据需求和特定的环境选择合适的通道和序列化的方式满足我们的应用。另外remoting技术具有非常好的扩展特性,我们甚至可以在remoting体系结构的每个组件上进行扩展和自定义来满足丰富的应用需求。

2.环境分析

网络环境分析
  假设公司是一家大型机构,内部出于安全的需要,不同的部门和不同的子部分可能被不同的网络防火墙隔离,但是不同的部门或者子部门需要共同协作来管理一些应用,因此我们可能需要穿越公司内部的防火墙来满足我们的应用需要。

软件环境分析
  假设公司的大部分应用建立在windows平台之上,软件大多运行在clr之上,开发架构采用主流的三层架构,服务方式采用c/s架构,出于安全考虑,公司拒绝非服务器IP直接访问生产数据库,同时可以预见的是需要处理大数据量,所以我们需要提供分布式处理来满足公司安全方面的要求和达到良好的性能要求。

3.remoting架构设计
通道选择、激活方式、对象调用方式、传输方式选择
  由于公司内部网络环境复杂,需要有较好的适应能力,选择httpchannel作为传输通道会有比较强的网络环境适应能力;激活方式采用服务端、singleton方式激活;业务操作类使用按址列集的方式进行传输,实体对象使用按值列集的方式进行传输。

解决方案设计
  在三层架构的基础上加上三个解决方案,分别为remoting服务端、remoting客户端、IDL,建议将IDL的定义和实体类类的定义放在一个解决方案中。IDL的程序接需要分别被remoting服务端、remoting客户端应用。

示例代码
IDL定义

  1. //接口定义

  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;

  5. namespace RemotingIDL
  6. {
  7.     public interface IService
  8.     {
  9.         OnlineOrder[] SayMessage(OnlineOrder o);
  10.         Sample SayMessage(Sample obj);
  11.     }
  12. }

  13. //实体类设计

  14. using System;
  15. using System.Collections.Generic;
  16. using System.Text;

  17. namespace RemotingIDL
  18. {
  19.     [Serializable]
  20.     public class Sample
  21.     {
  22.         private string message;

  23.         public string Message
  24.         {
  25.             get { return message; }
  26.             set { message = value; }
  27.         }

  28.     }
  29. }
复制代码
业务操作类
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using RemotingIDL;
  5. public class MyService : MarshalByRefObject, IService
  6.     {
  7.         public OnlineOrder[] SayMessage(OnlineOrder o)
  8.         {
  9.             List<OnlineOrder> orders = new List<OnlineOrder>();
  10.             for (int i = 0; i < 10; i++)
  11.             {
  12.                 OnlineOrder order = new OnlineOrder();
  13.                 order.Message = o.Message;
  14.                 orders.Add(order);
  15.             }
  16.             Console.WriteLine("Record count:"+orders.Count);
  17.             return orders.ToArray();
  18.         }
  19.         public Sample SayMessage(Sample obj)
  20.         {
  21.             obj.Message = "this is a remoting invoke";
  22.             Console.WriteLine("this is a remoting serilizable!");
  23.             return obj;
  24.         }
  25.     }
复制代码
服务端配置
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using RemotingIDL;
  5. using System.Runtime.Remoting;
  6. using System.Runtime.Remoting.Channels;
  7. using System.Runtime.Remoting.Channels.Tcp;
  8. using System.Runtime.Remoting.Channels.Http;
  9. namespace RemotingServer
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             HttpChannel http = new HttpChannel(81);
  16.             ChannelServices.RegisterChannel(http, false);
  17.             //RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyService), "MyService", WellKnownObjectMode.Singleton);
  18.             RemotingConfiguration.RegisterWellKnownServiceType(typeof(ShoppingCart), "ShoppingCart", WellKnownObjectMode.Singleton);
  19.             Console.WriteLine("Remoting is running");
  20.             Console.ReadLine();
  21.         }
  22.     }
复制代码
客户端调用
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using RemotingIDL;
  5. using System.Runtime.Remoting;
  6. using System.Runtime.Remoting.Channels;
  7. using System.Runtime.Remoting.Channels.Tcp;
  8. using System.Runtime.Remoting.Channels.Http;

  9. namespace RemotingClient
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             try
  16.             {
  17.                 HttpChannel http = new HttpChannel();
  18.                 ChannelServices.RegisterChannel(http, false);
  19.                 IShoppingCart shoppingCart = (IShoppingCart)Activator.GetObject(typeof(IShoppingCart), "http://localhost:81/ShoppingCart");
  20.                 if (shoppingCart != null)
  21.                 {
  22.                     Product[] products = shoppingCart.GetProducts("Architecture");
  23.                     Console.WriteLine("Product count:" + products.Length.ToString());
  24.                 }
  25.             }
  26.             catch (System.Runtime.Serialization.SerializationException ex)
  27.             {
  28.                 Console.WriteLine(ex.Message);
  29.                 Console.WriteLine(ex.StackTrace);
  30.             }
  31.             catch (System.ArgumentException ex)
  32.             {
  33.                 Console.WriteLine(ex.Message);
  34.                 Console.WriteLine(ex.StackTrace);
  35.             }
  36.             Console.ReadLine();
  37.         }
  38.     }
  39. }
复制代码
4.remoting设计中遇到的问题
list传输的问题

  在webservice中list在默认情况下被转换为数组进行传输,在remoting采用服务端、singleton方式激活的情况下,传输list直接抛出异常,不做默认的转换。

客户端对象传输的问题

  在remoting采用服务端、singleton方式激活的情况下,客户端对象按址列集传输时直接抛出异常,按值列集传输正常。

调试问题

  目前想到的调试方式:通过配置文件配置为调试状态和发布状态,在调试状态直接调用本地对象,只有在发布状态才通过remoting调用远程对象。

本人初次使用remoting技术解决应用需求的问题,还请大家多提宝贵意见,在此谢过了!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP