Skip to content

Commit

Permalink
Merge pull request #5133 from WeDataSphere/master-1.6.0-fix-gateway
Browse files Browse the repository at this point in the history
Fix Spring Cloud gateway not supporting fixed IP and port forwarding
  • Loading branch information
casionone authored Jun 25, 2024
2 parents ea159cf + 134cf87 commit cac834d
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import org.apache.spark.sql.execution.datasources.csv.UDF

import java.io._
import java.net.InetAddress
import java.security.SecureRandom
import java.util

import scala.collection.JavaConverters._
Expand Down Expand Up @@ -76,7 +77,7 @@ class SparkPythonExecutor(val sparkEngineSession: SparkEngineSession, val id: In
private val lineOutputStream = new RsOutputStream
val sqlContext = sparkEngineSession.sqlContext
val SUCCESS = "success"
private lazy val py4jToken: String = RandomStringUtils.randomAlphanumeric(256)
private lazy val py4jToken: String = SecureRandom.getInstance("SHA1PRNG").nextInt(100000).toString

private lazy val gwBuilder: GatewayServerBuilder = {
val builder = new GatewayServerBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.apache.linkis.gateway.security.LinkisPreFilter$;
import org.apache.linkis.gateway.security.SecurityFilter;
import org.apache.linkis.gateway.springcloud.SpringCloudGatewayConfiguration;
import org.apache.linkis.rpc.constant.RpcConstant;
import org.apache.linkis.server.Message;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -135,7 +134,7 @@ private Route getRealRoute(
exchange
.getRequest()
.mutate()
.header(RpcConstant.FIXED_INSTANCE, serviceInstance.getInstance());
.header(SpringCloudGatewayConstant.FIXED_INSTANCE, serviceInstance.getInstance());
}
return Route.async()
.id(route.getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package org.apache.linkis.gateway.springcloud.http;

import org.apache.linkis.rpc.constant.RpcConstant;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

Expand Down Expand Up @@ -58,7 +56,7 @@ public Mono<Response<ServiceInstance>> choose(Request request) {
((RequestDataContext) request.getContext())
.getClientRequest()
.getHeaders()
.get(RpcConstant.FIXED_INSTANCE);
.get(SpringCloudGatewayConstant.FIXED_INSTANCE);
String clientIp = CollectionUtils.isNotEmpty(clientIpList) ? clientIpList.get(0) : null;
ServiceInstanceListSupplier supplier =
serviceInstanceListSupplierProvider.getIfAvailable(NoopServiceInstanceListSupplier::new);
Expand Down Expand Up @@ -96,10 +94,10 @@ private Response<ServiceInstance> getInstanceResponse(
return new DefaultResponse(
instances.get(ThreadLocalRandom.current().nextInt(instances.size())));
}
ServiceInstance chooseInstance = null;
for (ServiceInstance instance : instances) {
if (Objects.equals(ipAndPort[0], instance.getHost())
&& Objects.equals(ipAndPort[1], String.valueOf(instance.getPort()))) {
logger.info("choose serviceInstance {}", instance);
return new DefaultResponse(instance);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
* limitations under the License.
*/

package org.apache.linkis.rpc.constant;
package org.apache.linkis.gateway.springcloud.http;

public class RpcConstant {
public class SpringCloudGatewayConstant {

public static final String FIXED_INSTANCE = "client-ip";
public static final String FIXED_INSTANCE = "FIXED_INSTANCE";
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import org.apache.linkis.gateway.parser.{DefaultGatewayParser, GatewayParser}
import org.apache.linkis.gateway.route.{DefaultGatewayRouter, GatewayRouter}
import org.apache.linkis.gateway.springcloud.http.{
GatewayAuthorizationFilter,
LinkisGatewayHttpHeadersFilter
LinkisGatewayHttpHeadersFilter,
LinkisLoadBalancerClientConfiguration
}
import org.apache.linkis.gateway.springcloud.websocket.SpringCloudGatewayWebsocketFilter
import org.apache.linkis.rpc.Sender
Expand All @@ -41,6 +42,7 @@ import org.springframework.cloud.gateway.route.builder.{
PredicateSpec,
RouteLocatorBuilder
}
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients
import org.springframework.context.annotation.{Bean, Configuration}
import org.springframework.web.reactive.socket.client.WebSocketClient
import org.springframework.web.reactive.socket.server.WebSocketService
Expand All @@ -49,6 +51,7 @@ import org.slf4j.{Logger, LoggerFactory}

@Configuration
@AutoConfigureAfter(Array(classOf[GatewaySpringConfiguration], classOf[GatewayAutoConfiguration]))
@LoadBalancerClients(defaultConfiguration = Array(classOf[LinkisLoadBalancerClientConfiguration]))
class SpringCloudGatewayConfiguration {
import SpringCloudGatewayConfiguration._

Expand Down

0 comments on commit cac834d

Please sign in to comment.