-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature 2023.x: Expand NacosLoadBalancer to conveniently support cust…
…om service list filtering and load balancing algorithms (#3794)
- Loading branch information
1 parent
3445916
commit e9ba0af
Showing
7 changed files
with
249 additions
and
17 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
...very/src/main/java/com/alibaba/cloud/nacos/loadbalancer/DefaultLoadBalancerAlgorithm.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,48 @@ | ||
/* | ||
* Copyright 2023-2024 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 com.alibaba.cloud.nacos.loadbalancer; | ||
|
||
import java.util.List; | ||
|
||
import com.alibaba.cloud.nacos.balancer.NacosBalancer; | ||
|
||
import org.springframework.cloud.client.ServiceInstance; | ||
import org.springframework.cloud.client.loadbalancer.Request; | ||
import org.springframework.core.Ordered; | ||
|
||
/** | ||
* This is a default implementation of load balancing algorithm. | ||
* use {@link com.alibaba.cloud.nacos.balancer.NacosBalancer} | ||
* | ||
* @author <a href="mailto:[email protected]">zhangbinhub</a> | ||
*/ | ||
public class DefaultLoadBalancerAlgorithm implements LoadBalancerAlgorithm { | ||
@Override | ||
public String getServiceId() { | ||
return LoadBalancerAlgorithm.DEFAULT_SERVICE_ID; | ||
} | ||
|
||
@Override | ||
public ServiceInstance getInstance(Request<?> request, List<ServiceInstance> serviceInstances) { | ||
return NacosBalancer.getHostByRandomWeight3(serviceInstances); | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return Ordered.LOWEST_PRECEDENCE; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...s-discovery/src/main/java/com/alibaba/cloud/nacos/loadbalancer/LoadBalancerAlgorithm.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,40 @@ | ||
/* | ||
* Copyright 2023-2024 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 com.alibaba.cloud.nacos.loadbalancer; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.cloud.client.ServiceInstance; | ||
import org.springframework.cloud.client.loadbalancer.Request; | ||
import org.springframework.core.Ordered; | ||
|
||
/** | ||
* Load Balancer algorithm interface. | ||
* When expanding the load balancing algorithm, implement this interface and register it as a bean. | ||
* | ||
* @author <a href="mailto:[email protected]">zhangbinhub</a> | ||
*/ | ||
public interface LoadBalancerAlgorithm extends Ordered { | ||
/** | ||
* default service id. | ||
*/ | ||
String DEFAULT_SERVICE_ID = "defaultServiceId"; | ||
|
||
String getServiceId(); | ||
|
||
ServiceInstance getInstance(Request<?> request, List<ServiceInstance> serviceInstances); | ||
} |
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
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
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
33 changes: 33 additions & 0 deletions
33
...s-discovery/src/main/java/com/alibaba/cloud/nacos/loadbalancer/ServiceInstanceFilter.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,33 @@ | ||
/* | ||
* Copyright 2023-2024 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 com.alibaba.cloud.nacos.loadbalancer; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.cloud.client.ServiceInstance; | ||
import org.springframework.cloud.client.loadbalancer.Request; | ||
import org.springframework.core.Ordered; | ||
|
||
/** | ||
* Service Instance Filter interface. | ||
* When custom service instance list filter, implement this interface and register it as a bean. | ||
* | ||
* @author <a href="mailto:[email protected]">zhangbinhub</a> | ||
*/ | ||
public interface ServiceInstanceFilter extends Ordered { | ||
List<ServiceInstance> filterInstance(Request<?> request, List<ServiceInstance> serviceInstances); | ||
} |
75 changes: 75 additions & 0 deletions
75
...t/java/com/alibaba/cloud/nacos/discovery/NacosDiscoveryLoadBalancerConfigurationTest.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,75 @@ | ||
/* | ||
* Copyright 2013-2023 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 com.alibaba.cloud.nacos.discovery; | ||
|
||
import com.alibaba.cloud.nacos.NacosServiceAutoConfiguration; | ||
import com.alibaba.cloud.nacos.loadbalancer.LoadBalancerAlgorithm; | ||
import com.alibaba.cloud.nacos.loadbalancer.LoadBalancerNacosAutoConfiguration; | ||
import com.alibaba.cloud.nacos.loadbalancer.NacosLoadBalancerClientConfiguration; | ||
import com.alibaba.cloud.nacos.registry.NacosServiceRegistryAutoConfiguration; | ||
import com.alibaba.cloud.nacos.util.UtilIPv6AutoConfiguration; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration; | ||
import org.springframework.cloud.commons.util.UtilAutoConfiguration; | ||
import org.springframework.cloud.loadbalancer.config.LoadBalancerAutoConfiguration; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">zhangbinhub</a> | ||
**/ | ||
public class NacosDiscoveryLoadBalancerConfigurationTest { | ||
|
||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||
.withConfiguration(AutoConfigurations.of( | ||
AutoServiceRegistrationConfiguration.class, | ||
NacosServiceRegistryAutoConfiguration.class, | ||
UtilAutoConfiguration.class, | ||
UtilIPv6AutoConfiguration.class, | ||
NacosServiceAutoConfiguration.class, | ||
NacosDiscoveryAutoConfiguration.class, | ||
NacosDiscoveryClientConfiguration.class, | ||
LoadBalancerAutoConfiguration.class, this.getClass())); | ||
|
||
@Test | ||
public void testNacosLoadBalancerEnabled() { | ||
contextRunner.withPropertyValues("spring.cloud.loadbalancer.nacos.enabled=true") | ||
.withConfiguration(AutoConfigurations.of( | ||
LoadBalancerNacosAutoConfiguration.class, | ||
NacosLoadBalancerClientConfiguration.class)) | ||
.run(context -> { | ||
assertThat(context).hasSingleBean(LoadBalancerAlgorithm.class); | ||
assertThat(context).hasBean("nacosLoadBalancer"); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testNacosLoadBalancerDisabled() { | ||
contextRunner.withPropertyValues("spring.cloud.loadbalancer.nacos.enabled=false") | ||
.withConfiguration(AutoConfigurations.of( | ||
LoadBalancerNacosAutoConfiguration.class, | ||
NacosLoadBalancerClientConfiguration.class)) | ||
.run(context -> { | ||
assertThat(context).doesNotHaveBean(LoadBalancerAlgorithm.class); | ||
assertThat(context).doesNotHaveBean("nacosLoadBalancer"); | ||
}); | ||
} | ||
|
||
} |