diff --git a/plugin.xml b/plugin.xml
index 0aa97e9..08c4e6a 100755
--- a/plugin.xml
+++ b/plugin.xml
@@ -3,13 +3,14 @@
com.fr.solution.plugin.db.redis.v10
yes
- 5.2
+ 6.0
10.0
2018-11-29
richie
+ [2019-03-21]增加代理服务配置。
+ [2019-03-18]修复连接没有释放的问题。
[2019-03-18]增加程序数据集,可以使用javascript进行取数,需要2019-03-01之后的版本。
[2019-02-15]结果集可以通过JavaScript脚本进行转换。
[2018-11-30]解决远程设计时无法查询key列表的问题。
diff --git a/src/main/java/com/fr/plugin/db/redis/core/RedisDatabaseConnection.java b/src/main/java/com/fr/plugin/db/redis/core/RedisDatabaseConnection.java
index 4d86e06..c82aab8 100755
--- a/src/main/java/com/fr/plugin/db/redis/core/RedisDatabaseConnection.java
+++ b/src/main/java/com/fr/plugin/db/redis/core/RedisDatabaseConnection.java
@@ -5,6 +5,7 @@ import com.fr.config.holder.factory.Holders;
import com.fr.data.impl.AbstractDatabaseConnection;
import com.fr.data.impl.Connection;
import com.fr.locale.InterProviderFactory;
+import com.fr.plugin.db.redis.core.emb.Redis;
import com.fr.security.SecurityToolbox;
import com.fr.stable.ArrayUtils;
import com.fr.stable.StableUtils;
@@ -19,14 +20,10 @@ import java.util.Set;
public class RedisDatabaseConnection extends AbstractDatabaseConnection {
private static final int DEFAULT_REDIS_PORT = 6379;
- private static final int MAX_TOTAL = 10;
- private static final int MAX_WAIT = -1;
private Conf host = Holders.simple(StringUtils.EMPTY);
private Conf port = Holders.simple(DEFAULT_REDIS_PORT);
private Conf password = Holders.simple(StringUtils.EMPTY);
- private Conf maxTotal = Holders.simple(MAX_TOTAL);
- private Conf maxWait = Holders.simple(MAX_WAIT);
public RedisDatabaseConnection() {
@@ -56,27 +53,12 @@ public class RedisDatabaseConnection extends AbstractDatabaseConnection {
this.password.set(password);
}
- public int getMaxTotal() {
- return maxTotal.get();
- }
-
- public void setMaxTotal(Integer maxTotal) {
- this.maxTotal.set(maxTotal);
- }
-
- public int getMaxWait() {
- return maxWait.get();
- }
-
- public void setMaxWait(int maxWait) {
- this.maxWait.set(maxWait);
- }
@Override
public void testConnection() throws Exception {
- Jedis client = createRedisClient();
+ Redis client = createRedisClient();
try {
- String text = client.ping();
+ String text = client.getClient().ping();
if (!"pong".equalsIgnoreCase(text)) {
throw new Exception(text);
}
@@ -86,8 +68,8 @@ public class RedisDatabaseConnection extends AbstractDatabaseConnection {
}
- public Jedis createRedisClient() {
- return RedisPool.getPool().getResource(getHost(), getPort(), getPassword(), getMaxTotal(), getMaxWait());
+ public Redis createRedisClient() {
+ return RedisPool.getPool().getResource(getHost(), getPort(), getPassword());
}
@Override
@@ -100,8 +82,11 @@ public class RedisDatabaseConnection extends AbstractDatabaseConnection {
if (ArrayUtils.isEmpty(args)) {
return ArrayUtils.EMPTY_STRING_ARRAY;
} else {
- Set keys = createRedisClient().keys(args[0]);
- return keys.toArray(new String[0]);
+ Redis redis = createRedisClient();
+ Set keys = redis.getClient().keys(args[0]);
+ String[] array = keys.toArray(new String[0]);
+ redis.close();
+ return array;
}
}
@@ -161,8 +146,6 @@ public class RedisDatabaseConnection extends AbstractDatabaseConnection {
if (StringUtils.isNotEmpty(pwd)) {
setPassword(SecurityToolbox.encrypt(pwd));
}
- setMaxTotal(reader.getAttrAsInt("maxTotal", MAX_TOTAL));
- setMaxWait(reader.getAttrAsInt("maxWait", MAX_WAIT));
}
}
}
@@ -176,8 +159,6 @@ public class RedisDatabaseConnection extends AbstractDatabaseConnection {
if (StringUtils.isNotEmpty(getPassword())) {
writer.attr("password", SecurityToolbox.decrypt(getPassword()));
}
- writer.attr("maxTotal", getMaxTotal());
- writer.attr("maxWait", getMaxWait());
writer.end();
}
@@ -187,8 +168,6 @@ public class RedisDatabaseConnection extends AbstractDatabaseConnection {
cloned.host = (Conf) host.clone();
cloned.port = (Conf) port.clone();
cloned.password = (Conf) password.clone();
- cloned.maxTotal = (Conf) maxTotal.clone();
- cloned.maxWait = (Conf) maxWait.clone();
return cloned;
}
}
\ No newline at end of file
diff --git a/src/main/java/com/fr/plugin/db/redis/core/RedisPool.java b/src/main/java/com/fr/plugin/db/redis/core/RedisPool.java
index 0cdd3d0..5087d88 100644
--- a/src/main/java/com/fr/plugin/db/redis/core/RedisPool.java
+++ b/src/main/java/com/fr/plugin/db/redis/core/RedisPool.java
@@ -1,5 +1,14 @@
package com.fr.plugin.db.redis.core;
+import com.fr.log.FineLoggerFactory;
+import com.fr.plugin.db.redis.core.emb.Redis;
+import com.fr.plugin.db.redis.core.emb.impl.ProxyRedis;
+import com.fr.plugin.db.redis.core.emb.impl.SimpleRedis;
+import com.fr.plugin.db.redis.core.pool.RedisConnectionPoolConfig;
+import com.fr.plugin.db.redis.core.pool.RedisConnectionProxyConfig;
+import com.fr.ssh.jsch.JSch;
+import com.fr.ssh.jsch.JSchException;
+import com.fr.ssh.jsch.Session;
import com.fr.stable.StringUtils;
import com.fr.third.redis.clients.jedis.Jedis;
import com.fr.third.redis.clients.jedis.JedisPool;
@@ -25,18 +34,30 @@ public class RedisPool {
private Map jedisPoolMap = new ConcurrentHashMap();
- public Jedis getResource(String host, int port, String password, int maxTotal, int maxWait) {
- JedisPool jedisPool = jedisPoolMap.get(host);
- if (jedisPool == null) {
- jedisPool = createJedisPool(host, port, password, maxTotal, maxWait);
- jedisPoolMap.put(host, jedisPool);
+ public Redis getResource(String host, int port, String password) {
+ if (RedisConnectionProxyConfig.getInstance().isOpen()) {
+ try {
+ return createJedisBySSH(host, port, password);
+ } catch (JSchException e) {
+ FineLoggerFactory.getLogger().error(e.getMessage(), e);
+ }
+ } else {
+ JedisPool jedisPool = jedisPoolMap.get(host);
+ if (jedisPool == null) {
+ jedisPool = createJedisPool(host, port, password);
+ jedisPoolMap.put(host, jedisPool);
+ }
+ return new SimpleRedis(jedisPool.getResource());
}
- return jedisPool.getResource();
+ Jedis jedis = new Jedis(host, port);
+ jedis.auth(password);
+ return new SimpleRedis(jedis);
}
- private JedisPool createJedisPool(String host, int port, String password, int maxTotal, int maxWait) {
+ private JedisPool createJedisPool(String host, int port, String password) {
+ RedisConnectionPoolConfig poolConfig = RedisConnectionPoolConfig.getInstance();
JedisPoolConfig config = new JedisPoolConfig();
- config.setMaxTotal(maxTotal);
+ config.setMaxTotal(poolConfig.getMaxTotal());
//连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
config.setBlockWhenExhausted(true);
//设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
@@ -51,7 +72,7 @@ public class RedisPool {
config.setMaxIdle(10);
//最大连接数,可配置
//获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1
- config.setMaxWaitMillis(maxWait);
+ config.setMaxWaitMillis(poolConfig.getMaxWait());
//逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
config.setMinEvictableIdleTimeMillis(1800000);
//最小空闲连接数, 默认0
@@ -72,4 +93,28 @@ public class RedisPool {
return new JedisPool(config, host, port, TIME_OUT);
}
}
+
+ private Redis createJedisBySSH(String host, int port, String password) throws JSchException {
+ RedisConnectionProxyConfig proxyConfig = RedisConnectionProxyConfig.getInstance();
+ JSch jsch = new JSch();
+ if (StringUtils.isNotBlank(proxyConfig.getPrivateKeyPath())) {
+ jsch.addIdentity(proxyConfig.getPrivateKeyPath());
+ }
+ Session session = jsch.getSession(proxyConfig.getUsername(), proxyConfig.getHost(), proxyConfig.getPort());
+ if (StringUtils.isNotBlank(proxyConfig.getPassword())) {
+ session.setPassword(proxyConfig.getPassword());
+ }
+ session.setConfig("StrictHostKeyChecking", "no");
+ session.connect();
+
+ int newPort = session.setPortForwardingL(0, host, port);
+
+ Jedis jedis = new Jedis(host, newPort);
+
+ if (StringUtils.isNotBlank(password)) {
+ jedis.auth(password);
+ }
+ return new ProxyRedis(session, jedis);
+
+ }
}
diff --git a/src/main/java/com/fr/plugin/db/redis/core/RedisScriptTableDataModel.java b/src/main/java/com/fr/plugin/db/redis/core/RedisScriptTableDataModel.java
index c673af4..f163ac8 100644
--- a/src/main/java/com/fr/plugin/db/redis/core/RedisScriptTableDataModel.java
+++ b/src/main/java/com/fr/plugin/db/redis/core/RedisScriptTableDataModel.java
@@ -5,6 +5,7 @@ import com.eclipsesource.v8.V8Array;
import com.eclipsesource.v8.V8Object;
import com.fr.base.TableData;
import com.fr.data.AbstractDataModel;
+import com.fr.plugin.db.redis.core.emb.Redis;
import com.fr.plugin.db.redis.help.Console;
import com.fr.plugin.db.redis.help.Files;
import com.fr.plugin.db.redis.help.RedisClient;
@@ -40,10 +41,10 @@ public class RedisScriptTableDataModel extends AbstractDataModel {
if (columnNames != null) {
return;
}
- Jedis jedis = mc.createRedisClient();
- jedis.select(dbIndex);
+ Redis redis = mc.createRedisClient();
+ redis.getClient().select(dbIndex);
V8 v8 = V8.createV8Runtime();
- RedisClient client = new RedisClient(v8, jedis);
+ RedisClient client = new RedisClient(v8, redis.getClient());
V8Object v8Console = initConsole(v8);
V8Object v8Require = initRequire(v8);
V8Object v8Client = initRedis(v8, client);
@@ -72,7 +73,7 @@ public class RedisScriptTableDataModel extends AbstractDataModel {
v8Row.release();
data.add(row);
}
- jedis.close();
+ redis.close();
v8Table.release();
v8Column.release();
returnObj.release();
diff --git a/src/main/java/com/fr/plugin/db/redis/core/RedisTableDataModel.java b/src/main/java/com/fr/plugin/db/redis/core/RedisTableDataModel.java
index f5a5870..f54dd9d 100755
--- a/src/main/java/com/fr/plugin/db/redis/core/RedisTableDataModel.java
+++ b/src/main/java/com/fr/plugin/db/redis/core/RedisTableDataModel.java
@@ -5,7 +5,9 @@ import com.fr.data.AbstractDataModel;
import com.fr.log.FineLoggerFactory;
import com.fr.plugin.PluginLicense;
import com.fr.plugin.PluginLicenseManager;
+import com.fr.plugin.db.redis.core.emb.Redis;
import com.fr.plugin.db.redis.core.visit.VisitorFactory;
+import com.fr.plugin.db.redis.util.RedisUtils;
import com.fr.script.Calculator;
import com.fr.stable.StringUtils;
import com.fr.third.redis.clients.jedis.Jedis;
@@ -29,17 +31,17 @@ public class RedisTableDataModel extends AbstractDataModel {
if (StringUtils.isEmpty(query)) {
return;
}
- Jedis redisClient = mc.createRedisClient();
- redisClient.select(dbIndex);
+ Redis redis = mc.createRedisClient();
+ redis.getClient().select(dbIndex);
FineLoggerFactory.getLogger().info("Connect to redis and select database:" + dbIndex);
try {
long start = System.currentTimeMillis();
- DataWrapper