Skip to content

Commit

Permalink
redis 集群case
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyueyi committed Sep 27, 2019
1 parent 7deed22 commit 63bc82c
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 0 deletions.
1 change: 1 addition & 0 deletions readme.md
Expand Up @@ -43,6 +43,7 @@ SpringBoot与SpringCloud学习过程中的源码汇总,沉淀记录下学习
- jedis环境构建 [spring-boot/121-redis-jedis-config](spring-boot/121-redis-jedis-config)
- redisTemplate使用姿势 [spring-boot/122-redis-template](spring-boot/122-redis-template)
- lettuce环境构建 [spring-boot/123-redis-lettuce-config](spring-boot/123-redis-lettuce-config)
- redis集群实例工程 [spring-boot/124-redis-cluster](spring-boot/124-redis-cluster)
- 排行榜应用实例工程 [spring-case/120-redis-ranklist](spring-case/120-redis-ranklist)
- 站点统计应用实例工程 [spring-case/124-redis-sitecount](spring-case/124-redis-sitecount)
- [ ] MemCache
Expand Down
30 changes: 30 additions & 0 deletions spring-boot/124-redis-cluster/.gitignore
@@ -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/
30 changes: 30 additions & 0 deletions spring-boot/124-redis-cluster/pom.xml
@@ -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>
@@ -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);
}
}
@@ -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 spring-boot/124-redis-cluster/src/main/resources/application.yml
@@ -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
1 change: 1 addition & 0 deletions spring-boot/pom.xml
Expand Up @@ -44,6 +44,7 @@
<module>206-web-beetl</module>
<module>003-log4j2</module>
<module>207-web-response</module>
<module>124-redis-cluster</module>
</modules>

<artifactId>spring-boot</artifactId>
Expand Down

0 comments on commit 63bc82c

Please sign in to comment.