Spring Cloud Gray 接入开发指南

工程模块

功能模块

模块 描述
spring-cloud-gray-utils 工具包
spring-cloud-gray-core 灰度数据模型/Java Bean定义,client端和server端通用
spring-cloud-gray-client 灰度客户端的核心代码,属于灰度客户端的内核
spring-cloud-gray-client-netflix 灰度客户端与spring cloud netflix集成的代码,与之相关的插件都依赖这个模块
spring-cloud-gray-plugin-webmvc 支撑灰度客户端在spring mvc运行的插件
spring-cloud-gray-plugin-webflux 支撑灰度客户端在spring webfulx运行的插件(B版及以上)
spring-cloud-gray-plugin-eureka 灰度客户端与注册中心eureka集成的插件
spring-cloud-gray-plugin-feign 灰度客户端与openFiegn集成的插件
spring-cloud-gray-plugin-zuul 灰度客户端与zuul 1.0集成的插件
spring-cloud-gray-plugin-gateway 灰度客户端与spring cloud gateway集成的插件(B版及以上)
spring-cloud-gray-plugin-event-stream 灰度客户端与spring cloud stream(rabbitmq)集成的插件
spring-cloud-gray-plugin-ribbon-nacos-discovery 灰度客户端支持ribbon与注册中心nacos集成的插件
spring-cloud-gray-server 灰度管控端的核心代码
spring-cloud-gray-server-plugin-eureka 灰度管控端与注册中心eureka集成的插件
spring-cloud-gray-server-plugin-event-stream 灰度管控端与spring cloud stream(rabbitmq)集成的插件
spring-cloud-gray-server-plugin-nacos-discovery 灰度管控端与注册中心nacos集成的插件
spring-cloud-starter-gray-client 灰度客户端starter
spring-cloud-starter-gray-server 灰度管控端starter
spring-cloud-starter-gray-eureka-server eureka server的灰度插件
spring-cloud-gray-webui 灰度管控端的web界面,vue编写

示例模块

模块 描述
spring-cloud-gray-eureka-sample eureka server/注册中心
spring-cloud-gray-server-sample 灰度管控端示例,界面是spring-cloud-gray-webui模块
spring-cloud-gray-service-a-sample 服务提供方示例
spring-cloud-gray-service-a1-sample 服务提供方示例
spring-cloud-gray-ervice-b-sample 服务消费方示例
spring-cloud-gray-zuul-sample zuul网关示例
spring-cloud-gray-gateway-sample spring-cloud-gateway网关示例(B版及以上)

灰度决策

灰度决策是灰度路由的关键,灰度决策由工厂类创建,工厂类的抽象接口是cn.springcloud.gray.decision.factory.GrayDecisionFactory

默认灰度决策实现

目前已有的灰度决策有:

名称 工厂类 描述
HttpHeader HttpHeaderGrayDecisionFactory 根据http请求头的字段进行判断
HttpMethod HttpMethodGrayDecisionFactory 根据http请求方法的字段进行判断
HttpParameter HttpParameterGrayDecisionFactory 根据http url参数进行判断
HttpTrackHeader HttpTrackHeaderGrayDecisionFactory 根据灰度追踪记录的http请求头的字段进行判断
HttpTrackParameter HttpTrackParameterGrayDecisionFactory 根据灰度追踪记录的http url参数进行判断
TraceIpGray TraceIpGrayDecisionFactory 根据灰度追踪记录的请求ip进行判断
TrackAttribute TrackAttributeGrayDecisionFactory 根据灰度追踪记录的属性值进行判断
FlowRateGray FlowRateGrayDecisionFactory 按百分比放量进行判断

自定义灰度决策实现

如果上面这些决策还不能满足需求,那么可以扩展cn.springcloud.gray.decision.factory.GrayDecisionFactory,实现自定义的逻辑,发布到Spring容器中即可。如:

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
import cn.springcloud.gray.decision.GrayDecision;
import cn.springcloud.gray.decision.factory.AbstractGrayDecisionFactory;
import cn.springcloud.gray.request.GrayHttpTrackInfo;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

@Component
public class VersionGrayDecisionFactory extends AbstractGrayDecisionFactory<VersionGrayDecisionFactory.Config> {

public VersionGrayDecisionFactory() {
super(VersionGrayDecisionFactory.Config.class);
}

@Override
public GrayDecision apply(Config configBean) {
return args -> {
GrayHttpTrackInfo grayRequest = (GrayHttpTrackInfo) args.getGrayRequest().getGrayTrackInfo();
int version = StringUtils.defaultIfNull(grayRequest.getAttribute(USER_ID_PARAM_NAME), "0");
if(StringUtils.equal(configBean.getCompareMode(), "LT")){
return configBean.getVersion() > version;
}else if(StringUtils.equal(configBean.getCompareMode(), "GT")){
return configBean.getVersion() < version;
}else{
return configBean.getVersion() == version;
}
};
}

@Setter
@Getter
public static class Config {
private String compareMode;
private int varsion;
}
}

灰度追踪

灰度追踪记录的逻辑是由cn.springcloud.gray.request.GrayInfoTracker的实现类实现

默认灰度追踪

目前已有的灰度追踪有:

名称 实现类 描述
HttpReceive HttpReceiveGrayInfoTracker 接收调用端传递过来的灰追踪信息
HttpHeader HttpHeaderGrayInfoTracker 获取http请求的header并记录到灰度追踪的Header中
HttpIP HttpIPGrayInfoTracker 获取http请求的ip并记录到灰度追踪中
HttpMethod HttpMethodGrayInfoTracker 获取http请求的请求方法并记录到灰度追踪中
HttpParameter HttpParameterGrayInfoTracker 获取http请求的url参数并记录到灰度追踪的parameter中
HttpURI HttpURIGrayInfoTracker 获取http请求的URI并记录到灰度追踪中

自定义灰度追踪实现

如果上面这些决策还不能满足需求,那么可以扩展cn.springcloud.gray.request.GrayInfoTracker,实现自定义的逻辑,发布到Spring容器中即可。如:

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
import cn.springcloud.gray.request.GrayHttpTrackInfo;
import cn.springcloud.gray.request.TrackArgs;
import cn.springcloud.gray.web.tracker.HttpGrayInfoTracker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@Component
public class UserIdGrayInfoTracker implements HttpGrayInfoTracker {

@Override
public void call(TrackArgs<GrayHttpTrackInfo, HttpServletRequest> args) {
SecurityContext securityContext = SecurityContextHolder.getContext();
Authentication authentication = securityContext.getAuthentication();
String userId = null;
if (authentication.getPrincipal() instanceof UserDetails) {
UserDetails springSecurityUser = (UserDetails) authentication.getPrincipal();
userId = springSecurityUser.getUsername();
} else if (authentication.getPrincipal() instanceof String) {
userId = (String) authentication.getPrincipal();
}
args.getTrackInfo().setAttribute("userId", userId);
}
}

项目扩展

项目已经实现了灰度的内核,如果要与其它的注册中心或者负载均衡中间件集成,只需实现相应的plugin即可,Spring Cloud Gray已经提供了EurekaRibbonFeignZuul以及Spring Cloud GatewaySpring Cloud Stream的plugin,添加相应的plugin依赖即可。

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2021 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :