forked from apache/servicecomb-java-chassis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
262 additions
and
9 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
48 changes: 48 additions & 0 deletions
48
governance/src/main/java/org/apache/servicecomb/governance/handler/LoadBalanceHandler.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 @@ | ||
/* | ||
* 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)); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
governance/src/main/java/org/apache/servicecomb/governance/policy/LoadBalancerPolicy.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,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; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...nce/src/main/java/org/apache/servicecomb/governance/properties/LoadBalanceProperties.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 @@ | ||
/* | ||
* 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; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
governance/src/main/java/org/apache/servicecomb/loadbanlance/LoadBalance.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,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(); | ||
} |
30 changes: 30 additions & 0 deletions
30
governance/src/main/java/org/apache/servicecomb/loadbanlance/LoadBalanceImpl.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,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; | ||
} | ||
} |
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
60 changes: 60 additions & 0 deletions
60
governance/src/test/java/org/apache/servicecomb/governance/LoadBalancerTest.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,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()); | ||
} | ||
} |
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