前端灰度方案

547 阅读1分钟

以下只涉及项目级别灰度

1、Nginx灰度

发布系统部署两套产物,通过nginx配置进行分流。 Nginx分流方式可以通过请求头信息判断,或者直接通过cookie存储是否灰度进行判断。通过cookie,nginx配置如下:

image.png

2、nginx + lua + redis

  • 当用户请求到达前段代理服务nginx,內嵌的lua模块解析nginx配置文件中的lua脚本代码

  • lua变量获取到客户端的ip地址,去查询redis缓存内是否有该建值,如果有返回值执行灰度版本逻辑,否则执行当前生产环境版本 nginx + lua + redis方案网上的资料也比较多

3、服务端渲染分流

实操:

1、服务端部署两套产物,eg. http://localhost:8081/, http://localhost:8082/ 2、node层实现分流逻辑,灵活性较强。分流规则可随意变动

eg. nest服务端代码:

@Get('/demo')  
public async getHtml(@Res() res: Response) {  
const grey = Math.random() * 10 < 5;  // 规则可任意定制,请求后端接口或BFF层内置
const url = grey ? 'http://localhost:8081/' : 'http://localhost:8082';  // 此处可以使用html,也可以使用入口manifest.json文件
const html = await this.request.get(url, {  
pureRequest: true,  
headers: {  
'content-type': 'text/html; charset=utf-8',  
},  
useResNestedFormat: true,  
});  
res.send(html);  
}

movie.gif

缺陷:

服务端压力较大

4、请求后端接口,代码处理,组件级别,if-else判断