Commit d9f0952b by Nepxion

支持设置全局context-path

parent 7ace1323
......@@ -17,6 +17,7 @@ public class DiscoveryConstant {
public static final String SPRING_APPLICATION_CONFIG_FORMAT = "spring.application.config.format";
public static final String SPRING_APPLICATION_CONFIG_PATH = "spring.application.config.path";
public static final String SPRING_APPLICATION_GROUP_KEY = "spring.application.group.key";
public static final String SPRING_APPLICATION_CONTEXT_PATH = "spring.application.context-path";
public static final String SPRING_APPLICATION_NAME = "spring.application.name";
public static final String GROUP = "group";
......
......@@ -18,8 +18,6 @@ import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.nepxion.discovery.common.constant.DiscoveryConstant;
public class RouterEntity implements Serializable {
private static final long serialVersionUID = -4480475963615166799L;
......@@ -27,6 +25,7 @@ public class RouterEntity implements Serializable {
private String version;
private String host;
private int port;
private String contextPath;
private List<RouterEntity> nexts = new ArrayList<RouterEntity>();
......@@ -62,6 +61,14 @@ public class RouterEntity implements Serializable {
this.port = port;
}
public String getContextPath() {
return contextPath;
}
public void setContextPath(String contextPath) {
this.contextPath = contextPath;
}
public List<RouterEntity> getNexts() {
return nexts;
}
......@@ -70,18 +77,6 @@ public class RouterEntity implements Serializable {
this.nexts = nexts;
}
public String toInfo() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[")
.append(DiscoveryConstant.SERVICE_ID).append("=").append(serviceId).append(", ")
.append(DiscoveryConstant.VERSION).append("=").append(version).append(", ")
.append(DiscoveryConstant.HOST).append("=").append(host).append(", ")
.append(DiscoveryConstant.PORT).append("=").append(port)
.append("]");
return stringBuilder.toString();
}
@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
......
package com.nepxion.discovery.common.util;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import org.apache.commons.lang3.StringUtils;
public class UrlUtil {
public static String formatContextPath(String contextPath) {
if (StringUtils.isEmpty(contextPath)) {
contextPath = "/";
} else {
if (!contextPath.startsWith("/")) {
contextPath = "/" + contextPath;
}
if (!contextPath.endsWith("/")) {
contextPath = contextPath + "/";
}
}
return contextPath;
}
}
\ No newline at end of file
......@@ -21,6 +21,7 @@ import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.core.type.TypeReference;
import com.nepxion.discovery.common.entity.ResultEntity;
import com.nepxion.discovery.common.entity.RouterEntity;
import com.nepxion.discovery.common.util.UrlUtil;
import com.nepxion.discovery.console.desktop.context.PropertiesContext;
import com.nepxion.discovery.console.desktop.entity.Instance;
import com.nepxion.discovery.console.desktop.serializer.JacksonSerializer;
......@@ -34,7 +35,7 @@ public class ServiceController {
}
public static Map<String, List<Instance>> getInstanceMap() {
String url = getUrl() + "/console/instance-map";
String url = getUrl() + "console/instance-map";
String result = restTemplate.getForEntity(url, String.class).getBody();
......@@ -43,7 +44,7 @@ public class ServiceController {
}
public static List<String> getVersions(Instance instance) {
String url = getUrl(instance) + "/version/view";
String url = getUrl(instance) + "version/view";
String result = restTemplate.getForEntity(url, String.class).getBody();
......@@ -52,7 +53,7 @@ public class ServiceController {
}
public static List<String> getRules(Instance instance) {
String url = getUrl(instance) + "/config/view";
String url = getUrl(instance) + "config/view";
String result = restTemplate.getForEntity(url, String.class).getBody();
......@@ -61,7 +62,7 @@ public class ServiceController {
}
public static RouterEntity routes(Instance instance, String routeServiceIds) {
String url = getUrl(instance) + "/router/routes";
String url = getUrl(instance) + "router/routes";
String result = restTemplate.postForEntity(url, routeServiceIds, String.class).getBody();
......@@ -70,7 +71,7 @@ public class ServiceController {
}
public static List<ResultEntity> versionUpdate(String serviceId, String version) {
String url = getUrl() + "/console/version/update/" + serviceId;
String url = getUrl() + "console/version/update/" + serviceId;
String result = restTemplate.postForEntity(url, version, String.class).getBody();
......@@ -79,7 +80,7 @@ public class ServiceController {
}
public static String versionUpdate(Instance instance, String version) {
String url = getUrl(instance) + "/version/update";
String url = getUrl(instance) + "version/update";
String result = restTemplate.postForEntity(url, version, String.class).getBody();
......@@ -92,7 +93,7 @@ public class ServiceController {
}
public static List<ResultEntity> versionClear(String serviceId) {
String url = getUrl() + "/console/version/clear/" + serviceId;
String url = getUrl() + "console/version/clear/" + serviceId;
String result = restTemplate.postForEntity(url, null, String.class).getBody();
......@@ -101,7 +102,7 @@ public class ServiceController {
}
public static String versionClear(Instance instance) {
String url = getUrl(instance) + "/version/clear";
String url = getUrl(instance) + "version/clear";
String result = restTemplate.postForEntity(url, null, String.class).getBody();
......@@ -114,7 +115,7 @@ public class ServiceController {
}
public static String remoteConfigUpdate(String group, String serviceId, String config) {
String url = getUrl() + "/console/remote-config/update/" + group + "/" + serviceId;
String url = getUrl() + "console/remote-config/update/" + group + "/" + serviceId;
// 解决中文乱码
HttpHeaders headers = new HttpHeaders();
......@@ -132,7 +133,7 @@ public class ServiceController {
}
public static List<ResultEntity> configUpdate(String serviceId, String config) {
String url = getUrl() + "/console/config/update-sync/" + serviceId;
String url = getUrl() + "console/config/update-sync/" + serviceId;
// 解决中文乱码
HttpHeaders headers = new HttpHeaders();
......@@ -146,7 +147,7 @@ public class ServiceController {
}
public static String configUpdate(Instance instance, String config) {
String url = getUrl(instance) + "/config/update-sync";
String url = getUrl(instance) + "config/update-sync";
// 解决中文乱码
HttpHeaders headers = new HttpHeaders();
......@@ -164,7 +165,7 @@ public class ServiceController {
}
public static String remoteConfigClear(String group, String serviceId) {
String url = getUrl() + "/console/remote-config/clear/" + group + "/" + serviceId;
String url = getUrl() + "console/remote-config/clear/" + group + "/" + serviceId;
String result = restTemplate.postForEntity(url, null, String.class).getBody();
......@@ -177,7 +178,7 @@ public class ServiceController {
}
public static List<ResultEntity> configClear(String serviceId) {
String url = getUrl() + "/console/config/clear/" + serviceId;
String url = getUrl() + "console/config/clear/" + serviceId;
String result = restTemplate.postForEntity(url, null, String.class).getBody();
......@@ -186,7 +187,7 @@ public class ServiceController {
}
public static String configClear(Instance instance) {
String url = getUrl(instance) + "/config/clear";
String url = getUrl(instance) + "config/clear";
String result = restTemplate.postForEntity(url, null, String.class).getBody();
......@@ -199,7 +200,7 @@ public class ServiceController {
}
public static String remoteConfigView(String group, String serviceId) {
String url = getUrl() + "/console/remote-config/view/" + group + "/" + serviceId;
String url = getUrl() + "console/remote-config/view/" + group + "/" + serviceId;
String result = restTemplate.getForEntity(url, String.class).getBody();
......@@ -216,7 +217,7 @@ public class ServiceController {
}
private static String getUrl(Instance instance) {
String url = "http://" + instance.getHost() + ":" + instance.getPort();
String url = "http://" + instance.getHost() + ":" + instance.getPort() + UrlUtil.formatContextPath(instance.getContextPath());
return url;
}
......
......@@ -47,6 +47,12 @@ public class Instance extends InstanceEntity {
this.dynamicRule = dynamicRule;
}
public String getContextPath() {
Map<String, String> metadata = getMetadata();
return metadata.get(DiscoveryConstant.SPRING_APPLICATION_CONTEXT_PATH);
}
public String getFilter() {
Map<String, String> metadata = getMetadata();
String filterKey = metadata.get(DiscoveryConstant.SPRING_APPLICATION_GROUP_KEY);
......
......@@ -24,6 +24,7 @@ import org.springframework.web.client.RestTemplate;
import com.nepxion.discovery.common.constant.DiscoveryConstant;
import com.nepxion.discovery.common.entity.ResultEntity;
import com.nepxion.discovery.common.util.UrlUtil;
import com.nepxion.discovery.console.handler.ConsoleErrorHandler;
public abstract class AbstractRestInvoker {
......@@ -46,11 +47,13 @@ public abstract class AbstractRestInvoker {
List<ResultEntity> resultEntityList = new ArrayList<ResultEntity>();
for (ServiceInstance serviceInstance : serviceInstances) {
Map<String, String> metadata = serviceInstance.getMetadata();
String host = serviceInstance.getHost();
int port = serviceInstance.getPort();
String url = getUrl(host, port);
String result = null;
String contextPath = metadata.get(DiscoveryConstant.SPRING_APPLICATION_CONTEXT_PATH);
String url = "http://" + host + ":" + port + UrlUtil.formatContextPath(contextPath) + getSuffixPath();
String result = null;
try {
checkPermission(serviceInstance);
......@@ -104,7 +107,7 @@ public abstract class AbstractRestInvoker {
protected abstract String getInfo();
protected abstract String getUrl(String host, int port);
protected abstract String getSuffixPath();
protected abstract String doRest(String url);
......
......@@ -25,8 +25,8 @@ public class ConfigClearRestInvoker extends AbstractRestInvoker {
}
@Override
protected String getUrl(String host, int port) {
return "http://" + host + ":" + port + "/config/clear";
protected String getSuffixPath() {
return "config/clear";
}
@Override
......
......@@ -34,8 +34,8 @@ public class ConfigUpdateRestInvoker extends AbstractRestInvoker {
}
@Override
protected String getUrl(String host, int port) {
return "http://" + host + ":" + port + "/config/update-" + (async ? "async" : "sync");
protected String getSuffixPath() {
return "config/update-" + (async ? "async" : "sync");
}
@Override
......
......@@ -29,8 +29,8 @@ public class VersionClearRestInvoker extends AbstractRestInvoker {
}
@Override
protected String getUrl(String host, int port) {
return "http://" + host + ":" + port + "/version/clear";
protected String getSuffixPath() {
return "version/clear";
}
@Override
......
......@@ -29,8 +29,8 @@ public class VersionUpdateRestInvoker extends AbstractRestInvoker {
}
@Override
protected String getUrl(String host, int port) {
return "http://" + host + ":" + port + "/version/update";
protected String getSuffixPath() {
return "version/update";
}
@Override
......
......@@ -34,6 +34,7 @@ import org.springframework.web.client.RestTemplate;
import com.nepxion.discovery.common.constant.DiscoveryConstant;
import com.nepxion.discovery.common.entity.RouterEntity;
import com.nepxion.discovery.common.exception.DiscoveryException;
import com.nepxion.discovery.common.util.UrlUtil;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
@RestController
......@@ -73,10 +74,10 @@ public class RouterEndpoint {
return getRouterEntityList(routeServiceId);
}
@RequestMapping(path = "/route/{routeServiceId}/{routeHost}/{routePort}", method = RequestMethod.GET)
@RequestMapping(path = "/route/{routeServiceId}/{routeHost}/{routePort}/{routeContextPath}", method = RequestMethod.GET)
@ApiOperation(value = "获取指定节点(根据IP和端口)可访问其他节点(根据服务名)的路由信息列表", notes = "", response = List.class, httpMethod = "GET")
public List<RouterEntity> route(@PathVariable(value = "routeServiceId") @ApiParam(value = "目标服务名", required = true) String routeServiceId, @PathVariable(value = "routeHost") @ApiParam(value = "目标服务所在机器的IP地址", required = true) String routeHost, @PathVariable(value = "routePort") @ApiParam(value = "目标服务所在机器的端口号", required = true) int routePort) {
return getRouterEntityList(routeServiceId, routeHost, routePort);
public List<RouterEntity> route(@PathVariable(value = "routeServiceId") @ApiParam(value = "目标服务名", required = true) String routeServiceId, @PathVariable(value = "routeHost") @ApiParam(value = "目标服务所在机器的IP地址", required = true) String routeHost, @PathVariable(value = "routePort") @ApiParam(value = "目标服务所在机器的端口号", required = true) int routePort, @PathVariable(value = "routeContextPath") @ApiParam(value = "目标服务的调用路径前缀", required = true, defaultValue = "/") String routeContextPath) {
return getRouterEntityList(routeServiceId, routeHost, routePort, routeContextPath);
}
@RequestMapping(path = "/routes", method = RequestMethod.POST)
......@@ -98,12 +99,14 @@ public class RouterEndpoint {
String version = pluginAdapter.getVersion();
String host = pluginAdapter.getHost();
int port = pluginAdapter.getPort();
String contextPath = pluginAdapter.getContextPath();
RouterEntity routerEntity = new RouterEntity();
routerEntity.setServiceId(serviceId);
routerEntity.setVersion(version);
routerEntity.setHost(host);
routerEntity.setPort(port);
routerEntity.setContextPath(contextPath);
return routerEntity;
}
......@@ -128,12 +131,14 @@ public class RouterEndpoint {
String version = metadata.get(DiscoveryConstant.VERSION);
String host = instance.getHost();
int port = instance.getPort();
String contextPath = metadata.get(DiscoveryConstant.SPRING_APPLICATION_CONTEXT_PATH);
RouterEntity routerEntity = new RouterEntity();
routerEntity.setServiceId(serviceId);
routerEntity.setVersion(version);
routerEntity.setHost(host);
routerEntity.setPort(port);
routerEntity.setContextPath(contextPath);
routerEntityList.add(routerEntity);
}
......@@ -142,8 +147,8 @@ public class RouterEndpoint {
}
@SuppressWarnings("unchecked")
public List<RouterEntity> getRouterEntityList(String routeServiceId, String routeHost, int routePort) {
String url = "http://" + routeHost + ":" + routePort + "/router/instances/" + routeServiceId;
public List<RouterEntity> getRouterEntityList(String routeServiceId, String routeHost, int routePort, String routeContextPath) {
String url = "http://" + routeHost + ":" + routePort + UrlUtil.formatContextPath(routeContextPath) + "router/instances/" + routeServiceId;
List<Map<String, ?>> instanceList = null;
try {
......@@ -163,12 +168,14 @@ public class RouterEndpoint {
String version = metadata.get(DiscoveryConstant.VERSION);
String host = instance.get(DiscoveryConstant.HOST).toString();
Integer port = (Integer) instance.get(DiscoveryConstant.PORT);
String contextPath = metadata.get(DiscoveryConstant.SPRING_APPLICATION_CONTEXT_PATH);
RouterEntity routerEntity = new RouterEntity();
routerEntity.setServiceId(serviceId);
routerEntity.setVersion(version);
routerEntity.setHost(host);
routerEntity.setPort(port);
routerEntity.setContextPath(contextPath);
routerEntityList.add(routerEntity);
}
......@@ -204,8 +211,9 @@ public class RouterEndpoint {
for (RouterEntity routerEntity : routerEntityList) {
String routeHost = routerEntity.getHost();
int routePort = routerEntity.getPort();
String routeContextPath = routerEntity.getContextPath();
route(routerEntity, serviceId, routeHost, routePort);
route(routerEntity, serviceId, routeHost, routePort, routeContextPath);
retrieveRouterEntityList(routerEntityMap, routerDepth).addAll(routerEntity.getNexts());
}
......@@ -224,8 +232,8 @@ public class RouterEndpoint {
}
}
private void route(RouterEntity routerEntity, String routeServiceId, String routeHost, int routePort) {
List<RouterEntity> routerEntityList = getRouterEntityList(routeServiceId, routeHost, routePort);
private void route(RouterEntity routerEntity, String routeServiceId, String routeHost, int routePort, String routeContextPath) {
List<RouterEntity> routerEntityList = getRouterEntityList(routeServiceId, routeHost, routePort, routeContextPath);
if (CollectionUtils.isNotEmpty(routerEntityList)) {
routerEntity.getNexts().addAll(routerEntityList);
}
......
......@@ -40,6 +40,7 @@ public class ConsulApplicationContextInitializer extends PluginApplicationContex
tags.add(DiscoveryConstant.SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED + "=" + PluginContextAware.isDiscoveryControlEnabled(environment));
tags.add(DiscoveryConstant.SPRING_APPLICATION_CONFIG_REST_CONTROL_ENABLED + "=" + PluginContextAware.isConfigRestControlEnabled(environment));
tags.add(DiscoveryConstant.SPRING_APPLICATION_GROUP_KEY + "=" + PluginContextAware.getGroupKey(environment));
tags.add(DiscoveryConstant.SPRING_APPLICATION_CONTEXT_PATH + "=" + PluginContextAware.getContextPath(environment));
return bean;
} else {
......
......@@ -40,6 +40,7 @@ public class EurekaApplicationContextInitializer extends PluginApplicationContex
metadataMap.put(DiscoveryConstant.SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED, PluginContextAware.isDiscoveryControlEnabled(environment).toString());
metadataMap.put(DiscoveryConstant.SPRING_APPLICATION_CONFIG_REST_CONTROL_ENABLED, PluginContextAware.isConfigRestControlEnabled(environment).toString());
metadataMap.put(DiscoveryConstant.SPRING_APPLICATION_GROUP_KEY, PluginContextAware.getGroupKey(environment));
metadataMap.put(DiscoveryConstant.SPRING_APPLICATION_CONTEXT_PATH, PluginContextAware.getContextPath(environment));
return bean;
} else {
......
......@@ -40,6 +40,7 @@ public class ZookeeperApplicationContextInitializer extends PluginApplicationCon
metadata.put(DiscoveryConstant.SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED, PluginContextAware.isDiscoveryControlEnabled(environment).toString());
metadata.put(DiscoveryConstant.SPRING_APPLICATION_CONFIG_REST_CONTROL_ENABLED, PluginContextAware.isConfigRestControlEnabled(environment).toString());
metadata.put(DiscoveryConstant.SPRING_APPLICATION_GROUP_KEY, PluginContextAware.getGroupKey(environment));
metadata.put(DiscoveryConstant.SPRING_APPLICATION_CONTEXT_PATH, PluginContextAware.getContextPath(environment));
return bean;
} else {
......
......@@ -68,6 +68,11 @@ public abstract class AbstractPluginAdapter implements PluginAdapter {
}
@Override
public String getContextPath() {
return getMetadata().get(DiscoveryConstant.SPRING_APPLICATION_CONTEXT_PATH);
}
@Override
public Map<String, String> getMetadata() {
return registration.getMetadata();
}
......
......@@ -23,6 +23,8 @@ public interface PluginAdapter {
int getPort();
String getContextPath();
Map<String, String> getMetadata();
String getVersion();
......
......@@ -109,6 +109,10 @@ public class PluginContextAware implements ApplicationContextAware {
return getGroupKey(environment);
}
public String getContextPath() {
return getContextPath(environment);
}
public static Boolean isRegisterControlEnabled(Environment environment) {
return environment.getProperty(DiscoveryConstant.SPRING_APPLICATION_REGISTER_CONTROL_ENABLED, Boolean.class, Boolean.TRUE);
}
......@@ -132,4 +136,8 @@ public class PluginContextAware implements ApplicationContextAware {
public static String getGroupKey(Environment environment) {
return environment.getProperty(DiscoveryConstant.SPRING_APPLICATION_GROUP_KEY, String.class, DiscoveryConstant.GROUP);
}
public static String getContextPath(Environment environment) {
return environment.getProperty(DiscoveryConstant.SPRING_APPLICATION_CONTEXT_PATH, String.class, "/");
}
}
\ No newline at end of file
......@@ -15,6 +15,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(value = "discovery-springcloud-example-a")
// Context-patch一旦被设置,在Feign也要带上context-path,外部Postman调用网关或者服务路径也要带context-path
// @FeignClient(value = "discovery-springcloud-example-a/nepxion")
public interface AFeign {
@RequestMapping(path = "/invoke", method = RequestMethod.POST)
String invoke(@RequestBody String value);
......
......@@ -15,6 +15,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(value = "discovery-springcloud-example-b")
// Context-patch一旦被设置,在Feign也要带上context-path,外部Postman调用网关或者服务路径也要带context-path
// @FeignClient(value = "discovery-springcloud-example-b/nepxion")
public interface BFeign {
@RequestMapping(path = "/invoke", method = RequestMethod.POST)
String invoke(@RequestBody String value);
......
......@@ -15,6 +15,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(value = "discovery-springcloud-example-c")
// Context-patch一旦被设置,在Feign也要带上context-path,外部Postman调用网关或者服务路径也要带context-path
// @FeignClient(value = "discovery-springcloud-example-c/nepxion")
public interface CFeign {
@RequestMapping(path = "/invoke", method = RequestMethod.POST)
String invoke(@RequestBody String value);
......
# Spring cloud config
# server.servlet.context-path=/nepxion
# Eureka config
eureka.client.serviceUrl.defaultZone=http://localhost:9528/eureka/
......@@ -52,6 +53,8 @@ management.security.enabled=false
# 为微服务归类的Key,一般通过group字段来归类,例如eureka.instance.metadataMap.group=xxx-group或者eureka.instance.metadataMap.application=xxx-application。缺失则默认为group
# spring.application.group.key=group
# spring.application.group.key=application
# 内置Rest调用路径的前缀,当配置了server.context-path或者server.servlet.context-path时候,需要同步配置下面的值,务必保持一致
# spring.application.context-path=/nepxion
# Plugin strategy config
# 开启和关闭用户自定义和编程灰度路由策略的控制,例如用户根据业务参数的不同,负载均衡到不同的服务器。缺失则默认为true
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment