手撸通用灰度规则框架(一)

什么是灰度

有时候,产品说,我们这个新版本上线后,先切20%的流量看看转化率。

有时候,领导说,这个重构的系统我们还是需要在生产小规模验证一下。

有时候,产品说,转化率符合预期,流量扩大到80%吧。

有时候,领导说,生产炸了,快、快全部切回老系统。

相信上面的场景很多人都遇到过,那么常见的解决这些问题的方式是什么呢?

使用配置文件,劣势,改配置需要发布。

使用db存储,劣势,每一次请求都要访问一次DB,不太合适
使用配置中心,劣势,不能做复杂的规则,例如10%的流量,排除掉某些个用户。

实现一个通用的灰度框架需要考虑哪些

1、规则是可以通过配置完成的

2、规则配置后是实时生效的,支持热加载的

3、不同的团队有不同的技术栈,需要支持市场上主流的配置中心

4、复杂规则可以自定义扩展、数据源支持自定义扩展

5、规则的执行是可跟踪的,跟踪的数据是可查看的,数据的存储是可以自定义的。

设计-先搭骨架

1、规则是由许多细小的表达式组成,例如 在0-20之间,在[1,2,3,4,6,7]中,等于“jack”,先设计一个class来描述这样的规则 ,我们可以设计成一个规则(Rule 包含 多个 Feature,规则的执行结果等于所有的Feature的执行结果)

1
2
3
4
5
6
7
8
9
10
java复制代码public class GrayRuleConfig
{
private List<Feature> features;
public static class Feature{
private String key;
private boolean enable;
private boolean unionAll;
private Map<String,String> metaData;
}
}

2、规则行为定义和Feature的行为

1
2
3
4
5
java复制代码public interface IRule {
String getKey();
boolean enable();
boolean check(Object value);
}
1
2
3
4
java复制代码public interface IFeature<F> {
F getFeature();
boolean check(Object targetValue);
}

3、规则和Feature的公有行为抽象出来,并定义好执行骨架

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
java复制代码public abstract class AbstractRule implements IRule {
private final ITraceRepository traceRepository;

public AbstractRule(ITraceRepository traceRepository) {
this.traceRepository = traceRepository;
}

public boolean check(Object value) {
boolean result = doCheck(value);
beforeGrayCheck(value);
try {
if (result) {
execResultPass();
} else {
execResultNoPass();
}
afterGrayCheck(value, result);
} catch (Exception ex) {
execGrayErrorAfter(ex, value);
}

return result;
}

protected void beforeGrayCheck(Object value) {
System.out.println(this.getClass() + ";beforeGrayCheck:" + value);
}

protected void afterGrayCheck(Object value, boolean result) {
System.out.println(this.getClass() + ";afterGrayCheck:" + value);

}

protected void execGrayErrorAfter(Exception ex, Object value) {
//todo log
System.out.println(this.getClass() + ";execGrayErrorAfter:" + value + "ex:" + ex);

}

private void execResultPass() {
traceRepository.pass(this.getKey());
}

private void execResultNoPass() {
traceRepository.noPass(this.getKey());
}
public abstract boolean doCheck(Object targetValue);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
java复制代码public abstract class AbstractFeature<F> implements IFeature<F>  {
private ITraceRepository traceRepository;
private F feature;

public void setTraceRepository(ITraceRepository traceRepository){
this.traceRepository=traceRepository;
}
public ITraceRepository getTraceRepository(){
return this.traceRepository;
}
protected IFeature buildFeature(String featureDesc){
return buildFeature(featureDesc,null);
}
protected IFeature buildFeature(String featureDesc,ITraceRepository traceRepository){
this.feature=parseFeature(featureDesc);
this.traceRepository=traceRepository;
return this;
}
public F getFeature() {
return this.feature;
}

public boolean check(Object targetValue) {
return doCheck(targetValue);
}
private F parseFeature(String featureDesc){
if (StringUtils.isEmpty(featureDesc)){
throw new GrayException("featureDesc can not be null");
}
return doParseFeature(featureDesc);
}

protected abstract F doParseFeature(String featureDesc);
protected abstract boolean doCheck(Object value);

默认支持的规则实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
java复制代码public class DefaultRule extends AbstractRule {
private String key;
private boolean enable;

public boolean isUnionAll() {
return unionAll;
}

public void setUnionAll(boolean unionAll) {
this.unionAll = unionAll;
}

private boolean unionAll;

public DefaultRule(ITraceRepository traceRepository) {
super(traceRepository);
}
public void setKey(String key){
this.key=key;
}
public void setEnable(boolean enable){
this.enable=enable;
}
public boolean enable() {
return this.enable;
}
public String getKey() {
return this.key;
}

private List<IFeature> features;
public void setFeatures(List<IFeature> features){
this.features=features;
}
public List<IFeature> getFeatures() {
return this.features;
}

public boolean doCheck(Object targetValue) {
boolean result=false;
for (IFeature feature:this.features){
result=feature.check(targetValue);
if (result&&!unionAll){
break;
}
if (!result&&unionAll){
break;
}
}
return result;
}

规则部分,先写到这里,github有源码

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%