TODO: 考虑文件流, registerMapping MappingInfo

This commit is contained in:
huangsimin 2019-06-25 17:32:31 +08:00
parent c1c7e85e81
commit b6afc3c0cb
6 changed files with 84 additions and 35 deletions

View File

@ -5,7 +5,6 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.config.GatewayAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

View File

@ -6,7 +6,6 @@ import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;

View File

@ -1,27 +1,20 @@
package cn.ecpark.service.usergw.config;
import java.io.InputStream;
import java.net.URI;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.bouncycastle.cert.crmf.RegTokenControl;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.rpc.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.cloud.gateway.filter.FilterDefinition;
import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionLocator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.util.UriBuilder;
import org.yaml.snakeyaml.Yaml;
import lombok.extern.slf4j.Slf4j;
@ -36,30 +29,40 @@ public class ConfigGateway implements RouteDefinitionLocator {
List<MediaType> mediaTypes = new ArrayList<>();
@Autowired
private ApplicationContext applicationContext;
@Override
public Flux<RouteDefinition> getRouteDefinitions() {
// streaming-media-types 这个先不管
NettyWriteResponseFilter nwrf = applicationContext.getBean(NettyWriteResponseFilter.class);
Object inputStream = null;
String[] gatewayConfigPathList = { "gateway.yml", "gateway.yaml", "Gateway.yml", "Gateway.yaml" };
for (String gatewayConfigPath : gatewayConfigPathList) {
inputStream = this.getClass().getClassLoader().getResourceAsStream(gatewayConfigPath);
if(inputStream != null) {
if (inputStream != null) {
break;
}
}
if (inputStream != null) {
Map<String, Object> configYaml = new Yaml().load((InputStream) inputStream);
if (configYaml != null) {
// streaming-media-types 这个先不管
// NettyWriteResponseFilter nwrf =
// applicationContext.getBean(NettyWriteResponseFilter.class);
// try {
// Field field = nwrf.getClass().getDeclaredField("streamingMediaTypes");
// field.setAccessible(true);
// List<MediaType> mts = new ArrayList<MediaType>();
// mts.add(MediaType.TEXT_EVENT_STREAM);
// mts.add(MediaType.APPLICATION_STREAM_JSON);
// field.set(nwrf, mts);
// } catch (Exception e) {
// log.error(e.toString());
// }
Map<String, Object> configDefault = (Map<String, Object>) configYaml.get("default");
Map<String, Object> configDubbo = (Map<String, Object>) configYaml.get("dubbo");
// ConfigProperties.createHttp2Dubbo(configDubbo);
return Flux.fromIterable(ConfigProperties.getConfigDefault(configDefault));
}
}

View File

@ -16,29 +16,25 @@
package cn.ecpark.service.usergw.config;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.rpc.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.gateway.filter.FilterDefinition;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.MediaType;
import org.springframework.context.ApplicationContext;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping;
import cn.ecpark.service.usergw.utils.Convert;
import lombok.extern.slf4j.Slf4j;
/**
@ -47,7 +43,13 @@ import lombok.extern.slf4j.Slf4j;
@Validated
@Slf4j
public class ConfigProperties {
@Autowired
private static ApplicationContext applicationContext;
@Autowired
private static RequestMappingHandlerMapping requestMapping;
public static List<RouteDefinition> getConfigDefault(Map<String, Object> configDefault) {
if (configDefault != null) {
List<RouteDefinition> RDList = new ArrayList<>();
@ -118,4 +120,28 @@ public class ConfigProperties {
return null;
}
private static void callMethod(ReferenceConfig ref, String name, String params) throws Exception {
Method method = ref.getClass().getMethod("set" + Convert.firstUpperCase(name));
method.invoke(ref, method.getParameterTypes(), params);
}
public static void createHttp2Dubbo(Map<String, Object> configDubbo) {
// requestMapping.registerMapping(mapping, handler, method);
// 引用远程服务
try {
ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>(); // 该实例很重量里面封装了所有与注册中心及服务提供方连接请缓存
reference.setInterface("com.xxx.XxxService"); // 弱类型接口名
reference.setVersion("2.0.0");
reference.setGeneric(true); // 声明为泛化接口
GenericService gs = reference.get();
} catch (Exception e) {
//TODO: 任何错误都直接终止退出
SpringApplication.exit(applicationContext);
}
// gs.$invoke(method, parameterTypes, args)
}
}

View File

@ -0,0 +1,16 @@
package cn.ecpark.service.usergw.utils;
/**
* convert
*/
public class Convert {
public static String firstUpperCase(String str) {
char[] ch = str.toCharArray();
if (ch[0] >= 'a' && ch[0] <= 'z') {
ch[0] = (char) (ch[0] - 32);
}
return new String(ch);
}
}

View File

@ -9,4 +9,10 @@ default:
predicates:
# - Path=/get
- Header=XX, \d+
dubbo:
routes:
- id: test
path: /dubbo/hello
interface: com.xxx.xxx
version: 1