dubbo uri设计问题
This commit is contained in:
parent
f9da760e8d
commit
1f1f6d99c8
|
@ -49,7 +49,7 @@ public class DubboFilter implements GlobalFilter, Ordered {
|
|||
String rkey = "";
|
||||
rkey += application;
|
||||
|
||||
if(rgroup != ""){
|
||||
if(rgroup != null && !rgroup.isEmpty()){
|
||||
rkey += "/" + rgroup;
|
||||
}
|
||||
|
||||
|
@ -72,8 +72,8 @@ public class DubboFilter implements GlobalFilter, Ordered {
|
|||
}
|
||||
|
||||
GenericService gs = gsPool.get(rkey);
|
||||
|
||||
Object result = gs.$invoke(rmethod, new String[] {"java.lang.String"}, new Object[] {"213"});
|
||||
gsPool.add(rkey, gs);
|
||||
|
||||
if (result != null) {
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
|
|
|
@ -4,6 +4,8 @@ import java.util.ArrayList;
|
|||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.locks.Condition;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
@ -21,9 +23,8 @@ import lombok.extern.slf4j.Slf4j;
|
|||
public class GenericServicePool {
|
||||
|
||||
private class GenericServiceManager {
|
||||
Lock idleLock = new ReentrantLock();
|
||||
LinkedList<GenericService> idle = new LinkedList<>();
|
||||
|
||||
BlockingQueue<GenericService> idle = new LinkedBlockingQueue<>();
|
||||
private String key;
|
||||
|
||||
@Override
|
||||
|
@ -46,33 +47,19 @@ public class GenericServicePool {
|
|||
}
|
||||
|
||||
public GenericService get() {
|
||||
try {
|
||||
if (idleLock.tryLock(15L, TimeUnit.SECONDS)) {
|
||||
while (idle.isEmpty()) {
|
||||
idle.wait();
|
||||
}
|
||||
return idle.pop();
|
||||
} else {
|
||||
log.error(this.key + ": 超时{}秒", 15);
|
||||
}
|
||||
try {
|
||||
return idle.take();
|
||||
} catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
log.error("悠闲队列出现问题: " + e.toString());
|
||||
} finally {
|
||||
idleLock.unlock();
|
||||
log.error("悠闲队列出现问题: " + e.getStackTrace());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void add(GenericService gs) {
|
||||
idle.add(gs);
|
||||
if (idle.isEmpty()) {
|
||||
idle.notify();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public GenericServiceManager() {
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ package cn.ecpark.service.usergw.biz.filters.factory;
|
|||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
@ -13,6 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
@ -20,59 +22,73 @@ import cn.ecpark.service.usergw.biz.filters.bean.GenericServicePool;
|
|||
import reactor.core.publisher.Mono;;
|
||||
|
||||
@Component
|
||||
public class DubboFilterFactory extends AbstractGatewayFilterFactory<DubboFilterFactory.Config> {
|
||||
public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<DubboGatewayFilterFactory.Config> {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext appContext;
|
||||
|
||||
public DubboFilterFactory() {
|
||||
/**
|
||||
* DUBBO_URI key.
|
||||
*/
|
||||
public static final String DUBBO_URI = "dubbo_uri";
|
||||
|
||||
public DubboGatewayFilterFactory() {
|
||||
super(Config.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> shortcutFieldOrder() {
|
||||
return Arrays.asList(DUBBO_URI);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GatewayFilter apply(Config config) {
|
||||
String dubboUri = config.dubboUri;
|
||||
String uri = config.dubboUri;
|
||||
|
||||
return (exchange, chain) -> {
|
||||
|
||||
|
||||
String application = "dubbo-exchange"; // 必须
|
||||
// reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
|
||||
String rgroup = "group";
|
||||
String rinterface = "ocean.demo.api.IExchange"; // 弱类型接口名 必须
|
||||
String rversion = "1.0.0";
|
||||
String rversion = "1.0.0";
|
||||
String rmethod = "Say";
|
||||
|
||||
List<String> rparamTypes = new ArrayList<String>();
|
||||
List<String> rparams = new ArrayList<String>();
|
||||
|
||||
|
||||
|
||||
String rkey = "";
|
||||
rkey += application;
|
||||
|
||||
if(rgroup != ""){
|
||||
|
||||
if (rgroup != null && !rgroup.isEmpty()) {
|
||||
rkey += "/" + rgroup;
|
||||
}
|
||||
|
||||
rkey += "/" + rinterface;
|
||||
|
||||
rkey += "/" + rinterface;
|
||||
rkey += ":" + rversion;
|
||||
|
||||
|
||||
GenericServicePool gsPool = appContext.getBean(GenericServicePool.class);
|
||||
if(!gsPool.contains(rkey)) {
|
||||
|
||||
ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>(); // 该实例很重量,里面封装了所有与注册中心及服务提供方连接,请缓存
|
||||
|
||||
reference.setApplication(new ApplicationConfig("dubbo-exchange")); // 必须
|
||||
// reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
|
||||
reference.setGroup("group");
|
||||
reference.setInterface("ocean.demo.api.IExchange"); // 弱类型接口名 必须
|
||||
reference.setVersion("1.0.0"); // 必须
|
||||
reference.setGeneric(true); // 声明为泛化接口
|
||||
|
||||
gsPool.add(rkey, reference.get());
|
||||
if (!gsPool.contains(rkey)) {
|
||||
|
||||
for (int i = 0; i < 1; i++) {
|
||||
ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>(); // 该实例很重量,里面封装了所有与注册中心及服务提供方连接,请缓存
|
||||
|
||||
reference.setApplication(new ApplicationConfig("dubbo-exchange")); // 必须
|
||||
// reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
|
||||
// reference.setGroup("group");
|
||||
reference.setInterface("ocean.demo.api.IExchange"); // 弱类型接口名 必须
|
||||
reference.setVersion("1.0.0"); // 必须
|
||||
reference.setGeneric(true); // 声明为泛化接口
|
||||
|
||||
gsPool.add(rkey, reference.get());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
GenericService gs = gsPool.get(rkey);
|
||||
Object result = gs.$invoke("Hello", new String[] {}, new Object[] {});
|
||||
|
||||
Object result = gs.$invoke("Say", new String[] { "java.lang.String" }, new Object[] { "222" });
|
||||
|
||||
gsPool.add(rkey, gs);
|
||||
|
||||
if (result != null) {
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
return response.writeWith(
|
|
@ -22,7 +22,7 @@ import org.springframework.stereotype.Controller;
|
|||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import cn.ecpark.service.usergw.biz.filters.bean.GenericServicePool;
|
||||
import cn.ecpark.service.usergw.biz.filters.factory.DubboFilterFactory;
|
||||
import cn.ecpark.service.usergw.biz.filters.factory.DubboGatewayFilterFactory;
|
||||
import cn.ecpark.service.usergw.impl.http.Http2Dubbo;
|
||||
import cn.ecpark.service.usergw.utils.Convert;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -49,9 +49,9 @@ public class ConfigGateway implements RouteDefinitionLocator {
|
|||
}
|
||||
|
||||
@Bean
|
||||
public DubboFilterFactory dubboFilterFactory() {
|
||||
return new DubboFilterFactory();
|
||||
}
|
||||
public DubboGatewayFilterFactory dubboFilterFactory() {
|
||||
return new DubboGatewayFilterFactory();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Http2Dubbo http2Dubbo;
|
||||
|
@ -142,8 +142,6 @@ public class ConfigGateway implements RouteDefinitionLocator {
|
|||
List<PredicateDefinition> predicates = new ArrayList<>();
|
||||
RouteDefinition rd = new RouteDefinition();
|
||||
|
||||
|
||||
|
||||
// 设置基础属性
|
||||
this.ParseAndSetBase(rd, iter);
|
||||
|
||||
|
@ -162,6 +160,66 @@ public class ConfigGateway implements RouteDefinitionLocator {
|
|||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void configHttp2Dubbo(List<RouteDefinition> routeList, Map<String, Object> configDubbo) {
|
||||
|
||||
try {
|
||||
|
||||
Object unknownRoutes = configDubbo.get("routes");
|
||||
if (unknownRoutes != null) {
|
||||
|
||||
List<LinkedHashMap<String, List<String>>> routes = (ArrayList<LinkedHashMap<String, List<String>>>) unknownRoutes;
|
||||
for (LinkedHashMap<String, List<String>> iter : routes) {
|
||||
|
||||
List<FilterDefinition> filters = new ArrayList<>();
|
||||
List<PredicateDefinition> predicates = new ArrayList<>();
|
||||
RouteDefinition rd = new RouteDefinition();
|
||||
|
||||
// 设置基础属性
|
||||
this.ParseAndSetBase(rd, iter);
|
||||
|
||||
// predicates: 下的相关属性
|
||||
this.ParseAndAddPredicates(predicates, iter, "predicates");
|
||||
|
||||
// filters: 下的相关属性
|
||||
this.ParseAndAddDubboFilters(filters, iter, "filters");
|
||||
|
||||
rd.setPredicates(predicates);
|
||||
rd.setFilters(filters);
|
||||
|
||||
routeList.add(rd);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// RequestMappingHandlerMapping requestMapping = (RequestMappingHandlerMapping)
|
||||
// applicationContext
|
||||
// .getBean("requestMappingHandlerMapping");
|
||||
|
||||
// requestMapping.registerMapping(RequestMappingInfo.paths("/test/xixi").methods(RequestMethod.POST).build(),
|
||||
// http2Dubbo, Http2Dubbo.class.getDeclaredMethod("H2DTest"));
|
||||
|
||||
// 引用远程服务
|
||||
// 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) {
|
||||
// TODO: 任何错误都直接终止退出
|
||||
log.error(e.toString());
|
||||
SpringApplication.exit(applicationContext);
|
||||
}
|
||||
|
||||
// gs.$invoke(method, parameterTypes, args)
|
||||
|
||||
}
|
||||
|
||||
private void ParseAndSetBase(RouteDefinition rd, LinkedHashMap<String, List<String>> iter) {
|
||||
// 设置id
|
||||
Object id = iter.get("id");
|
||||
|
@ -203,63 +261,23 @@ public class ConfigGateway implements RouteDefinitionLocator {
|
|||
}
|
||||
}
|
||||
|
||||
private void configHttp2Dubbo(List<RouteDefinition> routeList, Map<String, Object> configDubbo) {
|
||||
|
||||
try {
|
||||
|
||||
Object unknownRoutes = configDubbo.get("routes");
|
||||
if (unknownRoutes != null) {
|
||||
GenericServicePool pool = applicationContext.getBean(GenericServicePool.class);
|
||||
|
||||
List<LinkedHashMap<String, List<String>>> routes = (ArrayList<LinkedHashMap<String, List<String>>>) unknownRoutes;
|
||||
for (LinkedHashMap<String, List<String>> iter : routes) {
|
||||
|
||||
List<FilterDefinition> filters = new ArrayList<>();
|
||||
List<PredicateDefinition> predicates = new ArrayList<>();
|
||||
RouteDefinition rd = new RouteDefinition();
|
||||
|
||||
this.ParseAndSetBase(rd, iter);
|
||||
|
||||
// 设置基础属性
|
||||
this.ParseAndSetBase(rd, iter);
|
||||
|
||||
// predicates: 下的相关属性
|
||||
this.ParseAndAddPredicates(predicates, iter, "predicates");
|
||||
|
||||
// filters: 下的相关属性
|
||||
this.ParseAndAddFilters(filters, iter, "filters");
|
||||
|
||||
private void ParseAndAddDubboFilters(List<FilterDefinition> filters, LinkedHashMap<String, List<String>> iter,
|
||||
String yamlField) {
|
||||
List<String> filtersYaml = iter.get(yamlField);
|
||||
if (filtersYaml != null) {
|
||||
filters.addAll(defaultFilters);
|
||||
for (String filterString : filtersYaml) {
|
||||
FilterDefinition fd = new FilterDefinition(filterString);
|
||||
log.info(fd.getName());
|
||||
if (!fd.getName().equals("Dubbo")){
|
||||
filters.add(fd);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// RequestMappingHandlerMapping requestMapping = (RequestMappingHandlerMapping)
|
||||
// applicationContext
|
||||
// .getBean("requestMappingHandlerMapping");
|
||||
|
||||
// requestMapping.registerMapping(RequestMappingInfo.paths("/test/xixi").methods(RequestMethod.POST).build(),
|
||||
// http2Dubbo, Http2Dubbo.class.getDeclaredMethod("H2DTest"));
|
||||
|
||||
// 引用远程服务
|
||||
// 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) {
|
||||
// TODO: 任何错误都直接终止退出
|
||||
log.error(e.toString());
|
||||
SpringApplication.exit(applicationContext);
|
||||
}
|
||||
|
||||
// gs.$invoke(method, parameterTypes, args)
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
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);
|
||||
|
|
|
@ -5,6 +5,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
|
||||
/**
|
||||
* Http2Dubbo
|
||||
*/
|
||||
|
|
|
@ -9,3 +9,4 @@ dubbo.metadata-report.address=zookeeper://127.0.0.1:2181
|
|||
server.port=8888
|
||||
|
||||
# logging.level.org.springframework.cloud.gateway=debug
|
||||
logging.file=logs/log
|
||||
|
|
|
@ -4,8 +4,8 @@ default:
|
|||
routes:
|
||||
|
||||
- id: path_route
|
||||
uri: http://httpbin.org:80/get
|
||||
order: 10
|
||||
uri: http://httpbin.org:80/*
|
||||
order: 9
|
||||
predicates:
|
||||
- Path=/get
|
||||
- Header=XX, \d+
|
||||
|
@ -14,6 +14,7 @@ default:
|
|||
# order: 11
|
||||
# filters:
|
||||
# - RedirectTo=302, http://httpbin.org:80/get
|
||||
|
||||
dubbo:
|
||||
routes:
|
||||
- id: test
|
||||
|
@ -26,5 +27,5 @@ dubbo:
|
|||
predicates:
|
||||
- Path=/dubbo/hello
|
||||
filters:
|
||||
- Dubbo=234
|
||||
- Dubbo=http213313
|
||||
|
Loading…
Reference in New Issue
Block a user