java连接redis

单机连接
  1. pom文件

     <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>
    
  2. java代码

        // 连接池配置文件
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(20);
        jedisPoolConfig.setMaxIdle(10);
        jedisPoolConfig.setMinIdle(5);
        // 创建连接池 ,配置文件,ip,端口,超时,密码
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, "192.168.150.100", 6379, 3000, null);
        Jedis jedis = null;
        try {
            //获取连接
            jedis = jedisPool.getResource();
            System.out.println(jedis.set("jamin", "666"));
            System.out.println(jedis.get("jamin"));
        } catch (Exception e) {
            e.printStackTrace();
    
        }
    
哨兵架构连接
  1. 启动一主二从,三个哨兵[谨记最好一次成功,至少哨兵总数要对,不然很麻烦,可尝试删除id]
  2. java代码(用来延时故障转移,程序启动,kill 6379的进程 切记sentinel的监听master的ip为局域网ip 127.0.0.1是连接不上的)
     //连接池配置
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxTotal(2000);
    jedisPoolConfig.setMaxIdle(1000);
    jedisPoolConfig.setMinIdle(5);
    //master名称
    String masterName = "mymaster";
    //哨兵
    HashSet<String> hashSet = new HashSet<>();
    hashSet.add(new HostAndPort("192.168.150.100", 26379).toString());
    hashSet.add(new HostAndPort("192.168.150.100", 26380).toString());
    hashSet.add(new HostAndPort("192.168.150.100", 26381).toString());
    //创建连接池
    JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(masterName, hashSet, jedisPoolConfig, 3000, null);
    Jedis jedis = null;
    int i = 1;
    while (true) {
        try {
            jedis = jedisSentinelPool.getResource();
            jedis.set("sentinel" + i, "sentinel" + i);
            System.out.println("sentinel" + i);
            i++;
            Thread.sleep(3000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    }
    
    1. 在哨兵的日志中可以看到判断下线以及投票,设置新的master的一个过程,java客户端当6379被干掉持续请求,但是连接被拒绝,直到新的master被分配处理请求新的master
集群架构
```java
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMinIdle(5);
// 最大空闲数量
jedisPoolConfig.setMaxIdle(10);
jedisPoolConfig.setMaxTotal(20);
//创建集群节点
HashSet<HostAndPort> jedisClusterNode = new HashSet<>();
jedisClusterNode.add(new HostAndPort("192.168.150.101", 8001));
jedisClusterNode.add(new HostAndPort("192.168.150.102", 8002));
jedisClusterNode.add(new HostAndPort("192.168.150.103", 8003));
jedisClusterNode.add(new HostAndPort("192.168.150.101", 8004));
jedisClusterNode.add(new HostAndPort("192.168.150.102", 8005));
jedisClusterNode.add(new HostAndPort("192.168.150.103", 8006));
JedisCluster cluster = null;
try {
    // 第一个5000   连接超时时间 第二个5000 等待返回超时时间 10 最大尝试连接次数   jamin密码
    cluster = new JedisCluster(jedisClusterNode, 5000, 5000, 10, "jamin", jedisPoolConfig);
    System.out.println(cluster.set("test11", "111"));
    System.out.println(cluster.get("test11"));
} catch (Exception e) {
    e.printStackTrace();
}

}
```
使用springboot进行连接redis
  1. 连接单机
    1. pom.xml
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-redis</artifactId>
      </dependency>
      
    2. 配置文件
    spring:
      redis:
        #单机
        #    host: 192.168.150.100
        #    port: 6380
        #    哨兵
        #    sentinel:
        #      master: mymaster
        #      nodes: 192.168.150.100:26379,192.168.150.100:26380,192.168.150.100:26381
        #    集群
        cluster:
          nodes: 192.168.150.101:8001,192.168.150.101:8002,192.168.150.102:8003,192.168.150.102:8004,192.168.150.103:8005,192.168.150.103:8006
        #      密码
        password: jamin
    
    1. java代码
      package cn.jaminye.springbootredissentinel.test;
      
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.boot.test.context.SpringBootTest;
      import org.springframework.data.redis.core.RedisTemplate;
      import org.springframework.data.redis.core.StringRedisTemplate;
      
      /**
       * @author Jamin
       * @date 2020/8/1 12:21
       * 测试springboot连接redis
       */
      @SpringBootTest
      public class Test {
          @Autowired
          RedisTemplate redisTemplate;
          @Autowired
          StringRedisTemplate stringRedisTemplate;
      
          @org.junit.jupiter.api.Test
          public void test() {
              //set值    redisTemplate使用的是jdk的序列化策略    存入数据库的是不可读的例如"xacxedx00x05tx00x03key"也只能使用 redisTemplate取出
              redisTemplate.opsForValue().set("key", "value");
              // stringRedisTemplate使用的是String的redis序列化策略  是易读的 存入数据库的是可读 也只能使用 stringRedisTemplate取出
              stringRedisTemplate.opsForValue().set("key", "value");
              //get值
              String value = String.valueOf(redisTemplate.opsForValue().get("key"));
              String values = stringRedisTemplate.opsForValue().get("key");
              System.out.println(value);
              System.out.println(values);
          }
      }
      
作者: JaminYe
版权声明:本文原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文地址:https://www.cnblogs.com/JaminYe/p/13414297.html