-
Notifications
You must be signed in to change notification settings - Fork 827
[JAV-264]support zone aware load balance features #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2ab06dd
39d668d
4ec06bd
aca63f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2017 Huawei Technologies Co., Ltd | ||
* | ||
* 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 io.servicecomb.demo.springmvc.client; | ||
|
||
import io.servicecomb.serviceregistry.api.registry.Microservice; | ||
import java.util.List; | ||
|
||
public class MicroserviceArray { | ||
|
||
private List<Microservice> services; | ||
|
||
public List<Microservice> getServices() { | ||
return services; | ||
} | ||
|
||
public void setServices(List<Microservice> services) { | ||
this.services = services; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2017 Huawei Technologies Co., Ltd | ||
* | ||
* 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 io.servicecomb.demo.springmvc.client; | ||
|
||
import java.net.URI; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.RequestEntity; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
public class ServiceCenterExample { | ||
|
||
public static void main(String[] args) throws Exception { | ||
RestTemplate template = new RestTemplate(); | ||
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); | ||
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); | ||
headers.add("X-Tenant-Name", "default"); | ||
|
||
RequestEntity<String> requestEntity = new RequestEntity<String>(headers, HttpMethod.GET, | ||
new URI("http://127.0.0.1:9980/registry/v3/microservices")); | ||
ResponseEntity<String> stringResponseEntity = template.exchange(requestEntity, String.class); | ||
System.out.println(stringResponseEntity.getBody()); | ||
ResponseEntity<MicroserviceArray> microseriveResponseEntity = template | ||
.exchange(requestEntity, MicroserviceArray.class); | ||
MicroserviceArray microserives = microseriveResponseEntity.getBody(); | ||
System.out.println(microserives.getServices().get(1).getServiceId()); | ||
|
||
// instance | ||
headers.add("X-ConsumerId", microserives.getServices().get(1).getServiceId()); | ||
requestEntity = new RequestEntity<String>(headers, HttpMethod.GET, | ||
new URI("http://127.0.0.1:9980/registry/v3/microservices/" + microserives.getServices().get(1).getServiceId() | ||
+ "/instances")); | ||
ResponseEntity<String> microserviceInstanceResponseEntity = template.exchange(requestEntity, String.class); | ||
System.out.println(microserviceInstanceResponseEntity.getBody()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2017 Huawei Technologies Co., Ltd | ||
* | ||
* 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 io.servicecomb.loadbalance.filter; | ||
|
||
import com.netflix.loadbalancer.Server; | ||
import io.servicecomb.loadbalance.CseServer; | ||
import io.servicecomb.loadbalance.ServerListFilterExt; | ||
import io.servicecomb.serviceregistry.RegistryUtils; | ||
import io.servicecomb.serviceregistry.api.registry.MicroserviceInstance; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ZoneAwareServerListFilterExt implements ServerListFilterExt { | ||
|
||
@Override | ||
public List<Server> getFilteredListOfServers(List<Server> list) { | ||
List<Server> result = new ArrayList<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it possible to create cache for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not possible here because doing that we have to iterate 'list' to check if it is changed here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if want to use cache, must: it's a little complex, if this filter caused a performance problem in the furture, then can use this method to optimize. |
||
MicroserviceInstance myself = RegistryUtils.getMicroserviceInstance(); | ||
boolean find = false; | ||
for (Server server : list) { | ||
CseServer cseServer = (CseServer) server; | ||
if (regionAndAZMatch(myself, cseServer.getInstance())) { | ||
result.add(cseServer); | ||
find = true; | ||
} | ||
} | ||
|
||
if (!find) { | ||
for (Server server : list) { | ||
CseServer cseServer = (CseServer) server; | ||
if (regionMatch(myself, cseServer.getInstance())) { | ||
result.add(cseServer); | ||
find = true; | ||
} | ||
} | ||
} | ||
|
||
if (!find) { | ||
result = list; | ||
} | ||
return result; | ||
} | ||
|
||
private boolean regionAndAZMatch(MicroserviceInstance myself, MicroserviceInstance target) { | ||
if (myself.getDataCenterInfo() != null && target.getDataCenterInfo() != null) { | ||
return myself.getDataCenterInfo().getRegion().equals(target.getDataCenterInfo().getRegion()) && | ||
myself.getDataCenterInfo().getAvailableZone().equals(target.getDataCenterInfo().getAvailableZone()); | ||
} | ||
return false; | ||
} | ||
|
||
private boolean regionMatch(MicroserviceInstance myself, MicroserviceInstance target) { | ||
if (myself.getDataCenterInfo() != null && target.getDataCenterInfo() != null) { | ||
return myself.getDataCenterInfo().getRegion().equals(target.getDataCenterInfo().getRegion()); | ||
} | ||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* Copyright 2017 Huawei Technologies Co., Ltd | ||
* | ||
* 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 io.servicecomb.loadbalance.filter; | ||
|
||
import com.netflix.loadbalancer.Server; | ||
import io.servicecomb.loadbalance.CseServer; | ||
import io.servicecomb.serviceregistry.RegistryUtils; | ||
import io.servicecomb.serviceregistry.api.registry.DataCenterInfo; | ||
import io.servicecomb.serviceregistry.api.registry.MicroserviceInstance; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import mockit.Expectations; | ||
import mockit.Mock; | ||
import mockit.MockUp; | ||
import mockit.Mocked; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class TestZoneAwareServerListFilterExt { | ||
|
||
@Test | ||
public void testZoneAwareServerListFilterExt(@Mocked RegistryUtils registryUtils) { | ||
MicroserviceInstance myself = new MicroserviceInstance(); | ||
DataCenterInfo info = new DataCenterInfo(); | ||
info.setName("test"); | ||
info.setRegion("test-Region"); | ||
info.setAvailableZone("test-zone"); | ||
myself.setDataCenterInfo(info); | ||
|
||
MicroserviceInstance allmatchInstance = new MicroserviceInstance(); | ||
info = new DataCenterInfo(); | ||
info.setName("test"); | ||
info.setRegion("test-Region"); | ||
info.setAvailableZone("test-zone"); | ||
allmatchInstance.setDataCenterInfo(info); | ||
|
||
MicroserviceInstance regionMatchInstance = new MicroserviceInstance(); | ||
info = new DataCenterInfo(); | ||
info.setName("test"); | ||
info.setRegion("test-Region"); | ||
info.setAvailableZone("test-zone2"); | ||
regionMatchInstance.setDataCenterInfo(info); | ||
|
||
MicroserviceInstance noneMatchInstance = new MicroserviceInstance(); | ||
info = new DataCenterInfo(); | ||
info.setName("test"); | ||
info.setRegion("test-Region2"); | ||
info.setAvailableZone("test-zone2"); | ||
noneMatchInstance.setDataCenterInfo(info); | ||
|
||
new Expectations() { | ||
{ | ||
RegistryUtils.getMicroserviceInstance(); | ||
result = myself; | ||
} | ||
}; | ||
ZoneAwareServerListFilterExt filter = new ZoneAwareServerListFilterExt(); | ||
List<Server> servers = new ArrayList<>(); | ||
CseServer noneMatchServer = new MockUp<CseServer>() { | ||
@Mock | ||
public String toString() { | ||
return "noneMatchServer"; | ||
} | ||
|
||
@Mock | ||
public String getHost() { | ||
return "noneMatchServer"; | ||
} | ||
|
||
@Mock | ||
public MicroserviceInstance getInstance() { | ||
return noneMatchInstance; | ||
} | ||
}.getMockInstance(); | ||
CseServer regionMatchregionMatchServer = new MockUp<CseServer>() { | ||
@Mock | ||
public String toString() { | ||
return "regionMatchregionMatchServer"; | ||
} | ||
|
||
@Mock | ||
public String getHost() { | ||
return "regionMatchregionMatchServer"; | ||
} | ||
|
||
@Mock | ||
public MicroserviceInstance getInstance() { | ||
return regionMatchInstance; | ||
} | ||
}.getMockInstance(); | ||
|
||
CseServer allmatchServer = new MockUp<CseServer>() { | ||
@Mock | ||
public String toString() { | ||
return "allmatchServer"; | ||
} | ||
|
||
@Mock | ||
public String getHost() { | ||
return "allmatchServer"; | ||
} | ||
|
||
@Mock | ||
public MicroserviceInstance getInstance() { | ||
return allmatchInstance; | ||
} | ||
}.getMockInstance(); | ||
|
||
servers.add(noneMatchServer); | ||
List<Server> result = filter.getFilteredListOfServers(servers); | ||
Assert.assertEquals(result.size(), 1); | ||
Assert.assertEquals(result.get(0), noneMatchServer); | ||
|
||
servers.add(regionMatchregionMatchServer); | ||
result = filter.getFilteredListOfServers(servers); | ||
Assert.assertEquals(result.size(), 1); | ||
Assert.assertEquals(result.get(0), regionMatchregionMatchServer); | ||
|
||
servers.add(allmatchServer); | ||
result = filter.getFilteredListOfServers(servers); | ||
Assert.assertEquals(result.size(), 1); | ||
Assert.assertEquals(result.get(0), allmatchServer); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be better if we can pass the service center address from the exec argument options.