动态创建Requestmapping 和 Gateway HandlerMapping 有差别

This commit is contained in:
huangsimin 2019-06-26 18:31:08 +08:00
parent 9be72ebe6a
commit ae00f75cd1
2 changed files with 33 additions and 17 deletions

View File

@ -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;
@ -167,15 +170,20 @@ public class ConfigGateway implements RouteDefinitionLocator {
private void createHttp2Dubbo(Map<String, Object> configDubbo) { private void createHttp2Dubbo(Map<String, Object> configDubbo) {
try { try {
RequestMappingHandlerMapping requestMapping = (RequestMappingHandlerMapping) applicationContext.getBean("requestMappingHandlerMapping");
// Method targetMethod = ReflectionUtils.findMethod(Http2Dubbo.class, "H2DTest", HttpServerRequest.class, HttpServerResponse.class); // 找到处理该路由的方法 // RequestMappingHandlerMapping requestMapping = (RequestMappingHandlerMapping) applicationContext
requestMapping.registerMapping(RequestMappingInfo.paths("/test/xixi").methods(RequestMethod.POST).build(), // .getBean("requestMappingHandlerMapping");
http2Dubbo, Http2Dubbo.class.getDeclaredMethod("H2DTest"));
// // Method targetMethod = ReflectionUtils.findMethod(Http2Dubbo.class, "H2DTest",
// // HttpServerRequest.class, HttpServerResponse.class); // 找到处理该路由的方法
// requestMapping.registerMapping(RequestMappingInfo.paths("/test/xixi").methods(RequestMethod.POST).build(),
// http2Dubbo, Http2Dubbo.class.getDeclaredMethod("H2DTest"));
// 引用远程服务 // 引用远程服务
// try { // try {
// ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>(); // 该实例很重量里面封装了所有与注册中心及服务提供方连接请缓存 // ReferenceConfig<GenericService> reference = new
// ReferenceConfig<GenericService>(); // 该实例很重量里面封装了所有与注册中心及服务提供方连接请缓存
// reference.setInterface("com.xxx.XxxService"); // 弱类型接口名 // reference.setInterface("com.xxx.XxxService"); // 弱类型接口名
// reference.setVersion("2.0.0"); // reference.setVersion("2.0.0");

View File

@ -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";
}
} }