• 记录Ocelot + SignalR 多服务端测试


    前言

    分两个项目,一个Gatway,一个SignalR

    贴代码

    1、Gatway

    1、引用Ocelot

    2、添加一点点代码

    Startup.cs

    1556437515322

    3、简单配置ocelot

    ocelot.json

    {
      "ReRoutes": [
        {
          "DownstreamPathTemplate": "/{catchAll}", //下游路径
          "DownstreamScheme": "ws", //https  //下游协议
          "DownstreamHostAndPorts": [ // 下游主机及端口
            {
              "Host": "127.0.0.1",	// 这里是我后面signalr地址
              "Port": 53353
            },
            {
              "Host": "127.0.0.1",
              "Port": 53354
            },
            {
              "Host": "127.0.0.1",
              "Port": 53355
            }
          ],
          "UpstreamPathTemplate": "/gateway/{catchAll}", // 上游路径
          "UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE", "OPTIONS" ], //上游使用的http方法
          "LoadBalancerOptions": {
            "Type": "RoundRobin" //雨露均沾
            //LeastConnection 任务少的接客
            //NoLoadBalance 天将降大任于斯人也
          }
        }
      ],
      "GlobalConfiguration": {  //全局配置
        "BaseUrl": "http://127.0.0.1:5000"
      }
    }
    

    2、signalr

    1、Startup.cs

    1556438762257

    2、chat.js

    //const connection = new signalR.HubConnectionBuilder()
    //    .withUrl("http://127.0.0.1:5000/gateway/chatHub")  // 这里使用http
    //    .configureLogging(signalR.LogLevel.Information)
    //    .build();
    
    const connection = new signalR.HubConnectionBuilder()
        .withUrl("ws://127.0.0.1:5000/gateway/chatHub", {   // 这里使用WebSockets,不这样写连不上的
            skipNegotiation: true,
            transport: signalR.HttpTransportType.WebSockets
        })
        .configureLogging(signalR.LogLevel.Trace)
        .build();
    
    connection.on("ReceiveMessage", (user, message) => {
        const encodedMsg = user + " says " + message;
        const li = document.createElement("li");
        li.textContent = encodedMsg;
        document.getElementById("messagesList").appendChild(li);
    });
    
    document.getElementById("sendButton").addEventListener("click", event => {
        const user = document.getElementById("userInput").value;
        const message = document.getElementById("messageInput").value;
        connection.invoke("SendMessage", user, message).catch(err => console.error(err.toString()));
        event.preventDefault();
    });
    
    connection.start().catch(err => console.error(err.toString()));
    

    3、Program.cs

    1556439019364

    测试

    1、启动三个Signalr

    1556439429156

    2、启动Gateway项目

    1556439528846

    3、启动客户端

    新开三个客户端,发现分配到了三个地址。

    1556439986904

    1556440027608

    1556440052484

    也就是意味着这三个连这不同的服务端,发信息应该是不通的。这里我们测试一下。

    1556440286466

    那再开两个客户端试试

    1556440349057

    1556440389987

    不小心发了个54的消息,我们看下之前的54有没有消息。

    确实有。

    1556440437045

    4、测试结束

    好了,测试完了。也没看Ocelot源码。

    结论就是Ocelot这样连SignalR都是各玩个的。这样不能一起愉快的玩耍的。

    所以使用其他的方式实现一下。

  • 相关阅读:
    vue part1 基础
    【转载】NBU异机恢复oracle
    【转载】跨域请求
    [转载] django contenttypes
    rest_framework setting
    rest_framework 视图/路由/渲染器/认证授权/节流
    【转载整理】 mysql百万级数据库分页性能
    rest_framework 分页
    rest_framework 序列化
    django middleware
  • 原文地址:https://www.cnblogs.com/hanfan/p/10784971.html
Copyright © 2020-2023  润新知