设为首页 加入收藏

TOP

记录Ocelot + SignalR 多服务端测试
2019-09-17 18:34:01 】 浏览:25
Tags:记录 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都是各玩个的。这样不能一起愉快的玩耍的。

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

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇asp.net core系列 61 Ocelot 构建.. 下一篇.NET Core开发日志——GraphQL

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目