zoukankan      html  css  js  c++  java
  • 记录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都是各玩个的。这样不能一起愉快的玩耍的。

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

  • 相关阅读:
    firefox ajax async 弹出窗口提示阻止
    Spring加载resource时classpath*:与classpath:的区别(转)
    超链接的href属性 js调用
    jquery easyui tabs layout 记录
    PostgreSQL 中的递归查询 与oracle 的比较
    将字符串中的中文(英文)字符串转化为阿拉伯数字
    用CSS控制文字的宽度,超过省略号代替(转)
    制作gridview 固定列的2种简单解决方案
    自制树型控件(DataGrid) 支持多种响应
    备忘: select 对象的操作js(转)
  • 原文地址:https://www.cnblogs.com/hanfan/p/10784971.html
Copyright © 2011-2022 走看看