赞
踩
目录
(2)eureka工程,就是一个普通的eureka server。
灰度发布(又名金丝雀发布)是指在黑与白之间,能够平滑过渡的一种发布方式。在其上可以进行A/B testing,即让一部分用户继续用产品特性A,一部分用户开始用产品特性B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B上面来。灰度发布可以保证整体系统的稳定,在初始灰度的时候就可以发现、调整问题,以保证其影响度。(百度来的)
创建三个工程:分别为:订单、eureka、zuul
订单作为提供服务方,eureka作为注册中心,zuul作为网关。
具体创建就不在此详细赘述了。
order服务提供方,提供两个服务,端口分别为:8080、8081,配置如下:
server: port: 8080 eureka: instance: metadata-map: version: v1 client: service-url: defaultZone: http://localhost:7900/eureka spring: application: name: order --- spring: profiles: 8081 server: port: 8081 eureka: instance: metadata-map: #自定义元数据区 version: v2
order服务为了测试只提供一个controller,打印出各自的端口,代表访问到不同的机器。如下:
- @SpringBootApplication
- @RestController
- public class OrderApplication {
-
- @Value("${server.port}")
- private String port;
- public static void main(String[] args) {
- SpringApplication.run(OrderApplication.class, args);
- }
-
- @RequestMapping
- public String test(){
- return "port:" + port;
- }
- }
yml配置:
- server:
- port: 9100
- spring:
- application:
- name: zuul-gray
- eureka:
- client:
- service-url:
- defaultZone: http://localhost:7900/eureka
- instance:
- hostname: localhost
- instance-id: zuul-gray
pom.xml:需要引入eureka客户端、zuul、ribbon-discovery-filter-spring-cloud-starter这三个。
ribbon-discovery-filter-spring-cloud-starter主要用于网关选择集群中的某一个节点。主要方法是RibbonFilterContextHolder.getCurrentContext().add("version","v2");(详情见java代码)
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.3.10.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.kingdee</groupId>
- <artifactId>zuul</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>zuul</name>
- <description>Demo project for Spring Boot</description>
- <properties>
- <java.version>1.8</java.version>
- <spring-cloud.version>Hoxton.SR11</spring-cloud.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
- </dependency>
-
- <dependency>
- <groupId>io.jmnarloch</groupId>
- <artifactId>ribbon-discovery-filter-spring-cloud-starter</artifactId>
- <version>2.1.0</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- <exclusions>
- <exclusion>
- <groupId>org.junit.vintage</groupId>
- <artifactId>junit-vintage-engine</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- </dependencies>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring-cloud.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>
java代码:
- package com.kingdee.zuul.filter;
-
- import com.netflix.zuul.ZuulFilter;
- import com.netflix.zuul.context.RequestContext;
- import com.netflix.zuul.exception.ZuulException;
- import io.jmnarloch.spring.cloud.ribbon.support.RibbonFilterContextHolder;
- import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
- import org.springframework.stereotype.Component;
-
- import javax.servlet.http.HttpServletRequest;
-
- /**
- * @author :zhouhaitao
- * @date :Created in 2021/5/20 23:39
- * @description:
- * @modified By:
- * @version: v1.0
- */
- @Component
- public class GrayFilter extends ZuulFilter {
-
- @Override
- public String filterType() {
- return FilterConstants.ROUTE_TYPE;
- }
-
- @Override
- public int filterOrder() {
- return 0;
- }
-
- @Override
- public boolean shouldFilter() {
- return true;
- }
-
- @Override
- public Object run() throws ZuulException {
- RequestContext currentContent = RequestContext.getCurrentContext();
- HttpServletRequest request = currentContent.getRequest();
- String userId = request.getHeader("userId");
- //根据用户Id查规则
- if("1".equals(userId)){
- RibbonFilterContextHolder.getCurrentContext().add("version","v2");
- }else{
- RibbonFilterContextHolder.getCurrentContext().add("version","v1");
- }
-
- return null;
- }
- }
主逻辑在run()方法中控制,目前代码中的逻辑是,如果header中userId,设置值为1则走order 服务中元数据设置了version: v2的服务。
使用postman测试
直接访问8080端口服务结果:
直接访问8081端口服务结果:
使用zuul header传入userId=1访问结果:
使用zuul header传入userId !=1 访问结果:
多次访问发现结果都一样,说明配置的灰度发布成功了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。