redis配置文件_redis怎么连接

redis配置文件_redis怎么连接dd#redis配置开始#Redis数据库索引(默认为0)spring.redis.database=0#Redis服务器地址#redis.host=192.168.59.43redis.host1=192.168.58.11redis.host2=192.168.58.12redis.host3=192.168.58.13#Redis服务器连接端口redis.port=6379redis.master.port=6379redis.slave.port=6380#Re.

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺


1. 引入依赖

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

2. 参数配置

# Redis集群服务器地址
redis.host1=192.168.000.11
redis.host2=192.168.000.12
redis.host3=192.168.000.13
# Redis服务器连接端口
redis.master.port=6379
redis.slave.port=6380
# Redis服务器连接密码(默认为空)
redis.password=xxxx
# 连接超时时间
redis.connection-timeout=2000
# 读取数据超时时间
redis.so-timeout=2000
# 连接超时或读取超时进行重试的次数
redis.max-attempts=3
# 开启对象验证,保证可用
redis.testOnBorrow=true

3. 代码实现

import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

/** * Redis Cluster 配置 * * @author wangbo * @date 2021/6/15 */
@Slf4j
public class JedisClusterManager { 
   

    private JedisClusterManager() { 
   
    }

    private static final JedisCluster JEDIS_CLUSTER;

    static { 
   
        Properties props = new Properties();
        try { 
   
            props.load(JedisClusterManager.class.getResourceAsStream(PropertiesConstants.PROPERTIES_FILE_REDIS));
        } catch (IOException e) { 
   
            log.error("load redis config properties exception", e);
        }

        String redisHost1 = props.getProperty("redis.host1");
        String redisHost2 = props.getProperty("redis.host2");
        String redisHost3 = props.getProperty("redis.host3");
        int masterPort = Integer.parseInt(props.getProperty("redis.master.port"));
        int slavePort = Integer.parseInt(props.getProperty("redis.slave.port"));

        Set<HostAndPort> nodes = new HashSet<>();
        nodes.add(new HostAndPort(redisHost1, masterPort));
        nodes.add(new HostAndPort(redisHost2, masterPort));
        nodes.add(new HostAndPort(redisHost3, masterPort));
        nodes.add(new HostAndPort(redisHost1, slavePort));
        nodes.add(new HostAndPort(redisHost2, slavePort));
        nodes.add(new HostAndPort(redisHost3, slavePort));

        String password = props.getProperty("redis.password");
        int connectionTimeout = Integer.parseInt(props.getProperty("redis.connection-timeout"));
        int soTimeout = Integer.parseInt(props.getProperty("redis.so-timeout"));
        int maxAttempts = Integer.parseInt(props.getProperty("redis.max-attempts"));

        boolean testOnBorrow = Boolean.parseBoolean(props.getProperty("redis.testOnBorrow"));

        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setTestOnBorrow(testOnBorrow);

        JEDIS_CLUSTER = new JedisCluster(nodes, connectionTimeout, soTimeout, maxAttempts, password, jedisPoolConfig);
    }

    /** * 获取JedisCluster对象 */
    public static JedisCluster getJedis() { 
   
        return JEDIS_CLUSTER;
    }

}

然后每次使用的时候直接在程序中使用如下代码获取 JedisCluster 对象即可使用 Jedis 提供的各种操作 Redis 的方法:

JedisCluster jedisCluster = JedisClusterManager.getJedis();
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/195460.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Vue项目关闭eslint校验「建议收藏」

    Vue项目关闭eslint校验「建议收藏」1.vue-cli2.0关闭eslint校验vue-cli2.0实现2.vue-cli3.0关闭eslint校验报错:eslint-disable-next-linetoignorethenextline.解决方法:找到文件vue.config.js,打开文件:修改lintOnSave为false,如果没有就添加lintOnSave为false…

    2022年5月15日
    46
  • 旋转编码器原理「建议收藏」

    旋转编码器原理「建议收藏」文章整理于网络:一、简介旋转变压器(resolver)是一种电磁式传感器,又称同步分解器。它是一种测量角度用的小型交流电动机,用来测量旋转物体的转轴角位移和角速度,由定子和转子组成。其中定子绕组作为变压器的原边,接受励磁电压,励磁频率通常用400、3000及5000HZ等。转子绕…

    2022年10月1日
    0
  • (已解决)Unexpected token o in JSON at position 1

    (已解决)Unexpected token o in JSON at position 1讲这个问题之前先普及一下JSON.parse()和JSON.stringify()方面的知识:JSON.parse()方法用于将一个JSON字符串转换为对象,如varstr='{"name":"LeonWu","age":"18"}’JSON.parse(str);//结果为一个Object//age:"18";//name:"LeonWu";

    2022年10月17日
    0
  • python爬虫–验证码、cookie

    python爬虫–验证码、cookie

    2021年4月16日
    185
  • postMessage的使用

    postMessage的使用postMessage是H5的API,用来解决跨页面通信的。postMessage的使用分为发送方和接收方。发送方的代码用法如下:otherWindow.postMessage(message,targetOrigin,[transfer]);otherWindow是接收方的window对象。可以通过以下几种方法获得,例如window.open()方法返回的值就是打开页面的window对象,或…

    2022年7月13日
    33
  • fork join原理_典型相关分析

    fork join原理_典型相关分析privateForkJoinPool(intparallelism,ForkJoinWorkerThreadFactoryfactory,UncaughtExceptionHandlerhandler,intmode,StringworkerNamePrefix)fo…

    2022年9月20日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号