修改Dubbo一个版本重复注册的bug
This commit is contained in:
parent
ae00f75cd1
commit
2c0e118623
2
pom.xml
2
pom.xml
|
@ -53,7 +53,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.dubbo</groupId>
|
<groupId>org.apache.dubbo</groupId>
|
||||||
<artifactId>dubbo</artifactId>
|
<artifactId>dubbo</artifactId>
|
||||||
<version>${dubbo.version}</version>
|
<version>2.7.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
|
@ -1,12 +0,0 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
package cn.ecpark.service.usergw.biz;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter
|
|
||||||
*/
|
|
||||||
public class Filter {
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
package cn.ecpark.service.usergw.biz.filters;
|
||||||
|
|
||||||
|
import org.apache.dubbo.config.ApplicationConfig;
|
||||||
|
import org.apache.dubbo.config.ProtocolConfig;
|
||||||
|
import org.apache.dubbo.config.ReferenceConfig;
|
||||||
|
import org.apache.dubbo.config.RegistryConfig;
|
||||||
|
import org.apache.dubbo.rpc.service.GenericService;
|
||||||
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||||
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class DubboFilter implements GlobalFilter, Ordered {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return Ordered.LOWEST_PRECEDENCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
|
exchange.getRequest();
|
||||||
|
ReferenceConfig<GenericService> reference = new
|
||||||
|
ReferenceConfig<GenericService>(); // 该实例很重量,里面封装了所有与注册中心及服务提供方连接,请缓存
|
||||||
|
|
||||||
|
reference.setApplication(new ApplicationConfig("dubbo-exchange"));
|
||||||
|
// reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
|
||||||
|
reference.setInterface("ocean.demo.api.IExchange"); // 弱类型接口名
|
||||||
|
reference.setVersion("1.0.0");
|
||||||
|
reference.setGeneric(true); // 声明为泛化接口
|
||||||
|
GenericService gs = reference.get();
|
||||||
|
|
||||||
|
|
||||||
|
Object result = gs.$invoke("Hello", new String[]{}, new Object[]{});
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,68 @@
|
||||||
|
package cn.ecpark.service.usergw.biz.filters;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringBootConfiguration;
|
||||||
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||||
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
@SpringBootConfiguration
|
||||||
|
@Order(-200)
|
||||||
|
public class VerifyFilter implements GlobalFilter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
|
|
||||||
|
if(true) return chain.filter(exchange);
|
||||||
|
|
||||||
|
ServerHttpRequest request = exchange.getRequest();
|
||||||
|
HttpHeaders header = request.getHeaders();
|
||||||
|
|
||||||
|
ServerHttpResponse response = exchange.getResponse();
|
||||||
|
HttpHeaders respHeader = response.getHeaders();
|
||||||
|
respHeader.add("token-expire", String.valueOf(System.currentTimeMillis() / 1000));
|
||||||
|
respHeader.add("version", "v0.0.1");
|
||||||
|
|
||||||
|
String token = header.getFirst("token");
|
||||||
|
if (token != null) {
|
||||||
|
//
|
||||||
|
if (VerifyToken(token)) {
|
||||||
|
log.info("request token is Verify Successed: {}", token);
|
||||||
|
return chain.filter(exchange);
|
||||||
|
}
|
||||||
|
log.info("request token is Verify Fail: {} != {}", token, TEST_TOKEN);
|
||||||
|
} else {
|
||||||
|
log.warn("request token is empty: {}", request.getURI());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!response.setStatusCode(HttpStatus.UNAUTHORIZED)) {
|
||||||
|
log.error("if the status code has not been set because the HTTP response is already committed");
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.setComplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final String TEST_TOKEN = "yame";
|
||||||
|
|
||||||
|
public boolean VerifyToken(String token) {
|
||||||
|
if (token.equals(TEST_TOKEN)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
package cn.ecpark.service.usergw.biz;
|
package cn.ecpark.service.usergw.biz.routes;
|
||||||
|
|
||||||
import org.springframework.cloud.gateway.route.RouteLocator;
|
import org.springframework.cloud.gateway.route.RouteLocator;
|
||||||
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
|
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
|
||||||
|
@ -29,10 +29,7 @@ public class Route {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
|
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
|
||||||
return builder.routes()
|
return builder.routes().build();
|
||||||
.route("path_route", r -> r.path("/about")
|
|
||||||
.uri("http://ityouknow.com"))
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,7 +1,6 @@
|
||||||
package cn.ecpark.service.usergw.config;
|
package cn.ecpark.service.usergw.config;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -10,24 +9,16 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.apache.dubbo.config.ReferenceConfig;
|
import org.apache.dubbo.config.ReferenceConfig;
|
||||||
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.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;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
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.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;
|
||||||
|
@ -36,8 +27,6 @@ import cn.ecpark.service.usergw.impl.http.Http2Dubbo;
|
||||||
import cn.ecpark.service.usergw.utils.Convert;
|
import cn.ecpark.service.usergw.utils.Convert;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.netty.http.server.HttpServerRequest;
|
|
||||||
import reactor.netty.http.server.HttpServerResponse;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ConfigGateway
|
* ConfigGateway
|
||||||
|
@ -89,14 +78,23 @@ public class ConfigGateway implements RouteDefinitionLocator {
|
||||||
Map<String, Object> configDefault = (Map<String, Object>) configYaml.get("default");
|
Map<String, Object> configDefault = (Map<String, Object>) configYaml.get("default");
|
||||||
Map<String, Object> configDubbo = (Map<String, Object>) configYaml.get("dubbo");
|
Map<String, Object> configDubbo = (Map<String, Object>) configYaml.get("dubbo");
|
||||||
|
|
||||||
this.createHttp2Dubbo(configDubbo);
|
if ( configDubbo != null && !configDubbo.isEmpty()) {
|
||||||
return Flux.fromIterable(this.getConfigDefault(configDefault));
|
this.createHttp2Dubbo(configDubbo);
|
||||||
|
}
|
||||||
|
|
||||||
|
if( configDefault != null && !configDefault.isEmpty()) {
|
||||||
|
List<RouteDefinition> result = this.getConfigDefault(configDefault);
|
||||||
|
if (result != null ) {
|
||||||
|
return Flux.fromIterable(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return Flux.empty();
|
||||||
// return Flux.fromIterable(it);
|
// return Flux.fromIterable(it);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
private List<RouteDefinition> getConfigDefault(Map<String, Object> configDefault) {
|
private List<RouteDefinition> getConfigDefault(Map<String, Object> configDefault) {
|
||||||
if (configDefault != null) {
|
if (configDefault != null) {
|
||||||
List<RouteDefinition> RDList = new ArrayList<>();
|
List<RouteDefinition> RDList = new ArrayList<>();
|
||||||
|
@ -171,14 +169,11 @@ public class ConfigGateway implements RouteDefinitionLocator {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// RequestMappingHandlerMapping requestMapping = (RequestMappingHandlerMapping) applicationContext
|
RequestMappingHandlerMapping requestMapping = (RequestMappingHandlerMapping) applicationContext
|
||||||
// .getBean("requestMappingHandlerMapping");
|
.getBean("requestMappingHandlerMapping");
|
||||||
|
|
||||||
// // Method targetMethod = ReflectionUtils.findMethod(Http2Dubbo.class, "H2DTest",
|
requestMapping.registerMapping(RequestMappingInfo.paths("/test/xixi").methods(RequestMethod.POST).build(),
|
||||||
// // HttpServerRequest.class, HttpServerResponse.class); // 找到处理该路由的方法
|
http2Dubbo, Http2Dubbo.class.getDeclaredMethod("H2DTest"));
|
||||||
|
|
||||||
// requestMapping.registerMapping(RequestMappingInfo.paths("/test/xixi").methods(RequestMethod.POST).build(),
|
|
||||||
// http2Dubbo, Http2Dubbo.class.getDeclaredMethod("H2DTest"));
|
|
||||||
|
|
||||||
// 引用远程服务
|
// 引用远程服务
|
||||||
// try {
|
// try {
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package cn.ecpark.service.usergw.impl.http;
|
package cn.ecpark.service.usergw.impl.http;
|
||||||
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
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.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
@ -24,8 +22,8 @@ public class Http2Dubbo {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@RequestMapping("/test/fuck")
|
@RequestMapping("/test/request")
|
||||||
public String fuck() {
|
public String fuck() {
|
||||||
return "fuck";
|
return "request";
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -5,8 +5,8 @@ import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import cn.ecpark.service.usergw.api.IDemo;
|
import cn.ecpark.service.usergw.api.IDemo;
|
||||||
|
|
||||||
@Service(interfaceClass = IDemo.class)
|
// @Service(interfaceClass = IDemo.class)
|
||||||
@Component
|
// @Component
|
||||||
public class DemoHello implements IDemo {
|
public class DemoHello implements IDemo {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -2,6 +2,6 @@
|
||||||
spring.application.name=gateway
|
spring.application.name=gateway
|
||||||
dubbo.scan.base-packages=cn.ecpark.service.usergw.impl
|
dubbo.scan.base-packages=cn.ecpark.service.usergw.impl
|
||||||
dubbo.protocol.name=dubbo
|
dubbo.protocol.name=dubbo
|
||||||
dubbo.protocol.port=20080
|
dubbo.protocol.port=20999
|
||||||
dubbo.registry.address=zookeeper://127.0.0.1:2181
|
dubbo.registry.address=zookeeper://127.0.0.1:2181
|
||||||
server.port=8888
|
server.port=8888
|
||||||
|
|
|
@ -5,10 +5,15 @@ default:
|
||||||
|
|
||||||
- id: path_route
|
- id: path_route
|
||||||
uri: http://httpbin.org:80/get
|
uri: http://httpbin.org:80/get
|
||||||
order: 12
|
order: 10
|
||||||
predicates:
|
predicates:
|
||||||
# - Path=/get
|
- Path=/get
|
||||||
- Header=XX, \d+
|
- Header=XX, \d+
|
||||||
|
# - id: redirect_to
|
||||||
|
# uri: http://localhost/test/**
|
||||||
|
# order: 11
|
||||||
|
# filters:
|
||||||
|
# - RedirectTo=302, http://httpbin.org:80/get
|
||||||
dubbo:
|
dubbo:
|
||||||
routes:
|
routes:
|
||||||
- id: test
|
- id: test
|
||||||
|
|
Loading…
Reference in New Issue
Block a user