Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
.* | ||
|
||
!.gitignore | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/build/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
|
||
logs/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>spring-boot</artifactId> | ||
<groupId>com.git.hui.boot</groupId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>124-redis-cluster</artifactId> | ||
|
||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-redis</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>redis.clients</groupId> | ||
<artifactId>jedis</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-pool2</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
22 changes: 22 additions & 0 deletions
22
spring-boot/124-redis-cluster/src/main/java/com/git/hui/boot/redis/cluster/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.git.hui.boot.redis.cluster; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
|
||
/** | ||
* Created by @author yihui in 17:55 19/9/27. | ||
*/ | ||
@SpringBootApplication | ||
public class Application { | ||
|
||
public Application(RedisTemplate redisTemplate) { | ||
redisTemplate.opsForValue().set("spring-r-cluster-1", 123); | ||
redisTemplate.opsForValue().set("spring-r-cluster-2", 456); | ||
redisTemplate.opsForValue().set("spring-r-cluster-3", 789); | ||
} | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...24-redis-cluster/src/main/java/com/git/hui/boot/redis/cluster/config/RedisAutoConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.git.hui.boot.redis.cluster.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.RedisClusterConfiguration; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.connection.RedisNode; | ||
import org.springframework.data.redis.connection.RedisPassword; | ||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; | ||
import redis.clients.jedis.JedisPoolConfig; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by @author yihui in 18:22 18/10/30. | ||
*/ | ||
@Configuration | ||
public class RedisAutoConfig { | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory(JedisPoolConfig jedisPool, | ||
RedisClusterConfiguration jedisConfig) { | ||
JedisConnectionFactory factory = new JedisConnectionFactory(jedisConfig, jedisPool); | ||
factory.afterPropertiesSet(); | ||
return factory; | ||
} | ||
|
||
@Configuration | ||
public static class JedisConf { | ||
@Value("${spring.redis.cluster.nodes:127.0.0.1:7000,127.0.0.1:7001,127.0.0.1:7002}") | ||
private String nodes; | ||
@Value("${spring.redis.cluster.max-redirects:3}") | ||
private Integer maxRedirects; | ||
@Value("${spring.redis.password:}") | ||
private String password; | ||
@Value("${spring.redis.database:0}") | ||
private Integer database; | ||
|
||
@Value("${spring.redis.jedis.pool.max-active:8}") | ||
private Integer maxActive; | ||
@Value("${spring.redis.jedis.pool.max-idle:8}") | ||
private Integer maxIdle; | ||
@Value("${spring.redis.jedis.pool.max-wait:-1}") | ||
private Long maxWait; | ||
@Value("${spring.redis.jedis.pool.min-idle:0}") | ||
private Integer minIdle; | ||
|
||
@Bean | ||
public JedisPoolConfig jedisPool() { | ||
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); | ||
jedisPoolConfig.setMaxIdle(maxIdle); | ||
jedisPoolConfig.setMaxWaitMillis(maxWait); | ||
jedisPoolConfig.setMaxTotal(maxActive); | ||
jedisPoolConfig.setMinIdle(minIdle); | ||
return jedisPoolConfig; | ||
} | ||
|
||
@Bean | ||
public RedisClusterConfiguration jedisConfig() { | ||
RedisClusterConfiguration config = new RedisClusterConfiguration(); | ||
|
||
String[] sub = nodes.split(","); | ||
List<RedisNode> nodeList = new ArrayList<>(sub.length); | ||
String[] tmp; | ||
for (String s : sub) { | ||
tmp = s.split(":"); | ||
// fixme 先不考虑异常配置的case | ||
nodeList.add(new RedisNode(tmp[0], Integer.valueOf(tmp[1]))); | ||
} | ||
|
||
config.setClusterNodes(nodeList); | ||
config.setMaxRedirects(maxRedirects); | ||
config.setPassword(RedisPassword.of(password)); | ||
return config; | ||
} | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
spring-boot/124-redis-cluster/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
spring: | ||
redis: | ||
password: | ||
cluster: | ||
nodes: 192.168.0.203:7000,192.168.0.203:7001,192.168.0.203:7002 | ||
max-redirects: 3 | ||
jedis: | ||
pool: | ||
max-idle: 16 | ||
max-active: 32 | ||
min-idle: 8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters