WCF4.0支持路由机制,通过RoutingService实现请求分发、拦截处理。

  一、应用场景

  1、暴露一个endpoint在外网,其余服务部署于内网;

  2、请求分发,能对服务做负载功能;


  二、WCF4.0 路由服务

  

  图1- WCF路由示意图

    WCF RoutingService使用了消息过滤器的功能,内置定义了6个过滤器满足不同的需求:

    1、ActionMessageFilter:满足指定的操作集之一,也就操作匹配;

    2、EndpointAddressMessageFilter:满足指定的终结点地址;

    3、XPathMessageFilter:使用 XPath指定匹配的条件,用于实现基于内容路由的核心消息过滤器;

    4、MatchAllMessageFilter 与所有消息相匹配;

    5、MatchNoneMessageFilter 与所有消息都不匹配;

    对于以上默认提供的过滤器不能满足需求,还可以通过创建自己的 MessageFilter 实现消息过滤器。如下我们通过一个demo实现wcf服务分发负载处理。


  三、WCF RoutingService负载处理请求

  1、创建一个WCF服务,名称为:Aden.FK.WcfServiceA,基于console hosting实现,创建两个ServiceHost。

    (1)接口定义和实现为:

1
2
3
4
5
6
7
8
9
[ServiceContract]
    public interface IWcfServiceA
    {
        [OperationContract]
        int GetNumber();
 
        [OperationContract]
        string GetString();
    }

    (2)服务的实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class WcfServiceA : IWcfServiceA
    {
        public int GetNumber()
        {
            string msg = "Service :" + OperationContext.Current.EndpointDispatcher.EndpointAddress.Uri;
            Console.WriteLine(string.Format("print: {0}", msg));
 
            return new Random().Next();
        }
 
 
        public string GetString()
        {
            string msg = "Service :" + OperationContext.Current.EndpointDispatcher.EndpointAddress.Uri;
            Console.WriteLine(string.Format("print: {0}", msg));
 
            return msg;
        }
    }

    (3)创建服务host:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int num = 2;
                int index = 0;
                List<ServiceHost> serviceHost = new List<ServiceHost>();
                for (int i = 1; i <= num; i++)
                {
                    serviceHost.Add(new ServiceHost(typeof(WcfServiceA)));
                }
 
                serviceHost.ToList().ForEach(u =>
                {
                    var netTcp = new NetTcpBinding();
                    netTcp.Security.Mode = SecurityMode.None;
                    u.AddServiceEndpoint(typeof(IWcfServiceA), netTcp, string.Format("net.tcp://127.0.0.1:910{0}/services/WcfServiceA{1}", ++index, index));
                    u.Open();
                    Console.WriteLine("{0} Service Start,Address is {1}", u.Description.Name, u.Description.Endpoints[0].Address.Uri);
                }
                );

  

  2、创建一个路由服务,自定义扩展实现MessageFilter进行请求分发,路由与服务之间以TCP协议传输。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var serverEndpoints = new List<ServiceEndpoint>();
var netTcp = new NetTcpBinding();
netTcp.Security.Mode = SecurityMode.None;
 
serverEndpoints.Add(new ServiceEndpoint(new ContractDescription("IWcfServiceA"), netTcp, new EndpointAddress("net.tcp://127.0.0.1:9101/services/WcfServiceA1")));
serverEndpoints.Add(new ServiceEndpoint(new ContractDescription("IWcfServiceA"), netTcp, new EndpointAddress("net.tcp://127.0.0.1:9102/services/WcfServiceA2")));
 
using (var router = new ServiceHost(typeof(RoutingService)))
{
    int index = 10;
    string routerAddress = "net.tcp://127.0.0.1:8101/router/routerendpoint";
    router.AddServiceEndpoint(typeof(IRequestReplyRouter), netTcp, routerAddress);
    var config = new RoutingConfiguration();
    LoadBalancerFilter.EndpointsNumber = 2;
 
    serverEndpoints.ForEach(x => config.FilterTable.Add(new LoadBalancerFilter(), new[] { x }, index--));
    router.Description.Behaviors.Add(new RoutingBehavior(config));
 
    var debugBehavior = router.Description.Behaviors.Find<ServiceDebugBehavior>();
    debugBehavior.IncludeExceptionDetailInFaults = true;
 
    if (router.State != CommunicationState.Opening)
        router.Open();
 
 
    while (Console.ReadKey().Key == ConsoleKey.Enter)
    {
        break;
    }
    router.Close();
}

  

  3、创建一个客户端,客户端与路由之间以TCP协议传输。

1
2
3
4
5
6
7
8
var endpoint = new EndpointAddress("net.tcp://127.0.0.1:8101/router/routerendpoint");
            var netTcp = new NetTcpBinding();
            netTcp.Security.Mode = SecurityMode.None;
            var client = ChannelFactory<IWcfServiceA>.CreateChannel(netTcp, endpoint);
            while (Console.ReadKey().Key == ConsoleKey.Enter)
            {
                Console.WriteLine("--" + client.GetNumber());
            }

  四、运行结果

  1、客户端调用

  2、服务端显示,每次将请求分发到不同的服务处理。


  五、总结

  以上是一个简单的路由服务例子,可以根据实际情况扩展路由功能,实现上述描述是应用场景。考虑到路由的压力,可以在路由前架上Nginx负载。

引用:http://www.cnblogs.com/Andon_liu/p/5433538.html

WCF Routing服务,负载均衡的更多相关文章

  1. 基于Docker + Consul + Nginx + Consul-Template的服务负载均衡实现(转)

    转:https://www.jianshu.com/p/fa41434d444a 前言 上一篇文章使用 Consul 和 Registrator 在 docker 的容器环境中搭建了服务注册和发现集群 ...

  2. Windows服务器nginx+tomcat服务负载均衡

    一.安装两个tomcat服务自启动 1. 解压两个tomcat,名称为分别1,2 2. 配置环境变量 3. 修改文件server.xml中的三个端口号,使得两个tomcat不冲突 (1)<Ser ...

  3. HTTP服务负载均衡总结

    从一开始就要思考扩展的架构,所谓可扩展性指的是通过扩展规模提高承载能力的本领,往往体现在增加物理服务器或者集群节点.负载均衡是常见的水平扩展的手段. 目标:(1)减少单点故障(2)提升整体吞吐量(3) ...

  4. 【微服务】之四:轻松搞定SpringCloud微服务-负载均衡Ribbon

    对于任何一个高可用高负载的系统来说,负载均衡是一个必不可少的名称.在大型分布式计算体系中,某个服务在单例的情况下,很难应对各种突发情况.因此,负载均衡是为了让系统在性能出现瓶颈或者其中一些出现状态下可 ...

  5. springcloud第四步:ribbon搭建服务负载均衡

    使用ribbon实现负载均衡 启动两个会员服务工程,端口号分别为8762.8763,订单服务 使用负载均衡策略轮训到会员服务接口. 什么是ribbon ribbon是一个负载均衡客户端 类似nginx ...

  6. SpringCloud微服务负载均衡与网关

    1.使用ribbon实现负载均衡ribbon是一个负载均衡客户端 类似nginx反向代理,可以很好的控制htt和tcp的一些行为.Feign默认集成了ribbon. 启动两个会员服务工程,端口号分别为 ...

  7. 微服务负载均衡 —— ribbon

    负载均衡是系统高可用.缓解网络流量和处理能力扩容的重要手段,广义的负载均衡指的是服务端负载均衡,如硬件负载均衡(F5)和软件负载均衡(Nginx).负载均衡设备会维护一份可用的服务器的信息,当客户端请 ...

  8. SpringCloud 服务负载均衡和调用 Ribbon、OpenFeign

    1.Ribbon Spring Cloud Ribbon是基于Netflix Ribbon实现的-套客户端―负载均衡的工具. 简单的说,Ribbon是Netlix发布的开源项目,主要功能是提供客户端的 ...

  9. WCF Routing 服务

    WCF4.0支持路由机制,通过RoutingService实现请求分发.拦截处理. 一.应用场景 1.暴露一个endpoint在外网,其余服务部署于内网: 2.请求分发,能对服务做负载功能: 二.WC ...

随机推荐

  1. mybatis入门程序-(二)

    1. 添加配置文件 log4j.properties # Global logging configuration #开发环境下日志级别设置成DEBUG,生产环境设置成info或者error log4 ...

  2. mysql innobackupex 备份及恢复

    ----------------------------------全量备份恢复-------------------------------------1.生成一个完整的备份 innobackupe ...

  3. pip 18.1: pipenv graph results in ImportError: cannot import name 'get_installed_distributions'

    I'm currently using python3 -m pip install pip==10.0.1python3 -m pip install pipenv==2018.5.18 Once ...

  4. .NET C#错误:所生成项目的处理器框架“MSIL”与引用“wdapi_dotnet1021”的处理器架构“AMD64”不匹配

    .NET C#错误:所生成项目的处理器框架“MSIL”与引用“wdapi_dotnet1021”的处理器架构“AMD64”不匹配. 直接在项目右键属性->生成->x64. 即可解决

  5. oracle数据库_实例_用户_表空间之间的关系

    基础概念:Oracle数据库.实例.用户.表空间.表之间的关系 数据库:Oracle数据库是数据的物理存储.这就包括(数据文件ORA或者DBF.控制文件.联机日志.参数文件).其实Oracle数据库的 ...

  6. generator自动生成代码

    idea设置generator自动生成代码: http://blog.csdn.net/sunny243788557/article/details/45166397

  7. LeetCode(15):三数之和

    Medium! 题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答 ...

  8. Java之路(一) 一切皆对象

    Java语言假设我们只进行面向对象的程序设计,即在开始用Java进行设计前,我们需要将思想切换到面向对象的世界中. 1.用引用操纵对象 每种编程语言都有自己操纵内存中元素的方式.是直接操纵元素还是用某 ...

  9. 数据库相关--mysql中的单表查询

    一.完整的单表查询语句 select [distinct] *|字段1,字段2, .... from 表名 [where 条件1] [group by 字段名 [having 条件2] ] [orde ...

  10. Linux环境Tomcat运行报错java.lang.OutOfMemoryError

    java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "http-bio-8080-ex ...