parent
de85e5aef9
commit
6fc49f2699
@ -1,29 +1,30 @@ |
|||||||
* [首页](/?t=1561689549009) |
* [首页](/?t=1561970396914) |
||||||
* 开发文档 |
* 开发文档 |
||||||
* [快速体验](files/10010_快速体验.md?t=1561689549011) |
* [快速体验](files/10010_快速体验.md?t=1561970396914) |
||||||
* [项目接入到SOP](files/10011_项目接入到SOP.md?t=1561689549027) |
* [项目接入到SOP](files/10011_项目接入到SOP.md?t=1561970396931) |
||||||
* [新增接口](files/10020_新增接口.md?t=1561689549027) |
* [新增接口](files/10020_新增接口.md?t=1561970396931) |
||||||
* [业务参数校验](files/10030_业务参数校验.md?t=1561689549028) |
* [业务参数校验](files/10030_业务参数校验.md?t=1561970396931) |
||||||
* [错误处理](files/10040_错误处理.md?t=1561689549028) |
* [错误处理](files/10040_错误处理.md?t=1561970396931) |
||||||
* [编写文档](files/10041_编写文档.md?t=1561689549028) |
* [编写文档](files/10041_编写文档.md?t=1561970396931) |
||||||
* [接口交互详解](files/10050_接口交互详解.md?t=1561689549028) |
* [接口交互详解](files/10050_接口交互详解.md?t=1561970396931) |
||||||
* [easyopen支持](files/10070_easyopen支持.md?t=1561689549028) |
* [easyopen支持](files/10070_easyopen支持.md?t=1561970396932) |
||||||
* [使用签名校验工具](files/10080_使用签名校验工具.md?t=1561689549028) |
* [使用签名校验工具](files/10080_使用签名校验工具.md?t=1561970396932) |
||||||
* [ISV管理](files/10085_ISV管理.md?t=1561689549028) |
* [ISV管理](files/10085_ISV管理.md?t=1561970396932) |
||||||
* [自定义路由](files/10086_自定义路由.md?t=1561689549028) |
* [自定义路由](files/10086_自定义路由.md?t=1561970396932) |
||||||
* [路由授权](files/10090_路由授权.md?t=1561689549028) |
* [路由授权](files/10090_路由授权.md?t=1561970396932) |
||||||
* [接口限流](files/10092_接口限流.md?t=1561689549029) |
* [接口限流](files/10092_接口限流.md?t=1561970396932) |
||||||
* [监控日志](files/10093_监控日志.md?t=1561689549029) |
* [监控日志](files/10093_监控日志.md?t=1561970396932) |
||||||
* [SDK开发](files/10095_SDK开发.md?t=1561689549029) |
* [SDK开发](files/10095_SDK开发.md?t=1561970396932) |
||||||
* [使用SpringCloudGateway](files/10096_使用SpringCloudGateway.md?t=1561689549029) |
* [使用SpringCloudGateway](files/10096_使用SpringCloudGateway.md?t=1561970396932) |
||||||
* [应用授权](files/10097_应用授权.md?t=1561689549029) |
* [应用授权](files/10097_应用授权.md?t=1561970396932) |
||||||
* [更改数据节点名称](files/10099_更改数据节点名称.md?t=1561689549029) |
* [更改数据节点名称](files/10099_更改数据节点名称.md?t=1561970396932) |
||||||
* [对接前端](files/10100_对接前端.md?t=1561689549029) |
* [对接前端](files/10100_对接前端.md?t=1561970396933) |
||||||
* [自定义过滤器](files/10102_自定义过滤器.md?t=1561689549029) |
* [自定义过滤器](files/10102_自定义过滤器.md?t=1561970396933) |
||||||
* [文件上传](files/10104_文件上传.md?t=1561689549029) |
* [文件上传](files/10104_文件上传.md?t=1561970396933) |
||||||
|
* [nacos注册中心](files/10106_nacos注册中心.md?t=1561970396933) |
||||||
* 原理分析 |
* 原理分析 |
||||||
* [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1561689549029) |
* [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1561970396933) |
||||||
* [原理分析之路由存储](files/90011_原理分析之路由存储.md?t=1561689549030) |
* [原理分析之路由存储](files/90011_原理分析之路由存储.md?t=1561970396933) |
||||||
* [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1561689549030) |
* [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1561970396933) |
||||||
* [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1561689549030) |
* [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1561970396933) |
||||||
* [常见问题](files/90100_常见问题.md?t=1561689549030) |
* [常见问题](files/90100_常见问题.md?t=1561970396933) |
||||||
|
@ -0,0 +1,113 @@ |
|||||||
|
# nacos注册中心 |
||||||
|
|
||||||
|
完整代码在`registry-nacos`分支 |
||||||
|
|
||||||
|
SOP默认使用eureka注册中心,如果要换成nacos注册中心,步骤如下: |
||||||
|
|
||||||
|
- 实现`com.gitee.sop.registryapi.service.RegistryService`接口 |
||||||
|
|
||||||
|
1.找到`SOP/sop-common/sop-registry-api`工程,在service.impl包下新建一个类,实现RegistryService接口 |
||||||
|
|
||||||
|
```java |
||||||
|
public class RegistryServiceNacos implements RegistryService { |
||||||
|
|
||||||
|
@Override |
||||||
|
public List<ServiceInfo> listAllService(int pageNo, int pageSize) throws Exception { |
||||||
|
// TODO: 返回服务实例 |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void onlineInstance(ServiceInstance serviceInstance) throws Exception { |
||||||
|
// TODO: 实例上线 |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void offlineInstance(ServiceInstance serviceInstance) throws Exception { |
||||||
|
// TODO: 实例下线 |
||||||
|
} |
||||||
|
} |
||||||
|
``` |
||||||
|
|
||||||
|
2.在`com.gitee.sop.registryapi.config.BaseRegistryConfig`中新增 |
||||||
|
|
||||||
|
```java |
||||||
|
/** |
||||||
|
* 当配置了registry.name=nacos生效 |
||||||
|
* |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
@Bean |
||||||
|
@ConditionalOnProperty(prefix = "registry", name = "name", havingValue = "nacos") |
||||||
|
RegistryService registryServiceNacos() { |
||||||
|
return new RegistryServiceNacos(); |
||||||
|
} |
||||||
|
``` |
||||||
|
|
||||||
|
其中`@ConditionalOnProperty(prefix = "registry", name = "name", havingValue = "nacos")` |
||||||
|
表示`application.properties`配置了`registry.name=nacos`参数才能生效,registry.name=nacos下文会讲到。 |
||||||
|
|
||||||
|
|
||||||
|
- 微服务端修改 |
||||||
|
|
||||||
|
1.修改微服务应用pom.xml,注释eureka服务发现依赖,添加nacos服务发现依赖 |
||||||
|
|
||||||
|
```xml |
||||||
|
<!-- 注册中心【只能用一个,不用的注释掉】 --> |
||||||
|
<!-- 使用eureka注册中心 |
||||||
|
<dependency> |
||||||
|
<groupId>org.springframework.cloud</groupId> |
||||||
|
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> |
||||||
|
</dependency> |
||||||
|
--> |
||||||
|
|
||||||
|
<!-- 使用nacos注册中心 |
||||||
|
版本 0.2.x.RELEASE 对应的是 Spring Boot 2.x 版本,版本 0.1.x.RELEASE 对应的是 Spring Boot 1.x 版本。 |
||||||
|
https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-alibaba-nacos-discovery |
||||||
|
--> |
||||||
|
<dependency> |
||||||
|
<groupId>org.springframework.cloud</groupId> |
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> |
||||||
|
<version>0.2.2.RELEASE</version> |
||||||
|
</dependency> |
||||||
|
<!-- 注册中心end --> |
||||||
|
``` |
||||||
|
|
||||||
|
2.yml文件新增nacos配置,并注释掉eureka相关配置 |
||||||
|
|
||||||
|
```yaml |
||||||
|
spring: |
||||||
|
cloud: |
||||||
|
# nacos注册中心,和eureka只能用一个 |
||||||
|
nacos: |
||||||
|
discovery: |
||||||
|
server-addr: 127.0.0.1:8848 |
||||||
|
``` |
||||||
|
|
||||||
|
- 网关修改 |
||||||
|
|
||||||
|
找到`sop-gateway`工程,步骤同上 |
||||||
|
|
||||||
|
- SOP-admin修改 |
||||||
|
|
||||||
|
|
||||||
|
修改yml文件,新增nacos服务器地址,`registry.name`填nacos |
||||||
|
|
||||||
|
```yaml |
||||||
|
# 注册中心地址,根据实际情况改,这里只是参数,并不会去注册 |
||||||
|
registry: |
||||||
|
# 使用哪个配置中心,使用eureka,填eureka;使用nacos填nacos |
||||||
|
name: nacos |
||||||
|
eureka-server-addr: http://localhost:1111/eureka/ |
||||||
|
nacos-server-addr: 127.0.0.1:8848 |
||||||
|
``` |
||||||
|
|
||||||
|
- website-server修改 |
||||||
|
|
||||||
|
步骤同`SOP-admin修改` |
||||||
|
|
||||||
|
如果要改成consul注册中心,可参照以上步骤。 |
||||||
|
|
||||||
|
- 参考资料 |
||||||
|
|
||||||
|
1.[nacos介绍及安装](https://nacos.io/zh-cn/docs/quick-start.html) |
||||||
|
2.[nacos spring cloud注册发现](https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html) |
@ -0,0 +1,12 @@ |
|||||||
|
package com.gitee.sop.adminserver.config; |
||||||
|
|
||||||
|
import com.gitee.sop.registryapi.config.BaseRegistryConfig; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author tanghc |
||||||
|
*/ |
||||||
|
@Configuration |
||||||
|
public class RegistryConfig extends BaseRegistryConfig { |
||||||
|
|
||||||
|
} |
@ -1,8 +1,11 @@ |
|||||||
# sop-common |
# sop-common |
||||||
|
|
||||||
- sop-gateway-common:提供给网关使用 |
- sop-gateway-common:提供给网关使用 |
||||||
|
- sop-registry-api:注册中心API |
||||||
- sop-service-common:提供给微服务端使用,需要打成jar |
- sop-service-common:提供给微服务端使用,需要打成jar |
||||||
|
|
||||||
|
正式开发请将这些模块上传的maven私服 |
||||||
|
|
||||||
- 打包成jar:`mvn clean package` |
- 打包成jar:`mvn clean package` |
||||||
- 上传到本机仓库:`mvn clean install` |
- 上传到本机仓库:`mvn clean install` |
||||||
- 上传到maven私服:`mvn clean deploy` |
- 上传到maven私服:`mvn clean deploy` |
@ -1,3 +1,3 @@ |
|||||||
# sop-gateway-common |
# sop-gateway-common |
||||||
|
|
||||||
zuul通用组件,主要封装网关的一些功能。 |
网关通用组件,主要封装网关的一些功能。 |
||||||
|
@ -0,0 +1,74 @@ |
|||||||
|
<?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.gitee.sop</groupId> |
||||||
|
<artifactId>sop-registry-api</artifactId> |
||||||
|
<version>1.11.0-SNAPSHOT</version> |
||||||
|
|
||||||
|
<properties> |
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
||||||
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> |
||||||
|
<java.version>1.8</java.version> |
||||||
|
<okhttp.version>3.11.0</okhttp.version> |
||||||
|
</properties> |
||||||
|
|
||||||
|
<dependencies> |
||||||
|
<dependency> |
||||||
|
<groupId>com.squareup.okhttp3</groupId> |
||||||
|
<artifactId>okhttp</artifactId> |
||||||
|
<version>${okhttp.version}</version> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<!-- nacos sdk --> |
||||||
|
<dependency> |
||||||
|
<groupId>com.alibaba.nacos</groupId> |
||||||
|
<artifactId>nacos-client</artifactId> |
||||||
|
<version>1.0.1</version> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<dependency> |
||||||
|
<groupId>org.springframework</groupId> |
||||||
|
<artifactId>spring-context</artifactId> |
||||||
|
<version>5.1.6.RELEASE</version> |
||||||
|
<optional>true</optional> |
||||||
|
</dependency> |
||||||
|
<dependency> |
||||||
|
<groupId>org.projectlombok</groupId> |
||||||
|
<artifactId>lombok</artifactId> |
||||||
|
<version>1.18.6</version> |
||||||
|
<optional>true</optional> |
||||||
|
</dependency> |
||||||
|
<dependency> |
||||||
|
<groupId>org.springframework.boot</groupId> |
||||||
|
<artifactId>spring-boot-autoconfigure</artifactId> |
||||||
|
<version>2.1.4.RELEASE</version> |
||||||
|
<optional>true</optional> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<dependency> |
||||||
|
<groupId>junit</groupId> |
||||||
|
<artifactId>junit</artifactId> |
||||||
|
<version>4.11</version> |
||||||
|
<scope>test</scope> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
</dependencies> |
||||||
|
|
||||||
|
<build> |
||||||
|
<plugins> |
||||||
|
<plugin> |
||||||
|
<groupId>org.apache.maven.plugins</groupId> |
||||||
|
<artifactId>maven-compiler-plugin</artifactId> |
||||||
|
<version>3.1</version> |
||||||
|
<configuration> |
||||||
|
<source>${java.version}</source> |
||||||
|
<target>${java.version}</target> |
||||||
|
<encoding>UTF-8</encoding> |
||||||
|
</configuration> |
||||||
|
</plugin> |
||||||
|
</plugins> |
||||||
|
</build> |
||||||
|
|
||||||
|
</project> |
@ -0,0 +1,3 @@ |
|||||||
|
# sop-registry-api |
||||||
|
|
||||||
|
统一各注册中心接口 |
@ -1,4 +1,4 @@ |
|||||||
package com.gitee.sop.adminserver.bean; |
package com.gitee.sop.registryapi.bean; |
||||||
|
|
||||||
import lombok.Data; |
import lombok.Data; |
||||||
|
|
@ -1,4 +1,4 @@ |
|||||||
package com.gitee.sop.adminserver.bean; |
package com.gitee.sop.registryapi.bean; |
||||||
|
|
||||||
import lombok.Data; |
import lombok.Data; |
||||||
|
|
@ -1,4 +1,4 @@ |
|||||||
package com.gitee.sop.adminserver.bean; |
package com.gitee.sop.registryapi.bean; |
||||||
|
|
||||||
import lombok.Data; |
import lombok.Data; |
||||||
|
|
@ -1,4 +1,4 @@ |
|||||||
package com.gitee.sop.adminserver.bean; |
package com.gitee.sop.registryapi.bean; |
||||||
|
|
||||||
import lombok.Data; |
import lombok.Data; |
||||||
import org.springframework.util.CollectionUtils; |
import org.springframework.util.CollectionUtils; |
@ -0,0 +1,36 @@ |
|||||||
|
package com.gitee.sop.registryapi.config; |
||||||
|
|
||||||
|
import com.gitee.sop.registryapi.service.RegistryService; |
||||||
|
import com.gitee.sop.registryapi.service.impl.RegistryServiceEureka; |
||||||
|
import com.gitee.sop.registryapi.service.impl.RegistryServiceNacos; |
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author tanghc |
||||||
|
*/ |
||||||
|
public class BaseRegistryConfig { |
||||||
|
|
||||||
|
/** |
||||||
|
* 当配置了registry.name=eureka生效 |
||||||
|
* |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
@Bean |
||||||
|
@ConditionalOnProperty(prefix = "registry", name = "name", havingValue = "eureka") |
||||||
|
RegistryService registryServiceEureka() { |
||||||
|
return new RegistryServiceEureka(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 当配置了registry.name=nacos生效 |
||||||
|
* |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
@Bean |
||||||
|
@ConditionalOnProperty(prefix = "registry", name = "name", havingValue = "nacos") |
||||||
|
RegistryService registryServiceNacos() { |
||||||
|
return new RegistryServiceNacos(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -1,14 +1,14 @@ |
|||||||
package com.gitee.sop.adminserver.service; |
package com.gitee.sop.registryapi.service; |
||||||
|
|
||||||
import com.gitee.sop.adminserver.bean.ServiceInfo; |
import com.gitee.sop.registryapi.bean.ServiceInfo; |
||||||
import com.gitee.sop.adminserver.bean.ServiceInstance; |
import com.gitee.sop.registryapi.bean.ServiceInstance; |
||||||
|
|
||||||
import java.util.List; |
import java.util.List; |
||||||
|
|
||||||
/** |
/** |
||||||
* @author tanghc |
* @author tanghc |
||||||
*/ |
*/ |
||||||
public interface RegistrationService { |
public interface RegistryService { |
||||||
/** |
/** |
||||||
* 获取所有服务列表 |
* 获取所有服务列表 |
||||||
* |
* |
@ -0,0 +1,106 @@ |
|||||||
|
package com.gitee.sop.registryapi.service.impl; |
||||||
|
|
||||||
|
import com.alibaba.nacos.api.exception.NacosException; |
||||||
|
import com.alibaba.nacos.api.naming.NamingFactory; |
||||||
|
import com.alibaba.nacos.api.naming.NamingService; |
||||||
|
import com.alibaba.nacos.api.naming.pojo.Instance; |
||||||
|
import com.alibaba.nacos.api.naming.pojo.ListView; |
||||||
|
import com.gitee.sop.registryapi.bean.ServiceInfo; |
||||||
|
import com.gitee.sop.registryapi.bean.ServiceInstance; |
||||||
|
import com.gitee.sop.registryapi.service.RegistryService; |
||||||
|
import okhttp3.FormBody; |
||||||
|
import okhttp3.OkHttpClient; |
||||||
|
import okhttp3.Request; |
||||||
|
import okhttp3.Response; |
||||||
|
import org.apache.commons.lang3.StringUtils; |
||||||
|
import org.springframework.beans.factory.annotation.Value; |
||||||
|
import org.springframework.util.CollectionUtils; |
||||||
|
|
||||||
|
import javax.annotation.PostConstruct; |
||||||
|
import java.io.IOException; |
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
/** |
||||||
|
* nacos接口实现 |
||||||
|
* @author tanghc |
||||||
|
*/ |
||||||
|
public class RegistryServiceNacos implements RegistryService { |
||||||
|
|
||||||
|
OkHttpClient client = new OkHttpClient(); |
||||||
|
|
||||||
|
@Value("${registry.nacos-server-addr:}") |
||||||
|
private String nacosAddr; |
||||||
|
|
||||||
|
private NamingService namingService; |
||||||
|
|
||||||
|
@PostConstruct |
||||||
|
public void after() throws NacosException { |
||||||
|
if (StringUtils.isNotBlank(nacosAddr)) { |
||||||
|
namingService = NamingFactory.createNamingService(nacosAddr); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public List<ServiceInfo> listAllService(int pageNo, int pageSize) throws Exception { |
||||||
|
ListView<String> servicesOfServer = namingService.getServicesOfServer(pageNo, pageSize); |
||||||
|
List<String> serverList = servicesOfServer.getData(); |
||||||
|
List<ServiceInfo> serviceInfoList = new ArrayList<>(); |
||||||
|
for (String serviceName : serverList) { |
||||||
|
ServiceInfo serviceInfo = new ServiceInfo(); |
||||||
|
serviceInfo.setServiceId(serviceName); |
||||||
|
List<Instance> instanceList = namingService.getAllInstances(serviceName); |
||||||
|
if (!CollectionUtils.isEmpty(instanceList)) { |
||||||
|
serviceInfo.setInstances(new ArrayList<>(instanceList.size())); |
||||||
|
for (Instance instance : instanceList) { |
||||||
|
ServiceInstance serviceInstance = new ServiceInstance(); |
||||||
|
serviceInstance.setInstanceId(instance.getInstanceId()); |
||||||
|
serviceInstance.setServiceId(serviceInfo.getServiceId()); |
||||||
|
serviceInstance.setIp(instance.getIp()); |
||||||
|
serviceInstance.setPort(instance.getPort()); |
||||||
|
boolean isOnline = instance.getWeight() > 0; |
||||||
|
String status = isOnline ? "UP" : "OUT_OF_SERVICE"; |
||||||
|
serviceInstance.setStatus(status); |
||||||
|
serviceInfo.getInstances().add(serviceInstance); |
||||||
|
} |
||||||
|
} |
||||||
|
serviceInfoList.add(serviceInfo); |
||||||
|
} |
||||||
|
return serviceInfoList; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void onlineInstance(ServiceInstance serviceInstance) throws Exception { |
||||||
|
Map<String, String> params = new HashMap<>(); |
||||||
|
// 上线,把权重设置成1
|
||||||
|
params.put("weight", "1"); |
||||||
|
this.updateInstance(serviceInstance, params); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void offlineInstance(ServiceInstance serviceInstance) throws Exception { |
||||||
|
Map<String, String> params = new HashMap<>(); |
||||||
|
// 下线,把权重设置成0
|
||||||
|
params.put("weight", "0"); |
||||||
|
this.updateInstance(serviceInstance, params); |
||||||
|
} |
||||||
|
|
||||||
|
private Response updateInstance(ServiceInstance serviceInstance, Map<String, String> params) throws IOException { |
||||||
|
FormBody.Builder builder = new FormBody.Builder(); |
||||||
|
for (Map.Entry<String, String> entry : params.entrySet()) { |
||||||
|
builder.add(entry.getKey(), String.valueOf(entry.getValue())); |
||||||
|
} |
||||||
|
builder.add("serviceName", serviceInstance.getServiceId()) |
||||||
|
.add("ip", serviceInstance.getIp()) |
||||||
|
.add("port", String.valueOf(serviceInstance.getPort())); |
||||||
|
FormBody formBody = builder.build(); |
||||||
|
final Request request = new Request.Builder() |
||||||
|
.url("http://" + nacosAddr + "/nacos/v1/ns/instance") |
||||||
|
.put(formBody) |
||||||
|
.build(); |
||||||
|
|
||||||
|
return client.newCall(request).execute(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,64 @@ |
|||||||
|
package com.gitee.sop; |
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON; |
||||||
|
import com.gitee.sop.alipay.AlipayApiException; |
||||||
|
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 LoadBalanceTest extends TestBase { |
||||||
|
|
||||||
|
String url = "http://localhost:8081/api"; // zuul
|
||||||
|
String appId = "2019032617262200001"; |
||||||
|
// 支付宝私钥
|
||||||
|
String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXJv1pQFqWNA/++OYEV7WYXwexZK/J8LY1OWlP9X0T6wHFOvxNKRvMkJ5544SbgsJpVcvRDPrcxmhPbi/sAhdO4x2PiPKIz9Yni2OtYCCeaiE056B+e1O2jXoLeXbfi9fPivJZkxH/tb4xfLkH3bA8ZAQnQsoXA0SguykMRZntF0TndUfvDrLqwhlR8r5iRdZLB6F8o8qXH6UPDfNEnf/K8wX5T4EB1b8x8QJ7Ua4GcIUqeUxGHdQpzNbJdaQvoi06lgccmL+PHzminkFYON7alj1CjDN833j7QMHdPtS9l7B67fOU/p2LAAkPMtoVBfxQt9aFj7B8rEhGCz02iJIBAgMBAAECggEARqOuIpY0v6WtJBfmR3lGIOOokLrhfJrGTLF8CiZMQha+SRJ7/wOLPlsH9SbjPlopyViTXCuYwbzn2tdABigkBHYXxpDV6CJZjzmRZ+FY3S/0POlTFElGojYUJ3CooWiVfyUMhdg5vSuOq0oCny53woFrf32zPHYGiKdvU5Djku1onbDU0Lw8w+5tguuEZ76kZ/lUcccGy5978FFmYpzY/65RHCpvLiLqYyWTtaNT1aQ/9pw4jX9HO9NfdJ9gYFK8r/2f36ZE4hxluAfeOXQfRC/WhPmiw/ReUhxPznG/WgKaa/OaRtAx3inbQ+JuCND7uuKeRe4osP2jLPHPP6AUwQKBgQDUNu3BkLoKaimjGOjCTAwtp71g1oo+k5/uEInAo7lyEwpV0EuUMwLA/HCqUgR4K9pyYV+Oyb8d6f0+Hz0BMD92I2pqlXrD7xV2WzDvyXM3s63NvorRooKcyfd9i6ccMjAyTR2qfLkxv0hlbBbsPHz4BbU63xhTJp3Ghi0/ey/1HQKBgQC2VsgqC6ykfSidZUNLmQZe3J0p/Qf9VLkfrQ+xaHapOs6AzDU2H2osuysqXTLJHsGfrwVaTs00ER2z8ljTJPBUtNtOLrwNRlvgdnzyVAKHfOgDBGwJgiwpeE9voB1oAV/mXqSaUWNnuwlOIhvQEBwekqNyWvhLqC7nCAIhj3yvNQKBgQCqYbeec56LAhWP903Zwcj9VvG7sESqXUhIkUqoOkuIBTWFFIm54QLTA1tJxDQGb98heoCIWf5x/A3xNI98RsqNBX5JON6qNWjb7/dobitti3t99v/ptDp9u8JTMC7penoryLKK0Ty3bkan95Kn9SC42YxaSghzqkt+uvfVQgiNGQKBgGxU6P2aDAt6VNwWosHSe+d2WWXt8IZBhO9d6dn0f7ORvcjmCqNKTNGgrkewMZEuVcliueJquR47IROdY8qmwqcBAN7Vg2K7r7CPlTKAWTRYMJxCT1Hi5gwJb+CZF3+IeYqsJk2NF2s0w5WJTE70k1BSvQsfIzAIDz2yE1oPHvwVAoGAA6e+xQkVH4fMEph55RJIZ5goI4Y76BSvt2N5OKZKd4HtaV+eIhM3SDsVYRLIm9ZquJHMiZQGyUGnsvrKL6AAVNK7eQZCRDk9KQz+0GKOGqku0nOZjUbAu6A2/vtXAaAuFSFx1rUQVVjFulLexkXR3KcztL1Qu2k5pB6Si0K/uwQ="; |
||||||
|
|
||||||
|
// 测试稳定性
|
||||||
|
@Test |
||||||
|
public void testLoadBalance() throws Exception { |
||||||
|
int i = 0; |
||||||
|
while (i++ < 50) { |
||||||
|
doTest(); |
||||||
|
Thread.sleep(3000); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void doTest() throws AlipayApiException { |
||||||
|
// 公共请求参数
|
||||||
|
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 = get(url, params);// 发送请求
|
||||||
|
System.out.println(responseData); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,12 @@ |
|||||||
|
package com.gitee.sop.websiteserver.config; |
||||||
|
|
||||||
|
import com.gitee.sop.registryapi.config.BaseRegistryConfig; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author tanghc |
||||||
|
*/ |
||||||
|
@Configuration |
||||||
|
public class RegistryConfig extends BaseRegistryConfig { |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue