添加文档

1.x
tanghc 6 years ago
parent ee1a70dbfc
commit f9560f9e30
  1. 19
      doc/docs/_sidebar.md
  2. 75
      doc/docs/files/10060_使用SpringCloudGateway.md
  3. 39
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/configuration/BaseGatewayConfiguration.java
  4. 37
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/AbstractConfiguration.java
  5. 31
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/configuration/BaseZuulConfiguration.java
  6. 2
      sop-gateway/src/main/resources/application.yml
  7. 146
      sop-test/src/test/java/com/gitee/sop/SpringCloudGatewayClientPostTest.java

@ -1,11 +1,12 @@
* [首页](/?t=1553513181526) * [首页](/?t=1553564490530)
* 开发文档 * 开发文档
* [快速体验](files/10010_快速体验.md?t=1553513181526) * [快速体验](files/10010_快速体验.md?t=1553564490530)
* [项目接入到SOP](files/10011_项目接入到SOP.md?t=1553513181547) * [项目接入到SOP](files/10011_项目接入到SOP.md?t=1553564490570)
* [新增接口](files/10020_新增接口.md?t=1553513181547) * [新增接口](files/10020_新增接口.md?t=1553564490570)
* [业务参数校验](files/10030_业务参数校验.md?t=1553513181547) * [业务参数校验](files/10030_业务参数校验.md?t=1553564490570)
* [错误处理](files/10040_错误处理.md?t=1553513181547) * [错误处理](files/10040_错误处理.md?t=1553564490571)
* [接口交互详解](files/10050_接口交互详解.md?t=1553513181547) * [接口交互详解](files/10050_接口交互详解.md?t=1553564490571)
* [使用SpringCloudGateway](files/10060_使用SpringCloudGateway.md?t=1553564490571)
* 原理分析 * 原理分析
* [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1553513181547) * [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1553564490571)
* [原理分析之路由存储](files/90011_原理分析之路由存储.md?t=1553513181547) * [原理分析之路由存储](files/90011_原理分析之路由存储.md?t=1553564490571)

@ -0,0 +1,75 @@
# 使用SpringCloudGateway
SOP默认网关是使用Spring Cloud Zuul,您也可以切换成Spring Cloud Gateway。
**注:**:SOP对Spring Cloud Gateway的支持目前处于beta阶段,推荐使用zuul。
步骤如下:
- 打开sop-gateway/pom.xml,注释spring cloud zuul依赖,打开Spring Cloud Gateway依赖
```xml
<!-- ↓↓↓ 使用spring cloud zuul ↓↓↓
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</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
运行SpringCloudGatewayClientPostTest测试用例。

@ -1,18 +1,17 @@
package com.gitee.sop.gatewaycommon.gateway.configuration; package com.gitee.sop.gatewaycommon.gateway.configuration;
import com.gitee.sop.gatewaycommon.bean.ApiContext;
import com.gitee.sop.gatewaycommon.gateway.filter.GatewayModifyResponseGatewayFilter; import com.gitee.sop.gatewaycommon.gateway.filter.GatewayModifyResponseGatewayFilter;
import com.gitee.sop.gatewaycommon.gateway.filter.LoadBalancerClientExtFilter; import com.gitee.sop.gatewaycommon.gateway.filter.LoadBalancerClientExtFilter;
import com.gitee.sop.gatewaycommon.gateway.filter.ValidateFilter; import com.gitee.sop.gatewaycommon.gateway.filter.ValidateFilter;
import com.gitee.sop.gatewaycommon.gateway.handler.GatewayExceptionHandler; import com.gitee.sop.gatewaycommon.gateway.handler.GatewayExceptionHandler;
import com.gitee.sop.gatewaycommon.gateway.route.GatewayRouteRepository; import com.gitee.sop.gatewaycommon.gateway.route.GatewayRouteRepository;
import com.gitee.sop.gatewaycommon.gateway.route.GatewayZookeeperRouteManager;
import com.gitee.sop.gatewaycommon.gateway.route.NameVersionRoutePredicateFactory; import com.gitee.sop.gatewaycommon.gateway.route.NameVersionRoutePredicateFactory;
import com.gitee.sop.gatewaycommon.gateway.route.ReadBodyRoutePredicateFactory; import com.gitee.sop.gatewaycommon.gateway.route.ReadBodyRoutePredicateFactory;
import com.gitee.sop.gatewaycommon.gateway.route.GatewayZookeeperRouteManager; import com.gitee.sop.gatewaycommon.manager.AbstractConfiguration;
import com.gitee.sop.gatewaycommon.manager.RouteManager;
import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext; import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext;
import com.gitee.sop.gatewaycommon.message.ErrorFactory;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler; import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
@ -22,7 +21,6 @@ import org.springframework.core.env.Environment;
import org.springframework.http.codec.ServerCodecConfigurer; import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.result.view.ViewResolver; import org.springframework.web.reactive.result.view.ViewResolver;
import javax.annotation.PostConstruct;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -30,13 +28,7 @@ import java.util.List;
/** /**
* @author tanghc * @author tanghc
*/ */
public class BaseGatewayConfiguration { public class BaseGatewayConfiguration extends AbstractConfiguration {
@Autowired
protected Environment environment;
@Autowired
protected GatewayZookeeperRouteManager gatewayZookeeperApiMetaManager;
/** /**
* 自定义异常处理[@@]注册Bean时依赖的Bean会从容器中直接获取所以直接注入即可 * 自定义异常处理[@@]注册Bean时依赖的Bean会从容器中直接获取所以直接注入即可
@ -60,6 +52,7 @@ public class BaseGatewayConfiguration {
/** /**
* 处理返回结果 * 处理返回结果
*
* @return * @return
*/ */
@Bean @Bean
@ -69,6 +62,7 @@ public class BaseGatewayConfiguration {
/** /**
* 读取post请求参数 * 读取post请求参数
*
* @return * @return
*/ */
@Bean @Bean
@ -92,7 +86,7 @@ public class BaseGatewayConfiguration {
} }
@Bean @Bean
GatewayZookeeperRouteManager gatewayZookeeperRouteManager(Environment environment, GatewayRouteRepository gatewayRouteManager) { RouteManager gatewayZookeeperRouteManager(Environment environment, GatewayRouteRepository gatewayRouteManager) {
return new GatewayZookeeperRouteManager(environment, gatewayRouteManager); return new GatewayZookeeperRouteManager(environment, gatewayRouteManager);
} }
@ -103,23 +97,4 @@ public class BaseGatewayConfiguration {
return gatewayRouteRepository; return gatewayRouteRepository;
} }
@PostConstruct
protected final void after() {
if (RouteRepositoryContext.getRouteRepository() == null) {
throw new IllegalArgumentException("RouteRepositoryContext.setRouteRepository()方法未使用");
}
initMessage();
gatewayZookeeperApiMetaManager.refresh();
doAfter();
}
protected void doAfter() {
}
protected void initMessage() {
ErrorFactory.initMessageSource(ApiContext.getApiConfig().getI18nModules());
}
} }

@ -0,0 +1,37 @@
package com.gitee.sop.gatewaycommon.manager;
import com.gitee.sop.gatewaycommon.bean.ApiContext;
import com.gitee.sop.gatewaycommon.message.ErrorFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import javax.annotation.PostConstruct;
/**
* @author tanghc
*/
public class AbstractConfiguration {
@Autowired
protected Environment environment;
@Autowired
protected RouteManager apiMetaManager;
@PostConstruct
public final void after() {
if (RouteRepositoryContext.getRouteRepository() == null) {
throw new IllegalArgumentException("RouteRepositoryContext.setRouteRepository()方法未使用");
}
initMessage();
apiMetaManager.refresh();
doAfter();
}
protected void doAfter() {
}
protected void initMessage() {
ErrorFactory.initMessageSource(ApiContext.getApiConfig().getI18nModules());
}
}

@ -1,9 +1,8 @@
package com.gitee.sop.gatewaycommon.zuul.configuration; package com.gitee.sop.gatewaycommon.zuul.configuration;
import com.gitee.sop.gatewaycommon.bean.ApiContext; import com.gitee.sop.gatewaycommon.bean.ApiContext;
import com.gitee.sop.gatewaycommon.manager.RouteManager; import com.gitee.sop.gatewaycommon.manager.AbstractConfiguration;
import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext; import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext;
import com.gitee.sop.gatewaycommon.message.ErrorFactory;
import com.gitee.sop.gatewaycommon.zuul.filter.ErrorFilter; import com.gitee.sop.gatewaycommon.zuul.filter.ErrorFilter;
import com.gitee.sop.gatewaycommon.zuul.filter.PostResultFilter; import com.gitee.sop.gatewaycommon.zuul.filter.PostResultFilter;
import com.gitee.sop.gatewaycommon.zuul.filter.PreValidateFilter; import com.gitee.sop.gatewaycommon.zuul.filter.PreValidateFilter;
@ -18,12 +17,10 @@ import org.springframework.cloud.netflix.zuul.filters.pre.PreDecorationFilter;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import javax.annotation.PostConstruct;
/** /**
* @author tanghc * @author tanghc
*/ */
public class BaseZuulConfiguration { public class BaseZuulConfiguration extends AbstractConfiguration {
@Autowired @Autowired
protected ZuulProperties zuulProperties; protected ZuulProperties zuulProperties;
@ -31,12 +28,6 @@ public class BaseZuulConfiguration {
@Autowired @Autowired
protected ServerProperties server; protected ServerProperties server;
@Autowired
protected Environment environment;
@Autowired
protected RouteManager apiMetaManager;
/** /**
* 路由存储 * 路由存储
* @return * @return
@ -120,22 +111,4 @@ public class BaseZuulConfiguration {
return ApiContext.getApiConfig().getZuulErrorController(); return ApiContext.getApiConfig().getZuulErrorController();
} }
@PostConstruct
public final void after() {
if (RouteRepositoryContext.getRouteRepository() == null) {
throw new IllegalArgumentException("RouteRepositoryContext.setRouteRepository()方法未使用");
}
initMessage();
apiMetaManager.refresh();
doAfter();
}
protected void doAfter() {
}
protected void initMessage() {
ErrorFactory.initMessageSource(ApiContext.getApiConfig().getI18nModules());
}
} }

@ -10,7 +10,7 @@ eureka:
spring: spring:
cloud: cloud:
gateway: gateway: # Spring Cloud Gateway配置,如果用了zuul,这段配置没有效果
discovery: discovery:
locator: locator:
lower-case-service-id: true lower-case-service-id: true

@ -0,0 +1,146 @@
package com.gitee.sop;
import com.alibaba.fastjson.JSON;
import com.gitee.sop.alipay.AlipaySignature;
import org.junit.Test;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* 模仿支付宝客户端请求接口
*/
public class SpringCloudGatewayClientPostTest extends TestBase {
String url = "http://localhost:8081"; // spring cloud gateway
String appId = "alipay_test";
// 支付宝私钥
String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXJv1pQFqWNA/++OYEV7WYXwexZK/J8LY1OWlP9X0T6wHFOvxNKRvMkJ5544SbgsJpVcvRDPrcxmhPbi/sAhdO4x2PiPKIz9Yni2OtYCCeaiE056B+e1O2jXoLeXbfi9fPivJZkxH/tb4xfLkH3bA8ZAQnQsoXA0SguykMRZntF0TndUfvDrLqwhlR8r5iRdZLB6F8o8qXH6UPDfNEnf/K8wX5T4EB1b8x8QJ7Ua4GcIUqeUxGHdQpzNbJd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Hi5gwJb+CZF3+IeYqsJk2NF2s0w5WJTE70k1BSvQsfIzAIDz2yE1oPHvwVAoGAA6e+xQkVH4fMEph55RJIZ5goI4Y76BSvt2N5OKZKd4HtaV+eIhM3SDsVYRLIm9ZquJHMiZQGyUGnsvrKL6AAVNK7eQZCRDk9KQz+0GKOGqku0nOZjUbAu6A2/vtXAaAuFSFx1rUQVVjFulLexkXR3KcztL1Qu2k5pB6Si0K/uwQ=";
/*
参数 类型 是否必填 最大长度 描述 示例值
app_id String 32 支付宝分配给开发者的应用ID 2014072300007148
method String 128 接口名称 alipay.trade.fastpay.refund.query
format String 40 仅支持JSON JSON
charset String 10 请求使用的编码格式如utf-8,gbk,gb2312等 utf-8
sign_type String 10 商户生成签名字符串所使用的签名算法类型目前支持RSA2和RSA推荐使用RSA2 RSA2
sign String 344 商户请求参数的签名串详见签名 详见示例
timestamp String 19 发送请求的时间格式"yyyy-MM-dd HH:mm:ss" 2014-07-24 03:07:50
version String 3 调用的接口版本固定为1.0 1.0
app_auth_token String 40 详见应用授权概述
biz_content String 请求参数的集合最大长度不限除公共参数外所有请求参数都必须放在这个参数中传递具体参照各产品快速接入文档
*/
// 这个请求会路由到story服务
@Test
public void testPost() throws Exception {
// 公共请求参数
Map<String, String> params = new HashMap<String, String>();
params.put("app_id", appId);
params.put("method", "alipay.story.get");
params.put("format", "json");
params.put("charset", "utf-8");
params.put("sign_type", "RSA2");
params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
params.put("version", "1.0");
// 业务参数
Map<String, String> bizContent = new HashMap<>();
bizContent.put("id", "1");
bizContent.put("name", "葫芦娃");
params.put("biz_content", JSON.toJSONString(bizContent));
System.out.println("----------- 请求信息 -----------");
System.out.println("请求参数:" + buildParamQuery(params));
System.out.println("商户秘钥:" + privateKey);
String content = AlipaySignature.getSignContent(params);
System.out.println("待签名内容:" + content);
String sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8");
System.out.println("签名(sign):" + sign);
params.put("sign", sign);
System.out.println("----------- 返回结果 -----------");
String responseData = post(url, params);// 发送请求
System.out.println(responseData);
}
// 这个请求会路由到book服务
@Test
public void testPostBook() throws Exception {
// 公共请求参数
Map<String, String> params = new HashMap<String, String>();
params.put("app_id", appId);
params.put("method", "alipay.book.get");
params.put("format", "json");
params.put("charset", "utf-8");
params.put("sign_type", "RSA2");
params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
params.put("version", "1.0");
// 业务参数
Map<String, String> bizContent = new HashMap<>();
bizContent.put("id", "1");
bizContent.put("name", "葫芦娃");
// bizContent.put("name", "葫芦娃1234567890葫芦娃1234567890"); // 超出长度
params.put("biz_content", JSON.toJSONString(bizContent));
System.out.println("----------- 请求信息 -----------");
System.out.println("请求参数:" + buildParamQuery(params));
System.out.println("商户秘钥:" + privateKey);
String content = AlipaySignature.getSignContent(params);
System.out.println("待签名内容:" + content);
String sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8");
System.out.println("签名(sign):" + sign);
params.put("sign", sign);
System.out.println("----------- 返回结果 -----------");
String responseData = post(url, params);// 发送请求
System.out.println(responseData);
}
// 这个请求会路由到book服务,然后再调用story服务
// gateway -> book-service -> story-service
@Test
public void testPostBook2() throws Exception {
// 公共请求参数
Map<String, String> params = new HashMap<String, String>();
params.put("app_id", appId);
params.put("method", "alipay.book.story.get");
params.put("format", "json");
params.put("charset", "utf-8");
params.put("sign_type", "RSA2");
params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
params.put("version", "1.0");
// 业务参数
Map<String, String> bizContent = new HashMap<>();
bizContent.put("id", "1");
bizContent.put("name", "葫芦娃");
// bizContent.put("name", "葫芦娃1234567890葫芦娃1234567890"); // 超出长度
params.put("biz_content", JSON.toJSONString(bizContent));
System.out.println("----------- 请求信息 -----------");
System.out.println("请求参数:" + buildParamQuery(params));
System.out.println("商户秘钥:" + privateKey);
String content = AlipaySignature.getSignContent(params);
System.out.println("待签名内容:" + content);
String sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8");
System.out.println("签名(sign):" + sign);
params.put("sign", sign);
System.out.println("----------- 返回结果 -----------");
String responseData = post(url, params);// 发送请求
System.out.println(responseData);
}
}
Loading…
Cancel
Save