Skip to content

Commit

Permalink
add loadbalance governance
Browse files Browse the repository at this point in the history
  • Loading branch information
lbc97 committed Aug 8, 2022
1 parent 8bb6cb0 commit e6a5b75
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ public int getRefreshIntervalInMillis() {
return refreshIntervalInMillis;
}

public KieConfiguration setRefreshIntervalInMillis(int refreshIntervalInMillis) {
this.refreshIntervalInMillis = refreshIntervalInMillis;
public KieConfiguration setRefreshIntervalInMillis(int refreshIntervallnMillis) {
this.refreshIntervalInMillis = refreshIntervallnMillis;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.servicecomb.governance.handler.IdentifierRateLimitingHandler;
import org.apache.servicecomb.governance.handler.InstanceBulkheadHandler;
import org.apache.servicecomb.governance.handler.InstanceIsolationHandler;
import org.apache.servicecomb.governance.handler.LoadBalanceHandler;
import org.apache.servicecomb.governance.handler.RateLimitingHandler;
import org.apache.servicecomb.governance.handler.RetryHandler;
import org.apache.servicecomb.governance.handler.ext.AbstractCircuitBreakerExtension;
Expand All @@ -43,6 +44,7 @@
import org.apache.servicecomb.governance.properties.IdentifierRateLimitProperties;
import org.apache.servicecomb.governance.properties.InstanceBulkheadProperties;
import org.apache.servicecomb.governance.properties.InstanceIsolationProperties;
import org.apache.servicecomb.governance.properties.LoadBalanceProperties;
import org.apache.servicecomb.governance.properties.MatchProperties;
import org.apache.servicecomb.governance.properties.RateLimitProperties;
import org.apache.servicecomb.governance.properties.RetryProperties;
Expand Down Expand Up @@ -102,6 +104,11 @@ public FaultInjectionProperties faultInjectionProperties() {
return new FaultInjectionProperties();
}

@Bean
public LoadBalanceProperties loadBalanceProperties() {
return new LoadBalanceProperties();
}

// handlers configuration
@Bean
public BulkheadHandler bulkheadHandler(BulkheadProperties bulkheadProperties) {
Expand All @@ -113,6 +120,11 @@ public InstanceBulkheadHandler instanceBulkheadHandler(InstanceBulkheadPropertie
return new InstanceBulkheadHandler(instanceBulkheadProperties);
}

@Bean
public LoadBalanceHandler loadBalanceHandler(LoadBalanceProperties loadBalanceProperties) {
return new LoadBalanceHandler(loadBalanceProperties);
}

@Bean
public CircuitBreakerHandler circuitBreakerHandler(CircuitBreakerProperties circuitBreakerProperties,
AbstractCircuitBreakerExtension circuitBreakerExtension,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.servicecomb.governance.handler;

import org.apache.servicecomb.governance.marker.GovernanceRequest;
import org.apache.servicecomb.governance.policy.LoadBalancerPolicy;
import org.apache.servicecomb.governance.properties.LoadBalanceProperties;
import org.apache.servicecomb.loadbanlance.LoadBalance;

public class LoadBalanceHandler extends AbstractGovernanceHandler<LoadBalance, LoadBalancerPolicy> {

private final LoadBalanceProperties loadBalanceProperties;

public LoadBalanceHandler(LoadBalanceProperties loadBalanceProperties) {
this.loadBalanceProperties = loadBalanceProperties;
}

@Override
protected String createKey(GovernanceRequest governanceRequest, LoadBalancerPolicy policy) {
return LoadBalanceProperties.MATCH_LOADBANLANCER_KEY + "." + policy.getName();
}

@Override
public LoadBalancerPolicy matchPolicy(GovernanceRequest governanceRequest) {
return matchersManager.match(governanceRequest, loadBalanceProperties.getParsedEntity());
}

@Override
protected DisposableHolder<LoadBalance> createProcessor(String key, GovernanceRequest governanceRequest, LoadBalancerPolicy policy) {
return new DisposableHolder<>(key, LoadBalance.getLoadBalance(key, policy));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.servicecomb.governance.policy;

public class LoadBalancerPolicy extends AbstractPolicy {
private String rule;

public String getRule() {
return rule;
}

public void setRule(String rule) {
this.rule = rule;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.servicecomb.governance.properties;

import org.apache.servicecomb.governance.policy.LoadBalancerPolicy;

public class LoadBalanceProperties extends PolicyProperties<LoadBalancerPolicy> {
public static final String MATCH_LOADBANLANCER_KEY = "servicecomb.loadbalance";

public LoadBalanceProperties() {
super(MATCH_LOADBANLANCER_KEY);
}

@Override
protected Class<LoadBalancerPolicy> getEntityClass() {
return LoadBalancerPolicy.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.servicecomb.loadbanlance;

import org.apache.servicecomb.governance.policy.LoadBalancerPolicy;

public interface LoadBalance {

static LoadBalance getLoadBalance(String key, LoadBalancerPolicy policy) {
LoadBalance loadBalance = new LoadBalanceImpl(policy.getRule());
return loadBalance;
}

String getRule();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.servicecomb.loadbanlance;

public class LoadBalanceImpl implements LoadBalance {
private final String rule;

public LoadBalanceImpl(String rule) {
this.rule = rule;
}

public String getRule() {
return rule;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ public void tearDown() {

@Test
public void test_all_bean_is_loaded() {
Assertions.assertEquals(8, propertiesList.size());
Assertions.assertEquals(9, propertiesList.size());
}

@Test
public void test_match_properties_successfully_loaded() {
Map<String, TrafficMarker> markers = matchProperties.getParsedEntity();
Assertions.assertEquals(12, markers.size());
Assertions.assertEquals(14, markers.size());
TrafficMarker demoRateLimiting = markers.get("demo-rateLimiting");
List<Matcher> matchers = demoRateLimiting.getMatches();
Assertions.assertEquals(1, matchers.size());
Expand All @@ -188,17 +188,17 @@ public void test_match_properties_successfully_loaded() {
@Test
public void test_match_properties_delete() {
Map<String, TrafficMarker> markers = matchProperties.getParsedEntity();
Assertions.assertEquals(12, markers.size());
Assertions.assertEquals(14, markers.size());
dynamicValues.put("servicecomb.matchGroup.test", "matches:\n"
+ " - apiPath:\n"
+ " exact: \"/hello2\"\n"
+ " name: match0");
GovernanceEventManager.post(new GovernanceConfigurationChangedEvent(new HashSet<>(dynamicValues.keySet())));
markers = matchProperties.getParsedEntity();
Assertions.assertEquals(13, markers.size());
Assertions.assertEquals(15, markers.size());
tearDown();
markers = matchProperties.getParsedEntity();
Assertions.assertEquals(12, markers.size());
Assertions.assertEquals(14, markers.size());
}

@Test
Expand All @@ -215,7 +215,7 @@ public void test_match_properties_changed() {
GovernanceEventManager.post(new GovernanceConfigurationChangedEvent(new HashSet<>(dynamicValues.keySet())));

Map<String, TrafficMarker> markers = matchProperties.getParsedEntity();
Assertions.assertEquals(13, markers.size());
Assertions.assertEquals(15, markers.size());
TrafficMarker demoRateLimiting = markers.get("demo-rateLimiting");
List<Matcher> matchers = demoRateLimiting.getMatches();
Assertions.assertEquals(1, matchers.size());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.servicecomb.governance;

import org.apache.servicecomb.governance.handler.LoadBalanceHandler;
import org.apache.servicecomb.governance.marker.GovernanceRequest;
import org.apache.servicecomb.loadbanlance.LoadBalance;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;

@SpringBootTest
@ContextConfiguration(classes = {GovernanceConfiguration.class, MockConfiguration.class})
public class LoadBalancerTest {
private LoadBalanceHandler loadBalanceHandler;

@Autowired
public void setLoadBalanceHandler(LoadBalanceHandler loadBalanceHandler) {
this.loadBalanceHandler = loadBalanceHandler;
}

public LoadBalancerTest() {

}

@Test
public void test_loadbalance_random() {
GovernanceRequest request = new GovernanceRequest();
request.setUri("/loadrandom");
request.setServiceName("loadrandom");
LoadBalance loadBalance = loadBalanceHandler.getActuator(request);
Assert.assertEquals("Random", loadBalance.getRule());
}

@Test
public void test_loadbalance_roundRobin() {
GovernanceRequest request = new GovernanceRequest();
request.setUri("/loadroundRobin");
request.setServiceName("loadroundRobin");
LoadBalance loadBalance = loadBalanceHandler.getActuator(request);
Assert.assertEquals("RoundRobin", loadBalance.getRule());
}
}
12 changes: 11 additions & 1 deletion governance/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

servicecomb:
matchGroup:
demo-loadbalance-random: |
matches:
- serviceName: "loadrandom"
demo-loadbalance-roundRobin: |
matches:
- serviceName: "loadroundRobin"
demo-fallback-returnNull: |
matches:
- serviceName: "returnNull"
Expand Down Expand Up @@ -155,7 +161,11 @@ servicecomb:
wrongIngored: |
delayTime: -1
type: ERROR
loadbalance:
demo-loadbalance-random: |
rule: Random
demo-loadbalance-roundRobin: |
rule: RoundRobin
routeRule:
test_server1: | # 服务名
- precedence: 1 # 优先级,数字越大,优先级越高。
Expand Down

0 comments on commit e6a5b75

Please sign in to comment.