【问题标题】:Socket IO not working with multiple node js servers套接字 IO 不能与多个节点 js 服务器一起使用
【发布时间】:2021-10-08 07:51:39
【问题描述】:

我在 AWS 上有 3 台服务器。

什么时候

用户 A 连接到服务器 1 并且

用户 B 连接服务器 2

他们无法加入或聊天。但如果两者都连接到同一个服务器,那么他们就成功加入了。

处理这种情况的最佳方法是什么

在 AWS 上,我正在使用具有 Auto Scaling 的负载均衡器。

【问题讨论】:

标签: node.js socket.io amazon-elastic-beanstalk


【解决方案1】:

来自socket.iodocumentation

现在您有多个接受连接的 Socket.IO 节点,如果您想向所有人(甚至某个房间中的所有人)广播事件,您将需要某种方式在进程或计算机之间传递消息。

负责路由消息的接口就是我们所说的Adapter。您可以在 socket.io-adapter 之上实现自己的(通过继承它),或者您可以使用我们在 Redis 之上提供的:socket.io-redis:

在我们的基于 socket.io 的应用程序中实现 Redis 适配器以实现水平缩放只需向 index.js 文件添加几行代码:

var io = require('socket.io')(3000);
var redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));

要在您的应用程序中启用 AutoScaling,请将以下代码行添加到 scaling-policy.yml 文件中

# Enable autoscaling for the service
ScalableTarget:
  Type: AWS::ApplicationAutoScaling::ScalableTarget
  DependsOn: Service
  Properties:
    ServiceNamespace: 'ecs'
    ScalableDimension: 'ecs:service:DesiredCount'
    ResourceId:
      Fn::Join:
        - '/'
        - - service
          - Fn::ImportValue: !Join [':', [!Ref 'EnvironmentName', 'ClusterName']]
          - !Ref 'ServiceName'
    MinCapacity: 2
    MaxCapacity: 10
    RoleARN:
      Fn::ImportValue:
        !Join [':', [!Ref 'EnvironmentName', 'AutoscalingRole']]

# Create scaling policies for the service
ScaleDownPolicy:
  Type: AWS::ApplicationAutoScaling::ScalingPolicy
  DependsOn: ScalableTarget
  Properties:
    PolicyName:
      Fn::Join:
        - '/'
        - - scale
          - !Ref 'EnvironmentName'
          - !Ref 'ServiceName'
          - down
    PolicyType: StepScaling
    ResourceId:
      Fn::Join:
        - '/'
        - - service
          - Fn::ImportValue: !Join [':', [!Ref 'EnvironmentName', 'ClusterName']]
          - !Ref 'ServiceName'
    ScalableDimension: 'ecs:service:DesiredCount'
    ServiceNamespace: 'ecs'
    StepScalingPolicyConfiguration:
      AdjustmentType: 'ChangeInCapacity'
      StepAdjustments:
        - MetricIntervalUpperBound: 0
          ScalingAdjustment: -1
      MetricAggregationType: 'Average'
      Cooldown: 60

ScaleUpPolicy:
  Type: AWS::ApplicationAutoScaling::ScalingPolicy
  DependsOn: ScalableTarget
  Properties:
    PolicyName:
      Fn::Join:
        - '/'
        - - scale
          - !Ref 'EnvironmentName'
          - !Ref 'ServiceName'
          - up
    PolicyType: StepScaling
    ResourceId:
      Fn::Join:
        - '/'
        - - service
          - Fn::ImportValue: !Join [':', [!Ref 'EnvironmentName', 'ClusterName']]
          - !Ref 'ServiceName'
    ScalableDimension: 'ecs:service:DesiredCount'
    ServiceNamespace: 'ecs'
    StepScalingPolicyConfiguration:
      AdjustmentType: 'ChangeInCapacity'
      StepAdjustments:
        - MetricIntervalLowerBound: 0
          MetricIntervalUpperBound: 15
          ScalingAdjustment: 1
        - MetricIntervalLowerBound: 15
          MetricIntervalUpperBound: 25
          ScalingAdjustment: 2
        - MetricIntervalLowerBound: 25
          ScalingAdjustment: 3
      MetricAggregationType: 'Average'
      Cooldown: 60

# Create alarms to trigger these policies
LowCpuUsageAlarm:
  Type: AWS::CloudWatch::Alarm
  Properties:
    AlarmName:
      Fn::Join:
        - '-'
        - - low-cpu
          - !Ref 'EnvironmentName'
          - !Ref 'ServiceName'
    AlarmDescription:
      Fn::Join:
        - ' '
        - - "Low CPU utilization for service"
          - !Ref 'ServiceName'
          - "in stack"
          - !Ref 'EnvironmentName'
    MetricName: CPUUtilization
    Namespace: AWS/ECS
    Dimensions:
      - Name: ServiceName
        Value: !Ref 'ServiceName'
      - Name: ClusterName
        Value:
          Fn::ImportValue: !Join [':', [!Ref 'EnvironmentName', 'ClusterName']]
    Statistic: Average
    Period: 60
    EvaluationPeriods: 1
    Threshold: 20
    ComparisonOperator: LessThanOrEqualToThreshold
    AlarmActions:
      - !Ref ScaleDownPolicy

HighCpuUsageAlarm:
  Type: AWS::CloudWatch::Alarm
  Properties:
    AlarmName:
      Fn::Join:
        - '-'
        - - high-cpu
          - !Ref 'EnvironmentName'
          - !Ref 'ServiceName'
    AlarmDescription:
      Fn::Join:
        - ' '
        - - "High CPU utilization for service"
          - !Ref 'ServiceName'
          - "in stack"
          - !Ref 'EnvironmentName'
    MetricName: CPUUtilization
    Namespace: AWS/ECS
    Dimensions:
      - Name: ServiceName
        Value: !Ref 'ServiceName'
      - Name: ClusterName
        Value:
          Fn::ImportValue: !Join [':', [!Ref 'EnvironmentName', 'ClusterName']]
    Statistic: Average
    Period: 60
    EvaluationPeriods: 1
    Threshold: 70
    ComparisonOperator: GreaterThanOrEqualToThreshold
    AlarmActions:
      - !Ref ScaleUpPolicy

【讨论】:

    猜你喜欢
    • 2016-05-03
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2018-01-14
    • 2020-04-28
    • 1970-01-01
    相关资源
    最近更新 更多