spring-cloud / spring-cloud-gateway

An API Gateway built on Spring Framework and Spring Boot providing routing and more.
http://cloud.spring.io
Apache License 2.0
4.5k stars 3.31k forks source link

`mrniko/netty-socketio` How to balance load in spring-cloud-gateway #1857

Open qmdx opened 4 years ago

qmdx commented 4 years ago

Describe the bug route not supported format lb://serviceid:port

Sample mrniko/netty-socketio How to balance load in spring-cloud-gateway

https://github.com/mrniko/netty-socketio

Looking forward to your reply, thank you

spencergibb commented 4 years ago

Remove the port

qmdx commented 4 years ago

Remove the port

There are two ports use netty-socketio + spring boot

Hope to support format lb://serviceid:port

OlgaMaciaszek commented 3 years ago

Can you explain step by step what the issue is and what exactly you are trying to do? In general, for load-balanced scenarios, you would not add port, since you use the loadbalancer to determine where to execute the calls.

qmdx commented 3 years ago

Can you explain step by step what the issue is and what exactly you are trying to do? In general, for load-balanced scenarios, you would not add port, since you use the loadbalancer to determine where to execute the calls.

Because my service has multiple ports,I hope the route can be specified to the specific port location of the service。 The way we deal with it now

public class ChatLoadBalancerClientFilter extends LoadBalancerClientFilter {

    public ChatLoadBalancerClientFilter(LoadBalancerClient loadBalancer, LoadBalancerProperties properties) {
        super(loadBalancer, properties);
    }

    @Override
    protected ServiceInstance choose(ServerWebExchange exchange) {
        ServiceInstance serviceInstance = super.choose(exchange);
        if(null != serviceInstance && "chat".equals(serviceInstance.getServiceId())
                && exchange.getRequest().getPath().toString().contains("/socket.io/")) {
            // Specify port 23456
            return new DefaultServiceInstance(serviceInstance.getInstanceId(), serviceInstance.getServiceId(),
                    serviceInstance.getHost(), 23456, serviceInstance.isSecure());
        }
        return serviceInstance;
    }
}
spencergibb commented 3 years ago

It should be registered as another service

qmdx commented 3 years ago

It should be registered as another service

The port can be specified, Don't you think about support?

xuxinping commented 3 years ago

Have you implemented this already? I have the same issue

xuxinping commented 3 years ago

How to implement socket forwarding in gateway

manang commented 3 years ago

Did you solve this issue? I'm trying to follow this example: https://programmersought.com/article/47377267070/ but LoadBalancerClientFilter is deprecated and I don't know how to convert the socket loadbalancer filter. Thanks

miaomiao1992 commented 1 year ago

hi,everyone! now the version of spring cloud i use is 2021.0.4,and i can't import the two important classes: //import org.springframework.cloud.gateway.config.LoadBalancerProperties; //now spring cloud 2021.0.4 invalid //import org.springframework.cloud.gateway.filter.LoadBalancerClientFilter; //now spring cloud 2021.0.4 invalid who can tell me the right packages of these two classes? thanks

miaomiao1992 commented 1 year ago

hi,everyone! now the version of spring cloud i use is 2021.0.4,and i can't import the two important classes: //import org.springframework.cloud.gateway.config.LoadBalancerProperties; //now spring cloud 2021.0.4 invalid //import org.springframework.cloud.gateway.filter.LoadBalancerClientFilter; //now spring cloud 2021.0.4 invalid who can tell me the right packages of these two classes? thanks

i want to update my projects and loadbalance socket-server with spring cloud 2021.0.4 and nacos,but now i don't know the right packages of LoadBalancerProperties and LoadBalancerClientFilter.

i used spring-cloud Hoxton.SR9 and spring-boot 2.3.5.RELEASE,and my application could normally run.

the old version:

org.springframework.cloud spring-cloud-starter-gateway
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

package com.sam.cloud.gateway.filter;

import java.util.Map;

import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.cloud.gateway.config.LoadBalancerProperties; import org.springframework.cloud.gateway.filter.LoadBalancerClientFilter; import org.springframework.web.server.ServerWebExchange;

public class SocketioLoadBalancerClientFilter extends LoadBalancerClientFilter {

public SocketioLoadBalancerClientFilter(LoadBalancerClient loadBalancer, LoadBalancerProperties properties) {
    super(loadBalancer, properties);
}

/**
 * 实现转发到socketio集群,但是集群内socketio服务端口必须固定
 */
protected ServiceInstance choose(ServerWebExchange exchange) {
    ServiceInstance serviceInstance = super.choose(exchange);
    if(null != serviceInstance && "socketio".equals(serviceInstance.getServiceId())
            && exchange.getRequest().getPath().toString().contains("/socket.io/")) {
        Map<String, String> metadata = serviceInstance.getMetadata();
        String socketioPort = metadata.get("socketio-port");
        int sport;
        if (socketioPort != null && !"".equals(socketioPort)) {
            try {
                sport = Integer.parseInt(socketioPort);
            } catch (NumberFormatException e) {
                sport = 10003;
            }
        } else {
            sport = 10003;
        }
        // Specify port 23456
        return new DefaultServiceInstance(serviceInstance.getInstanceId(), serviceInstance.getServiceId(),
                serviceInstance.getHost(), sport, serviceInstance.isSecure());
    }
    return serviceInstance;
}

}

spencergibb commented 1 year ago

https://github.com/spring-cloud/spring-cloud-gateway/blob/main/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayLoadBalancerProperties.java and https://github.com/spring-cloud/spring-cloud-gateway/blob/main/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/ReactiveLoadBalancerClientFilter.java

loong024 commented 1 year ago

大家好! 现在我使用的 Spring Cloud 版本是 2021.0.4,我无法导入两个重要的类://import org.springframework.cloud.gateway.config.LoadBalancerProperties;现在 Spring Cloud 2021.0.4 无效 //import org.springframework.cloud.gateway.filter.LoadBalancerClientFilter;现在春云 2021.0.4 无效 谁能告诉我这两个类的正确包?谢谢

我想使用 Spring Cloud 2021.0.4 和 Nacos 更新我的项目和负载平衡套接字服务器,但现在我不知道负载均衡器属性和负载均衡器客户端过滤器的正确包。

我使用了 spring-cloud Hoxton.SR9 和 spring-boot 2.3.5.RELEASE,我的应用程序可以正常运行。

旧版本:org.springframework.cloud春云启动网关

  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  </dependency>

  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>

包 com.sam.cloud.gateway.filter;

导入java.util.Map;

import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.cloud.gateway.config.LoadBalancerProperties; import org.springframework.cloud.gateway.filter.LoadBalancerClientFilter; import org.springframework.web.server.ServerWebExchange;

public class SocketioLoadBalancerClientFilter extensions LoadBalancerClientFilter {

public SocketioLoadBalancerClientFilter(LoadBalancerClient loadBalancer, LoadBalancerProperties properties) {
    super(loadBalancer, properties);
}

/**
 * 实现转发到socketio集群,但是集群内socketio服务端口必须固定
 */
protected ServiceInstance choose(ServerWebExchange exchange) {
    ServiceInstance serviceInstance = super.choose(exchange);
    if(null != serviceInstance && "socketio".equals(serviceInstance.getServiceId())
            && exchange.getRequest().getPath().toString().contains("/socket.io/")) {
      Map<String, String> metadata = serviceInstance.getMetadata();
      String socketioPort = metadata.get("socketio-port");
      int sport;
      if (socketioPort != null && !"".equals(socketioPort)) {
          try {
              sport = Integer.parseInt(socketioPort);
          } catch (NumberFormatException e) {
              sport = 10003;
          }
      } else {
          sport = 10003;
      }
        // Specify port 23456
        return new DefaultServiceInstance(serviceInstance.getInstanceId(), serviceInstance.getServiceId(),
                serviceInstance.getHost(), sport, serviceInstance.isSecure());
    }
    return serviceInstance;
}

}

I have the same problem. Have you solved it

fygsrs commented 1 year ago

继承 ReactiveLoadBalancerClientFilter