修复springmvc路由获取问题

pull/1/head
tanghc 5 years ago
parent 9d6d24bd5b
commit 99fd427eaa
  1. 17
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ServiceRoutesLoader.java
  2. 10
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceRouteInfoBuilder.java
  3. 21
      sop-example/sop-springmvc/src/main/java/com/gitee/app/config/OpenServiceConfig.java

@ -34,6 +34,8 @@ import java.util.Objects;
@Slf4j
public class ServiceRoutesLoader<T extends TargetRoute> {
private static final String SERVER_CONTEXT_PATH = "server.servlet.context-path";
private static final String SECRET = "a3d9sf!1@odl90zd>fkASwq";
private static final int FIVE_SECONDS = 1000 * 5;
@ -95,35 +97,40 @@ public class ServiceRoutesLoader<T extends TargetRoute> {
List<Instance> allInstances = namingService.getAllInstances(serviceName);
if (CollectionUtils.isEmpty(allInstances)) {
// 如果没有服务列表,则删除所有路由信息
log.info("服务下线,删除路由配置,serviceId: {}", serviceName);
baseRouteCache.remove(serviceName);
configService.removeConfig(dataId, groupId);
} else {
for (Instance instance : allInstances) {
String url = getRouteRequestUrl(instance);
log.info("拉取路由配置,serviceId: {}, url: {}", serviceName, url);
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
if (responseEntity.getStatusCode() == HttpStatus.OK) {
String body = responseEntity.getBody();
ServiceRouteInfo serviceRouteInfo = JSON.parseObject(body, ServiceRouteInfo.class);
baseRouteCache.load(serviceRouteInfo, callback -> {
try {
log.info("推送路由配置到nacos,dataId={}, groupId={}",dataId, groupId);
log.info("推送路由配置到nacos,dataId: {}, groupId: {}", dataId, groupId);
configService.publishConfig(dataId, groupId, body);
} catch (NacosException e) {
log.error("nacos推送失败,serviceId:{}, instance:{}",serviceName, instance);
log.error("nacos推送失败,serviceId: {}, instance: {}", serviceName, instance);
}
});
} else {
log.error("拉取路由配置异常,url: {}, status: {}, body: {}", url, responseEntity.getStatusCodeValue(), responseEntity.getBody());
}
}
}
} catch (NacosException e) {
log.error("选择服务实例失败,serviceName:{}", serviceName, e);
} catch (Exception e) {
log.error("选择服务实例失败,serviceName: {}", serviceName, e);
}
}
}
private static String getRouteRequestUrl(Instance instance) {
String query = buildQuery(SECRET);
return "http://" + instance.getIp() + ":" + instance.getPort() + "/sop/routes" + query;
String contextPath = instance.getMetadata().getOrDefault(SERVER_CONTEXT_PATH, "");
return "http://" + instance.getIp() + ":" + instance.getPort() + contextPath + "/sop/routes" + query;
}
private static String buildQuery(String secret) {

@ -118,8 +118,14 @@ public class ServiceRouteInfoBuilder {
if (servletPath == null) {
servletPath = "";
}
servletPath = StringUtils.trimLeadingCharacter(servletPath, '/');
return contextPath + servletPath;
if (!servletPath.startsWith(PATH_SPLIT)) {
servletPath = PATH_SPLIT + servletPath;
}
if (DEFAULT_CONTEXT_PATH.equals(contextPath)) {
return servletPath;
} else {
return contextPath + servletPath;
}
}
private void checkPath(String path, String errorMsg) {

@ -4,6 +4,7 @@ import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.annotation.NacosProperties;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.client.naming.utils.NetUtils;
import com.alibaba.nacos.spring.context.annotation.discovery.EnableNacosDiscovery;
import com.gitee.sop.servercommon.bean.ServiceConfig;
@ -21,6 +22,7 @@ public class OpenServiceConfig extends SpringMvcServiceConfiguration {
public static final String SPRING_APPLICATION_NAME = "spring.application.name";
public static final String SERVER_CONTEXT_PATH = "server.servlet.context-path";
public static final String SERVER_IP = "server.ip";
public static final String SERVER_PORT = "server.port";
@ -28,7 +30,8 @@ public class OpenServiceConfig extends SpringMvcServiceConfiguration {
ServiceConfig.getInstance().setDefaultVersion("1.0");
}
// 这两个参数需要从配置文件中获取
/** 对应tomcat中的contextPath */
private String contextPath = "/sop-springmvc";
private String serviceId = "sop-springmvc";
private int port = 2223;
@ -39,11 +42,14 @@ public class OpenServiceConfig extends SpringMvcServiceConfiguration {
protected void doAfter() {
super.doAfter();
try {
System.setProperty(SPRING_APPLICATION_NAME, serviceId);
String ip = NetUtils.localIP();
namingService.registerInstance(serviceId, ip, port);
System.setProperty(SPRING_APPLICATION_NAME, serviceId);
System.setProperty(SERVER_IP, ip);
System.setProperty(SERVER_PORT, String.valueOf(port));
System.setProperty(SERVER_CONTEXT_PATH, contextPath);
Instance instance = this.getInstance(serviceId, ip, port, contextPath);
namingService.registerInstance(serviceId, instance);
log.info("注册到nacos, serviceId:{}, ip:{}, port:{}", serviceId, ip, port);
} catch (NacosException e) {
log.error("注册nacos失败", e);
@ -51,4 +57,13 @@ public class OpenServiceConfig extends SpringMvcServiceConfiguration {
}
}
private Instance getInstance(String serviceId, String ip, int port, String contextPath) {
Instance instance = new Instance();
instance.setServiceName(serviceId);
instance.setIp(ip);
instance.setPort(port);
instance.getMetadata().put(SERVER_CONTEXT_PATH, contextPath);
return instance;
}
}

Loading…
Cancel
Save