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
16 changed files
with
573 additions
and
45 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
...ation-common/src/main/java/org/apache/servicecomb/foundation/common/event/AlarmEvent.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,35 @@ | ||
/* | ||
* 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.foundation.common.event; | ||
|
||
public class AlarmEvent { | ||
|
||
Type type; | ||
|
||
public AlarmEvent(Type type) { | ||
this.type = type; | ||
} | ||
|
||
public Type getType() { | ||
return this.type; | ||
} | ||
|
||
public enum Type { | ||
OPEN, | ||
CLOSE | ||
}; | ||
} |
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
73 changes: 73 additions & 0 deletions
73
...er-bizkeeper/src/main/java/org/apache/servicecomb/bizkeeper/CustomizeCommandGroupKey.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,73 @@ | ||
/* | ||
* 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.bizkeeper; | ||
|
||
import org.apache.servicecomb.core.Invocation; | ||
|
||
import com.netflix.hystrix.HystrixCommandGroupKey; | ||
import com.netflix.hystrix.HystrixKey; | ||
import com.netflix.hystrix.util.InternMap; | ||
|
||
public class CustomizeCommandGroupKey extends HystrixKey.HystrixKeyDefault implements HystrixCommandGroupKey { | ||
|
||
private String invocationType; | ||
|
||
private String microserviceName; | ||
|
||
private String schema; | ||
|
||
private String operation; | ||
|
||
public CustomizeCommandGroupKey(Invocation invocation) { | ||
super(invocation.getInvocationType().name() + "." + invocation.getOperationMeta().getMicroserviceQualifiedName()); | ||
this.invocationType = invocation.getInvocationType().name(); | ||
this.microserviceName = invocation.getMicroserviceName(); | ||
this.schema = invocation.getSchemaId(); | ||
this.operation = invocation.getOperationName(); | ||
} | ||
|
||
private static final InternMap<Invocation, CustomizeCommandGroupKey> intern = | ||
new InternMap<Invocation, CustomizeCommandGroupKey>( | ||
new InternMap.ValueConstructor<Invocation, CustomizeCommandGroupKey>() { | ||
@Override | ||
public CustomizeCommandGroupKey create(Invocation invocation) { | ||
return new CustomizeCommandGroupKey(invocation); | ||
} | ||
}); | ||
|
||
|
||
public static HystrixCommandGroupKey asKey(String type, Invocation invocation) { | ||
return intern.interned(invocation); | ||
} | ||
|
||
public String getInvocationType() { | ||
return invocationType; | ||
} | ||
|
||
public String getMicroserviceName() { | ||
return microserviceName; | ||
} | ||
|
||
public String getOperation() { | ||
return operation; | ||
} | ||
|
||
public String getSchema() { | ||
return schema; | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
...er-bizkeeper/src/main/java/org/apache/servicecomb/bizkeeper/event/CircutBreakerEvent.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,112 @@ | ||
/* | ||
* 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.bizkeeper.event; | ||
|
||
|
||
import org.apache.servicecomb.bizkeeper.CustomizeCommandGroupKey; | ||
import org.apache.servicecomb.foundation.common.event.AlarmEvent; | ||
|
||
import com.netflix.hystrix.HystrixCommandKey; | ||
import com.netflix.hystrix.HystrixCommandMetrics; | ||
|
||
public class CircutBreakerEvent extends AlarmEvent { | ||
|
||
private String role; | ||
|
||
private String microservice; | ||
|
||
private String schema; | ||
|
||
private String operation; | ||
|
||
//当前总请求数 | ||
private long currentTotalRequest; | ||
|
||
//当前请求出错计数 | ||
private long currentErrorCount; | ||
|
||
//当前请求出错百分比 | ||
private long currentErrorPercentage; | ||
|
||
private int requestVolumeThreshold; | ||
|
||
private int sleepWindowInMilliseconds; | ||
|
||
private int errorThresholdPercentage; | ||
|
||
public CircutBreakerEvent(HystrixCommandKey commandKey, Type type) { | ||
super(type); | ||
HystrixCommandMetrics hystrixCommandMetrics = | ||
HystrixCommandMetrics.getInstance(commandKey); | ||
if (hystrixCommandMetrics != null) { | ||
CustomizeCommandGroupKey customizeCommandGroupKey = | ||
(CustomizeCommandGroupKey) hystrixCommandMetrics.getCommandGroup(); | ||
this.microservice = customizeCommandGroupKey.getMicroserviceName(); | ||
this.role = customizeCommandGroupKey.getInvocationType(); | ||
this.schema = customizeCommandGroupKey.getSchema(); | ||
this.operation = customizeCommandGroupKey.getOperation(); | ||
this.currentTotalRequest = hystrixCommandMetrics.getHealthCounts().getTotalRequests(); | ||
this.currentErrorPercentage = hystrixCommandMetrics.getHealthCounts().getErrorCount(); | ||
this.currentErrorCount = hystrixCommandMetrics.getHealthCounts().getErrorPercentage(); | ||
this.requestVolumeThreshold = hystrixCommandMetrics.getProperties().circuitBreakerRequestVolumeThreshold().get(); | ||
this.sleepWindowInMilliseconds = | ||
hystrixCommandMetrics.getProperties().circuitBreakerSleepWindowInMilliseconds().get(); | ||
this.errorThresholdPercentage = | ||
hystrixCommandMetrics.getProperties().circuitBreakerErrorThresholdPercentage().get(); | ||
} | ||
} | ||
|
||
public String getRole() { | ||
return role; | ||
} | ||
|
||
public String getMicroservice() { | ||
return microservice; | ||
} | ||
|
||
public String getSchema() { | ||
return schema; | ||
} | ||
|
||
public String getOperation() { | ||
return operation; | ||
} | ||
|
||
public long getCurrentTotalRequest() { | ||
return currentTotalRequest; | ||
} | ||
|
||
public long getCurrentErrorCount() { | ||
return currentErrorCount; | ||
} | ||
|
||
public long getCurrentErrorPercentage() { | ||
return currentErrorPercentage; | ||
} | ||
|
||
public int getRequestVolumeThreshold() { | ||
return requestVolumeThreshold; | ||
} | ||
|
||
public int getSleepWindowInMilliseconds() { | ||
return sleepWindowInMilliseconds; | ||
} | ||
|
||
public int getErrorThresholdPercentage() { | ||
return errorThresholdPercentage; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...eper/src/main/java/org/apache/servicecomb/bizkeeper/event/CircutBreakerEventNotifier.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,61 @@ | ||
/* | ||
* 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.bizkeeper.event; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import org.apache.servicecomb.foundation.common.event.EventManager; | ||
import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx; | ||
import org.apache.servicecomb.foundation.common.event.AlarmEvent.Type; | ||
|
||
import com.google.common.eventbus.EventBus; | ||
import com.netflix.hystrix.HystrixCommandKey; | ||
import com.netflix.hystrix.HystrixEventType; | ||
import com.netflix.hystrix.strategy.eventnotifier.HystrixEventNotifier; | ||
|
||
public class CircutBreakerEventNotifier extends HystrixEventNotifier { | ||
|
||
/** | ||
* 使用circuitFlag来记录被熔断的接口 | ||
*/ | ||
private ConcurrentHashMapEx<String, AtomicBoolean> circuitFlag = new ConcurrentHashMapEx<>(); | ||
|
||
EventBus eventBus = EventManager.getEventBus(); | ||
|
||
@Override | ||
public void markEvent(HystrixEventType eventType, HystrixCommandKey key) { | ||
String keyName = key.name(); | ||
AtomicBoolean flag = circuitFlag.computeIfAbsent(keyName, k -> new AtomicBoolean()); | ||
switch (eventType) { | ||
case SHORT_CIRCUITED: | ||
if (flag.compareAndSet(false, true)) { | ||
eventBus.post(new CircutBreakerEvent(key, Type.OPEN)); | ||
} | ||
break; | ||
|
||
case SUCCESS: | ||
if (flag.compareAndSet(true, false)) { | ||
eventBus.post(new CircutBreakerEvent(key, Type.CLOSE)); | ||
} | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
handlers/handler-bizkeeper/src/main/resources/hystrix-plugins.properties
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 @@ | ||
hystrix.plugin.HystrixEventNotifier.implementation=org.apache.servicecomb.bizkeeper.event.CircutBreakerEventNotifier |
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
Oops, something went wrong.