forked from apache/linkis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c604486
commit f094821
Showing
6 changed files
with
209 additions
and
129 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
...gateway/src/main/java/org/apache/linkis/gateway/springcloud/constant/GatewayConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.linkis.gateway.springcloud.constant; | ||
|
||
public class GatewayConstant { | ||
|
||
public static final String FIXED_INSTANCE = "client-ip"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
.../org/apache/linkis/gateway/springcloud/loadbalancer/GatewayLoadBalancerConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.linkis.gateway.springcloud.loadbalancer; | ||
|
||
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@LoadBalancerClients(defaultConfiguration = {LinkisLoadBalancerClientConfiguration.class}) | ||
public class GatewayLoadBalancerConfiguration {} |
35 changes: 35 additions & 0 deletions
35
...apache/linkis/gateway/springcloud/loadbalancer/LinkisLoadBalancerClientConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.linkis.gateway.springcloud.loadbalancer; | ||
|
||
import org.springframework.cloud.client.ServiceInstance; | ||
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer; | ||
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; | ||
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.core.env.Environment; | ||
|
||
public class LinkisLoadBalancerClientConfiguration { | ||
@Bean | ||
public ReactorLoadBalancer<ServiceInstance> customLoadBalancer( | ||
Environment environment, LoadBalancerClientFactory loadBalancerClientFactory) { | ||
String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME); | ||
return new ServiceInstancePriorityLoadBalancer( | ||
loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name); | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
...g/apache/linkis/gateway/springcloud/loadbalancer/ServiceInstancePriorityLoadBalancer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.linkis.gateway.springcloud.loadbalancer; | ||
|
||
import org.apache.linkis.gateway.springcloud.constant.GatewayConstant; | ||
|
||
import org.apache.commons.collections.CollectionUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.cloud.client.ServiceInstance; | ||
import org.springframework.cloud.client.loadbalancer.*; | ||
import org.springframework.cloud.loadbalancer.core.NoopServiceInstanceListSupplier; | ||
import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer; | ||
import org.springframework.cloud.loadbalancer.core.SelectedInstanceCallback; | ||
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Random; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import reactor.core.publisher.Mono; | ||
|
||
public class ServiceInstancePriorityLoadBalancer implements ReactorServiceInstanceLoadBalancer { | ||
|
||
private static final Log log = LogFactory.getLog(ServiceInstancePriorityLoadBalancer.class); | ||
private final String serviceId; | ||
|
||
final AtomicInteger position; | ||
private final ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider; | ||
|
||
public ServiceInstancePriorityLoadBalancer( | ||
ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider, | ||
String serviceId) { | ||
this(serviceInstanceListSupplierProvider, serviceId, (new Random()).nextInt(1000)); | ||
} | ||
|
||
public ServiceInstancePriorityLoadBalancer( | ||
ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider, | ||
String serviceId, | ||
int seedPosition) { | ||
this.serviceId = serviceId; | ||
this.serviceInstanceListSupplierProvider = serviceInstanceListSupplierProvider; | ||
this.position = new AtomicInteger(seedPosition); | ||
} | ||
|
||
@Override | ||
public Mono<Response<ServiceInstance>> choose(Request request) { | ||
List<String> clientIpList = | ||
((RequestDataContext) request.getContext()) | ||
.getClientRequest() | ||
.getHeaders() | ||
.get(GatewayConstant.FIXED_INSTANCE); | ||
String clientIp = CollectionUtils.isNotEmpty(clientIpList) ? clientIpList.get(0) : null; | ||
ServiceInstanceListSupplier supplier = | ||
serviceInstanceListSupplierProvider.getIfAvailable(NoopServiceInstanceListSupplier::new); | ||
return supplier | ||
.get(request) | ||
.next() | ||
.map(serviceInstances -> processInstanceResponse(supplier, serviceInstances, clientIp)); | ||
} | ||
|
||
private Response<ServiceInstance> processInstanceResponse( | ||
ServiceInstanceListSupplier supplier, | ||
List<ServiceInstance> serviceInstances, | ||
String clientIp) { | ||
Response<ServiceInstance> serviceInstanceResponse = | ||
getInstanceResponse(serviceInstances, clientIp); | ||
if (supplier instanceof SelectedInstanceCallback && serviceInstanceResponse.hasServer()) { | ||
((SelectedInstanceCallback) supplier) | ||
.selectedServiceInstance(serviceInstanceResponse.getServer()); | ||
} | ||
return serviceInstanceResponse; | ||
} | ||
|
||
private Response<ServiceInstance> getInstanceResponse( | ||
List<ServiceInstance> instances, String clientIp) { | ||
if (instances.isEmpty()) { | ||
if (log.isWarnEnabled()) { | ||
log.warn("No servers available for service: " + serviceId); | ||
} | ||
return new EmptyResponse(); | ||
} | ||
int pos = this.position.incrementAndGet() & Integer.MAX_VALUE; | ||
|
||
if (StringUtils.isEmpty(clientIp)) { | ||
return new DefaultResponse(instances.get(pos % instances.size())); | ||
} | ||
for (ServiceInstance instance : instances) { | ||
String[] ipAndPort = clientIp.split(":"); | ||
if (ipAndPort.length == 2 | ||
&& Objects.equals(ipAndPort[0], instance.getHost()) | ||
&& Objects.equals(ipAndPort[1], instance.getPort())) { | ||
return new DefaultResponse(instance); | ||
} | ||
} | ||
|
||
return new DefaultResponse(instances.get(pos % instances.size())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters