Skip to content

Commit 23dc70e

Browse files
author
bob
committed
学习springboot-redis和自己搭建redis-starter、jedisCluster-starter
1 parent 7d5a459 commit 23dc70e

File tree

33 files changed

+1055
-17
lines changed

33 files changed

+1055
-17
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ target/
1212
### IntelliJ IDEA ###
1313
.idea
1414
*.iws
15-
springboot-stu02-jpa/springboot-stu02-jpa.iml
15+
*.iml
1616
*.ipr
1717

1818
### NetBeans ###

bob-starter-jediscluster/pom.xml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.bob.stu</groupId>
8+
<artifactId>bob-starter-jediscluster</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
15+
<java.version>1.8</java.version>
16+
<boot.version>1.5.3.RELEASE</boot.version>
17+
<redis.version>2.9.0</redis.version>
18+
<commons-lang3.version>3.3.2</commons-lang3.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot</artifactId>
25+
<version>${boot.version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-autoconfigure</artifactId>
30+
<version>${boot.version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-configuration-processor</artifactId>
35+
<version>${boot.version}</version>
36+
<optional>true</optional>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-test</artifactId>
41+
<version>${boot.version}</version>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>redis.clients</groupId>
46+
<artifactId>jedis</artifactId>
47+
<version>${redis.version}</version>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.apache.commons</groupId>
52+
<artifactId>commons-lang3</artifactId>
53+
<version>${commons-lang3.version}</version>
54+
</dependency>
55+
</dependencies>
56+
57+
<build>
58+
<plugins>
59+
<plugin>
60+
<groupId>org.springframework.boot</groupId>
61+
<artifactId>spring-boot-maven-plugin</artifactId>
62+
</plugin>
63+
</plugins>
64+
</build>
65+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.bob.jediscluster.autoconfigure;
2+
3+
import com.bob.jediscluster.client.JedisClusterClient;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.beans.factory.annotation.Qualifier;
6+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
7+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
8+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
import redis.clients.jedis.HostAndPort;
12+
import redis.clients.jedis.JedisCluster;
13+
14+
import java.util.HashSet;
15+
import java.util.Map;
16+
import java.util.Set;
17+
18+
/**
19+
* 自动装配JedisCluster
20+
*
21+
* @author bob <bobyang_coder@163.com>
22+
* @since 2017/5/30
23+
*/
24+
@Configuration()
25+
@EnableConfigurationProperties(JedisClusterProperties.class)
26+
@ConditionalOnClass(JedisClusterClient.class)
27+
public class JedisClusterAutoConfiguration {
28+
29+
@Autowired
30+
private JedisClusterProperties jcp;
31+
32+
@Bean("jedisCluster")
33+
public JedisCluster jedisCluster() {
34+
Map<Integer, String> nodesMap = jcp.getNodes();
35+
if (null == nodesMap || nodesMap.size() < 1) {
36+
throw new IllegalArgumentException("必要参数jedis.cluster.nodes为空");
37+
}
38+
Set<HostAndPort> nodes = new HashSet<>();
39+
for (int port : nodesMap.keySet()) {
40+
String host = nodesMap.get(port);
41+
HostAndPort hostAndPort = new HostAndPort(host, port);
42+
nodes.add(hostAndPort);
43+
}
44+
return new JedisCluster(nodes, jcp.getConnectTimeOut(), jcp.getJedisPoolConfig());
45+
}
46+
47+
@Bean
48+
@ConditionalOnMissingBean(JedisClusterClient.class)//容器中如果没有JedisClusterClient这个类,那么自动配置这个JedisClusterClient
49+
public JedisClusterClient jedisClusterClient(@Qualifier("jedisCluster") JedisCluster jedisCluster) {
50+
JedisClusterClient jedisClusterClient = new JedisClusterClient();
51+
jedisClusterClient.setJedisCluster(jedisCluster);
52+
return jedisClusterClient;
53+
}
54+
55+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.bob.jediscluster.autoconfigure;
2+
3+
import org.springframework.boot.context.properties.ConfigurationProperties;
4+
import redis.clients.jedis.JedisPoolConfig;
5+
6+
import java.util.Map;
7+
8+
/**
9+
* JedisCluster配置对象
10+
*
11+
* @author bob <bobyang_coder@163.com>
12+
* @since 2017/5/30
13+
*/
14+
@ConfigurationProperties(prefix = "jedis.cluster")
15+
public class JedisClusterProperties {
16+
17+
private int expireSeconds;
18+
19+
private String clusterNodes;//集群节点
20+
21+
private int connectTimeOut;//连接超时时间
22+
23+
private Map<Integer, String> nodes;//集群节点
24+
25+
private JedisPoolConfig jedisPoolConfig;
26+
27+
public int getExpireSeconds() {
28+
return expireSeconds;
29+
}
30+
31+
public void setExpireSeconds(int expireSeconds) {
32+
this.expireSeconds = expireSeconds;
33+
}
34+
35+
public String getClusterNodes() {
36+
return clusterNodes;
37+
}
38+
39+
public void setClusterNodes(String clusterNodes) {
40+
this.clusterNodes = clusterNodes;
41+
}
42+
43+
public int getConnectTimeOut() {
44+
return connectTimeOut;
45+
}
46+
47+
public void setConnectTimeOut(int connectTimeOut) {
48+
this.connectTimeOut = connectTimeOut;
49+
}
50+
51+
public Map<Integer, String> getNodes() {
52+
return nodes;
53+
}
54+
55+
public void setNodes(Map<Integer, String> nodes) {
56+
this.nodes = nodes;
57+
}
58+
59+
public JedisPoolConfig getJedisPoolConfig() {
60+
return jedisPoolConfig;
61+
}
62+
63+
public void setJedisPoolConfig(JedisPoolConfig jedisPoolConfig) {
64+
this.jedisPoolConfig = jedisPoolConfig;
65+
}
66+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.bob.jediscluster.client;
2+
3+
import redis.clients.jedis.JedisCluster;
4+
5+
/**
6+
* JedisCluster客户端
7+
*
8+
* @author bob <bobyang_coder@163.com>
9+
* @since 2017/5/30
10+
*/
11+
public class JedisClusterClient {
12+
private JedisCluster jedisCluster;
13+
14+
/**
15+
* set方法
16+
*
17+
* @param key
18+
* @param value
19+
* @return
20+
*/
21+
public String set(final String key, final String value) {
22+
return jedisCluster.set(key, value);
23+
}
24+
25+
/**
26+
* 保存数据到redis中,并且设置过期时间
27+
*
28+
* @param key
29+
* @param value
30+
* @param expire
31+
* @return
32+
*/
33+
public String set(final String key, final String value, final Integer expire) {
34+
String str = jedisCluster.set(key, value);// 设置数据
35+
jedisCluster.expire(key, expire);
36+
return str;
37+
}
38+
39+
/**
40+
* 设置过期时间
41+
*
42+
* @param key
43+
* @param expire
44+
* @return
45+
*/
46+
public Long expire(final String key, final Integer expire) {
47+
return jedisCluster.expire(key, expire);
48+
}
49+
50+
/**
51+
* get 方法
52+
*
53+
* @param key
54+
* @return
55+
*/
56+
public String get(final String key) {
57+
return jedisCluster.get(key);
58+
}
59+
60+
/**
61+
* 根据key删除redis中的数据
62+
*
63+
* @param key
64+
* @return
65+
*/
66+
public Long del(final String key) {
67+
return jedisCluster.del(key);
68+
}
69+
70+
public void setJedisCluster(JedisCluster jedisCluster) {
71+
this.jedisCluster = jedisCluster;
72+
}
73+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Auto Configure
2+
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
3+
com.bob.jediscluster.autoconfigure.JedisClusterAutoConfiguration
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#为该starter默认设置参数,如果需要修改此配置,可在自己项目的application文件中定义参数值
2+
#JedisCluster默认配置
3+
jedis :
4+
cluster:
5+
nodes:
6+
6379: 127.0.0.1
7+
expire-seconds: 10
8+
jedisPoolConfig:
9+
#最大连接数
10+
maxTotal: 30
11+
#最大空闲连接数
12+
maxIdle: 10
13+
#每次释放连接的最大数目
14+
numTestsPerEvictionRun: 1024
15+
#释放连接的扫描间隔(毫秒)
16+
timeBetweenEvictionRunsMillis: 30000
17+
#连接最小空闲时间
18+
minEvictableIdleTimeMillis: 1800000
19+
#连接空闲多久后释放, 当空闲时间>该值且空闲连接>最大空闲连接数时直接释放
20+
softMinEvictableIdleTimeMillis: 10000
21+
#获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1
22+
maxWaitMillis: 1500
23+
#在获取连接的时候检查有效性, 默认false
24+
testOnBorrow: true
25+
#在空闲时检查有效性, 默认false
26+
testWhileIdle: true
27+
#连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
28+
blockWhenExhausted: false
29+
server :
30+
port : 8088

bob-starter-redis/pom.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.bob.stu</groupId>
8+
<artifactId>bob-starter-redis</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
15+
<java.version>1.8</java.version>
16+
<boot.version>1.5.3.RELEASE</boot.version>
17+
<redis.version>2.9.0</redis.version>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot</artifactId>
24+
<version>${boot.version}</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-autoconfigure</artifactId>
29+
<version>${boot.version}</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-configuration-processor</artifactId>
34+
<version>${boot.version}</version>
35+
<optional>true</optional>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-test</artifactId>
40+
<version>${boot.version}</version>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>redis.clients</groupId>
45+
<artifactId>jedis</artifactId>
46+
<version>${redis.version}</version>
47+
</dependency>
48+
</dependencies>
49+
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-maven-plugin</artifactId>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
</project>

0 commit comments

Comments
 (0)