Merge remote-tracking branch 'origin/master'
commit
d96fb26ce4
16
spring-cloud-alibaba-starters/spring-cloud-starter-dubbo-gateway/src/main/java/com/alibaba/cloud/dubbo/gateway/DubboCloudGatewayConstants.java → spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/registry/NacosRegistrationCustomizer.java
16
spring-cloud-alibaba-starters/spring-cloud-starter-dubbo-gateway/src/main/java/com/alibaba/cloud/dubbo/gateway/DubboCloudGatewayConstants.java → spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/registry/NacosRegistrationCustomizer.java
@ -0,0 +1,104 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.alibaba.cloud.nacos.registry;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationHandler;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientConfiguration;
|
||||||
|
import com.alibaba.nacos.api.NacosFactory;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.powermock.api.mockito.PowerMockito;
|
||||||
|
import org.powermock.api.support.MethodProxy;
|
||||||
|
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||||
|
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||||
|
import org.powermock.modules.junit4.PowerMockRunner;
|
||||||
|
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author L.cm
|
||||||
|
*/
|
||||||
|
@RunWith(PowerMockRunner.class)
|
||||||
|
@PowerMockIgnore("javax.management.*")
|
||||||
|
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||||
|
@PrepareForTest({ NacosFactory.class })
|
||||||
|
@SpringBootTest(classes = NacosRegistrationCustomizerTest.TestConfig.class,
|
||||||
|
properties = { "spring.application.name=myTestService1",
|
||||||
|
"spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848" },
|
||||||
|
webEnvironment = RANDOM_PORT)
|
||||||
|
public class NacosRegistrationCustomizerTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private NacosAutoServiceRegistration nacosAutoServiceRegistration;
|
||||||
|
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
Method method = PowerMockito.method(NacosFactory.class, "createNamingService",
|
||||||
|
Properties.class);
|
||||||
|
MethodProxy.proxy(method, new InvocationHandler() {
|
||||||
|
@Override
|
||||||
|
public Object invoke(Object proxy, Method method, Object[] args)
|
||||||
|
throws Throwable {
|
||||||
|
return new MockNamingService();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void contextLoads() throws Exception {
|
||||||
|
NacosRegistration registration = nacosAutoServiceRegistration.getRegistration();
|
||||||
|
Map<String, String> metadata = registration.getMetadata();
|
||||||
|
Assert.assertEquals("test1", metadata.get("test1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
@ImportAutoConfiguration({ AutoServiceRegistrationConfiguration.class,
|
||||||
|
NacosDiscoveryClientConfiguration.class,
|
||||||
|
NacosServiceRegistryAutoConfiguration.class })
|
||||||
|
public static class TestConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public NacosRegistrationCustomizer nacosRegistrationCustomizer() {
|
||||||
|
return registration -> {
|
||||||
|
Map<String, String> metadata = registration.getMetadata();
|
||||||
|
metadata.put("test1", "test1");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,167 +0,0 @@
|
|||||||
<?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">
|
|
||||||
|
|
||||||
<parent>
|
|
||||||
<groupId>com.alibaba.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-alibaba-starters</artifactId>
|
|
||||||
<version>${revision}</version>
|
|
||||||
<relativePath>../pom.xml</relativePath>
|
|
||||||
</parent>
|
|
||||||
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
|
|
||||||
<artifactId>spring-cloud-starter-dubbo-gateway</artifactId>
|
|
||||||
<name>Spring Cloud Starter Dubbo Gateway</name>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
|
|
||||||
<!-- Spring Boot dependencies -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-actuator</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-actuator-autoconfigure</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- Spring Cloud dependencies -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-commons</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-context</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- <!– Zuul –>-->
|
|
||||||
<!-- <dependency>-->
|
|
||||||
<!-- <groupId>org.springframework.cloud</groupId>-->
|
|
||||||
<!-- <artifactId>spring-cloud-starter-netflix-zuul</artifactId>-->
|
|
||||||
<!-- <optional>true</optional>-->
|
|
||||||
<!-- </dependency>-->
|
|
||||||
|
|
||||||
<!-- Spring Cloud Gateway -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- Dubbo Spring Cloud Starter -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.alibaba.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-starter-dubbo</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.commons</groupId>
|
|
||||||
<artifactId>commons-lang3</artifactId>
|
|
||||||
<version>3.11</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- Testing -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-test</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-test</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- Eureka Service Discovery -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- Zookeeper Service Discovery -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
|
|
||||||
<version>${spring-cloud-zookeeper.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<groupId>org.apache.zookeeper</groupId>
|
|
||||||
<artifactId>zookeeper</artifactId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.zookeeper</groupId>
|
|
||||||
<artifactId>zookeeper</artifactId>
|
|
||||||
<version>3.4.14</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<groupId>org.slf4j</groupId>
|
|
||||||
<artifactId>slf4j-log4j12</artifactId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.curator</groupId>
|
|
||||||
<artifactId>curator-framework</artifactId>
|
|
||||||
<version>${curator.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- Spring Cloud Consul -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
|
|
||||||
<version>${spring-cloud-consul.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- Nacos Service Discovery -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.alibaba.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
</project>
|
|
@ -1,164 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013-2018 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.alibaba.cloud.dubbo.gateway;
|
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import com.alibaba.cloud.dubbo.http.MutableHttpServerRequest;
|
|
||||||
import com.alibaba.cloud.dubbo.metadata.DubboRestServiceMetadata;
|
|
||||||
import com.alibaba.cloud.dubbo.metadata.RequestMetadata;
|
|
||||||
import com.alibaba.cloud.dubbo.metadata.RestMethodMetadata;
|
|
||||||
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
|
|
||||||
import com.alibaba.cloud.dubbo.service.DubboGenericServiceExecutionContext;
|
|
||||||
import com.alibaba.cloud.dubbo.service.DubboGenericServiceExecutionContextFactory;
|
|
||||||
import com.alibaba.cloud.dubbo.service.DubboGenericServiceFactory;
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
import org.apache.dubbo.rpc.service.GenericException;
|
|
||||||
import org.apache.dubbo.rpc.service.GenericService;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.ObjectProvider;
|
|
||||||
import org.springframework.core.convert.ConversionService;
|
|
||||||
import org.springframework.format.support.DefaultFormattingConversionService;
|
|
||||||
import org.springframework.http.HttpRequest;
|
|
||||||
|
|
||||||
import static com.alibaba.cloud.dubbo.http.util.HttpUtils.getParameters;
|
|
||||||
import static org.apache.commons.lang3.StringUtils.substringAfter;
|
|
||||||
import static org.apache.commons.lang3.StringUtils.substringBetween;
|
|
||||||
import static org.apache.dubbo.common.constants.CommonConstants.PATH_SEPARATOR;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The executor of Dubbo Cloud Gateway that handles the HTTP request and responses the
|
|
||||||
* result of execution of the generic invocation to the Dubbo service providers.
|
|
||||||
*
|
|
||||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class DubboCloudGatewayExecutor {
|
|
||||||
|
|
||||||
private final Log logger = LogFactory.getLog(getClass());
|
|
||||||
|
|
||||||
private final DubboServiceMetadataRepository repository;
|
|
||||||
|
|
||||||
private final DubboGenericServiceFactory serviceFactory;
|
|
||||||
|
|
||||||
private final DubboGenericServiceExecutionContextFactory contextFactory;
|
|
||||||
|
|
||||||
private final DubboCloudGatewayProperties dubboCloudGatewayProperties;
|
|
||||||
|
|
||||||
private final ConversionService conversionService;
|
|
||||||
|
|
||||||
private final Map<String, Object> dubboTranslatedAttributes = new HashMap<>();
|
|
||||||
|
|
||||||
public DubboCloudGatewayExecutor(DubboServiceMetadataRepository repository,
|
|
||||||
DubboGenericServiceFactory serviceFactory,
|
|
||||||
DubboGenericServiceExecutionContextFactory contextFactory,
|
|
||||||
DubboCloudGatewayProperties dubboCloudGatewayProperties,
|
|
||||||
ObjectProvider<ConversionService> conversionService) {
|
|
||||||
this.repository = repository;
|
|
||||||
this.serviceFactory = serviceFactory;
|
|
||||||
this.contextFactory = contextFactory;
|
|
||||||
this.dubboCloudGatewayProperties = dubboCloudGatewayProperties;
|
|
||||||
this.conversionService = conversionService
|
|
||||||
.getIfAvailable(DefaultFormattingConversionService::new);
|
|
||||||
// TODO : Replace these hard-code configurations
|
|
||||||
this.dubboTranslatedAttributes.put("protocol", "dubbo");
|
|
||||||
this.dubboTranslatedAttributes.put("cluster", "failover");
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object execute(HttpRequest request) {
|
|
||||||
|
|
||||||
String serviceName = resolveServiceName(request);
|
|
||||||
|
|
||||||
String restPath = substringAfter(request.getURI().getPath(), serviceName);
|
|
||||||
|
|
||||||
// 初始化 serviceName 的 REST 请求元数据
|
|
||||||
repository.initializeMetadata(serviceName);
|
|
||||||
// 将 HttpServletRequest 转化为 RequestMetadata
|
|
||||||
RequestMetadata clientMetadata = buildRequestMetadata(request, restPath);
|
|
||||||
|
|
||||||
DubboRestServiceMetadata dubboRestServiceMetadata = repository.get(serviceName,
|
|
||||||
clientMetadata);
|
|
||||||
|
|
||||||
Object result = null;
|
|
||||||
|
|
||||||
if (dubboRestServiceMetadata != null) {
|
|
||||||
|
|
||||||
RestMethodMetadata dubboRestMethodMetadata = dubboRestServiceMetadata
|
|
||||||
.getRestMethodMetadata();
|
|
||||||
|
|
||||||
GenericService genericService = serviceFactory
|
|
||||||
.create(dubboRestServiceMetadata, dubboTranslatedAttributes);
|
|
||||||
|
|
||||||
byte[] body = getRequestBody(request);
|
|
||||||
|
|
||||||
MutableHttpServerRequest httpServerRequest = new MutableHttpServerRequest(
|
|
||||||
request, body);
|
|
||||||
|
|
||||||
DubboGenericServiceExecutionContext context = contextFactory
|
|
||||||
.create(dubboRestMethodMetadata, httpServerRequest);
|
|
||||||
|
|
||||||
GenericException exception = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
result = genericService.$invoke(context.getMethodName(),
|
|
||||||
context.getParameterTypes(), context.getParameters());
|
|
||||||
|
|
||||||
String returnType = dubboRestMethodMetadata.getReturnType();
|
|
||||||
|
|
||||||
logger.info("The result is " + result);
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (GenericException e) {
|
|
||||||
exception = e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private String resolveServiceName(HttpRequest request) {
|
|
||||||
URI uri = request.getURI();
|
|
||||||
String requestURI = uri.getPath();
|
|
||||||
String servletPath = dubboCloudGatewayProperties.getContextPath();
|
|
||||||
String part = substringAfter(requestURI, servletPath);
|
|
||||||
String serviceName = substringBetween(part, PATH_SEPARATOR, PATH_SEPARATOR);
|
|
||||||
return serviceName;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* TODO : Get the Request Body from HttpRequest.
|
|
||||||
* @param request {@link HttpRequest}
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private byte[] getRequestBody(HttpRequest request) {
|
|
||||||
return new byte[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
private RequestMetadata buildRequestMetadata(HttpRequest request, String restPath) {
|
|
||||||
RequestMetadata requestMetadata = new RequestMetadata();
|
|
||||||
requestMetadata.setPath(restPath);
|
|
||||||
requestMetadata.setMethod(request.getMethod().toString());
|
|
||||||
requestMetadata.setParams(getParameters(request));
|
|
||||||
requestMetadata.setHeaders(request.getHeaders());
|
|
||||||
return requestMetadata;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,57 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013-2018 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.alibaba.cloud.dubbo.gateway;
|
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
||||||
|
|
||||||
import static com.alibaba.cloud.dubbo.gateway.DubboCloudGatewayConstants.CONFIG_PROPERTY_PREFIX;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The Configuration Properties for Dubbo Cloud Gateway.
|
|
||||||
*
|
|
||||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
|
||||||
*/
|
|
||||||
@ConfigurationProperties(prefix = CONFIG_PROPERTY_PREFIX)
|
|
||||||
public class DubboCloudGatewayProperties {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Enabled or not.
|
|
||||||
*/
|
|
||||||
private boolean enabled = true;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The context path for the gateway request mapping.
|
|
||||||
*/
|
|
||||||
private String contextPath = "";
|
|
||||||
|
|
||||||
public boolean isEnabled() {
|
|
||||||
return enabled;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setEnabled(boolean enabled) {
|
|
||||||
this.enabled = enabled;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getContextPath() {
|
|
||||||
return contextPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setContextPath(String contextPath) {
|
|
||||||
this.contextPath = contextPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,56 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013-2018 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.alibaba.cloud.dubbo.gateway.autoconfigure;
|
|
||||||
|
|
||||||
import com.alibaba.cloud.dubbo.autoconfigure.DubboMetadataAutoConfiguration;
|
|
||||||
import com.alibaba.cloud.dubbo.autoconfigure.DubboServiceAutoConfiguration;
|
|
||||||
import com.alibaba.cloud.dubbo.gateway.DubboCloudGatewayExecutor;
|
|
||||||
import com.alibaba.cloud.dubbo.gateway.DubboCloudGatewayProperties;
|
|
||||||
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
|
|
||||||
import com.alibaba.cloud.dubbo.service.DubboGenericServiceExecutionContextFactory;
|
|
||||||
import com.alibaba.cloud.dubbo.service.DubboGenericServiceFactory;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.ObjectProvider;
|
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.core.convert.ConversionService;
|
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
|
||||||
@ConditionalOnProperty(prefix = "dubbo.cloud.gateway", name = "enabled",
|
|
||||||
havingValue = "true", matchIfMissing = true)
|
|
||||||
@AutoConfigureAfter({ DubboServiceAutoConfiguration.class,
|
|
||||||
DubboMetadataAutoConfiguration.class })
|
|
||||||
@EnableConfigurationProperties(DubboCloudGatewayProperties.class)
|
|
||||||
public class DubboCloudGatewayAutoConfiguration {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnMissingBean(DubboCloudGatewayExecutor.class)
|
|
||||||
public DubboCloudGatewayExecutor dubboCloudGatewayExecutor(
|
|
||||||
DubboServiceMetadataRepository repository,
|
|
||||||
DubboGenericServiceFactory serviceFactory,
|
|
||||||
DubboGenericServiceExecutionContextFactory contextFactory,
|
|
||||||
DubboCloudGatewayProperties dubboCloudGatewayProperties,
|
|
||||||
ObjectProvider<ConversionService> conversionServices) {
|
|
||||||
return new DubboCloudGatewayExecutor(repository, serviceFactory, contextFactory,
|
|
||||||
dubboCloudGatewayProperties, conversionServices);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,75 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013-2018 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.alibaba.cloud.dubbo.gateway.autoconfigure;
|
|
||||||
|
|
||||||
import com.alibaba.cloud.dubbo.autoconfigure.DubboMetadataAutoConfiguration;
|
|
||||||
import com.alibaba.cloud.dubbo.autoconfigure.DubboServiceAutoConfiguration;
|
|
||||||
import com.alibaba.cloud.dubbo.gateway.DubboCloudGatewayExecutor;
|
|
||||||
import com.alibaba.cloud.dubbo.gateway.DubboCloudGatewayProperties;
|
|
||||||
import com.alibaba.cloud.dubbo.gateway.bootstrap.DubboSpringCloudGatewayFilter;
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
||||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
||||||
import org.springframework.cloud.gateway.route.RouteLocator;
|
|
||||||
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.http.codec.ServerCodecConfigurer;
|
|
||||||
import org.springframework.http.codec.support.DefaultServerCodecConfigurer;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The Auto-{@link Configuration} of Dubbo Spring Cloud Gateway.
|
|
||||||
*
|
|
||||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
|
||||||
*/
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
|
||||||
@ConditionalOnClass(GlobalFilter.class)
|
|
||||||
@ConditionalOnProperty(prefix = "dubbo.cloud.gateway", name = "enabled",
|
|
||||||
havingValue = "true", matchIfMissing = true)
|
|
||||||
@AutoConfigureAfter({ DubboServiceAutoConfiguration.class,
|
|
||||||
DubboMetadataAutoConfiguration.class, DubboCloudGatewayAutoConfiguration.class })
|
|
||||||
@EnableConfigurationProperties(DubboCloudGatewayProperties.class)
|
|
||||||
public class DubboSpringCloudGatewayAutoConfiguration {
|
|
||||||
|
|
||||||
private final Log logger = LogFactory.getLog(getClass());
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnMissingBean(ServerCodecConfigurer.class)
|
|
||||||
public ServerCodecConfigurer serverCodecConfigurer() {
|
|
||||||
return new DefaultServerCodecConfigurer();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public DubboSpringCloudGatewayFilter dubboSpringCloudGatewayFilter(
|
|
||||||
DubboCloudGatewayExecutor dubboCloudGatewayExecutor) {
|
|
||||||
return new DubboSpringCloudGatewayFilter(dubboCloudGatewayExecutor);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) {
|
|
||||||
return builder.routes()
|
|
||||||
.route(r -> r.path("/**").uri("http://localhost:9090").id("first"))
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,74 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013-2018 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.alibaba.cloud.dubbo.gateway.bootstrap;
|
|
||||||
|
|
||||||
import com.alibaba.cloud.dubbo.gateway.DubboCloudGatewayExecutor;
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
import reactor.core.publisher.Mono;
|
|
||||||
|
|
||||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
|
||||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
||||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
||||||
import org.springframework.core.Ordered;
|
|
||||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The Spring Cloud {@link GatewayFilter Gateway Filter} for Dubbo.
|
|
||||||
*
|
|
||||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
|
||||||
*/
|
|
||||||
public class DubboSpringCloudGatewayFilter
|
|
||||||
implements GatewayFilter, GlobalFilter, Ordered {
|
|
||||||
|
|
||||||
private final Log logger = LogFactory.getLog(getClass());
|
|
||||||
|
|
||||||
private int order;
|
|
||||||
|
|
||||||
private final DubboCloudGatewayExecutor dubboCloudGatewayExecutor;
|
|
||||||
|
|
||||||
public DubboSpringCloudGatewayFilter(
|
|
||||||
DubboCloudGatewayExecutor dubboCloudGatewayExecutor) {
|
|
||||||
this.dubboCloudGatewayExecutor = dubboCloudGatewayExecutor;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
|
||||||
|
|
||||||
Object result = dubboCloudGatewayExecutor.execute(exchange.getRequest());
|
|
||||||
|
|
||||||
if (result == null) {
|
|
||||||
chain.filter(exchange);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
ServerHttpResponse response = exchange.getResponse();
|
|
||||||
}
|
|
||||||
|
|
||||||
return Mono.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setOrder(int order) {
|
|
||||||
this.order = order;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getOrder() {
|
|
||||||
return order;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,3 +0,0 @@
|
|||||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
|
||||||
com.alibaba.cloud.dubbo.gateway.autoconfigure.DubboCloudGatewayAutoConfiguration,\
|
|
||||||
com.alibaba.cloud.dubbo.gateway.autoconfigure.DubboSpringCloudGatewayAutoConfiguration
|
|
@ -1,40 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013-2018 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.alibaba.cloud.dubbo.gateway.bootstrap;
|
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
||||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
|
||||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The bootstrap class of Dubbo Spring Cloud Gateway.
|
|
||||||
*
|
|
||||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
|
||||||
*/
|
|
||||||
@EnableAutoConfiguration
|
|
||||||
@EnableDiscoveryClient
|
|
||||||
public final class DubboSpringCloudGatewayBootstrap {
|
|
||||||
|
|
||||||
private DubboSpringCloudGatewayBootstrap() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
new SpringApplicationBuilder(DubboSpringCloudGatewayBootstrap.class)
|
|
||||||
.properties("spring.profiles.active=nacos").run(args);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
dubbo:
|
|
||||||
protocols:
|
|
||||||
dubbo:
|
|
||||||
port: -1
|
|
||||||
|
|
||||||
server:
|
|
||||||
port: 9090
|
|
@ -1,66 +0,0 @@
|
|||||||
spring:
|
|
||||||
application:
|
|
||||||
name: spring-cloud-alibaba-dubbo-gateway
|
|
||||||
main:
|
|
||||||
allow-bean-definition-overriding: true
|
|
||||||
|
|
||||||
|
|
||||||
# default disable all
|
|
||||||
cloud:
|
|
||||||
nacos:
|
|
||||||
username: nacos
|
|
||||||
password: nacos
|
|
||||||
discovery:
|
|
||||||
enabled: false
|
|
||||||
register-enabled: false
|
|
||||||
zookeeper:
|
|
||||||
enabled: false
|
|
||||||
consul:
|
|
||||||
enabled: false
|
|
||||||
|
|
||||||
eureka:
|
|
||||||
client:
|
|
||||||
enabled: false
|
|
||||||
|
|
||||||
|
|
||||||
---
|
|
||||||
spring:
|
|
||||||
profiles: nacos
|
|
||||||
|
|
||||||
cloud:
|
|
||||||
nacos:
|
|
||||||
discovery:
|
|
||||||
enabled: true
|
|
||||||
register-enabled: true
|
|
||||||
server-addr: 127.0.0.1:8848
|
|
||||||
|
|
||||||
|
|
||||||
---
|
|
||||||
spring:
|
|
||||||
profiles: eureka
|
|
||||||
|
|
||||||
eureka:
|
|
||||||
client:
|
|
||||||
enabled: true
|
|
||||||
service-url:
|
|
||||||
defaultZone: http://127.0.0.1:8761/eureka/
|
|
||||||
|
|
||||||
|
|
||||||
---
|
|
||||||
spring:
|
|
||||||
profiles: zookeeper
|
|
||||||
cloud:
|
|
||||||
zookeeper:
|
|
||||||
enabled: true
|
|
||||||
connect-string: 127.0.0.1:2181
|
|
||||||
|
|
||||||
|
|
||||||
---
|
|
||||||
spring:
|
|
||||||
profiles: consul
|
|
||||||
|
|
||||||
cloud:
|
|
||||||
consul:
|
|
||||||
enabled: true
|
|
||||||
host: 127.0.0.1
|
|
||||||
port: 8500
|
|
Loading…
Reference in New Issue