6 Star 61 Fork 24

Gitee 极速下载 / spring-cloud-gray

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/SpringCloud/spring-cloud-gray
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

Spring Cloud Gray - 微服务灰度中间件

License A Release B Release B Release B Release

介绍

Spring Cloud Gray 是一套开源的微服务灰度路由解决方案,它由spring-cloud-gray-client,spring-cloud-gray-client-netflix 和 spring-cloud-tray-server,spring-cloud-gray-webui组成。
spring-cloud-gray-client定义了一套灰度路由决策模型,灰度信息追踪模型,以及和spring-cloud-gray-server的基本通信功能。
spring-cloud-gray-client-netflix在spring-cloud-gray-client的基础上集成了微服务注册中心eureka,扩展ribbon的负载均衡规则,提供了对zuul,feign,RestTemplate的灰度路由能力,并且无缝支持hystrix线程池隔离。
spring-cloud-gray-server负责灰度决策、灰度追踪等信息的管理以及持久化。
spring-cloud-gray-webui提供操作界面。

钉钉交流群

二维码:

群链接:
https://qr.dingtalk.com/action/joingroup?code=v1,k1,a3CBsbJY9SfCfm+h+Yp7dQdDJufTZDPHgibBtIRoVRc=&_dt_no_comment=1&origin=11

Demo

点击查看

在管控端数据库执行一条insert语名:

insert into `user` ( `user_id`, `account`, `name`, `password`, `roles`, `status`, `create_time`, `operator`, `operate_time`) values ( 'admin', 'admin', 'Admin', 'e7a57e51394e91cba19deca3337bfab0', 'admin', '1', now(), 'admin', now());

这是添加管理员账号的,用户名:admin 密码:abc123

使用手册

管控端部署手册
客户端部署手册
管控端配置参数说明
客户端配置参数说明
管控端界面使用手册

版本信息

项目分支 项目版本 srpingcloud版本 springboot版本
A A.2.0.0 Edgware 1.5.*
B B.1.0.0 Finchley 2.0.*
C C.1.0.0 Greenwich 2.1.*
D D.0.0.1 Hoxton 2.3.*

Spring Cloud Gray 能做什么

  1. 金丝雀测试

    先发布1台实例,用于测试验证,指定测试的流量进入这台实例,其它流量依然进入其它正常的实例。优势在于发布成本小,快速测试,并且不影响正常用户体验影响,即使测试不通过,也只需回滚这一台实例,用户无感知。

  2. 灰度放量

    通过金丝雀测试后,可以逐渐放量到新的版本上。例如,根据userId或者ip放5%的流量到其中一台灰度实例上,观察一段时间没异常,可调整放入20%的流量,如果一台实例扛不住,可再发一台或多台实例。将发布产生的风险保持在可控范围内。

  3. 切断实例流量

    当线上出现问题,可将某台实例的流量切断,保留现场,设置指定的请求进入实例,在线调试并且不影响其它用户。

  4. 数据透传

    借助灰度追踪的能力,在网关处记录用户请求的最初的数据,可以将之透传到请求完整的调用链中。

  5. 借助“破窗”能力,实例蓝绿发布

    首次上灰度时,会存在两种环境,一种是已经依赖了灰度客户端的环境,另一种是正常运行的当前环境。假如微服务的负载均衡是由ribbon实现,那么当前环境会请求路由到实例状态为UP的实例上,而依赖了灰度客户端的环境,则可以通过"破窗"能力,跟灰度路由结合,可以将匹配灰度策略的请求路由到实例状态为STARTING的实例上,不匹配灰度策略的请求路由到实例状态为UP的实例上。

设计思想

在微服务架构中,接口的调用通常是服务消费方按照某种负载均衡策略去选择服务实例;但这无法满足线上更特殊化的一些路由逻辑,比如根据一次请求携带的请求头中的信息路由到某一个服务实例上。Spring Cloud Gray正是为此而创建。
在Spring Cloud Gray中定义了几个角色灰度客户端(gray-client)、灰度管控端(gray-server)、注册中心。

注册中心 负责服务的注册和发现。

灰度客户端 灰度的客户端是指依赖了spring-cloud-gray-client的服务,一般是指服务消费方。

灰度管控端 负责灰度信息的管理、持久化等维护工作。
灰度客户端会从灰度管控端拉取一份灰度信息的清单,并在内存中维护这份清单信息,清单中包含服务,服务实例,灰度策略,灰度追踪字段等。
当请求达到网关时,网关就会在灰度追踪中将需要透传的信息记录下来,并将传递给转发的服务实例,后面的接口调用也会按照同样的逻辑将追踪信息透传下去,从而保证所有一个请求在微服务调用链中的灰度路由。
如下图所示:

管控端的功能

  1. 用户管理

    可添加用户,禁用用户,重置密码等。

  2. 服务列表

  3. 权限控制

    灰度的权限控制是以服务为对象的,拥有服务的权限,就可以操作服务的所有灰度信息。在服务的权限控制中,分为两种角色,owner和管理者,owner拥有最大的权限,管理者除了不能删除owner的权限,其它权限同owner一样。 owner list authrity list

  4. 灰度实例管理

    列出服务的灰度实例列表

  5. 在线实例列表

    列出指定服务在注册中心注册的实例,点击【Add】按钮,可快速添加为实例实例

  6. 编辑灰度策略

    从实例列表点击【策略】按钮进入灰度策略列表,可在策略列表中添加灰度策略和灰度决策。

    实例的灰度策略,包含可多个灰度决策。
 策略是从灰度实例列表进入。一个实例可以有多个灰度策略,策略与策略之间是"或"的关系。就是说,一个请求只要 满足实例的任意一个灰度策略,这个请求被路由到该实例上。

    决策是灰度中进行比对的最小项。它定义一种规则,对请求进行比对,返回 true/false。当请求调用时,灰度调用端可以根据灰度实例的灰度决策,进行对比,以判断灰度实例是否可以受理该请求。多个决策是"与"的关系。 灰度策略 灰度决策

  7. 编辑灰度追踪

    从服务列表点击【追踪】按钮进入

  8. 改变实例状态

    可在实例列表中,通过【实例状态】按钮修改实例状态。提前是实例得依赖了灰度客户端的jar包,并且uri没有设置前缀(server.servlet.context-path)

  9. 操作审记

    所有的POST,PUT,DELETE操作都会被记录下来,可能通过操作记录查询,用于事后审计。

    查询维度包括:

  • StartTime - EndTime 记录时间

  • Operator Id 操作人Id

  • ApiRes Code 接口返回的ApiRes Code

  • operate State 操作的结果

  • Request Hander spring mvc 的接口(Controller/Handler)的类名和方法

  • URI 接口的uri

  • IP http请求(操作人)的ip

工程模块

功能模块

模块 描述
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依赖即可。

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright {yyyy} {name of copyright owner} Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

Spring Cloud Gray 是一套开源的微服务灰度路由解决方案,它由 spring-cloud-gray-client,spring-cloud-gray-client-n 展开 收起
Java 等 5 种语言
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/mirrors/spring-cloud-gray.git
git@gitee.com:mirrors/spring-cloud-gray.git
mirrors
spring-cloud-gray
spring-cloud-gray
master

搜索帮助

14c37bed 8189591 565d56ea 8189591