TODO: 需要完成对象池

This commit is contained in:
huangsimin 2019-07-01 18:30:03 +08:00
parent 10b79b527b
commit 1b0f865003
4 changed files with 117 additions and 13 deletions

View File

@ -22,7 +22,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
<dubbo.version>2.7.1</dubbo.version>
<dubbo.version>2.7.2</dubbo.version>
</properties>
@ -47,7 +47,7 @@
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
<version>2.7.1</version>
</dependency>
<dependency>
@ -59,7 +59,7 @@
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<version>2.7.1</version>
<type>pom</type>
<exclusions>
<exclusion>

View File

@ -1,47 +1,56 @@
package cn.ecpark.service.usergw.biz.filters;
import java.nio.ByteBuffer;
import java.util.Map;
import com.alibaba.fastjson.JSON;
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.config.utils.ReferenceConfigCache;
import org.apache.dubbo.rpc.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.ApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import com.alibaba.fastjson.*;
import io.netty.util.Recycler;
import reactor.core.publisher.Mono;
@Component
public class DubboFilter implements GlobalFilter, Ordered {
@Autowired
private ApplicationContext appContext;
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
ReferenceConfigCache cache = ReferenceConfigCache.getCache();
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
exchange.getRequest();
// 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.setGroup("group");
reference.setInterface("ocean.demo.api.IExchange"); // 弱类型接口名
reference.setVersion("1.0.0");
reference.setGeneric(true); // 声明为泛化接口
ReferenceConfigCache cache = ReferenceConfigCache.getCache();
GenericService gs = cache.get(reference);
Recycler a;
GenericService gs = cache.get(reference);
Object result = gs.$invoke("Hello", new String[] {}, new Object[] {});
if (result != null) {
ServerHttpResponse response = exchange.getResponse();
return response.writeWith(

View File

@ -0,0 +1,93 @@
package cn.ecpark.service.usergw.biz.filters.bean;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.rpc.service.GenericService;
import lombok.extern.slf4j.Slf4j;
/**
* GenericServicePool
*/
@Slf4j
public class GenericServicePool {
private class GenericServiceManager {
Lock lock = new ReentrantLock();
LinkedList<GenericService> idle = new LinkedList<>();
LinkedList<GenericService> busy = new LinkedList<>();
private String key;
@Override
public int hashCode() {
return this.key.hashCode();
}
/**
* @return String return the key
*/
public String getKey() {
return key;
}
/**
* @param key the key to set
*/
public void setKey(String key) {
this.key = key;
}
public GenericService get() {
try {
if(lock.tryLock(15L, TimeUnit.SECONDS)) {
return idle.pop();
} else {
log.error(this.key + ": 超时{}秒", 15);
}
} catch (Exception e) {
//TODO: handle exception
} finally {
lock.unlock();
}
return null;
}
public GenericServiceManager() {
idle = new LinkedList<>();
busy = new LinkedList<>();
}
}
HashMap<String, GenericServiceManager> gsDictionary;
public GenericServicePool() {
gsDictionary = new HashMap<String, GenericServiceManager>();
}
public GenericService get(String key) {
}
public GenericService add(String key, Object genericService) {
}
}

View File

@ -4,6 +4,8 @@ dubbo.scan.base-packages=cn.ecpark.service.usergw.impl
dubbo.protocol.name=dubbo
dubbo.protocol.port=20999
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.config-center.address=zookeeper://127.0.0.1:2181
dubbo.metadata-report.address=zookeeper://127.0.0.1:2181
server.port=8888
logging.level.org.springframework.cloud.gateway=debug
# logging.level.org.springframework.cloud.gateway=debug