Skip to content

Commit e0e57e0

Browse files
authored
Merge pull request #299 from JiazhenBao/main
redis依赖库优化
2 parents 6cf9291 + e454ddf commit e0e57e0

File tree

4 files changed

+165
-86
lines changed

4 files changed

+165
-86
lines changed

redis-template-tool/pom.xml

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,67 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
55

6-
<groupId>com.netease.lowcode</groupId>
7-
<artifactId>redis-template-tool</artifactId>
8-
<version>0.2.0</version>
9-
<parent>
10-
<groupId>org.springframework.boot</groupId>
11-
<artifactId>spring-boot-starter-parent</artifactId>
12-
<version>2.2.9.RELEASE</version><!--与当前制品应用默认版本统一-->
13-
</parent>
14-
<properties>
15-
<java.version>8</java.version>
16-
<maven.compiler.source>${java.version}</maven.compiler.source>
17-
<maven.compiler.target>${java.version}</maven.compiler.target>
18-
<nasl.ide.version>3.3</nasl.ide.version>
19-
</properties>
6+
<groupId>com.netease.lowcode</groupId>
7+
<artifactId>redis-template-tool</artifactId>
8+
<version>1.0.0</version>
9+
<parent>
10+
<groupId>org.springframework.boot</groupId>
11+
<artifactId>spring-boot-starter-parent</artifactId>
12+
<version>2.2.9.RELEASE</version><!--与当前制品应用默认版本统一-->
13+
</parent>
14+
<properties>
15+
<java.version>8</java.version>
16+
<maven.compiler.source>${java.version}</maven.compiler.source>
17+
<maven.compiler.target>${java.version}</maven.compiler.target>
18+
<nasl.ide.version>3.3</nasl.ide.version>
19+
</properties>
2020

2121

22-
<dependencies>
23-
<dependency>
24-
<groupId>org.springframework.boot</groupId>
25-
<artifactId>spring-boot-starter-data-redis</artifactId>
26-
</dependency>
27-
<dependency>
28-
<artifactId>nasl-metadata-collector</artifactId>
29-
<groupId>com.netease.lowcode</groupId>
30-
<version>0.8.0</version>
31-
<optional>true</optional>
32-
</dependency>
33-
<dependency>
34-
<groupId>org.springframework.boot</groupId>
35-
<artifactId>spring-boot-starter-test</artifactId>
36-
<scope>test</scope>
37-
</dependency>
38-
<dependency>
39-
<groupId>junit</groupId>
40-
<artifactId>junit</artifactId>
41-
<scope>test</scope>
42-
</dependency>
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-data-redis</artifactId>
26+
</dependency>
27+
<dependency>
28+
<artifactId>nasl-metadata-collector</artifactId>
29+
<groupId>com.netease.lowcode</groupId>
30+
<version>0.8.0</version>
31+
<optional>true</optional>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>junit</groupId>
40+
<artifactId>junit</artifactId>
41+
<scope>test</scope>
42+
</dependency>
4343

44-
</dependencies>
45-
<build>
46-
<plugins>
47-
<plugin>
48-
<groupId>com.netease.lowcode</groupId>
49-
<artifactId>nasl-metadata-maven-plugin</artifactId>
50-
<version>1.3.0</version>
51-
<configuration>
52-
<jarWithDependencies>false</jarWithDependencies>
53-
</configuration>
54-
<executions>
55-
<execution>
56-
<goals>
57-
<goal>archive</goal>
58-
</goals>
59-
</execution>
60-
</executions>
61-
</plugin>
62-
</plugins>
63-
</build>
44+
</dependencies>
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>com.netease.lowcode</groupId>
49+
<artifactId>nasl-metadata-maven-plugin</artifactId>
50+
<version>1.5.1</version>
51+
<configuration>
52+
<jarWithDependencies>false</jarWithDependencies>
53+
<!-- <rewriteVersion>true</rewriteVersion>-->
54+
</configuration>
55+
<executions>
56+
<execution>
57+
<goals>
58+
<goal>archive</goal>
59+
</goals>
60+
</execution>
61+
</executions>
62+
</plugin>
63+
</plugins>
64+
</build>
6465

6566

6667
</project>

redis-template-tool/src/main/java/com/netease/lib/redistemplatetool/config/RedisConfig.java

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
package com.netease.lib.redistemplatetool.config;
22

3+
import com.netease.lowcode.core.EnvironmentType;
4+
import com.netease.lowcode.core.annotation.Environment;
35
import com.netease.lowcode.core.annotation.NaslConfiguration;
46
import org.springframework.context.annotation.Configuration;
57

68

79
@Configuration
810
public class RedisConfig {
11+
/**
12+
* redis配置类型1.url 2.单机 3.sentinel 4.cluster
13+
*/
14+
15+
@NaslConfiguration(defaultValue = {@Environment(type = EnvironmentType.DEV, value = "1"),
16+
@Environment(type = EnvironmentType.ONLINE, value = "1")})
17+
public String redisMode;
918
/**
1019
* redis 地址。redis模式多选一,按需配置。无用的配置可空
1120
*/
@@ -32,26 +41,22 @@ public class RedisConfig {
3241
*/
3342
@NaslConfiguration
3443
public String redisSentinelNodes;
35-
/**
36-
* redis哨兵密码
37-
*/
38-
@NaslConfiguration
39-
private String redisSentinelPassword;
4044
/**
4145
* redis cluster 节点
4246
*/
4347
@NaslConfiguration
4448
public String redisClusterNodes;
4549
/**
46-
* redis slave 地址
50+
* redis cluster 节点最大重定向次数
4751
*/
4852
@NaslConfiguration
49-
public String redisSlaveHost;
53+
private String redisClusterMaxRedirects;
54+
5055
/**
51-
* redis slave 端口
56+
* redis哨兵密码
5257
*/
5358
@NaslConfiguration
54-
public String redisSlavePort;
59+
private String redisSentinelPassword;
5560
/**
5661
* Redis 数据库索引
5762
*/
@@ -68,6 +73,16 @@ public class RedisConfig {
6873
*/
6974
@NaslConfiguration
7075
private String springRedisSsl;
76+
/**
77+
* redis url
78+
*/
79+
@NaslConfiguration
80+
private String redisUrl;
81+
/**
82+
* 客户端名称
83+
*/
84+
@NaslConfiguration
85+
private String redisClientName;
7186
/**
7287
* 连接池最大连接数。
7388
*/
@@ -84,18 +99,13 @@ public class RedisConfig {
8499
@NaslConfiguration
85100
private String springRedisLettucePoolMinIdle;
86101
/**
87-
* 执行命令的超时时间,默认为 3 秒
102+
* 获取连接时的最大等待时间,默认为 100 毫秒
88103
*/
89104
@NaslConfiguration
90-
private String springRedisLettuceCommandTimeout;
105+
private String springRedisLettucePoolMaxWait;
91106
/**
92107
* 关闭连接时的超时时间,默认为 100 毫秒。
93108
*/
94109
@NaslConfiguration
95110
private String springRedisLettuceShutdownTimeout;
96-
/**
97-
* 设置客户端名称。
98-
*/
99-
@NaslConfiguration
100-
private String springRedisLettuceClientName;
101111
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.netease.lib.redistemplatetool.util;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public enum RedisModeEnum {
7+
// url
8+
URL_MODE("1", Arrays.asList("extensions.redis_template_tool.custom.redisUrl")),
9+
//单机
10+
SINGLE_MODE("2", Arrays.asList("extensions.redis_template_tool.custom.redisHost", "extensions.redis_template_tool.custom.redisPort")),
11+
//哨兵
12+
SENTINEL_MODE("3", Arrays.asList("extensions.redis_template_tool.custom.redisSentinelMaster", "extensions.redis_template_tool.custom.redisSentinelNodes", "extensions.redis_template_tool.custom.redisSentinelPassword")),
13+
//集群
14+
CLUSTER_MODE("4", Arrays.asList("extensions.redis_template_tool.custom.redisClusterNodes", "extensions.redis_template_tool.custom.redisClusterMaxRedirects")),
15+
;
16+
17+
private final String key;
18+
private final List<String> dataMap;
19+
20+
// 构造函数
21+
RedisModeEnum(String key, List<String> dataMap) {
22+
this.dataMap = dataMap;
23+
this.key = key;
24+
}
25+
26+
public static List<String> getRedisModeValueByKey(String key) {
27+
RedisModeEnum[] redisModeEnums = RedisModeEnum.values();
28+
for (int i = 0; i < redisModeEnums.length; i++) {
29+
if (redisModeEnums[i].getKey().equals(key)) {
30+
return redisModeEnums[i].getDataMap();
31+
}
32+
}
33+
return null;
34+
}
35+
36+
public String getKey() {
37+
return key;
38+
}
39+
40+
public List<String> getDataMap() {
41+
return dataMap;
42+
}
43+
}

redis-template-tool/src/main/java/com/netease/lib/redistemplatetool/util/RedisToolEnvironmentMapperPostProcessor.java

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,39 @@
88
import org.springframework.util.StringUtils;
99

1010
import java.util.HashMap;
11+
import java.util.List;
1112
import java.util.Map;
1213

1314
@Order
1415
public class RedisToolEnvironmentMapperPostProcessor implements EnvironmentPostProcessor {
1516
private static final Map<String, String> REDIS_CONFIG_MAPPER = new HashMap<>();
17+
private static final Map<String, String> REDIS_CONFIG_MAPPER_COMMON = new HashMap<>();
1618

1719
//File文件类型 数据权限
1820
static {
21+
//url
22+
REDIS_CONFIG_MAPPER.put("extensions.redis_template_tool.custom.redisUrl", "spring.redis.url");
23+
//单机
1924
REDIS_CONFIG_MAPPER.put("extensions.redis_template_tool.custom.redisHost", "spring.redis.host");
2025
REDIS_CONFIG_MAPPER.put("extensions.redis_template_tool.custom.redisPort", "spring.redis.port");
21-
REDIS_CONFIG_MAPPER.put("extensions.redis_template_tool.custom.redisDatabase", "spring.redis.database");
22-
REDIS_CONFIG_MAPPER.put("extensions.redis_template_tool.custom.redisPassword", "spring.redis.password");
26+
//哨兵
2327
REDIS_CONFIG_MAPPER.put("extensions.redis_template_tool.custom.redisSentinelMaster", "spring.redis.sentinel.master");
2428
REDIS_CONFIG_MAPPER.put("extensions.redis_template_tool.custom.redisSentinelNodes", "spring.redis.sentinel.nodes");
25-
REDIS_CONFIG_MAPPER.put("extensions.redis_template_tool.custom.redisClusterNodes", "spring.redis.cluster.nodes");
26-
REDIS_CONFIG_MAPPER.put("extensions.redis_template_tool.custom.redisSlaveHost", "spring.redis.slave.host");
27-
REDIS_CONFIG_MAPPER.put("extensions.redis_template_tool.custom.redisSlavePort", "spring.redis.slave.port");
2829
REDIS_CONFIG_MAPPER.put("extensions.redis_template_tool.custom.redisSentinelPassword", "spring.redis.sentinel.password");
29-
REDIS_CONFIG_MAPPER.put("extensions.redis_template_tool.custom.springRedisTimeout", "spring.redis.timeout");
30-
REDIS_CONFIG_MAPPER.put("extensions.redis_template_tool.custom.springRedisSsl", "spring.redis.ssl");
31-
REDIS_CONFIG_MAPPER.put("extensions.redis_template_tool.custom.springRedisLettucePoolMaxActive", "spring.redis.lettuce.pool.max-active");
32-
REDIS_CONFIG_MAPPER.put("extensions.redis_template_tool.custom.springRedisLettucePoolMaxIdle", "spring.redis.lettuce.pool.max-idle");
33-
REDIS_CONFIG_MAPPER.put("extensions.redis_template_tool.custom.springRedisLettucePoolMinIdle", "spring.redis.lettuce.pool.min-idle");
34-
REDIS_CONFIG_MAPPER.put("extensions.redis_template_tool.custom.springRedisLettuceCommandTimeout", "spring.redis.lettuce.command-timeout");
35-
REDIS_CONFIG_MAPPER.put("extensions.redis_template_tool.custom.springRedisLettuceShutdownTimeout", "spring.redis.lettuce.shutdown-timeout");
36-
REDIS_CONFIG_MAPPER.put("extensions.redis_template_tool.custom.springRedisLettuceClientName", "spring.redis.lettuce.client-name");
30+
//集群
31+
REDIS_CONFIG_MAPPER.put("extensions.redis_template_tool.custom.redisClusterNodes", "spring.redis.cluster.nodes");
32+
REDIS_CONFIG_MAPPER.put("extensions.redis_template_tool.custom.redisClusterMaxRedirects", "spring.redis.cluster.maxRedirects");
33+
//公共
34+
REDIS_CONFIG_MAPPER_COMMON.put("extensions.redis_template_tool.custom.redisDatabase", "spring.redis.database");
35+
REDIS_CONFIG_MAPPER_COMMON.put("extensions.redis_template_tool.custom.redisPassword", "spring.redis.password");
36+
REDIS_CONFIG_MAPPER_COMMON.put("extensions.redis_template_tool.custom.redisClientName", "spring.redis.clientName");
37+
REDIS_CONFIG_MAPPER_COMMON.put("extensions.redis_template_tool.custom.springRedisTimeout", "spring.redis.timeout");
38+
REDIS_CONFIG_MAPPER_COMMON.put("extensions.redis_template_tool.custom.springRedisSsl", "spring.redis.ssl");
39+
REDIS_CONFIG_MAPPER_COMMON.put("extensions.redis_template_tool.custom.springRedisLettucePoolMaxActive", "spring.redis.lettuce.pool.max-active");
40+
REDIS_CONFIG_MAPPER_COMMON.put("extensions.redis_template_tool.custom.springRedisLettucePoolMaxIdle", "spring.redis.lettuce.pool.max-idle");
41+
REDIS_CONFIG_MAPPER_COMMON.put("extensions.redis_template_tool.custom.springRedisLettucePoolMinIdle", "spring.redis.lettuce.pool.min-idle");
42+
REDIS_CONFIG_MAPPER_COMMON.put("extensions.redis_template_tool.custom.springRedisLettucePoolMaxWait", "spring.redis.lettuce.pool.max-wait");
43+
REDIS_CONFIG_MAPPER_COMMON.put("extensions.redis_template_tool.custom.springRedisLettuceShutdownTimeout", "spring.redis.lettuce.shutdown-timeout");
3744
}
3845

3946
/**
@@ -45,7 +52,25 @@ public class RedisToolEnvironmentMapperPostProcessor implements EnvironmentPostP
4552
@Override
4653
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
4754
Map<String, Object> mapperProperties = new HashMap<>();
55+
String redisMode = environment.getProperty("extensions.redis_template_tool.custom.redisMode");
56+
if (StringUtils.isEmpty(redisMode)) {
57+
redisMode = "1";
58+
}
59+
List<String> redisCustomConfigList = RedisModeEnum.getRedisModeValueByKey(redisMode);
60+
//自定义模式配置
4861
REDIS_CONFIG_MAPPER.forEach((key, value) -> {
62+
if (environment.containsProperty(value)) {
63+
return;
64+
}
65+
if (environment.containsProperty(key) && redisCustomConfigList.contains(key)) {
66+
String property = environment.getProperty(key);
67+
if (!StringUtils.isEmpty(property)) {
68+
mapperProperties.put(value, property);
69+
}
70+
}
71+
});
72+
//公共配置
73+
REDIS_CONFIG_MAPPER_COMMON.forEach((key, value) -> {
4974
if (environment.containsProperty(value)) {
5075
return;
5176
}

0 commit comments

Comments
 (0)