赞
踩
目录
使用的服务件(nacos\gateway\ribbon\cloud\redis)
https://download.csdn.net/download/m0_73427585/89382001
单体项目包含多个模块,任意模块的修改都需要对整个项目进行编译、打包和部署
简单来说就是,发布比较简单,但是耦合性比较高,出现问题需要整体项目打包。
会需求一样的服务器逻辑上有蓝组和绿组,项目发布时候会对新版本假设为拦阻进行发布,此时绿组会被负载均衡器摘除,拦阻提供服务;当绿组升级完毕时候,负载均衡会重新接入绿组,再停掉蓝组等待蓝的升级,一次类推,直到最后两组都成最新版本,负载均衡器会将其全部接入,呈现都对外提供展示的效果。
简单来说就是,新的发布,更新老系统,代价需要机器成本上升,而且出现问题时侯对所有用户都有影响。
根据请求的流量,将流量分流,分一小部分转至新版本的使用,这个新版本即为灰度发布方案,灰度的使用通过后,会通过调整新版本的使用流量进行融合
简单说:新功能上线,就新老系统共存,共存的空间就是灰度空间,这样不影响用户的体验,通过慢慢的放开用户来使用新系统。
区分可以进行灰度规则v2的用户和原先系统v1的用户,使用Redis的Hash结构来保存用户id数据
调用方式1:网关调用灰度服务
调用方式2:ribbon调用灰度服务(服务调用服务)
通过Nacos作为注册中心为灰度服务设置元数据信息,添加gray=v2
的元数据来表明当前服务是灰度服务,未添加这个元数据标识的服务为未更新的旧服务
Nacos是一款阿里巴巴开源的微服务注册中心与配置中心。
官网:Nacos 快速开始
下载(我们这里用的2.0.1):https://github.com/alibaba/nacos/releases/download/2.0.1/nacos-server-2.0.1.zip
启动:
startup.cmd -m standalone
停止 shutdown.cmd
访问:http://localhost:8848/nacos
默认账号密码:nacos/nacos
基于服务发现路由定位
基于 Filter 链(定义过滤器对请求过滤)提供了网关基本的功能,例如:鉴权、流量控制、熔断、路径重写、日志监控等。
基于http和tcp的客户端负载均衡器,是基于Netflix Ribbon实现的。
在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http Restful的。
Ribbon是一个服务调用组件,并且是一个客户端实现负载均衡的处理的组件。
Ribbon的负载均衡规则接口:
IRule
,主要职责是从所有服务中选择一个服务进行调用,默认实现:ZoneAvoidanceRule
ZoneAvoidanceRule(默认) | public class ZoneAvoidanceRule extends PredicateBasedRule | 复合判断server所在区域的性能和server的可用性选择server | 使用ZoneAvoidancePredicate和AvailabilityPredicate来判断是否选择某个server,前一个判断判定一个zone的运行性能是否可用,剔除不可用的zone(的所有server),AvailabilityPredicate用于过滤掉连接数过多的Server。 |
Spring Cloud有两种服务调用方式,一种是RestTemplate
,另一种是OpenFeign
。
- @SpringBootApplication
- public class ConsumerApplication {
- public static void main(String[] args) {
- SpringApplication.run(ConsumerApplication.class, args);
- }
-
- @Bean
- @LoadBalanced
- RestTemplate restTemplate() {
- return new RestTemplate();
- }
- }
- @FeignClient("spring-cloud-gray-provider")
- public interface IndexApi {
- @GetMapping("/index")
- Map<String, Object> index();
- }
使用Redis对灰度用户进行存储
官网:Redis - The Real-time Data Platform
下载地址:Downloads - Redis
因Redis官网未提供Windows版本的Redis下载,故需要下载社区编译的Windows版本:https://github.com/tporadowski/redis/releases/download/v5.0.14/Redis-x64-5.0.14.msi
启动:redis-server.exe
停止:redis-cli shutdown
用户id为1的访问新服务(灰度服务)
其它用户访问旧服务
在redis中添加一条规则数据:
hset gray_rule 1 'v2'
http的请求测试
### 网关-灰度
GET http://localhost:8000/spring-cloud-gray-provider/index
userId: 1### 网关-无灰度
GET http://localhost:8000/spring-cloud-gray-provider/index
userId:2
### ribbon-灰度
GET http://localhost:8200/hello
userId: 1### ribbon-无灰度
GET http://localhost:8200/hello
userId: 3
网关:spring-cloud-gray-gateway
提供服务:spring-cloud-gray-provider
消费服务:spring-cloud-gray-consumer
api接口:
spring-cloud-gray-api
- <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>com.dh.edu</groupId>
- <artifactId>spring-cloud-gray</artifactId>
- <packaging>pom</packaging>
- <version>1.0-SNAPSHOT</version>
- <modules>
- <module>spring-cloud-gray-gateway</module>
- <module>spring-cloud-gray-provider</module>
- <module>spring-cloud-gray-api</module>
- <module>spring-cloud-gray-consumer</module>
- </modules>
-
- <properties>
- <maven.compiler.source>11</maven.compiler.source>
- <maven.compiler.target>11</maven.compiler.target>
- <spring-boot.version>2.3.12.RELEASE</spring-boot.version>
- <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
- <spring-cloud-alibaba.version>2.2.6.RELEASE</spring-cloud-alibaba.version>
- </properties>
-
- <dependencyManagement>
- <dependencies>
- <!-- ============================framework start============================ -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-dependencies</artifactId>
- <version>${spring-boot.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring-cloud.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-alibaba-dependencies</artifactId>
- <version>${spring-cloud-alibaba.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- <!-- ============================framework end============================ -->
- </dependencies>
- </dependencyManagement>
-
- <build>
- <plugins>
- <!-- java编译插件 -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.2</version>
- <configuration>
- <source>1.8</source>
- <target>1.8</target>
- <encoding>UTF-8</encoding>
- </configuration>
- </plugin>
- </plugins>
- </build>
-
- </project>
- <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>spring-cloud-gray</artifactId>
- <groupId>com.dh.edu</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
-
- <artifactId>spring-cloud-gray-api</artifactId>
-
- <properties>
- <maven.compiler.source>11</maven.compiler.source>
- <maven.compiler.target>11</maven.compiler.target>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-openfeign</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
- </dependency>
- </dependencies>
-
- </project>
- <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>spring-cloud-gray</artifactId>
- <groupId>com.dh.edu</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
-
- <artifactId>spring-cloud-gray-provider</artifactId>
-
- <properties>
- <maven.compiler.source>11</maven.compiler.source>
- <maven.compiler.target>11</maven.compiler.target>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- </dependencies>
-
- <build>
- <finalName>${artifactId}</finalName>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <executions>
- <execution>
- <goals>
- <goal>build-info</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
-
- </project>
- spring.application.name=spring-cloud-gray-provider
- server.port=8100
-
- ## nacos注册中心
- spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
- spring.cloud.nacos.discovery.namespace=public
spring-cloud-gray-consumer
- <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>spring-cloud-gray</artifactId>
- <groupId>com.dh.edu</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
-
- <artifactId>spring-cloud-gray-consumer</artifactId>
-
- <properties>
- <maven.compiler.source>11</maven.compiler.source>
- <maven.compiler.target>11</maven.compiler.target>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis</artifactId>
- </dependency>
-
- <dependency>
- <groupId>com.dh.edu</groupId>
- <artifactId>spring-cloud-gray-api</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- </dependencies>
-
- <build>
- <finalName>${artifactId}</finalName>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <executions>
- <execution>
- <goals>
- <goal>build-info</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
-
- </project>
- spring.application.name=spring-cloud-gray-consumer
- server.port=8200
-
- ## nacos注册中心
- spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
- spring.cloud.nacos.discovery.namespace=public
-
- # redis
- spring.redis.host=localhost
- spring.redis.port=6379
- spring.redis.database=0
spring-cloud-gray-gateway
- <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <parent>
- <groupId>com.dh.edu</groupId>
- <artifactId>spring-cloud-gray</artifactId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
-
- <artifactId>spring-cloud-gray-gateway</artifactId>
-
- <properties>
- <maven.compiler.source>11</maven.compiler.source>
- <maven.compiler.target>11</maven.compiler.target>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-gateway</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-loadbalancer</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-openfeign</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>1.2.78</version>
- <scope>compile</scope>
- </dependency>
- </dependencies>
-
- </project>
spring.application.name=spring-cloud-gray-gateway server.port=8000 ## nacos注册中心 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 spring.cloud.nacos.discovery.namespace=public ## 路由网关配置 ### 基于服务发现的路由定位 spring.cloud.gateway.discovery.locator.enabled=true ### 服务实例id名称大小写支持 spring.cloud.gateway.discovery.locator.lower-case-service-id=true # redis spring.redis.host=localhost spring.redis.port=6379 spring.redis.database=0
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。