基于spring cloud 的灰度发布实践_【收藏】基于spring cloud灰度发版方案
簡介
敏捷開發(fā)迭代周期短發(fā)布快,每周都可能面臨版本發(fā)版上線,為最大可能的降低對用戶的影響提高服務(wù)可用率,大部分團(tuán)隊(duì)都需要等到半夜做發(fā)布和支持。本文就如何基于spring cloud體系做灰度發(fā)版改造提供了方案,讓我們終于白天也能偷偷摸摸的無感知發(fā)版,驗(yàn)證,上線等動作,從此再也不用因?yàn)榘l(fā)版要熬夜了。
本文闡述的方案是灰度發(fā)版方案的一種實(shí)現(xiàn)(各種部署方案可參考文檔最后的附錄),屬于一種比較節(jié)約資源的部署方案,通過精準(zhǔn)的導(dǎo)流和開關(guān)控制實(shí)現(xiàn)用戶無感知的熱部署,比較適合中小企業(yè)采納應(yīng)用。整體技術(shù)架構(gòu)基于nepxion discovery插件結(jié)合項(xiàng)目中各個實(shí)踐場景做了方案說明和源代碼展示,如需要做權(quán)重,分組等策略可自行擴(kuò)展。
術(shù)語與配置
| 灰度節(jié)點(diǎn) | 被標(biāo)記為灰度的節(jié)點(diǎn) |
| 灰度入口 | 前端部署的節(jié)點(diǎn)被標(biāo)記為灰度的節(jié)點(diǎn) |
| 灰度用戶 | 賬號被標(biāo)記位灰度的用戶 |
| 灰度流量 | 路由是需要優(yōu)先選擇灰度節(jié)點(diǎn)的請求鏈 |
| 灰度開關(guān) | 是否開啟灰度路由,值為:開啟/關(guān)閉 |
| 灰度流量開關(guān) | 是否所有流量都是灰度流量,值為開啟/關(guān)閉 |
開關(guān)與流量關(guān)系
| (灰度總開關(guān))開 (灰度流量開關(guān))關(guān) | 灰度節(jié)點(diǎn)發(fā)版,新版本驗(yàn)證階段 | 舊版本體驗(yàn) | 新版本體驗(yàn) |
| (灰度總開關(guān))開 (灰度流量開關(guān))開 | 正常節(jié)點(diǎn)發(fā)版,新版本批量部署階段 | 新版本體驗(yàn) | 新版本體驗(yàn) |
| (灰度總開關(guān))關(guān) (灰度流量開關(guān))開/關(guān) | 新版本完成上線 | 新版本體驗(yàn) | 新版本體驗(yàn) |
灰度配置 Gray Properties

用戶白名單:
- 節(jié)點(diǎn)清單加載可以從eureka獲取
????????//本地配置的服務(wù)map
????????Map?servicesLocalMap?=?getServicesLocalMap();//要返回的服務(wù)清單
????????List?services?=?new?ArrayList();
????????discoveryClient.getServices().forEach(service?->?{final?List?instances?=?new?ArrayList();
????????????discoveryClient.getInstances(service).forEach(instanceInfo?->?{
????????????????instances.add(toInstance(instanceInfo));
????????????});//優(yōu)先使用本地實(shí)例if?(null?!=?servicesLocalMap.get(service))?{final?List?serviceLocalInstances?=?servicesLocalMap.get(service).getData();//更新狀態(tài)
????????????????List?serviceLocalInstancesHasLatestStatus?=?serviceLocalInstances.stream()
????????????????????????.map(instanceLocal?->?instances.stream()
????????????????????????????????????????.filter(instance?->?StringUtils.join(instance.getHost(),?instance.getPort().toString()).equals(StringUtils.join(instanceLocal.getHost(),?instanceLocal.getPort().toString())))
????????????????????????????????????????.findFirst().map(m?->?{
????????????????????????????????????????????instanceLocal.setStatus(m.getStatus());return?instanceLocal;
????????????????????????????????????????})//????????????????????????????????????????.orElse(null)
????????????????????????????????????????.orElseGet(()?->?{
????????????????????????????????????????????instanceLocal.setStatus("OFFLINE");return?instanceLocal;
????????????????????????????????????????})
????????????????????????).filter(Objects::nonNull).collect(Collectors.toList());//去除eureka中本地已配置的實(shí)例
????????????????List?instancesRemoveLocal?=?instances.stream().filter(instance?->?!serviceLocalInstancesHasLatestStatus.stream()
????????????????????????.anyMatch(instanceLocal?->?StringUtils.join(instance.getHost(),?instance.getPort().toString()).equals(StringUtils.join(instanceLocal.getHost(),?instanceLocal.getPort().toString()))))
????????????????????????.collect(Collectors.toList());//清空并重新添加處理過的本地和遠(yuǎn)程實(shí)例
????????????????instances.clear();
????????????????instances.addAll(instancesRemoveLocal);
????????????????instances.addAll(serviceLocalInstancesHasLatestStatus);//本地服務(wù)實(shí)例排除已經(jīng)添加的服務(wù)
????????????????servicesLocalMap.remove(service);
????????????}//排序
????????????Collections.sort(instances);//單個服務(wù)和節(jié)點(diǎn)添加
????????????Service?serviceResp?=?new?Service();
????????????serviceResp.setService(service);
????????????serviceResp.setData(instances);
????????????services.add(serviceResp);
????????});//添加eureka中不存在,本地存在的服務(wù)
????????servicesLocalMap.values().forEach(service->{
????????????Collections.sort(service.getData());
????????????services.add(service);
????????});
????????Collections.sort(services);return?ResultCode.SUCCESS.withData(services);
????}
依賴 Gray Dependency
<dependency>
????<groupId>com.sfgroupId>
????<artifactId>cloud-discovery-service-starterartifactId>
????<version>0.0.1-SNAPSHOTversion>
dependency>
<dependency>
????<groupId>com.sfgroupId>
????<artifactId>cloud-discovery-gateway-starterartifactId>
????<version>0.0.1-SNAPSHOTversion>
dependency>
<plugin>
????<groupId>com.sfgroupId>
????<artifactId>maven-pluginartifactId>
????<version>0.0.1-SNAPSHOTversion>
????<executions>
????????<execution>
????????????<phase>compilephase>
????????????<goals>
????????????????<goal>gray-plugingoal>
????????????goals>
????????????<configuration>
????????????????<grayBuildLocationExclude>grayBuildLocationExclude>
????????????configuration>
????????execution>
????executions>
plugin>
灰度頭部 GrayHeader
灰度頭部信息信息,編碼支持的類有 < GrayHeader,GrayHeaderConstant,GrayUtil, ServiceGrayUtil >
灰度對象存放的上下文有:自定義實(shí)現(xiàn)類 < GrayHeaderHolder >,Request內(nèi)置實(shí)現(xiàn)類 < RequestContextHolder >
| h-gray-is | true/false | 是否為灰度流量 |
| h-gray-domain | 域名 | 用戶請求的域名 |
| h-gray-userid | xxx | 用戶請求的賬戶 |
技術(shù)改造點(diǎn)
灰度改造分三大類:網(wǎng)關(guān)改造,服務(wù)改造,場景改造。主要目的是實(shí)現(xiàn)灰度頭部計算、復(fù)用、續(xù)傳,負(fù)載均衡的改造。
網(wǎng)關(guān)改造 cloud-discovery-gateway-starter
修改pom.xml依賴
<dependency>????<groupId>com.nepxiongroupId>
????<artifactId>discovery-plugin-starter-eurekaartifactId>
????<version>0.0.2-SNAPSHOTversion>
dependency>
<dependency>
????<groupId>com.nepxiongroupId>
????<artifactId>discovery-plugin-strategy-starter-gatewayartifactId>
????<version>0.0.2-SNAPSHOTversion>
dependency>
configure加載項(xiàng)
@Configurationpublic?class?DiscoveryGatewayAutoConfiguration?{
????//負(fù)載均衡改造注入
????@Bean
????public?DiscoveryEnabledAdapter?discoveryEnabledAdapter()?{
????????return?new?GatewayGrayDiscoveryEnabledAdapter();
????}
????//灰度路由計算注入
????@Bean
????public?GrayRouteFlagFilter?grayRouteFilter()?{
????????return?new?GrayRouteFlagFilter();
????}
????//灰度配置獲取注入
????@Bean
????public?GrayPropertiesLoader?grayPropertiesLoader()?{
????????return?new?GrayPropertiesLoader(gatewayRedisson);
????}
}
[路由場景]增加header
在網(wǎng)關(guān)新增Filter,將request上下文和灰度配置匹配,算出灰度路由標(biāo)記
public?class?GrayRouteFlagFilter?implements?GlobalFilter,?Ordered?{????@Autowired
????private?GrayPropertiesLoader?grayPropertiesLoader;
????@Override
????public?Mono?filter(ServerWebExchange?exchange,?GatewayFilterChain?chain)?{
????????try?{
????????????ServerHttpRequest?request?=?exchange.getRequest();
????????????Boolean?isGray?=?grayPropertiesLoader.calculateGrayFlag(request.getHeaders().getFirst(GrayHeaderConstant.GRAY_IS),
????????????????????request.getHeaders().getFirst(GrayHeaderConstant.GRAY_NGINX_IP),
????????????????????request.getHeaders().getFirst(GrayHeaderConstant.GRAY_USER_ACCOUNT));
????????????request?=?request.mutate().header(GrayHeaderConstant.GRAY_IS,?isGray.toString()).build();
????????????exchange?=?exchange.mutate().request(request).build();
????????}catch?(Throwable?e){
????????????log.error("未知錯誤",e);
????????}finally?{
????????????return?chain.filter(exchange);
????????}
????}
}
[路由場景]改造feign負(fù)載均衡實(shí)現(xiàn)節(jié)點(diǎn)篩選
改造負(fù)載均衡,計算灰度路由標(biāo)記,灰度開關(guān)和節(jié)點(diǎn)清單的匹配性,篩選出符合條件的節(jié)點(diǎn)
public?class?GatewayGrayDiscoveryEnabledAdapter?extends?DefaultDiscoveryEnabledAdapter?{????????@Autowired
????????private?GrayPropertiesLoader?grayPropertiesLoader;
????????public?boolean?apply(Server?server)?{
????????????if?(!this.applyVersion(server))?{
????????????????return?false;
????????????}?
????????}
????????private?boolean?applyVersion(Server?server)?{
????????????//判斷灰度總開關(guān)
????????????if(!Boolean.valueOf(grayPropertiesLoader.get().getEnable())){
????????????????return?true;
????????????}
????????????Boolean?isGray?=?Boolean.valueOf(strategyContextHolder.getHeader(GrayHeaderConstant.GRAY_IS));
????????????try{
????????????????return?GrayLoadBalanceUtil.isServerMatch(this.pluginAdapter.getServerServiceId(server),?isGray,?server.getHost(),?server.getPort());
????????????}catch(Exception?e){
????????????????return?true;
????????????}
????????}
????}
服務(wù)改造 cloud-discovery-service-starter
修改pom.xml依賴
<dependency>????<groupId>com.nepxiongroupId>
????<artifactId>discovery-plugin-starter-eurekaartifactId>
????<version>0.0.2-SNAPSHOTversion>
dependency>
<dependency>
????<groupId>com.nepxiongroupId>
????<artifactId>discovery-plugin-strategy-starter-serviceartifactId>
????<version>0.0.2-SNAPSHOTversion>
dependency>
configure加載項(xiàng)
@Configurationpublic?class?DiscoveryServiceAutoConfiguration?{
????@Autowired
????Redisson?getRedisson;
?//灰度負(fù)載均衡注入
????@Bean
????public?DiscoveryEnabledAdapter?discoveryEnabledAdapter()?{
????????return?new?ServiceGrayDiscoveryEnabledAdapter();
????}????
????//Feign灰度頭部續(xù)傳改造注入
????@Bean
????public?FeignStrategyInterceptor?feignStrategyInterceptor()?{
????????return?new?ServiceFeignStrategyInterceptor(GrayHeaderConstant.STRATEGY_REQUEST_HEADERS);
????}
?//灰度配置獲取注入
????@Bean
????public?GrayPropertiesLoader?grayPropertiesLoader()?{
????????return?new?GrayPropertiesLoader(getRedisson);
????}
?//restTemplate灰度頭部續(xù)傳改造注入
????@Bean
????public?RestTemplateStrategyInterceptor?restTemplateStrategyInterceptor()?{
????????return?new?ServiceRestTemplateStrategyInterceptor(GrayHeaderConstant.STRATEGY_REQUEST_HEADERS);
????}
????//線程池灰度頭部續(xù)傳改造注入
????@Bean
????public?RequestContextDecorator?requestContextDecorator()?{
????????return?new?RequestContextDecorator();
????}
}
- [feign同步調(diào)用場景]改造feign負(fù)載均衡實(shí)現(xiàn)節(jié)點(diǎn)篩選 < ServiceGrayDiscoveryEnabledAdapter >
????????@Autowired
????????private?GrayPropertiesLoader?grayPropertiesLoader;
????????public?boolean?apply(Server?server)?{
????????????if?(!this.applyVersion(server))?{
????????????????return?false;
????????????}
????????????return?true;
????????}
????????private?boolean?applyVersion(Server?server)?{
????????????//灰度開關(guān)是否開啟
????????????if(!Boolean.valueOf(grayPropertiesLoader.get().getEnable())){
????????????????return?true;
????????????}
????????????Boolean?isGray?=?Boolean.valueOf(ServiceGrayUtil.getGrayHeaderFromContext().getIsGray());
????????????try{
????????????????return?GrayLoadBalanceUtil.isServerMatch(this.pluginAdapter.getServerServiceId(server),?isGray,?server.getHost(),?server.getPort());
????????????}catch(Exception?e){
????????????????return?true;
????????????}
????????}
}
- [restTemplate調(diào)用場景]支持header續(xù)傳 < ServiceRestTemplateStrategyInterceptor >
????public?ClientHttpResponse?intercept(HttpRequest?request,?byte[]?body,?ClientHttpRequestExecution?execution)?throws?IOException?{
????????this.applyOuterHeader(request);
????????return?execution.execute(request,?body);
????}
????private?void?applyOuterHeader(HttpRequest?request)?{
????????HttpHeaders?headers?=?request.getHeaders();
????????GrayUtil.toMap(ServiceGrayUtil.getGrayHeaderFromContext()).forEach((key,?value)->{
????????????if(null==headers.get(key)){
????????????????headers.add(key,?value);
????????????}
????????});
????}
}
場景改造
線程池灰度改造 ThreadPoolTaskExecutor
對于線程池的使用請使用封裝過的taskExecutor,這樣就自動實(shí)現(xiàn)了跨線程的頭部續(xù)傳,裝飾類為: < RequestContextDecorator >
public class RequestContextDecorator implements TaskDecorator {@Override
public Runnable decorate(Runnable runnable) {
try{
RequestAttributes context = RequestContextHolder.currentRequestAttributes();
GrayHeader grayHeader = ServiceGrayUtil.getGrayHeaderFromContext();
log.debug(" --- gray debug: THREAD parent:{}", Thread.currentThread().getName());
return () -> {
try {
try {
RequestContextHolder.setRequestAttributes(context);
GrayUtil.setGrayHeader2Local(grayHeader);
log.debug(" --- gray debug: THREAD child:{}", Thread.currentThread().getName());
}catch(Exception e){
log.error("跨線程傳遞變量出錯:", e);
}
runnable.run();
}catch(Exception e){
log.error("定時任務(wù)執(zhí)行出錯:", e);
}finally{
RequestContextHolder.resetRequestAttributes();
GrayUtil.removeGrayHeader();
}
};
}catch(Exception e){
log.debug("線程裝飾出錯:", e);
}
return () -> {
runnable.run();
};
}
}
- 使用場景
@Resource
ThreadPoolTaskExecutor?taskExecutor;
//場景1,異步注解方式
*?@Async("taskExcutor")
//場景2,異步調(diào)用
taskExcutor.submit(task);
//場景3,異步回調(diào)方式
*?CompletableFuture?userAccountCompletableFuture?=?CompletableFuture.completedFuture(null)
????????????????.thenApplyAsync(dummy?->?{...},?taskExcutor);
自定義executor注意添加setTaskDecorator(new?RequestContextDecorator())private?ThreadPoolTaskExecutor?executorService;@PostConstructpublic?void?init()?{
????????executorService?=?new?ThreadPoolTaskExecutor();
????????...//此處添加了灰度header續(xù)傳
????????executorService.setTaskDecorator(new?RequestContextDecorator());
????????executorService.initialize();
????}
定時任務(wù)改造 Job
- 需要考慮支持的定時任務(wù)調(diào)度模式模式
全隨機(jī):從所有的節(jié)點(diǎn)選一個執(zhí)行任務(wù) 灰度隨機(jī):從灰度節(jié)點(diǎn)中選一個執(zhí)行任務(wù) 并行隨機(jī):從非灰度節(jié)點(diǎn)中選一個執(zhí)行任務(wù),同時從灰度節(jié)點(diǎn)中選一個執(zhí)行任務(wù) 非灰度隨機(jī):從非灰度節(jié)點(diǎn)中選一個執(zhí)行任務(wù)
- 以Quartz Job Scheduling分布式任務(wù)為例,其他xxl-job可參考此方案改造
public class QuartzJobRunner implements Job {
@Override ? ?public final void execute(JobExecutionContext context) throws JobExecutionException { ? ? //對不同的調(diào)度模式執(zhí)行不同的負(fù)載均衡算法選擇節(jié)點(diǎn),可參考附件工具類 ? ? GrayLoadBalanceUtil.getServerUrl(String serviceName, Boolean isGray, String grayDomain, String grayUserid) ? ?} }
消息隊(duì)列改造 Queue
統(tǒng)一標(biāo)準(zhǔn)的消息隊(duì)列結(jié)構(gòu)封裝,帶入GrayHeader信息。
統(tǒng)一改造封裝消費(fèi)方,如果GrayHeader信息與消費(fèi)方機(jī)器匹配才做消費(fèi)。
遺留問題
第三方組件未做隔離帶來的問題
因此方案從節(jié)約成本的考慮仍然共用redis,db等第三方組件,如開發(fā)過程中應(yīng)盡量避免第三方存儲的數(shù)據(jù)結(jié)構(gòu)發(fā)生變更,需要評估新舊版本是否會存在差異導(dǎo)致服務(wù)不可用來決定是否停服維護(hù)。
跨平臺的灰度服務(wù)改造探討
- 對于spring cloud體系發(fā)起調(diào)用時,統(tǒng)一按灰度規(guī)則改造Http Client實(shí)現(xiàn)灰度負(fù)載均衡,實(shí)現(xiàn)GrayHeader傳遞
- 對spring cloud體系接收調(diào)用時,統(tǒng)一規(guī)范第三方填入GrayHeader信息
- 對于通過消息隊(duì)列交互的第三方,可定制A,B topic輪換策略來實(shí)現(xiàn)灰度發(fā)版
附錄:
灰度工具類代碼
- GrayUtil,ServiceGrayUtil提供了一系列工具類,如獲取灰度上下文,構(gòu)造灰度上下文,設(shè)置灰度上下文到下一個請求等
GrayHeader?ServiceGrayUtil.getGrayHeaderFromContext()??
/**?跨線程的灰度頭部續(xù)傳范例?**/
GrayUtil.snapshotGrayHeaderProperties();
...
GrayUtil.recoverGrayHeaderProperties(new?HttpHeaders(),?grayHeaderMap));
GrayUtil
public?class?GrayUtil1?{????public?static?List?requestHeaderList?=?StringUtil.splitToList(GrayHeaderConstant.STRATEGY_REQUEST_HEADERS.toLowerCase(),?";");/**
?????*?此方法只適用于網(wǎng)關(guān)入口處獲取灰度頭部,不能在續(xù)傳過程中使用,如續(xù)傳使用請參見ServiceGrayUtil.getGrayHeaderFromContext()
?????*?從頭部獲取灰度灰度參數(shù)對象,指定grayUserid覆蓋頭部中的參數(shù)
?????*?此方法不會重用header:is-gray標(biāo)記,只會重新計算。
?????*?@param?headers
?????*?@param?grayUserid
?????*/public?static?GrayHeader?getGrayHeaderFromHeader(HttpHeaders?headers,?String?grayUserid){
????????GrayHeader?grayHeader?=?new?GrayHeader();if(StringUtils.isEmpty(grayUserid)){
????????????grayUserid?=?headers.getFirst(GrayHeaderConstant.GRAY_USER_ACCOUNT);
????????}
????????GrayPropertiesLoader?grayPropertiesLoader?=?GrayContextUtil.getBean(GrayPropertiesLoader.class);/**
?????????*?無法獲取grayUserid和grayDomain的情況下,從緩存灰度配置拿灰度flag:滿足多數(shù)用戶的流量走向
?????????*/if(null!=?grayPropertiesLoader){
????????????grayHeader?=?grayPropertiesLoader.calculateGrayHeader(null,?headers.getFirst(GrayHeaderConstant.GRAY_NGINX_IP),?grayUserid);
????????}return?grayHeader;
????}/**
?????*?設(shè)置當(dāng)前header的灰度頭部信息
?????*?@param?headers
?????*?@param?grayHeader
?????*/public?static?HttpHeaders?appendGrayHeader(HttpHeaders?headers,?GrayHeader?grayHeader){try{
????????????Iterator>?iter?=?toMap(grayHeader).entrySet().iterator();while(iter.hasNext()){
????????????????Map.Entry?row?=?iter.next();if(!headers.containsKey(row.getKey())){
????????????????????headers.add(row.getKey(),?row.getValue());
????????????????}
????????????}return?headers;
????????}catch(Exception?e){
????????????log.error("恢復(fù)灰度頭部信息失敗",?e);
????????}return?headers;
????}/**
?????*
?????*?@param?headersJson?request?header?json
?????*?@param?grayHeaderString?gray??header?json
?????*?@return
?????*/public?static?String?appendGrayHeader(String?headersJson,?String?grayHeaderString){try{
????????????JSONObject?headParamObj?=?new?JSONObject();if(StringUtils.isNotEmpty(headersJson)){
????????????????headParamObj?=?JSONObject.parseObject(headersJson);
????????????}
????????????JSONObject?grayHeaderJson?=?new?JSONObject();if(StringUtils.isNotEmpty(grayHeaderString)){
????????????????grayHeaderJson?=?JSONObject.parseObject(grayHeaderString);
????????????}
????????????Iterator>?iter?=?grayHeaderJson.entrySet().iterator();while(iter.hasNext()){
????????????????JSONObject.Entry?row?=?iter.next();if(StringUtils.isEmpty(headParamObj.getString(row.getKey()))){
????????????????????headParamObj.put(row.getKey(),?row.getValue());
????????????????}
????????????}return??headParamObj.toJSONString();
????????}catch(Exception?e){
????????????log.error("設(shè)置恢復(fù)灰度頭部信息失敗",?e);
????????}return?headersJson;
????}/**
?????*?把grayHeader寫入到當(dāng)前線程local(適合消息隊(duì)列等通過消息接收到grayHeader信息后寫入threadlocak,提供給restTemplate發(fā)起時抓取并塞入頭部)
?????*?@param?grayHeader
?????*/public?static?void?setGrayHeader2Local(GrayHeader?grayHeader){
????????GrayHeaderHolder.setGrayContext(grayHeader);
????}/**
?????*?清除threadLocal存儲的灰度路由信息
?????*/public?static?void?removeGrayHeader(){
????????GrayHeaderHolder.removeGrayContext();
????}/**
?????*?Gray頭部對象轉(zhuǎn)map
?????*?@param?grayHeader
?????*?@return
?????*/public?static?Map?toMap(GrayHeader?grayHeader){
????????Map?res?=?new?HashMap();if(null!=grayHeader.getIsGray()){
????????????res.put(GrayHeaderConstant.GRAY_IS,?grayHeader.getIsGray().toString());
????????}if(null!=grayHeader.getGrayUserid()){
????????????res.put(GrayHeaderConstant.GRAY_USER_ACCOUNT,?grayHeader.getGrayUserid());
????????}if(null!=grayHeader.getGrayDomain()){
????????????res.put(GrayHeaderConstant.GRAY_NGINX_IP,?grayHeader.getGrayDomain());
????????}return?res;
????}/**
?????*?map中獲取grayHeader對象
?????*?@param?map
?????*?@return
?????*/public?static?GrayHeader?toGrayHeader(Map?map){
????????GrayHeader?grayHeader?=?new?GrayHeader();if(null!=map.get(GrayHeaderConstant.GRAY_IS)){
????????????grayHeader.setIsGray(Boolean.valueOf(map.get(GrayHeaderConstant.GRAY_IS)));
????????}if(null!=map.get(GrayHeaderConstant.GRAY_USER_ACCOUNT)){
????????????grayHeader.setGrayUserid((String)map.get(GrayHeaderConstant.GRAY_USER_ACCOUNT));
????????}if(null!=map.get(GrayHeaderConstant.GRAY_NGINX_IP)){
????????????grayHeader.setGrayDomain((String)map.get(GrayHeaderConstant.GRAY_NGINX_IP));
????????}return?grayHeader;
????}public?static?boolean?isHeaderContains(String?headerName)?{return?headerName.startsWith("n-d-")?||?requestHeaderList.contains(headerName);
????}public?static?boolean?isHeaderContainsExcludeInner(String?headerName)?{return?isHeaderContains(headerName)?;
????}
}
ServiceGrayUtil
public?class?ServiceGrayUtil?{????/**
?????*?獲取當(dāng)前header的灰度頭部信息,優(yōu)先從discoveryRequest上下文獲取,如果抓不到則從threadLocal獲取
?????*?@return
?????*/
????public?static?GrayHeader?getGrayHeaderFromContext(){
????????String?grayDomain?=?null;
????????String?grayUserid?=?null;
????????GrayHeader?grayHeader?=?new?GrayHeader();
????????try{
????????????/**
?????????????*?從request上下文獲取
?????????????*/
????????????Map?grayHeaderMap?=?new?HashMap();
????????????ServiceStrategyContextHolder?serviceStrategyContextHolder?=?GrayContextUtil.getBean(ServiceStrategyContextHolder.class);
????????????ServiceStrategyRouteFilter?serviceStrategyRouteFilter?=?GrayContextUtil.getBean(ServiceStrategyRouteFilter.class);
????????????ServletRequestAttributes?attributes?=?serviceStrategyContextHolder.getRestAttributes();if?(attributes?!=?null)?{
????????????????HttpServletRequest?previousRequest?=?attributes.getRequest();
????????????????Enumeration?headerNames?=?previousRequest.getHeaderNames();if?(headerNames?!=?null)?{
????????????????????String?routeRegionWeight;while?(headerNames.hasMoreElements())?{
????????????????????????routeRegionWeight?=?(String)?headerNames.nextElement();
????????????????????????String?headerValue?=?previousRequest.getHeader(routeRegionWeight);boolean?isHeaderContains?=?GrayUtil.isHeaderContainsExcludeInner(routeRegionWeight.toLowerCase());if?(isHeaderContains)?{
????????????????????????????grayHeaderMap.put(routeRegionWeight,?headerValue);
????????????????????????}
????????????????????}
????????????????}
????????????}
????????????grayHeader?=?GrayUtil.toGrayHeader(grayHeaderMap);
????????????grayDomain?=?null==grayDomain?grayHeader.getgrayDomain():grayDomain;
????????????grayUserid?=?null==grayUserid?grayHeader.getgrayUserid():grayUserid;if(null!=grayHeader.getIsGray()){
????????????????log.debug("?---?gray?debug:從request頭部獲取灰度信息成功:{}",?JsonUtil.toJson(grayHeader));return?grayHeader;
????????????}
????????}catch(Exception?e){
????????????log.debug("從request頭部獲取灰度信息失敗",?e);
????????}try{/**
?????????????*?從threadlocal獲取:如果request上下文未獲取到isGray標(biāo)記則進(jìn)入
?????????????*/if(null!=?GrayHeaderHolder.getGrayContext()){
????????????????grayHeader?=?GrayHeaderHolder.getGrayContext();
????????????????grayDomain?=?null==grayDomain?grayHeader.getgrayDomain():grayDomain;
????????????????grayUserid?=?null==grayUserid?grayHeader.getgrayUserid():grayUserid;
????????????}if(null!=grayHeader.getIsGray()){
????????????????log.debug("?---?gray?debug:從threadlocal獲取灰度信息成功:{}",?JsonUtil.toJson(grayHeader));return?grayHeader;
????????????}
????????}catch(Exception?e){
????????????log.debug("從threadlocal獲取灰度信息失敗",?e);
????????}try{
????????????GrayPropertiesLoader?grayPropertiesLoader?=?GrayContextUtil.getBean(GrayPropertiesLoader.class);/**
?????????????*?無法獲取grayUserid和grayDomain的情況下,從緩存灰度配置拿灰度flag:滿足多數(shù)用戶的流量走向
?????????????*/if(null!=?grayPropertiesLoader){
????????????????grayHeader?=?grayPropertiesLoader.calculateGrayHeader(null,?grayDomain,?grayUserid);
????????????}if(null!=grayHeader.getIsGray()){
????????????????log.debug("?---?gray?debug:本地上下文獲取灰度信息失敗,從配置計算灰度信息信息成功:{}?,?grayDomain:{},?grayUserid:{}",?JsonUtil.toJson(grayHeader),grayDomain,grayUserid?);return?grayHeader;
????????????}
????????}catch(Exception?e){
????????????log.debug("從配置獲取灰度標(biāo)志失敗",?e);
????????}return?grayHeader;
????}public?static?String?getGrayHeaderStrFromContext(){return??JsonUtil.toJson(GrayUtil.toMap(getGrayHeaderFromContext()));
????}
}
GrayLoadBalanceUtil
public?class?GrayLoadBalanceUtil?{????public?static?GrayLoadBalanceUtil1?grayLoadBalanceUtil;
????@Autowired
????private?DiscoveryClient?discoveryClient;
????@Autowired
????private?Registration?registration;
????@Autowired
????private?GrayPropertiesLoader?grayPropertiesLoader;
????private?static?AntPathMatcher?matcher?=?new?AntPathMatcher();
????@PostConstruct
????public?void?init()?{
????????grayLoadBalanceUtil?=?this;
????}
????public?static?String?getServerUrl(String?serviceName,?Boolean?isGray,?String?grayDomain,?String?grayUserid){
????????List?serviceInstancesList?=?grayLoadBalanceUtil.discoveryClient.getInstances(serviceName);if(null!=serviceInstancesList){for(ServiceInstance?serviceInstance:?serviceInstancesList){try{//計算isGray的過濾器在此入口之后,所以這里自己計算一遍isGray
????????????????????isGray?=?grayLoadBalanceUtil.grayPropertiesLoader.calculateGrayFlag(null==isGray?null:isGray.toString(),?grayDomain,?grayUserid);if(isServerMatch(serviceInstance.getServiceId(),?isGray,?serviceInstance.getHost(),?serviceInstance.getPort())){return?serviceInstance.getUri().toString();
????????????????????}
????????????????}catch(Exception?e){
????????????????????log.info("?---?gray?inf:?獲取版本或者服務(wù)名失敗?>?{}?",?JsonUtil.toJson(serviceInstance));return?serviceInstance.getUri().toString();
????????????????}
????????????}
????????}
????????log.error("自定義負(fù)載均衡匹配失敗,無法繼續(xù)請求服務(wù):{},?{}",?serviceName,?isGray);return?"";
????}/**
?????*?當(dāng)前節(jié)點(diǎn)是否被選中
?????*?@param?isGray?路由模式,true為灰度路由
?????*?@param?serviceName?服務(wù)名稱
?????*?@return
?????*/public?static?Boolean?isServerMatch(String?serviceName,?Boolean?isGray,?String?host,?Integer?port){if(!grayLoadBalanceUtil.grayPropertiesLoader.get().getGrayServiceMap().containsKey(serviceName)){//該服務(wù)未配置灰度節(jié)點(diǎn)則匹配第一個return?true;
????????}else{//該服務(wù)配置了灰度節(jié)點(diǎn)則需要匹配return?(isGray?&&?isGrayNode(host,port))?||?(!isGray?&&?!isGrayNode(host,port));
????????}
????}/**
?????*?匹配版本規(guī)則
?????*?@param?pattern
?????*?@param?value
?????*?@return
?????*/private?static?boolean?match(String?pattern,?String?value)?{return?GrayLoadBalanceUtil1.matcher.match(pattern,?value);
????}/**
?????*?獲取當(dāng)前服務(wù)
?????*?@return
?????*/public?static?ServiceInstance?getLocalServiceInstance()?{return?grayLoadBalanceUtil.registration;
????}/**
?????*?當(dāng)前節(jié)點(diǎn)是否為金絲雀節(jié)點(diǎn)
?????*?@return
?????*/public?static?Boolean?isCurrentGrayNode(){return?isGrayNode(grayLoadBalanceUtil.registration.getHost(),?grayLoadBalanceUtil.registration.getPort());
????}public?static?Boolean?isGrayNode(String?host,?Integer?port){try{return?grayLoadBalanceUtil.grayPropertiesLoader.get().getGrayInstanceMap().containsKey(StringUtils.join(host,?":",port));
????????}catch(Exception?e){
????????????log.error("?---?gray?debug:??node?judge??獲取當(dāng)前節(jié)點(diǎn)是否為灰度節(jié)點(diǎn)失敗",?e);
????????}return?false;
????}
}
GrayHeaderHolder
public class GrayHeaderHolder {private static final InheritableThreadLocal grayContextLocal = new InheritableThreadLocal<>();
public static GrayHeader getGrayContext(){
return grayContextLocal.get();
}
public static void setGrayContext(GrayHeader grayHeader){
grayContextLocal.set(grayHeader);
}
public static void removeGrayContext(){
grayContextLocal.remove();
}
}
GrayProperties
public class GrayProperties implements Serializable {public GrayProperties(){
}
public GrayProperties(Boolean enable,Boolean online, String userWhitelist,String frontNginxipWhitelist, String userWhitelistRemark){
this.enable = enable;
this.online = online;
this.userWhitelist = userWhitelist;
this.frontNginxipWhitelist = frontNginxipWhitelist;
this.userWhitelistRemark = userWhitelistRemark;
}
//金絲雀路由啟用總開關(guān)
private Boolean enable = false;
//金絲雀節(jié)點(diǎn)匹配流量控制開關(guān)true為導(dǎo)流全部流量,false按service金絲雀節(jié)點(diǎn)匹配規(guī)則導(dǎo)流量
private Boolean online = false;
//用戶金絲雀路由條件----客戶端用戶白名單
private String userWhitelist;
//用戶金絲雀路由條件----客戶端用戶白名單備注
private String userWhitelistRemark;
//用戶金絲雀路由條件----前端nginxip白名單(具體參數(shù)為最新前端代碼統(tǒng)一部署的負(fù)載均衡ip)
private String frontNginxipWhitelist;
private Map userWhitelistMap = null;
private Map frontNginxipWhitelistMap = null;
private GrayProductVersion productVersion;
/**
* 金絲雀服務(wù)清單
*/
private List servicesLocal;
//金絲雀節(jié)點(diǎn)清單
private Map grayInstanceMap = null;
//存在金絲雀節(jié)點(diǎn)的服務(wù)清單
private Map grayServiceMap = null;
@PostConstruct
public void initMap() {
userWhitelistMap = transfer2Map(userWhitelist);
frontNginxipWhitelistMap = transfer2Map(frontNginxipWhitelist);
grayInstanceMap = new HashMap();
grayServiceMap = new HashMap();
if(null!=servicesLocal && servicesLocal.size()>0){
servicesLocal.forEach(serviceLocal->{
if(null!=serviceLocal.getData() && serviceLocal.getData().size()>0){
grayServiceMap.put(serviceLocal.getService(), "");
serviceLocal.getData().forEach(instanceLocal->{
grayInstanceMap.put(StringUtils.join(instanceLocal.getHost(),":", instanceLocal.getPort()), "");
});
}
});
}
}
/**
* 轉(zhuǎn)換逗號分隔的字符串到map, value為null
* @param str
* @return
*/
Map transfer2Map(String str){
Map map = new HashMap();
if(StringUtils.isNotEmpty(str)){
for(String key: str.trim().split(",")){
map.put(key.toLowerCase(), null);
}
}
return map;
}
}
GrayPropertiesLoader
public class GrayPropertiesLoader {public static String GRAY_PROPERTIES_SYNC_TOPIC = "gray-properties-topic";
public static String GRAY_PROPERTIES_KEY = "GLOBAL:CONFIG:GRAY_PROPERTIES";
Redisson redisson;
GrayProperties grayProperties = null;
public GrayPropertiesLoader(Redisson redisson){
this.redisson = redisson;
}
/**
* 本地獲取配置
*/
public GrayProperties get(){
if(null!=grayProperties){
return grayProperties;
}else{
log.debug(" --- gray debug: PROPERTIES > {}", "未加載到配置本地緩存,開始從遠(yuǎn)程獲取");
return load();
}
}
/**
* 從緩存或db加載配置
* @return
*/
private synchronized GrayProperties load(){
//緩存獲取
GrayProperties cacheGrayProperties = null;
try{
RBucket bucket = redisson.getBucket(GrayPropertiesLoader.GRAY_PROPERTIES_KEY);
cacheGrayProperties = bucket.get();
}catch(Exception e){
log.error("從redis獲取金絲雀配置信息失敗", e);
}
if(null!=cacheGrayProperties){
cacheGrayProperties.initMap();
grayProperties = cacheGrayProperties;
log.debug(" --- gray debug: PROPERTIES > {}, {}", "從緩存加載:", JsonUtil.toJson(grayProperties));
return grayProperties;
}else{
//數(shù)據(jù)庫獲取(暫不實(shí)現(xiàn),redis重啟后需要修改配置來加載)
log.debug(" --- gray debug: PROPERTIES > {}", "未加載到配置緩存,跳過數(shù)據(jù)庫加載");
//都沒有則,返回一個空的
return new GrayProperties();
}
}
/**
* 金絲雀灰度設(shè)置修改監(jiān)聽
*/
@PostConstruct
public void listener(){
RTopic topic = redisson.getTopic(GRAY_PROPERTIES_SYNC_TOPIC, new JsonJacksonCodec());
topic.addListener(GrayProperties.class, new MessageListener() {
@Override
public void onMessage(CharSequence charSequence, GrayProperties cacheGrayProperties) {
if(null!=cacheGrayProperties){
cacheGrayProperties.initMap();
grayProperties = cacheGrayProperties;
log.debug(" --- gray debug: PROPERTIES > {}, {}", "訂閱的配置更新了:", JsonUtil.toJson(grayProperties));
}
}
});
}
/**
* 發(fā)布配置修改
* @param grayProperties
*/
public void publish(GrayProperties grayProperties){
RTopic topic = redisson.getTopic(GRAY_PROPERTIES_SYNC_TOPIC, new JsonJacksonCodec());
topic.publish(grayProperties);
log.debug(" --- gray debug: PROPERTIES > {}, {}", "修改了配置并發(fā)布訂閱:", JsonUtil.toJson(grayProperties));
}
/**
* 根據(jù)頭部傳參判斷請求是否走灰度
* @param isGray
* @param nginxIp
* @param userAccount
* @return
*/
public Boolean calculateGrayFlag(String isGray, String nginxIp, String userAccount){
Boolean flag = false;
/**
* 保底方案1: nginxIp,userAccount為空會根據(jù)開關(guān)狀態(tài)來返回灰度標(biāo)記
*/
if(null!=isGray){
flag = Boolean.valueOf(isGray);
log.debug(" --- gray debug - 【金絲雀標(biāo)記】 - 優(yōu)先使用已有的金絲雀標(biāo)記:{}", flag);
return flag;
}else{
if(this.get().getEnable()){ //金絲雀全局開關(guān)開啟--金絲雀發(fā)版到上線階段
if(this.get().getOnline()){ //金絲雀上線開關(guān)開啟--金絲雀密集上線階段
flag = true;
log.debug(" --- gray debug - 【金絲雀標(biāo)記】 - 總開關(guān)開【開】,金絲雀開關(guān)【開】:{}", flag);
return flag;
}else{//金絲雀上線開關(guān)關(guān)閉--金絲雀驗(yàn)證階段
log.debug(" --- gray debug: USER > {} {}", nginxIp, userAccount);
if((null!=userAccount && this.get().getUserWhitelistMap().containsKey(userAccount)) || (null!=nginxIp && this.get().getFrontNginxipWhitelistMap().containsKey(nginxIp))){
flag = true;
log.debug(" --- gray debug - 【金絲雀標(biāo)記】 - 總開關(guān)開【開】,金絲雀開關(guān)【關(guān)】, 用戶和nginxip【匹配成功】:{}", flag);
return flag;
}
/**
* 保底方案2: 根據(jù)當(dāng)前節(jié)點(diǎn)是否為金絲雀節(jié)點(diǎn)來計算灰度
*/
flag = GrayLoadBalanceUtil.isCurrentGrayNode();
log.debug(" --- gray debug - 【金絲雀標(biāo)記】 - 總開關(guān)開【開】,金絲雀開關(guān)【關(guān)】, 用戶和nginxip【匹配失敗】,判斷當(dāng)前CLOUD節(jié)點(diǎn)是否為金絲雀節(jié)點(diǎn): {}", flag);
return flag;
}
}else{
log.debug(" --- gray debug - 【金絲雀標(biāo)記】 - 總開關(guān)開【關(guān)】:{}", flag);
}
}
return flag;
}
public GrayHeader calculateGrayHeader(String isGray, String nginxIp, String userAccount){
GrayHeader grayHeader = new GrayHeader();
grayHeader.setIsGray(calculateGrayFlag(isGray, nginxIp, userAccount));
if(null!=nginxIp){
grayHeader.setNginxIp(nginxIp);
}
if(null!=userAccount){
grayHeader.setUserAccount(userAccount);
}
return grayHeader;
}
}
GrayHeaderConstant
public class GrayHeaderConstant {public static String GRAY_IS = "cloudp-is-gray";
public static String GRAY_NGINX_IP= "nginxip";
public static String GRAY_USER_ACCOUNT= "user-account";
public static String STRATEGY_REQUEST_HEADERS="sf-domain;cloudp-is-gray;user-account;nginxip;access_token;authorization;refresh_token;redirectUrl;cloudType;Referer;back-pass";
}
GrayHeader
public?class?GrayHeader?implements?Serializable?{????public?GrayHeader(){
????}
????public?GrayHeader(Boolean?isGray,?String?nginxIp,?String?userAccount){
????????this.isGray?=?isGray;
????????this.nginxIp?=?nginxIp;
????????this.userAccount?=?userAccount;
????}
????//是否為灰度
????Boolean?isGray;
????//前端路由ip
????String?nginxIp;
????//用戶賬號
????String?userAccount;
}
自定義負(fù)載均衡算法參考
/*** 根據(jù)灰度規(guī)則在服務(wù)器節(jié)點(diǎn)中選擇節(jié)點(diǎn)
* @return
*/
private NodeCacheVO selectServer(ProviderCacheVO providerCacheVO, String isGray, String userAccount, String nginxIp){
NodeCacheVO nodeCacheVO = null;
GrayProperties grayProperties = grayPropertiesLoader.get();
/* 第①部分 */
//總開關(guān)關(guān)閉,則所有節(jié)點(diǎn)隨機(jī)跳轉(zhuǎn)
if(!grayProperties.getEnable()){
return providerCacheVO.randomNodeByWeightInAll();
}
/* 第②部分*/
Boolean gray = grayPropertiesLoader.calculateGrayFlag(isGray, nginxIp, userAccount);
if(gray){//走灰度
nodeCacheVO = providerCacheVO.randomNodeByWeightInCanary();
}else{//走正常節(jié)點(diǎn)
nodeCacheVO = providerCacheVO.randomNodeByWeightInStandard();
}
/* 第③部分*/
if(null==nodeCacheVO){//兜底:如果總開關(guān)開,灰度開關(guān)關(guān),找不到節(jié)點(diǎn)時在所有節(jié)點(diǎn)隨機(jī)選擇
nodeCacheVO = providerCacheVO.randomNodeByWeightInAll();
}
return nodeCacheVO;
}
灰度路由算法保底方案
- getGrayHeaderFromContext()實(shí)現(xiàn)原理
//①?GrayHeader從request上下文獲取(isGray有就直接取,userAccount/nginxIp有就傳遞下去)
//②?GrayHeader從GrayHeaderHolder的ThreadLocal中獲取(isGray有就直接取,userAccount/nginxIp有就傳遞下去)
//③?GrayHeader從GrayPropertiesLoader進(jìn)入重新計算灰度標(biāo)記模式
灰度標(biāo)記算法實(shí)現(xiàn)
GrayPropertiesLoader.calculateGrayFlag
實(shí)現(xiàn)了重新計算灰度標(biāo)記,其中采用了保底方案:保底方案1:計算時userAccout,nginxIp不存在則根據(jù)開關(guān)規(guī)則來判斷 保底方案2:則根據(jù)當(dāng)前節(jié)點(diǎn)是不是灰度節(jié)點(diǎn)來設(shè)置后續(xù)節(jié)點(diǎn)是不是走灰度
?部署方案對比(此小節(jié)來自網(wǎng)絡(luò))
藍(lán)綠部署
圖片來源知乎@稻草先生所謂藍(lán)綠部署,是指同時運(yùn)行兩個版本的應(yīng)用,如上圖所示,藍(lán)綠部署的時候,并不停止掉老版本,而是直接部署一套新版本,等新版本運(yùn)行起來后,再將流量切換到新版本上。但是藍(lán)綠部署要求在升級過程中,同時運(yùn)行兩套程序,對硬件的要求就是日常所需的二倍,比如日常運(yùn)行時,需要10臺服務(wù)器支撐業(yè)務(wù),那么使用藍(lán)綠部署,你就需要購置二十臺服務(wù)器。
「滾動發(fā)布」
滾動發(fā)布能夠解決掉藍(lán)綠部署時對硬件要求增倍的問題。
圖片來源知乎@稻草先生所謂滾動升級,就是在升級過程中,并不一下子啟動所有新版本,是先啟動一臺新版本,再停止一臺老版本,然后再啟動一臺新版本,再停止一臺老版本,直到升級完成,這樣的話,如果日常需要10臺服務(wù)器,那么升級過程中也就只需要11臺就行了。
但是滾動升級有一個問題,在開始滾動升級后,流量會直接流向已經(jīng)啟動起來的新版本,但是這個時候,新版本是不一定可用的,比如需要進(jìn)一步的測試才能確認(rèn)。那么在滾動升級期間,整個系統(tǒng)就處于非常不穩(wěn)定的狀態(tài),如果發(fā)現(xiàn)了問題,也比較難以確定是新版本還是老版本造成的問題。
為了解決這個問題,我們需要為滾動升級實(shí)現(xiàn)流量控制能力。
「灰度發(fā)布」
灰度發(fā)布也叫金絲雀發(fā)布,起源是,礦井工人發(fā)現(xiàn),金絲雀對瓦斯氣體很敏感,礦工會在下井之前,先放一只金絲雀到井中,如果金絲雀不叫了,就代表瓦斯?jié)舛雀摺?/p>圖片來源知乎@稻草先生
在灰度發(fā)布開始后,先啟動一個新版本應(yīng)用,但是并不直接將流量切過來,而是測試人員對新版本進(jìn)行線上測試,啟動的這個新版本應(yīng)用,就是我們的金絲雀。如果沒有問題,那么可以將少量的用戶流量導(dǎo)入到新版本上,然后再對新版本做運(yùn)行狀態(tài)觀察,收集各種運(yùn)行時數(shù)據(jù),如果此時對新舊版本做各種數(shù)據(jù)對比,就是所謂的A/B測試。
當(dāng)確認(rèn)新版本運(yùn)行良好后,再逐步將更多的流量導(dǎo)入到新版本上,在此期間,還可以不斷地調(diào)整新舊兩個版本的運(yùn)行的服務(wù)器副本數(shù)量,以使得新版本能夠承受越來越大的流量壓力。直到將100%的流量都切換到新版本上,最后關(guān)閉剩下的老版本服務(wù),完成灰度發(fā)布。
如果在灰度發(fā)布過程中(灰度期)發(fā)現(xiàn)了新版本有問題,就應(yīng)該立即將流量切回老版本上,這樣,就會將負(fù)面影響控制在最小范圍內(nèi)。
推薦實(shí)戰(zhàn)文章
Java實(shí)現(xiàn)圖片水印+壓縮So easy!分布式定時任務(wù)xxJob的常用姿勢都集齊了,So Easy!這個輪子讓SpringBoot實(shí)現(xiàn)api加密So Easy!實(shí)戰(zhàn):SpringBoot集成xxl-sso實(shí)現(xiàn)單點(diǎn)登錄神奇!不需要服務(wù)器,搭建免費(fèi)個人Blog,so easy!(待發(fā)布)分布式事務(wù)框架...總結(jié)
以上是生活随笔為你收集整理的基于spring cloud 的灰度发布实践_【收藏】基于spring cloud灰度发版方案的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python做毕业设计选题_软妹子的毕业
- 下一篇: python中break和continu