分布式微服务现在成为了很多公司架构首先项,据我了解,很多java公司架构都是 Maven+Dubbo+Zookeeper基础上扩展的。

     Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合)。从服务模型的角度来看,Dubbo采用的是一种非常简单的模型,要么是提供方提供服务,要么是消费方消费服务,所以基于这一点可以抽象出服务提供方(Provider)和服务消费方(Consumer)两个角色。

     关于更多Dubbo更多资料,可以查看Dubbo官方文档。值得一提的是,Dubbo官方文档也是一份非常好的分布式学习资料。

     那么.net下能不能类似dubbo那样玩呢?目前好像没有找到.net下类似的实现。感觉很多公司应该有自己类似实现,但是没有开源出来吧。

     我这里先做一个类似的基于接口的远程调用的实现,项目的服务实现使用WCF,IOC注入使用Autofac。

     一,解决方案整体设计

     使用接口的方式调用远程服务 ------ 利用动态调用服务,实现.net下类似Dubbo的玩法。

      (一)Cn.Code.Demo 

       如上图,Cn.Code.Demo 解决方案包含了所有的项目。

       使用接口的方式调用远程服务 ------ 利用动态调用服务,实现.net下类似Dubbo的玩法。

    如上图:1 Cn.Code.Common 是项目一些公共方法的实现。

               2 Cn.Code.Core 是项目所有的对外接口,此项目只包含接口。

               3 Cn.Code.Demo 是一个MVC项目,用于测试我们的接口调用。

               4 Cn.Code.FirstService 第一个服务实现,实现了Cn.Code.Core 部分接口。

               5 Cn.Code.SecondService 第二个服务实现,实现了Cn.Code.Core 部分接口。

        (二)Cn.Code.FirstService

          使用接口的方式调用远程服务 ------ 利用动态调用服务,实现.net下类似Dubbo的玩法。

 

          此解决方案只包含 Cn.Code.Core 和Cn.Code.FirstService 。

         (三)Cn.Code.SecondService

          使用接口的方式调用远程服务 ------ 利用动态调用服务,实现.net下类似Dubbo的玩法。

 

         此解决方案只包含 Cn.Code.Core 和Cn.Code.SecondService 。

 

        二,接口层 --Cn.Code.Core 

        使用接口的方式调用远程服务 ------ 利用动态调用服务,实现.net下类似Dubbo的玩法。

        接口层只包含如下2个接口,这里我使用WCF进行测试,所以需要添加System.ServiceModel 模块引用。

        IFirstService 接口如下:

1     [ServiceContract]
2     public interface IFirstService
3     {
4         [OperationContract]
5         string GetData();
6     }

         ISecondService接口如下:

1     [ServiceContract]
2     public interface ISecondService
3     {
4         [OperationContract]
5         string GetData();
6     }

        三,服务实现 --Cn.Code.FirstService 和Cn.Code.SecondService

        为了方便测试,这里的服务实现都非常简单。

1     public class FirstService :IFirstService
2     {
3         public string GetData()
4         {
5             return string.Format("您调用了 FirstService !");
6         }
7 
8     }
1     public class SecondService :ISecondService
2     {
3         public string GetData()
4         {
5             return string.Format("您调用了 SecondService !");
6         }
7     }

         四,公共方法实现 --Cn.Code.Common

         首先是WCF动态调用的构建工厂。

 1    /// <summary>
 2     /// Wcf动态调用构建
 3     /// </summary>
 4     public class WcfInvokeFactory
 5     {
 6         #region WCF服务工厂
 7         public static T GetService<T>(string url)
 8         {
 9             return GetService<T>(url, "basicHttpBinding");
10         }
11 
12 
13         public static T GetService<T>(string url, string bing)
14         {
15             try
16             {
17                 if (string.IsNullOrEmpty(url)) throw new NotSupportedException("This url is not Null or Empty!");
18                 EndpointAddress address = new EndpointAddress(url);
19                 Binding binding = CreateBinding(bing);
20                 ChannelFactory<T> factory = new ChannelFactory<T>(binding, address);
21                 return factory.CreateChannel();
22             }
23             catch (Exception ex)
24             {
25                 throw new Exception("创建服务工厂出现异常.");
26             }
27         }
28         #endregion
29 
30         #region 创建传输协议
31         /// <summary>
32         /// 创建传输协议
33         /// </summary>
34         /// <param name="binding">传输协议名称</param>
35         /// <returns></returns>
36         private static Binding CreateBinding(string binding)
37         {
38             Binding bindinginstance = null;
39             if (binding.ToLower() == "basichttpbinding")
40             {
41                 BasicHttpBinding ws = new BasicHttpBinding();
42                 ws.MaxBufferSize = 2147483647;
43                 ws.MaxBufferPoolSize = 2147483647;
44                 ws.MaxReceivedMessageSize = 2147483647;
45                 ws.ReaderQuotas.MaxStringContentLength = 2147483647;
46                 ws.CloseTimeout = new TimeSpan(0, 30, 0);
47                 ws.OpenTimeout = new TimeSpan(0, 30, 0);
48                 ws.ReceiveTimeout = new TimeSpan(0, 30, 0);
49                 ws.SendTimeout = new TimeSpan(0, 30, 0);
50 
51                 bindinginstance = ws;
52             }
53             else if (binding.ToLower() == "nettcpbinding")
54             {
55                 NetTcpBinding ws = new NetTcpBinding();
56                 ws.MaxReceivedMessageSize = 65535000;
57                 ws.Security.Mode = SecurityMode.None;
58                 bindinginstance = ws;
59             }
60             else if (binding.ToLower() == "wshttpbinding")
61             {
62                 WSHttpBinding ws = new WSHttpBinding(SecurityMode.None);
63                 ws.MaxReceivedMessageSize = 65535000;
64                 ws.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.Windows;
65                 ws.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;
66                 bindinginstance = ws;
67             }
68             return bindinginstance;
69 
70         }
71         #endregion
72     }
WcfInvokeFactory

相关文章:

  • 2021-05-23
  • 2022-12-23
  • 2021-09-28
  • 2021-11-20
  • 2021-09-15
  • 2021-07-03
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
  • 2021-10-04
  • 2022-12-23
  • 2021-11-13
  • 2022-12-23
相关资源
相似解决方案