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