diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/App.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/App.java index 457898b..faf39c3 100644 --- a/usergw-service/src/main/java/cn/ecpark/service/usergw/App.java +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/App.java @@ -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; diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/Route.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/Route.java index dee56f0..a120301 100644 --- a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/Route.java +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/Route.java @@ -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; diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigGateway.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigGateway.java index 394bb07..29b700b 100644 --- a/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigGateway.java +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigGateway.java @@ -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 mediaTypes = new ArrayList<>(); - @Autowired - private ApplicationContext applicationContext; - @Override public Flux 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 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 mts = new ArrayList(); + // mts.add(MediaType.TEXT_EVENT_STREAM); + // mts.add(MediaType.APPLICATION_STREAM_JSON); + // field.set(nwrf, mts); + // } catch (Exception e) { + // log.error(e.toString()); + // } + Map configDefault = (Map) configYaml.get("default"); Map configDubbo = (Map) configYaml.get("dubbo"); + + // ConfigProperties.createHttp2Dubbo(configDubbo); return Flux.fromIterable(ConfigProperties.getConfigDefault(configDefault)); } } diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigProperties.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigProperties.java index 6bbf58e..724d713 100644 --- a/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigProperties.java +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigProperties.java @@ -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 getConfigDefault(Map configDefault) { if (configDefault != null) { List 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 configDubbo) { + // requestMapping.registerMapping(mapping, handler, method); + // 引用远程服务 + try { + ReferenceConfig reference = new ReferenceConfig(); // 该实例很重量,里面封装了所有与注册中心及服务提供方连接,请缓存 + + 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) + + } } diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/utils/Convert.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/utils/Convert.java new file mode 100644 index 0000000..4a794a1 --- /dev/null +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/utils/Convert.java @@ -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); + } +} \ No newline at end of file diff --git a/usergw-service/src/main/resources/gateway.yaml b/usergw-service/src/main/resources/gateway.yaml index 5f41fb6..2a37469 100644 --- a/usergw-service/src/main/resources/gateway.yaml +++ b/usergw-service/src/main/resources/gateway.yaml @@ -9,4 +9,10 @@ default: predicates: # - Path=/get - Header=XX, \d+ +dubbo: + routes: + - id: test + path: /dubbo/hello + interface: com.xxx.xxx + version: 1 \ No newline at end of file