-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[1071] support affinity tag Service Instance Filter
- Loading branch information
1 parent
1190cf7
commit f69eb5f
Showing
11 changed files
with
253 additions
and
4 deletions.
There are no files selected for viewing
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
28 changes: 28 additions & 0 deletions
28
...nacos/src/main/java/com/huaweicloud/nacos/loadbalancer/NacosAffinityTagFilterAdapter.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,28 @@ | ||
/* | ||
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* 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 | ||
* | ||
* 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 com.huaweicloud.nacos.loadbalancer; | ||
|
||
import com.huaweicloud.router.client.loadbalancer.AffinityTagFilterAdapter; | ||
|
||
/** | ||
* nacos affinity tag adapter | ||
* | ||
* @author provenceee | ||
* @since 2023-09-19 | ||
*/ | ||
public class NacosAffinityTagFilterAdapter implements AffinityTagFilterAdapter { | ||
} |
50 changes: 50 additions & 0 deletions
50
...cos/src/main/java/com/huaweicloud/nacos/registry/MetadataNacosRegistrationCustomizer.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,50 @@ | ||
/* | ||
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* 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 | ||
* | ||
* 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 com.huaweicloud.nacos.registry; | ||
|
||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import org.apache.commons.configuration.EnvironmentConfiguration; | ||
|
||
import com.alibaba.cloud.nacos.registry.NacosRegistration; | ||
import com.alibaba.cloud.nacos.registry.NacosRegistrationCustomizer; | ||
|
||
/** | ||
* add nacos metadata | ||
* | ||
* @author provenceee | ||
* @since 2023-09-19 | ||
*/ | ||
public class MetadataNacosRegistrationCustomizer implements NacosRegistrationCustomizer { | ||
private static final String INSTANCE_PROPS = "SERVICECOMB_INSTANCE_PROPS"; | ||
|
||
@Override | ||
public void customize(NacosRegistration registration) { | ||
EnvironmentConfiguration envConfig = new EnvironmentConfiguration(); | ||
String[] instancePropArray = envConfig.getStringArray(INSTANCE_PROPS); | ||
if (instancePropArray.length != 0) { | ||
registration.getMetadata().putAll(parseProps(instancePropArray)); | ||
} | ||
} | ||
|
||
private Map<String, String> parseProps(String... value) { | ||
return Arrays.stream(value).map(v -> v.split(":")) | ||
.filter(v -> v.length == 2) | ||
.collect(Collectors.toMap(v -> v[0], v -> v[1])); | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
...er/src/main/java/com/huaweicloud/router/client/loadbalancer/AffinityTagFilterAdapter.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,38 @@ | ||
/* | ||
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* 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 | ||
* | ||
* 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 com.huaweicloud.router.client.loadbalancer; | ||
|
||
import org.springframework.cloud.client.ServiceInstance; | ||
import org.springframework.cloud.client.serviceregistry.Registration; | ||
|
||
/** | ||
* affinity tag adapter | ||
* | ||
* @author provenceee | ||
* @since 2023-09-19 | ||
*/ | ||
public interface AffinityTagFilterAdapter { | ||
String AFFINITY_TAG = "affinity-tag"; | ||
|
||
default String getAffinityTag(ServiceInstance serviceInstance) { | ||
return serviceInstance.getMetadata().get(AFFINITY_TAG); | ||
} | ||
|
||
default String getAffinityTag(Registration registration) { | ||
return registration.getMetadata().get(AFFINITY_TAG); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...ain/java/com/huaweicloud/router/client/loadbalancer/AffinityTagServiceInstanceFilter.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,59 @@ | ||
/* | ||
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* 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 | ||
* | ||
* 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 com.huaweicloud.router.client.loadbalancer; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
import org.springframework.cloud.client.ServiceInstance; | ||
import org.springframework.cloud.client.loadbalancer.Request; | ||
import org.springframework.cloud.client.serviceregistry.Registration; | ||
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
import com.huaweicloud.governance.adapters.loadbalancer.ServiceInstanceFilter; | ||
|
||
/** | ||
* affinity tag instance filter | ||
* @author provenceee | ||
* @since 2023-09-19 | ||
*/ | ||
public class AffinityTagServiceInstanceFilter implements ServiceInstanceFilter { | ||
private final Registration registration; | ||
|
||
private final AffinityTagFilterAdapter adapter; | ||
|
||
public AffinityTagServiceInstanceFilter(Registration registration, AffinityTagFilterAdapter adapter) { | ||
this.registration = registration; | ||
this.adapter = adapter; | ||
} | ||
|
||
@Override | ||
public List<ServiceInstance> filter(ServiceInstanceListSupplier supplier, List<ServiceInstance> instances, | ||
Request<?> request) { | ||
String affinityTag = adapter.getAffinityTag(registration); | ||
List<ServiceInstance> serviceInstances = instances.stream().filter( | ||
instance -> Objects.equals(affinityTag, adapter.getAffinityTag(instance))).collect(Collectors.toList()); | ||
return CollectionUtils.isEmpty(serviceInstances) ? instances : serviceInstances; | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return AFFINITY_TAG_ORDER; | ||
} | ||
} |
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
41 changes: 41 additions & 0 deletions
41
...m/huaweicloud/servicecomb/discovery/loadbalancer/ServiceCombAffinityTagFilterAdapter.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,41 @@ | ||
/* | ||
* Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* 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 | ||
* | ||
* 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 com.huaweicloud.servicecomb.discovery.loadbalancer; | ||
|
||
import org.apache.servicecomb.service.center.client.model.MicroserviceInstance; | ||
import org.springframework.cloud.client.serviceregistry.Registration; | ||
|
||
import com.huaweicloud.router.client.loadbalancer.AffinityTagFilterAdapter; | ||
import com.huaweicloud.servicecomb.discovery.registry.ServiceCombRegistration; | ||
|
||
/** | ||
* serviceComb affinity tag adapter | ||
* | ||
* @author provenceee | ||
* @since 2023-09-19 | ||
*/ | ||
public class ServiceCombAffinityTagFilterAdapter implements AffinityTagFilterAdapter { | ||
@Override | ||
public String getAffinityTag(Registration registration) { | ||
ServiceCombRegistration serviceCombRegistration = (ServiceCombRegistration) registration; | ||
MicroserviceInstance microserviceInstance = serviceCombRegistration.getMicroserviceInstance(); | ||
if (microserviceInstance.getProperties() == null) { | ||
return null; | ||
} | ||
return microserviceInstance.getProperties().get(AFFINITY_TAG); | ||
} | ||
} |