mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
网关二合一
This commit is contained in:
@@ -1,92 +1,16 @@
|
||||
# 使用SpringCloudGateway
|
||||
|
||||
SOP默认网关是使用Spring Cloud Zuul,您也可以切换成Spring Cloud Gateway,完整代码见`spring-cloud-gateway`分支。
|
||||
|
||||
如果您的系统并发量不大,建议使用zuul,因为zuul的功能更全面,有新功能会优先实现在zuul上。
|
||||
|
||||
- SOP中 Spring Cloud Zuul 和 Spring Cloud Gateway功能对比
|
||||
|
||||
| 功能 | Spring Cloud Zuul | Spring Cloud Gateway |
|
||||
| ----- | ---- | ----------------------- |
|
||||
| 签名验证|√ | √ |
|
||||
| 统一异常处理|√ | √ |
|
||||
| 统一返回内容|√ | √ |
|
||||
| session管理|√ | √ |
|
||||
| 秘钥管理|√ | √ |
|
||||
| 微服务端自动验证(JSR-303)|√ | √ |
|
||||
| 文件上传|√ | √ |
|
||||
| 文件下载|√ | √ |
|
||||
| 接口限流|√ | √ |
|
||||
| 文档整合|√ | √ |
|
||||
| 应用授权|√ | √ |
|
||||
| 监控日志|√ | √ |
|
||||
| 支持nacos|√ | √ |
|
||||
| 网关动态修改参数|√ | √ |
|
||||
|
||||
使用Spring Cloud Gateway步骤如下:
|
||||
|
||||
- 打开sop-gateway/pom.xml,注释spring cloud zuul依赖,打开Spring Cloud Gateway依赖
|
||||
修改`sop-gateway/pom.xml`配置,artifactId部分改成`sop-bridge-gateway`即可
|
||||
|
||||
```xml
|
||||
<!-- ↓↓↓ 使用spring cloud zuul ↓↓↓
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
|
||||
<groupId>com.gitee.sop</groupId>
|
||||
<!-- 使用zuul作为网关 -->
|
||||
<!--<artifactId>sop-bridge-zuul</artifactId>-->
|
||||
<!-- 使用spring cloud gateway作为网关 -->
|
||||
<artifactId>sop-bridge-gateway</artifactId>
|
||||
<version>2.5.10-SNAPSHOT</version>
|
||||
</dependency>
|
||||
-->
|
||||
<!-- ↑↑↑ 使用spring cloud zuul ↑↑↑ -->
|
||||
|
||||
|
||||
<!-- ↓↓↓ 使用spring cloud gateway,处于beta阶段,推荐使用zuul ↓↓↓ -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- ↑↑↑ 使用spring cloud gateway ↑↑↑ -->
|
||||
```
|
||||
|
||||
- 打开启动类`SopGatewayApplication.java`, 注释zuul相关注解
|
||||
|
||||
```java
|
||||
package com.gitee.sop.gateway;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
//import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
|
||||
|
||||
// 开启网关功能
|
||||
//@EnableZuulProxy
|
||||
@SpringBootApplication
|
||||
public class SopGatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SopGatewayApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
- 禁用ZuulConfig类,注释掉@Configuration注解即可
|
||||
|
||||
```java
|
||||
//@Configuration
|
||||
public class ZuulConfig extends AlipayZuulConfiguration {...}
|
||||
```
|
||||
|
||||
- 启用GatewayConfig类,打开@Configuration注释
|
||||
|
||||
```java
|
||||
@Configuration
|
||||
public class GatewayConfig extends AlipayGatewayConfiguration {...}
|
||||
```
|
||||
|
||||
修改完毕,重启sop-gateway
|
||||
|
@@ -11,7 +11,7 @@
|
||||
|open1.domain.com |网关服务器1 |
|
||||
|openpre.domain.com | 网关服务器2,作为预发布请求入口|
|
||||
|
||||
线上网关入口为`http://open.domain.com/api`,请求网关`http://open.domain.com/api`会负载均衡到这两台服务器
|
||||
线上网关入口为`http://open.domain.com/`,请求网关`http://open.domain.com/`会负载均衡到这两台服务器
|
||||
|
||||
SOP开启预发布步骤如下:
|
||||
|
||||
@@ -26,18 +26,16 @@ pre.domain=openpre.domain.com
|
||||
微服务启动参数添加:`--spring.cloud.nacos.discovery.metadata.env=pre`(eureka下是:`--eureka.instance.metadata-map.env=pre`)。
|
||||
建议线上配两套启动脚本,其中预发布启动脚本添加启动参数`--eureka.instance.metadata-map.env=pre`
|
||||
|
||||
登录SOP-Admin,在服务列表中点击预发布,然后预发布请求地址变成:`http://openpre.domain.com/api`。
|
||||
登录SOP-Admin,在服务列表中点击预发布,然后预发布请求地址变成:`http://openpre.domain.com/`。
|
||||
从`openpre.domain.com`请求进来的用户都会进预发布服务器,其它情况都走非预发布服务器。
|
||||
|
||||
## 使用灰度发布
|
||||
|
||||
灰度发布可允许指定的用户访问灰度服务器,其它用户还是走正常流程。
|
||||
|
||||
微服务启动参数添加:`--spring.cloud.nacos.discovery.metadata.env=gray`(eureka下是:`--eureka.instance.metadata-map.env=gray`)。
|
||||
登录SOP-Admin,前往`服务列表`。
|
||||
|
||||
登录SOP-Admin,前往服务列表。
|
||||
|
||||
- 先设置灰度参数,指定灰度用户和灰度接口
|
||||
- 先设置灰度参数,指定灰度appId和灰度接口
|
||||
- 服务器实例开启灰度
|
||||
|
||||
参考类:
|
||||
|
Reference in New Issue
Block a user