本文源码:GitHub·点这里 || GitEE·点这里
一、根本逻辑
申请通过8001服务,在灰度规定中,会读取下次申请的服务列表,依据版本号参数规定,选中路由的服务。
配置版本号,辨别灰度版本和默认失常版本;
自定义拦截器,治理版本号或其余标识参数在申请中传递;
自定义服务选中策略,基于版本标识路由服务;
如果灰度服务不存在,则基于规定选中默认服务;
二、版本配置
在node12-server集群配置两个服务:在8002端口配置版本v7.0.0,在8003端口配置版本v7.0.1,用来测试灰度版本抉择。
8002服务
eureka: metadata-map: version: v7.0.0
8003服务
eureka: metadata-map: version: v7.0.1
Eureka注册核心,服务列表:
三、参数传递
微服务下通过实现RequestInterceptor接口,治理服务之间的Feign申请拦截器,在申请路由到服务前,能够对申请执行一些解决操作,常见操作例如传递版本号,用户Token等申请头等属性。
/** * 申请拦截器 */@Componentpublic class GrayReqInterceptor implements RequestInterceptor { private static final String VERSION_KEY = "versionId" ; /** * 解决申请头参数携带问题 */ @Override public void apply(RequestTemplate requestTemplate) { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); String versionId = request.getHeader(VERSION_KEY); if (StringUtils.isNotEmpty(versionId)){ requestTemplate.header(VERSION_KEY,versionId); } }}
这里就传递一个versionId参数,作为下次申请路由服务的外围标识。
四、灰度规定
在申请头的Header中增加要拜访的版本号,如果有匹配的服务,则路由所有申请的灰度服务,如果没有则返回默认服务。
@Configurationpublic class GrayRule extends ZoneAvoidanceRule { @Bean public GrayReqInterceptor grayReqInterceptor(){ return new GrayReqInterceptor(); } private static final String VERSION_KEY = "versionId" ; @Override public Server choose(Object key) { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); String versionId = request.getHeader(VERSION_KEY); // 服务匹配 List<Server> serverList = this.getPredicate().getEligibleServers(this.getLoadBalancer().getAllServers(), key); Server toServer = getServer(serverList,versionId); if (toServer != null){ return toServer ; } else { return getServer(serverList,GrayConstant.VERSION_DEF); } } private Server getServer (List<Server> serverList,String version){ Server toServer = null ; for (Server server : serverList) { Map<String, String> metadata = ((DiscoveryEnabledServer) server).getInstanceInfo().getMetadata(); String metaVersion = metadata.get("version"); if (!StringUtils.isEmpty(metaVersion)) { if (metaVersion.equals(version)) { toServer = server; } } } return toServer ; }}
在理论的过程中,服务的抉择是十分复杂的,如果没有灰度服务,须要依据理论状况制订服务匹配的规定,例如依据响应工夫,或者默认轮询等。
更须要留神的一点是,一旦应用底层API的二次封装,我的项目的整体就会受到框架版本升级的影响,须要继续关注框架的环境。
五、测试流程
1.启动相干服务,察看注册核心服务列表;
2.申请8001服务的接口,并带上版本号;
3.察看不同版本号的路由服务;
4.不携带版本号,察看默认服务抉择;
六、源代码地址
GitHub地址:知了一笑https://github.com/cicadasmile/spring-cloud-baseGitEE地址:知了一笑https://gitee.com/cicadasmile/spring-cloud-base
举荐浏览:编程体系整顿
序号 | 项目名称 | GitHub地址 | GitEE地址 | 举荐指数 |
---|---|---|---|---|
01 | Java形容设计模式,算法,数据结构 | GitHub·点这里 | GitEE·点这里 | ☆☆☆☆☆ |
02 | Java根底、并发、面向对象、Web开发 | GitHub·点这里 | GitEE·点这里 | ☆☆☆☆ |
03 | SpringCloud微服务根底组件案例详解 | GitHub·点这里 | GitEE·点这里 | ☆☆☆ |
04 | SpringCloud微服务架构实战综合案例 | GitHub·点这里 | GitEE·点这里 | ☆☆☆☆☆ |
05 | SpringBoot框架根底利用入门到进阶 | GitHub·点这里 | GitEE·点这里 | ☆☆☆☆ |
06 | SpringBoot框架整合开发罕用中间件 | GitHub·点这里 | GitEE·点这里 | ☆☆☆☆☆ |
07 | 数据管理、分布式、架构设计根底案例 | GitHub·点这里 | GitEE·点这里 | ☆☆☆☆☆ |
08 | 大数据系列、存储、组件、计算等框架 | GitHub·点这里 | GitEE·点这里 | ☆☆☆☆☆ |