Redis哨兵配置与jedis客户端使用

文章目录

    • Redis client
    • Installation
    • Redis sentinel
      • 1、配置主从
      • 2、哨兵配置
      • 3、Java连接(Jedis客户端)
    • springboot+jedis配置
      • pom依赖
      • 配置类(配置了哨兵模式,单机模式,配置文件配置了哨兵优先使用哨兵模式)
      • 配置文件 application.yml
      • 自定义Jedis执行器
      • JedisService与其实现类

Redis client

官方推文:QuickRedis 是一款 永久免费 开源 的 Redis 可视化管理工具。它支持直连、哨兵、集群模式,支持亿万数量级的 key,还有令人兴奋的 UI。QuickRedis 支持 Windows 、 Mac OS X 和 Linux 下运行。
QuickRedis 是一个效率工具,当别人在努力敲命令的时候,而你已经在喝茶。

下载地址:

Gitee :https://gitee.com/quick123official/quick_redis_blog/

Github:https://github.com/quick123official/quick_redis_blog/

Installation

$ wget https://download.redis.io/releases/redis-6.0.9.tar.gz
$ tar xzf redis-6.0.9.tar.gz
$ cd redis-6.0.9
$ make

Redis sentinel

1、配置主从

修改配置文件redis.conf

#允许所有网络访问
bind 0.0.0.0
#认证密码
requirepass redis123
#主节点忽略 从节点配置,指定主节点地址端口
slaveof 192.168.1.200 6379
#主节点忽略 从节点配置,主服务密码
masterauth redis123
#是否开启保护模式
protected-mode yes
port 6380

2、哨兵配置

修改配置文件sentinel.conf

哨兵的配置文件都是一致的(如果只有一台机器进行模拟的话,只需要改变端口号、pidfile的文件名也是可以的)

#禁止保护模式
protected-mode no
#端口
port 26379
#进程守护
daemonize no
pidfile "/var/run/redis-sentinel26379.pid"
#此处为Redis主从的主节点地址,(即使哨兵和主节点在同一台机器上,此处主节点地址也最好不要配置localhost或127.0.0.1,
#否则使用哨兵远程连接会找不到主节点的,因为哨兵的配置文件是localhost或127.0.0.1,会找本地)
sentinel monitor mymaster 192.168.1.200 6379 2
# 哨兵自己的密码 需要认证
requirepass s123
# redis密码
sentinel auth-pass mymaster redis123

启动命令:redis主目录路径下

##先起主从,命令
$ ./src/redis-server redis6379.conf
##哨兵启动
$ ./src/redis-sentinel sentinel26379.conf 

3、Java连接(Jedis客户端)

  • 引入pom依赖
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.3.0</version>
</dependency>
  • Java代码
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;

import java.util.HashSet;
import java.util.Set;

public class SentinelTest {
     
    public static void main(String[] args) {
     
        Set<String> sentinels = new HashSet<String>(16);
        sentinels.add("192.168.1.200:26379");
        sentinels.add("192.168.1.200:26380");
        sentinels.add("192.168.1.200:26381");
        JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(
                "mymaster", sentinels, "redis123", "s123");
        Jedis resource = jedisSentinelPool.getResource();
        String key = "test";
        resource.set(key, "hello sentinel");
        System.out.println(resource.get(key));
        resource.close();
    }
}

springboot+jedis配置

java版本:jdk8

pom依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.4.0</version>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.3.0</version>
</dependency>

配置类(配置了哨兵模式,单机模式,配置文件配置了哨兵优先使用哨兵模式)

@Configuration
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig {
     

    @Autowired
    private RedisProperties redisProperties;

    @Bean
    public JedisPoolAbstract jedisPool() {
     
        if (redisProperties.getSentinel() != null) {
     
            List<String> nodes = redisProperties.getSentinel().getNodes();
            Set<String> nodeSets = new HashSet<>();
            for (String node : nodes) {
     
                nodeSets.add(node);
            }
            JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(
                    redisProperties.getSentinel().getMaster()
                    , nodeSets
                    , redisProperties.getPassword()
                    , redisProperties.getSentinel().getPassword()
            );
            return jedisSentinelPool;
        } else {
     
            return new JedisPool(getJedisPoolConfig(), redisProperties.getHost(), redisProperties.getPort(),
                    (int) redisProperties.getTimeout().toMillis(), redisProperties.getPassword());
        }
    }


    /**
     * jedisPoolConfig
     *
     * @return
     */
    private JedisPoolConfig getJedisPoolConfig() {
     
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        RedisProperties.Pool pool = redisProperties.getJedis().getPool();
        if (pool != null) {
     
            jedisPoolConfig.setMaxIdle(pool.getMaxIdle());
            jedisPoolConfig.setMaxWaitMillis(pool.getMaxWait().toMillis());
            //其他配置根据需要配置
        }
        return jedisPoolConfig;
    }
}

配置文件 application.yml

server:
  port: 9004
spring:
  redis:
    ## 单机配置 ## 没密码不要写 password
    host: localhost
    port: 6379
    timeout: 10000
    ## 哨兵模式
#    sentinel:
#      master: mymaster
#      nodes:
#        - 192.168.1.200:26379
#        - 192.168.1.200:26380
#        - 192.168.1.200:26381
#      password: s123
#    password: redis123
    jedis:
      pool:
        max-idle: 8
        max-wait: 10000

自定义Jedis执行器

@FunctionalInterface
public interface JedisExecutor<T, R> {
     

    /**
     * jedis执行器
     *
     * @param t
     * @return
     * @throws JedisConnectionException
     */
    R excute(T t) throws JedisConnectionException;
}

JedisService与其实现类

public interface JedisService {
     
    /**
     * set操作
     *
     * @param key
     * @param value
     * @return
     */
    String set(String key, String value);

    /**
     * get操作
     *
     * @param key
     * @return
     */
    String get(String key);
}
@Service
public class JedisServiceImpl implements JedisService {
     

    @Autowired
    private JedisPoolAbstract jedisPool;

    /**
     * jedis执行器
     * 

* 带资源的try语句(try-with-resource)的最简形式为: * try(Resource res = xxx) //可指定多个资源 * { * work with res * } * try块退出时,会自动调用res.close()方法,关闭资源。 * * @param executor * @param * @return * @throws JedisConnectionException */ private <T> T excuteByJedis(JedisExecutor<Jedis, T> executor) throws JedisConnectionException { try (Jedis jedis = jedisPool.getResource()) { return executor.excute(jedis); } catch (Exception e) { throw new JedisConnectionException(e.getMessage()); } } @Override public String set(String key, String value) { return excuteByJedis(jedis -> jedis.set(key, value)); } @Override public String get(String key) { return excuteByJedis(jedis -> jedis.get(key)); } }

源码地址:https://github.com/joyfulStalker/helper/tree/dev/helper-java/helper-jedis

你可能感兴趣的:(java相关,redis,jedis)