1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

Polish alibaba/spring-cloud-alibaba#1661 : [Feature] Introducing Dubbo Gateway

This commit is contained in:
mercyblitz
2020-08-25 16:22:12 +08:00
parent 52e6147b10
commit c0bcd52381
18 changed files with 815 additions and 32 deletions

View File

@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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 com.alibaba.cloud.dubbo.util.DubboCloudConstants;
/**
* The constants of Dubbo Cloud Gateway
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
public interface DubboCloudGatewayConstants {
/**
* The property prefix of Configuration
*/
String CONFIG_PROPERTY_PREFIX = DubboCloudConstants.CONFIG_PROPERTY_PREFIX
+ ".gateway";
}

View File

@@ -0,0 +1,166 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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> conversionServices) {
this.repository = repository;
this.serviceFactory = serviceFactory;
this.contextFactory = contextFactory;
this.dubboCloudGatewayProperties = dubboCloudGatewayProperties;
this.conversionService = conversionServices
.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;
}
}

View File

@@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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;
}
}

View File

@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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);
}
}

View File

@@ -0,0 +1,74 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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.standard;
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.autoconfigure.DubboCloudGatewayAutoConfiguration;
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();
}
}

View File

@@ -0,0 +1,74 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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.standard;
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;
}
}

View File

@@ -0,0 +1,3 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.alibaba.cloud.dubbo.gateway.autoconfigure.DubboCloudGatewayAutoConfiguration,\
com.alibaba.cloud.dubbo.gateway.standard.DubboSpringCloudGatewayAutoConfiguration

View File

@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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.standard;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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>
*/
@SpringBootApplication
@EnableDiscoveryClient
public class DubboSpringCloudGatewayBootstrap {
public static void main(String[] args) {
new SpringApplicationBuilder(DubboSpringCloudGatewayBootstrap.class)
.properties("spring.profiles.active=zookeeper").run(args);
}
}

View File

@@ -0,0 +1,74 @@
spring:
application:
name: spring-cloud-alibaba-dubbo-gateway
main:
allow-bean-definition-overriding: true
dubbo:
protocols:
dubbo:
port: -1
server:
port: 9090
# default disable all
cloud:
nacos:
discovery:
enabled: false
register-enabled: false
zookeeper:
enabled: false
consul:
enabled: false
eureka:
client:
enabled: false
---
spring:
profiles: nacos
cloud:
nacos:
username: nacos
password: nacos
discovery:
enabled: true
register-enabled: true
server-addr: 127.0.0.1:8848
ephemeral: false
---
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