1. 程式人生 > 實用技巧 >Gym100810J Journey to the "The World's Start" (二分答案+動態規劃)

Gym100810J Journey to the "The World's Start" (二分答案+動態規劃)

單機連線
  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的序列化策略    存入資料庫的是不可讀的例如"\xac\xed\x00\x05t\x00\x03key"也只能使用 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);
          }
      }