spring-cloud-gateway动态路由的实现方法
概述
线上项目发布一般有以下几种方案:
- 机发布
- 蓝绿部署
- 滚动部署
- 灰度发布
停机发布 这种发布一般在夜里或者进行大版本升级的时候发布,因为需要停机,所以现在大家都在研究 devops
方案。
蓝绿部署 需要准备两个相同的环境。一个环境新版本,一个环境旧版本,通过负载均衡进行切换与回滚,目的是为了减少服务停止时间。
滚动部署 就是在升级过程中,并不一下子启动所有新版本,是先启动一台新版本,再停止一台老版本,然后再启动一台新版本,再停止一台老版本,直到升级完成。基于 k8s
的升级方案默认就是滚动部署。
灰度发布 也叫金丝雀发布,灰度发布中,常常按照用户设置路由权重,例如 90%的用户维持使用老版本,10%的用户尝鲜新版本。不同版本应用共存,经常与 a/b 测试一起使用,用于测试选择多种方案。
上边介绍的几种发布方案,主要是引出我们接下来介绍的 spring-cloud-gateway
动态路由,我们可以基于动态路由、负载均衡和策略加载去实现 灰度发布
。当然现在有很多开源的框架可以实现 灰度发布
,这里只是研究学习。
动态路由
spring-cloud-gateway
默认将路由加载在内存中。具体可以参见 inmemoryroutedefinitionrepository
类的实现。
这里我们基于 redis
实现动态路由。基础项目见 spring-cloud-gateway 简介
1. 将 actuator 的端点暴露出来。
management: endpoints: web: exposure: include: "*"
2. redis 配置
@configuration public class redisconfig { @bean(name = {"redistemplate", "stringredistemplate"}) public stringredistemplate stringredistemplate(redisconnectionfactory factory) { stringredistemplate redistemplate = new stringredistemplate(); redistemplate.setconnectionfactory(factory); return redistemplate; } }
3. 将原内存路由持久化到 redis
@component public class redisroutedefinitionrepository implements routedefinitionrepository { /** * hash存储的key */ public static final string gateway_routes = "gateway_dynamic_route"; @resource private stringredistemplate redistemplate; /** * 获取路由信息 * @return */ @override public flux<routedefinition> getroutedefinitions() { list<routedefinition> routedefinitions = new arraylist<>(); redistemplate.opsforhash().values(gateway_routes).stream() .foreach(routedefinition -> routedefinitions.add(json.parseobject(routedefinition.tostring(), routedefinition.class))); return flux.fromiterable(routedefinitions); } @override public mono<void> save(mono<routedefinition> route) { return route.flatmap(routedefinition -> { redistemplate.opsforhash().put(gateway_routes, routedefinition.getid(), jsonobject.tojsonstring(routedefinition)); return mono.empty(); }); } @override public mono<void> delete(mono<string> routeid) { return routeid.flatmap(id -> { if (redistemplate.opsforhash().haskey(gateway_routes, id)) { redistemplate.opsforhash().delete(gateway_routes, id); return mono.empty(); } return mono.defer(() -> mono.error(new notfoundexception("route definition is not found, routeid:" + routeid))); }); } }
4. 重写动态路由服务
@service public class gatewaydynamicrouteservice implements applicationeventpublisheraware { @resource private redisroutedefinitionrepository redisroutedefinitionrepository; private applicationeventpublisher applicationeventpublisher; /** * 增加路由 * @param routedefinition * @return */ public int add(routedefinition routedefinition) { redisroutedefinitionrepository.save(mono.just(routedefinition)).subscribe(); applicationeventpublisher.publishevent(new refreshroutesevent(this)); return 1; } /** * 更新 * @param routedefinition * @return */ public int update(routedefinition routedefinition) { redisroutedefinitionrepository.delete(mono.just(routedefinition.getid())); redisroutedefinitionrepository.save(mono.just(routedefinition)).subscribe(); applicationeventpublisher.publishevent(new refreshroutesevent(this)); return 1; } /** * 删除 * @param id * @return */ public mono<responseentity<object>> delete(string id) { return redisroutedefinitionrepository.delete(mono.just(id)).then(mono.defer(() -> mono.just(responseentity.ok().build()))) .onerrorresume(t -> t instanceof notfoundexception, t -> mono.just(responseentity.notfound().build())); } @override public void setapplicationeventpublisher(applicationeventpublisher applicationeventpublisher) { this.applicationeventpublisher = applicationeventpublisher; } }
5. 对外暴露接口
@restcontroller @requestmapping("/gateway") public class gatewaydynamicroutecontroller { @resource private gatewaydynamicrouteservice gatewaydynamicrouteservice; @postmapping("/add") public string create(@requestbody routedefinition entity) { int result = gatewaydynamicrouteservice.add(entity); return string.valueof(result); } @postmapping("/update") public string update(@requestbody routedefinition entity) { int result = gatewaydynamicrouteservice.update(entity); return string.valueof(result); } @deletemapping("/delete/{id}") public mono<responseentity<object>> delete(@pathvariable string id) { return gatewaydynamicrouteservice.delete(id); } }
测试
测试前删除我们配置的静态路由,因为静态路由和 redis 动态路由同时存在时取并集。
访问 , 可以看到只有默认路由。
[ { "route_id": "compositediscoveryclient_consul", "route_definition": { "id": "compositediscoveryclient_consul", "predicates": [ { "name": "path", "args": { "pattern": "/consul/**" } } ], "filters": [ { "name": "rewritepath", "args": { "regexp": "/consul/(?<remaining>.*)", "replacement": "/${remaining}" } } ], "uri": "lb://consul", "order": 0 }, "order": 0 }, { "route_id": "compositediscoveryclient_idc-gateway", "route_definition": { "id": "compositediscoveryclient_idc-gateway", "predicates": [ { "name": "path", "args": { "pattern": "/idc-gateway/**" } } ], "filters": [ { "name": "rewritepath", "args": { "regexp": "/idc-gateway/(?<remaining>.*)", "replacement": "/${remaining}" } } ], "uri": "lb://idc-gateway", "order": 0 }, "order": 0 }, { "route_id": "compositediscoveryclient_idc-provider1", "route_definition": { "id": "compositediscoveryclient_idc-provider1", "predicates": [ { "name": "path", "args": { "pattern": "/idc-provider1/**" } } ], "filters": [ { "name": "rewritepath", "args": { "regexp": "/idc-provider1/(?<remaining>.*)", "replacement": "/${remaining}" } } ], "uri": "lb://idc-provider1", "order": 0 }, "order": 0 }, { "route_id": "compositediscoveryclient_idc-provider2", "route_definition": { "id": "compositediscoveryclient_idc-provider2", "predicates": [ { "name": "path", "args": { "pattern": "/idc-provider2/**" } } ], "filters": [ { "name": "rewritepath", "args": { "regexp": "/idc-provider2/(?<remaining>.*)", "replacement": "/${remaining}" } } ], "uri": "lb://idc-provider2", "order": 0 }, "order": 0 } ]
这个时候访问 根据结果可以推测能正确路由到 provider1
, 测试结果一致。
创建 provider1
路由,将路径设置为 /p1/**
,测试是否生效。
post
请求
{ "id":"provider1", "predicates":[ { "name":"path", "args":{ "_genkey_0":"/p1/**" } }, { "name":"remoteaddr", "args":{ "_genkey_0":"192.168.124.5/16" } } ], "filters":[ { "name":"stripprefix", "args":{ "_genkey_0":"1" } } ], "uri":"lb://idc-provider1", "order":0 }
查看 redis
存储,或者请求 , 都可以看到配置成功。
访问
curl http://localhost:2000/p1/provider1/1
结果输出 2001,与期望一致。
由此可见动态路由已经生效。
到此这篇关于spring-cloud-gateway动态路由的实现方法的文章就介绍到这了,更多相关spring-cloud-gateway动态路由内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 小米11不送耳机:背后有玄机