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容器中即可。如:
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容器中即可。如:
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
已经提供了Eureka
、Ribbon
、Feign
、Zuul
以及Spring Cloud Gateway
和Spring Cloud Stream
的plugin,添加相应的plugin依赖即可。
版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/27/spring-cloud-gray-access-development-guide/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。
共有 0 条评论