-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apollo-client): the spi of config service load balancer client (#…
- Loading branch information
Showing
9 changed files
with
239 additions
and
7 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
34 changes: 34 additions & 0 deletions
34
...-client/src/main/java/com/ctrip/framework/apollo/spi/ConfigServiceLoadBalancerClient.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,34 @@ | ||
/* | ||
* Copyright 2022 Apollo 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 | ||
* | ||
* 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.ctrip.framework.apollo.spi; | ||
|
||
import com.ctrip.framework.apollo.core.dto.ServiceDTO; | ||
import com.ctrip.framework.apollo.core.spi.Ordered; | ||
import com.ctrip.framework.apollo.internals.ConfigServiceLocator; | ||
import java.util.List; | ||
|
||
public interface ConfigServiceLoadBalancerClient extends Ordered { | ||
|
||
/** | ||
* choose 1 config service from multiple service instances | ||
* | ||
* @param configServices the return of {@link ConfigServiceLocator#getConfigServices()} | ||
* @return return 1 config service chosen, null if there is no unavailable config service | ||
* @throws IllegalArgumentException if arg is null of empty | ||
*/ | ||
ServiceDTO chooseOneFrom(List<ServiceDTO> configServices); | ||
} |
46 changes: 46 additions & 0 deletions
46
...t/src/main/java/com/ctrip/framework/apollo/spi/RandomConfigServiceLoadBalancerClient.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,46 @@ | ||
/* | ||
* Copyright 2022 Apollo 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 | ||
* | ||
* 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.ctrip.framework.apollo.spi; | ||
|
||
import com.ctrip.framework.apollo.core.dto.ServiceDTO; | ||
import java.util.List; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
/** | ||
* default service provider of {@link ConfigServiceLoadBalancerClient} | ||
*/ | ||
public class RandomConfigServiceLoadBalancerClient implements ConfigServiceLoadBalancerClient { | ||
|
||
private static final int ORDER = 0; | ||
|
||
@Override | ||
public ServiceDTO chooseOneFrom(List<ServiceDTO> configServices) { | ||
if (null == configServices) { | ||
throw new IllegalArgumentException("arg is null"); | ||
} | ||
if (configServices.isEmpty()) { | ||
throw new IllegalArgumentException("arg is empty"); | ||
} | ||
int index = ThreadLocalRandom.current().nextInt(configServices.size()); | ||
return configServices.get(index); | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return ORDER; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...esources/META-INF/services/com.ctrip.framework.apollo.spi.ConfigServiceLoadBalancerClient
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 @@ | ||
com.ctrip.framework.apollo.spi.RandomConfigServiceLoadBalancerClient |
56 changes: 56 additions & 0 deletions
56
...ent/src/test/java/com/ctrip/framework/apollo/spi/ConfigServiceLoadBalancerClientTest.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,56 @@ | ||
/* | ||
* Copyright 2022 Apollo 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 | ||
* | ||
* 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.ctrip.framework.apollo.spi; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import com.ctrip.framework.foundation.internals.ServiceBootstrap; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ConfigServiceLoadBalancerClientTest { | ||
|
||
/** | ||
* all {@link ConfigServiceLoadBalancerClient}'s implementations need to conform it. | ||
*/ | ||
@Test | ||
void chooseOneFrom() { | ||
Iterator<ConfigServiceLoadBalancerClient> loadBalancerClientIterator = | ||
ServiceBootstrap.loadAll(ConfigServiceLoadBalancerClient.class); | ||
while (loadBalancerClientIterator.hasNext()) { | ||
ConfigServiceLoadBalancerClient configServiceLoadBalancerClient = loadBalancerClientIterator.next(); | ||
expectException(configServiceLoadBalancerClient); | ||
} | ||
} | ||
|
||
private static void expectException(ConfigServiceLoadBalancerClient loadBalancerClient) { | ||
// arg is null | ||
assertThrows(IllegalArgumentException.class, () -> loadBalancerClient.chooseOneFrom(null)); | ||
// arg is empty | ||
assertThrows(IllegalArgumentException.class, | ||
() -> loadBalancerClient.chooseOneFrom(new ArrayList<>())); | ||
} | ||
|
||
@Test | ||
void classTypeMatch() { | ||
ConfigServiceLoadBalancerClient loadBalancerClient = | ||
ServiceBootstrap.loadPrimary(ConfigServiceLoadBalancerClient.class); | ||
assertTrue(loadBalancerClient instanceof RandomConfigServiceLoadBalancerClient); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...c/test/java/com/ctrip/framework/apollo/spi/RandomConfigServiceLoadBalancerClientTest.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,58 @@ | ||
/* | ||
* Copyright 2022 Apollo 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 | ||
* | ||
* 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.ctrip.framework.apollo.spi; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import com.ctrip.framework.apollo.core.dto.ServiceDTO; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class RandomConfigServiceLoadBalancerClientTest { | ||
|
||
@Test | ||
public void chooseOneFrom() { | ||
ConfigServiceLoadBalancerClient loadBalancerClient = new RandomConfigServiceLoadBalancerClient(); | ||
List<ServiceDTO> configServices = generateConfigServices(); | ||
for (int i = 0; i < 100; i++) { | ||
ServiceDTO serviceDTO = loadBalancerClient.chooseOneFrom(configServices); | ||
// always contains it | ||
assertTrue(configServices.contains(serviceDTO)); | ||
} | ||
} | ||
|
||
private static List<ServiceDTO> generateConfigServices() { | ||
List<ServiceDTO> configServices = new ArrayList<>(); | ||
{ | ||
ServiceDTO serviceDTO = new ServiceDTO(); | ||
serviceDTO.setAppName("appName1"); | ||
configServices.add(serviceDTO); | ||
} | ||
{ | ||
ServiceDTO serviceDTO = new ServiceDTO(); | ||
serviceDTO.setAppName("appName2"); | ||
configServices.add(serviceDTO); | ||
} | ||
{ | ||
ServiceDTO serviceDTO = new ServiceDTO(); | ||
serviceDTO.setAppName("appName3"); | ||
configServices.add(serviceDTO); | ||
} | ||
return configServices; | ||
} | ||
} |
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