mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
4.0
This commit is contained in:
@@ -10,7 +10,7 @@ import com.gitee.sop.gatewaycommon.bean.InstanceDefinition;
|
||||
import com.gitee.sop.gatewaycommon.bean.ServiceGrayDefinition;
|
||||
import com.gitee.sop.gatewaycommon.manager.DefaultEnvGrayManager;
|
||||
import com.gitee.sop.gatewaycommon.route.RegistryEvent;
|
||||
import com.gitee.sop.gatewaycommon.zuul.loadbalancer.ServiceGrayConfig;
|
||||
import com.gitee.sop.gatewaycommon.loadbalancer.ServiceGrayConfig;
|
||||
import com.google.common.collect.Sets;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
35
sop-gateway/src/main/resources/META-INF/gateway.properties
Normal file
35
sop-gateway/src/main/resources/META-INF/gateway.properties
Normal file
@@ -0,0 +1,35 @@
|
||||
# 固定不变,不能改
|
||||
spring.application.name=sop-gateway
|
||||
# 不用改,如果要改,请全局替换修改
|
||||
sop.secret=MZZOUSTua6LzApIWXCwEgbBmxSzpzC
|
||||
|
||||
# 网关入口
|
||||
sop.gateway-index-path=/
|
||||
|
||||
# nacos cloud配置
|
||||
spring.cloud.nacos.discovery.server-addr=${register.url}
|
||||
|
||||
# eureka地址
|
||||
eureka.client.serviceUrl.defaultZone=${register.url}
|
||||
|
||||
# 数据库配置
|
||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
spring.datasource.url=jdbc:mysql://${mysql.host}/sop?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
|
||||
spring.datasource.username=${mysql.username}
|
||||
spring.datasource.password=${mysql.password}
|
||||
|
||||
|
||||
spring.cloud.gateway.discovery.locator.lower-case-service-id=true
|
||||
spring.cloud.gateway.discovery.locator.enabled=true
|
||||
|
||||
# 不用改
|
||||
mybatis.fill.com.gitee.fastmybatis.core.support.DateFillInsert=gmt_create
|
||||
mybatis.fill.com.gitee.fastmybatis.core.support.DateFillUpdate=gmt_modified
|
||||
|
||||
# 文件上传配置
|
||||
spring.servlet.multipart.enabled=true
|
||||
# 这里设置大一点没关系,真实大小由upload.max-file-size控制
|
||||
spring.servlet.multipart.max-file-size=50MB
|
||||
|
||||
# 允许上传文件大小,不能超过这个值,单位:B,KB,MB
|
||||
upload.max-file-size=2MB
|
@@ -1,3 +1,4 @@
|
||||
# 自定义自动配置类,如果有多个类,使用逗号(,)分隔,\正斜杠标示换行还可以读取到指定的类
|
||||
org.springframework.boot.env.EnvironmentPostProcessor=com.gitee.sop.gatewaycommon.config.SopGatewayEnvironmentPostProcessor
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.gitee.sop.bridge.SopGatewayAutoConfiguration
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.gitee.sop.gatewaycommon.config.SopGatewayAutoConfiguration,\
|
||||
com.gitee.sop.bridge.SopRegisterAutoConfiguration
|
||||
|
@@ -5,7 +5,8 @@ mysql.host=localhost:3306
|
||||
mysql.username=root
|
||||
mysql.password=root
|
||||
|
||||
# nacos地址
|
||||
nacos.url=127.0.0.1:8848
|
||||
# 注册中心地址
|
||||
register.url=127.0.0.1:8848
|
||||
#register.url=http://localhost:1111/eureka/
|
||||
|
||||
logging.level.com.gitee=debug
|
@@ -0,0 +1,22 @@
|
||||
package com.gitee.sop.gateway;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
*/
|
||||
public class ExcludeTest extends TestCase {
|
||||
public void testRegex() {
|
||||
String serviceId = "com.aaa.bbb.story-service";
|
||||
String sopServiceExcludeRegex = "com\\..*;story\\-.*";
|
||||
if (StringUtils.isNotBlank(sopServiceExcludeRegex)) {
|
||||
String[] regexArr = sopServiceExcludeRegex.split(";");
|
||||
for (String regex : regexArr) {
|
||||
if (serviceId.matches(regex)) {
|
||||
System.out.println("111");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,62 @@
|
||||
package com.gitee.sop.gateway;
|
||||
|
||||
import com.gitee.sop.gatewaycommon.util.LoadBalanceUtil;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 轮询选择一台机器。
|
||||
*
|
||||
* @author tanghc
|
||||
*/
|
||||
public class RoundRobinTest extends TestCase {
|
||||
|
||||
public void testDo() {
|
||||
String serviceId = "order-service";
|
||||
List<String> serverList = new ArrayList<>(Arrays.asList("server1", "server2", "server3"));
|
||||
System.out.println(chooseRoundRobinServer(serviceId, serverList));
|
||||
System.out.println(chooseRoundRobinServer(serviceId, serverList));
|
||||
System.out.println(chooseRoundRobinServer(serviceId, serverList));
|
||||
System.out.println(chooseRoundRobinServer(serviceId, serverList));
|
||||
System.out.println(chooseRoundRobinServer(serviceId, serverList));
|
||||
}
|
||||
|
||||
public void testAdd() {
|
||||
String serviceId = "order-service";
|
||||
List<String> serverList = new ArrayList<>(Arrays.asList("server1", "server2", "server3"));
|
||||
System.out.println(chooseRoundRobinServer(serviceId, serverList));
|
||||
System.out.println(chooseRoundRobinServer(serviceId, serverList));
|
||||
// 中途添加一个服务器
|
||||
serverList.add("server4");
|
||||
System.out.println(chooseRoundRobinServer(serviceId, serverList));
|
||||
System.out.println(chooseRoundRobinServer(serviceId, serverList));
|
||||
System.out.println(chooseRoundRobinServer(serviceId, serverList));
|
||||
}
|
||||
|
||||
public void testRemove() {
|
||||
String serviceId = "order-service";
|
||||
List<String> serverList = new ArrayList<>(Arrays.asList("server1", "server2", "server3"));
|
||||
System.out.println(chooseRoundRobinServer(serviceId, serverList));
|
||||
System.out.println(chooseRoundRobinServer(serviceId, serverList));
|
||||
// 中途减少一台服务器
|
||||
serverList.remove(2);
|
||||
System.out.println(chooseRoundRobinServer(serviceId, serverList));
|
||||
System.out.println(chooseRoundRobinServer(serviceId, serverList));
|
||||
System.out.println(chooseRoundRobinServer(serviceId, serverList));
|
||||
}
|
||||
|
||||
/**
|
||||
* 轮询选择一台机器。
|
||||
* 假设有N台服务器:S = {S1, S2, …, Sn},一个指示变量i表示上一次选择的服务器ID。变量i被初始化为N-1。
|
||||
*
|
||||
* @param serviceId serviceId
|
||||
* @param servers 服务器列表
|
||||
* @return 返回一台服务器实例
|
||||
*/
|
||||
private <T> T chooseRoundRobinServer(String serviceId, List<T> servers) {
|
||||
return LoadBalanceUtil.chooseByRoundRobin(serviceId, servers);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user