Skip to content

Commit

Permalink
哨兵redis
Browse files Browse the repository at this point in the history
  • Loading branch information
rstyro committed Apr 24, 2021
1 parent dbf4333 commit 036e40c
Show file tree
Hide file tree
Showing 10 changed files with 324 additions and 0 deletions.
62 changes: 62 additions & 0 deletions springboot-redis/springboot-redis-sentinel/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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">
<modelVersion>4.0.0</modelVersion>

<groupId>top.lrshuai</groupId>
<artifactId>springboot-redis-sentinel</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>springboot-redis-sentinel</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>top.lrshuai</groupId>
<artifactId>springboot-redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<!-- 排除lettuce包,使用jedis代替,如果使用lettuce,则把下面的exclusions注释上-->
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package top.lrshuai;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootRedisApplication {

public static void main(String[] args) {
SpringApplication.run(SpringbootRedisApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package top.lrshuai.config;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
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.RedisNode;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
*
* @author rstyro
* jedis 配置
*/
@Configuration
public class RedisJedisConfig {

@Value("${spring.redis.sentinel.password}")
private String password;

@Value("${spring.redis.sentinel.master}")
private String masterName;

@Value("${spring.redis.sentinel.nodes}")
private String[] sentinels;

@Bean
public JedisConnectionFactory redisConnectionFactory() {
// 哨兵配置
RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
redisSentinelConfiguration.setMaster(masterName);
for (String sentinel : sentinels) {
String[] arr = sentinel.split(":");
redisSentinelConfiguration.sentinel(new RedisNode(arr[0], Integer.parseInt(arr[1])));
}
redisSentinelConfiguration.setPassword(RedisPassword.of(password));
return new JedisConnectionFactory(redisSentinelConfiguration);
}

@Bean
public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String,Object> template = new RedisTemplate<>();
//使用fastjson序列化
FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
// value值的序列化采用fastJsonRedisSerializer
template.setValueSerializer(fastJsonRedisSerializer);
template.setHashValueSerializer(fastJsonRedisSerializer);
// key的序列化采用StringRedisSerializer
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package top.lrshuai.config;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
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.RedisNode;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
*
* @author rstyro
* lettuce 配置,springboot2 默认的
*
*/
//@Configuration
public class RedisLettuceConfig {

@Value("${spring.redis.sentinel.password}")
private String password;

@Value("${spring.redis.sentinel.master}")
private String masterName;

@Value("${spring.redis.sentinel.nodes}")
private String[] sentinels;

@Bean
public LettuceConnectionFactory lettuceConnectionFactory() {
// 哨兵配置
RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
redisSentinelConfiguration.setMaster(masterName);
for (String sentinel : sentinels) {
String[] arr = sentinel.split(":");
redisSentinelConfiguration.sentinel(new RedisNode(arr[0], Integer.parseInt(arr[1])));
}
redisSentinelConfiguration.setPassword(RedisPassword.of(password));
return new LettuceConnectionFactory(redisSentinelConfiguration);
}

@Bean
public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
RedisTemplate<String,Object> template = new RedisTemplate<>();
//使用fastjson序列化
FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
// value值的序列化采用fastJsonRedisSerializer
template.setValueSerializer(fastJsonRedisSerializer);
template.setHashValueSerializer(fastJsonRedisSerializer);
// key的序列化采用StringRedisSerializer
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package top.lrshuai.entity;

import java.io.Serializable;

public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;

private String username;
private int age;

public User() {
super();
}
public User(String username, int age) {
super();
this.username = username;
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return "User [username=" + username + ", age=" + age + "]";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#server.port=80

# redis db number default number is 0
spring:
redis:
timeout: 10000ms
sentinel:
nodes: 192.168.31.245:26379,192.168.31.245:26380,192.168.31.245:26381
master: rstyroMaster
password: rstyro6868
jedis:
pool:
max-active: 256
max-wait: 30000
max-idle: 64
min-idle: 32
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 哨兵redis,默认lettuce 连接池
spring:
redis:
lettuce:
pool:
max-idle: 8
min-idle: 0
max-active: 8
max-wait: -1ms
timeout: 10000ms
sentinel:
nodes: 192.168.31.245:26379,192.168.31.245:26380,192.168.31.245:26381
master: rstyroMaster
password: rstyro6868
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#server.port=80

# 单点 redis,默认lettuce 连接池
spring:
redis:
password:
database: 0
port: 6379
host: 127.0.0.1
lettuce:
pool:
max-idle: 8
min-idle: 0
max-active: 8
max-wait: -1ms
timeout: 10000ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#server.port=80

spring:
profiles:
active: jedis
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package top.lrshuai;

import com.alibaba.fastjson.JSON;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import top.lrshuai.entity.User;


@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRedisApplicationTests {


@Autowired
private RedisTemplate<String, Object> redisTemplate;


@Test
public void test() throws Exception {

// 保存对象
User user = new User("C++", 40);
redisTemplate.opsForValue().set(user.getUsername(), user);

user = new User("Java", 30);
redisTemplate.opsForValue().set(user.getUsername(), user);

user = new User("Python", 20);
redisTemplate.opsForValue().set(user.getUsername(), user);
System.out.println(JSON.toJSONString(redisTemplate.opsForValue().get("C++")));
System.out.println(JSON.toJSONString(redisTemplate.opsForValue().get("Java")));
System.out.println(JSON.toJSONString(redisTemplate.opsForValue().get("Python")));

}

}

0 comments on commit 036e40c

Please sign in to comment.