TODO: 动态泛化参数设置

This commit is contained in:
eson 2019-07-01 01:40:37 +08:00
parent 606dde8aba
commit 10b79b527b
3 changed files with 54 additions and 50 deletions

View File

@ -3,7 +3,6 @@ package cn.ecpark.service.usergw.biz.filters;
import java.nio.ByteBuffer;
import java.util.Map;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.ReferenceConfig;
@ -20,12 +19,6 @@ import com.alibaba.fastjson.*;
import reactor.core.publisher.Mono;
@Component
public class DubboFilter implements GlobalFilter, Ordered {
@ -37,8 +30,7 @@ public class DubboFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
exchange.getRequest();
ReferenceConfig<GenericService> reference = new
ReferenceConfig<GenericService>(); // 该实例很重量里面封装了所有与注册中心及服务提供方连接请缓存
ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>(); // 该实例很重量里面封装了所有与注册中心及服务提供方连接请缓存
reference.setApplication(new ApplicationConfig("dubbo-exchange"));
// reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
@ -48,16 +40,14 @@ public class DubboFilter implements GlobalFilter, Ordered {
ReferenceConfigCache cache = ReferenceConfigCache.getCache();
GenericService gs = cache.get(reference);
Object result = gs.$invoke("Hello", new String[]{}, new Object[]{});
Object result = gs.$invoke("Hello", new String[] {}, new Object[] {});
if (result != null) {
ServerHttpResponse response = exchange.getResponse();
return response.writeWith( Mono.just(response.bufferFactory().wrap(ByteBuffer.wrap(JSON.toJSONString(result).getBytes()))));
return response.writeWith(
Mono.just(response.bufferFactory().wrap(ByteBuffer.wrap(JSON.toJSONString(result).getBytes()))));
}
return chain.filter(exchange);
}
}

View File

@ -45,6 +45,7 @@ public class ConfigGateway implements RouteDefinitionLocator {
private Http2Dubbo http2Dubbo;
@Override
@SuppressWarnings("unchecked")
public Flux<RouteDefinition> getRouteDefinitions() {
// WebFluxConfigurationSupport a;
@ -113,44 +114,18 @@ public class ConfigGateway implements RouteDefinitionLocator {
Object unknownRoutes = configDefault.get("routes");
if (unknownRoutes != null) {
List<LinkedHashMap> routes = (ArrayList<LinkedHashMap>) unknownRoutes;
for (LinkedHashMap iter : routes) {
List<LinkedHashMap<String, List<String>>> routes = (ArrayList<LinkedHashMap<String, List<String>>>) unknownRoutes;
for (LinkedHashMap<String, List<String>> iter : routes) {
RouteDefinition rd = new RouteDefinition();
// 设置id
Object id = iter.get("id");
if (id != null) {
rd.setId((String) id);
}
// 设置uri
Object uri = iter.get("uri");
if (uri != null) {
rd.setUri(URI.create((String) uri));
}
// 设置uri
Object order = iter.get("order");
if (order != null) {
rd.setOrder((int) order);
}
// 设置基础属性
this.ParseAndSetBase(rd, iter);
// predicates: 下的相关属性
List<String> unknownPredicatesYaml = (ArrayList<String>) iter.get("predicates");
if (unknownPredicatesYaml != null) {
List<String> predicatesYaml = (ArrayList<String>) unknownPredicatesYaml;
for (String predicateString : predicatesYaml) {
predicates.add(new PredicateDefinition(predicateString));
}
}
this.ParseAndAddPredicates(predicates, iter, "predicates");
// filters: 下的相关属性
List<String> unknownFiltersYaml = (ArrayList<String>) iter.get("filters");
if (unknownFiltersYaml != null) {
List<String> filtersYaml = (ArrayList<String>) unknownFiltersYaml;
for (String filterString : filtersYaml) {
filters.add(new FilterDefinition(filterString));
}
}
this.ParseAndAddFilters(filters, iter, "filters");
rd.setPredicates(predicates);
rd.setFilters(filters);
@ -165,6 +140,45 @@ public class ConfigGateway implements RouteDefinitionLocator {
return null;
}
private void ParseAndSetBase(RouteDefinition rd, LinkedHashMap<String, List<String>> iter) {
// 设置id
Object id = iter.get("id");
if (id != null) {
rd.setId((String) id);
}
// 设置uri
Object uri = iter.get("uri");
if (uri != null) {
rd.setUri(URI.create((String) uri));
}
// 设置uri
Object order = iter.get("order");
if (order != null) {
rd.setOrder((int) order);
}
}
private void ParseAndAddPredicates(List<PredicateDefinition> predicates, LinkedHashMap<String, List<String>> iter, String yamlField) {
List<String> predicatesYaml = iter.get(yamlField);
if (predicatesYaml != null) {
for (String predicateString : predicatesYaml) {
predicates.add(new PredicateDefinition(predicateString));
}
}
}
private void ParseAndAddFilters(List<FilterDefinition> filters, LinkedHashMap<String, List<String>> iter, String yamlField) {
List<String> filtersYaml = iter.get(yamlField);
if (filtersYaml != null) {
for (String filterString : filtersYaml) {
filters.add(new FilterDefinition(filterString));
}
}
}
private void createHttp2Dubbo(Map<String, Object> configDubbo) {
try {

View File

@ -18,6 +18,6 @@ dubbo:
routes:
- id: test
path: /dubbo/hello
interface: com.xxx.xxx
version: 2.0.0
interface: ocean.demo.api.IExchange
version: 1.0.0