动态创建Requestmapping 和 Gateway HandlerMapping 有差别
This commit is contained in:
parent
9be72ebe6a
commit
ae00f75cd1
@ -13,8 +13,10 @@ import org.apache.dubbo.config.ReferenceConfig;
|
|||||||
import org.apache.dubbo.rpc.service.GenericService;
|
import org.apache.dubbo.rpc.service.GenericService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||||
import org.springframework.cloud.gateway.filter.FilterDefinition;
|
import org.springframework.cloud.gateway.filter.FilterDefinition;
|
||||||
import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter;
|
import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter;
|
||||||
|
import org.springframework.cloud.gateway.handler.RoutePredicateHandlerMapping;
|
||||||
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
|
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
|
||||||
import org.springframework.cloud.gateway.route.RouteDefinition;
|
import org.springframework.cloud.gateway.route.RouteDefinition;
|
||||||
import org.springframework.cloud.gateway.route.RouteDefinitionLocator;
|
import org.springframework.cloud.gateway.route.RouteDefinitionLocator;
|
||||||
@ -25,6 +27,7 @@ import org.springframework.stereotype.Controller;
|
|||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
import org.springframework.web.reactive.config.WebFluxConfigurationSupport;
|
import org.springframework.web.reactive.config.WebFluxConfigurationSupport;
|
||||||
|
import org.springframework.web.reactive.result.method.AbstractHandlerMethodMapping;
|
||||||
import org.springframework.web.reactive.result.method.RequestMappingInfo;
|
import org.springframework.web.reactive.result.method.RequestMappingInfo;
|
||||||
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping;
|
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping;
|
||||||
import org.yaml.snakeyaml.Yaml;
|
import org.yaml.snakeyaml.Yaml;
|
||||||
@ -53,9 +56,9 @@ public class ConfigGateway implements RouteDefinitionLocator {
|
|||||||
private Http2Dubbo http2Dubbo;
|
private Http2Dubbo http2Dubbo;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Flux<RouteDefinition> getRouteDefinitions() {
|
public Flux<RouteDefinition> getRouteDefinitions() {
|
||||||
// WebFluxConfigurationSupport a;
|
// WebFluxConfigurationSupport a;
|
||||||
|
|
||||||
Object inputStream = null;
|
Object inputStream = null;
|
||||||
String[] gatewayConfigPathList = { "gateway.yml", "gateway.yaml", "Gateway.yml", "Gateway.yaml" };
|
String[] gatewayConfigPathList = { "gateway.yml", "gateway.yaml", "Gateway.yml", "Gateway.yaml" };
|
||||||
for (String gatewayConfigPath : gatewayConfigPathList) {
|
for (String gatewayConfigPath : gatewayConfigPathList) {
|
||||||
@ -164,23 +167,28 @@ public class ConfigGateway implements RouteDefinitionLocator {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createHttp2Dubbo(Map<String, Object> configDubbo) {
|
private void createHttp2Dubbo(Map<String, Object> configDubbo) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
RequestMappingHandlerMapping requestMapping = (RequestMappingHandlerMapping) applicationContext.getBean("requestMappingHandlerMapping");
|
|
||||||
|
// RequestMappingHandlerMapping requestMapping = (RequestMappingHandlerMapping) applicationContext
|
||||||
// Method targetMethod = ReflectionUtils.findMethod(Http2Dubbo.class, "H2DTest", HttpServerRequest.class, HttpServerResponse.class); // 找到处理该路由的方法
|
// .getBean("requestMappingHandlerMapping");
|
||||||
requestMapping.registerMapping(RequestMappingInfo.paths("/test/xixi").methods(RequestMethod.POST).build(),
|
|
||||||
http2Dubbo, Http2Dubbo.class.getDeclaredMethod("H2DTest"));
|
// // Method targetMethod = ReflectionUtils.findMethod(Http2Dubbo.class, "H2DTest",
|
||||||
|
// // HttpServerRequest.class, HttpServerResponse.class); // 找到处理该路由的方法
|
||||||
// 引用远程服务
|
|
||||||
// try {
|
|
||||||
// ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>(); // 该实例很重量,里面封装了所有与注册中心及服务提供方连接,请缓存
|
|
||||||
|
|
||||||
// reference.setInterface("com.xxx.XxxService"); // 弱类型接口名
|
// requestMapping.registerMapping(RequestMappingInfo.paths("/test/xixi").methods(RequestMethod.POST).build(),
|
||||||
// reference.setVersion("2.0.0");
|
// http2Dubbo, Http2Dubbo.class.getDeclaredMethod("H2DTest"));
|
||||||
// reference.setGeneric(true); // 声明为泛化接口
|
|
||||||
// GenericService gs = reference.get();
|
// 引用远程服务
|
||||||
|
// 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) {
|
} catch (Exception e) {
|
||||||
// TODO: 任何错误都直接终止退出
|
// TODO: 任何错误都直接终止退出
|
||||||
|
@ -3,6 +3,7 @@ package cn.ecpark.service.usergw.impl.http;
|
|||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@ -10,6 +11,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
* Http2Dubbo
|
* Http2Dubbo
|
||||||
*/
|
*/
|
||||||
@Controller
|
@Controller
|
||||||
|
@RestController
|
||||||
public class Http2Dubbo {
|
public class Http2Dubbo {
|
||||||
|
|
||||||
|
|
||||||
@ -17,7 +19,13 @@ public class Http2Dubbo {
|
|||||||
public String H2DTest() {
|
public String H2DTest() {
|
||||||
|
|
||||||
//Do clever stuff here
|
//Do clever stuff here
|
||||||
return "123";
|
return "H2DTest";
|
||||||
// return new ResponseEntity<>(HttpStatus.OK);
|
// return new ResponseEntity<>(HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@RequestMapping("/test/fuck")
|
||||||
|
public String fuck() {
|
||||||
|
return "fuck";
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user