복붙노트

[REDIS] 센티넬로 Jedis 예 구함 [폐쇄]

REDIS

센티넬로 Jedis 예 구함 [폐쇄]

나는 레디 스의 감시를 사용 jedis의 예를 찾고 있어요. 나는 감시에 대한 jedis 시설을 사용할 수없는 나는, 나는 어떤 좋은 예제 나 문서를 찾을 수 없습니다.

해결법

  1. ==============================

    1.Jedis 라이브러리는 불행히도 나쁜 설명서와 함께, 멋진 솔루션입니다.

    Jedis 라이브러리는 불행히도 나쁜 설명서와 함께, 멋진 솔루션입니다.

    그래서,

    @Autowired private JedisSentinelPool pool;
    
    public void mymethod() {
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            jedis.hset(....
        } catch (JedisException je) {
            throw je;
        } finally {
            if (jedis != null) pool.returnResource(jedis);
        }
    }
    

    나는 봄을 사용하고, 나는이 필요합니다

    <bean id="redisSentinel" class="redis.clients.jedis.JedisSentinelPool">
    <constructor-arg index="0" value="mymaster" />
    <constructor-arg index="1">
         <set>  
             <value>hostofsentinel:26379</value>  
        </set> 
    </constructor-arg>
    <constructor-arg index="2" ref="jedisPoolConfig"/>
    </bean>
    
  2. ==============================

    2.당신이 봄을 사용하고 레디 스의 센티넬 관리하는 레디 스 마스터 / 슬레이브 설정에 Jedis를 통해 간단한 연결이 필요하지 않을 때 다음 예입니다

    당신이 봄을 사용하고 레디 스의 센티넬 관리하는 레디 스 마스터 / 슬레이브 설정에 Jedis를 통해 간단한 연결이 필요하지 않을 때 다음 예입니다

    public class JedisTestSentinelEndpoint {
        private static final String MASTER_NAME = "mymaster";
        public static final String PASSWORD = "foobared";
        private static final Set sentinels;
        static {
            sentinels = new HashSet();
            sentinels.add("mymaster-0.servers.example.com:26379");
            sentinels.add("mymaster-1.servers.example.com:26379");
            sentinels.add("mymaster-2.servers.example.com:26379");
        }
    
        public JedisTestSentinelEndpoint() {
        }
    
        private void runTest() throws InterruptedException {
            JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels);
            Jedis jedis = null;
                try {
                    printer("Fetching connection from pool");
                    jedis = pool.getResource();
                    printer("Authenticating...");
                    jedis.auth(PASSWORD);
                    printer("auth complete...");
                    Socket socket = jedis.getClient().getSocket();
                    printer("Connected to " + socket.getRemoteSocketAddress());
                    printer("Writing...");
                    jedis.set("java-key-999", "java-value-999");
                    printer("Reading...");
                    jedis.get("java-key-999");
                } catch (JedisException e) {
                    printer("Connection error of some sort!");
                    printer(e.getMessage());
                    Thread.sleep(2 * 1000);
                } finally {
                    if (jedis != null) {
                        jedis.close();
                    }
                }
        }
    ...
    }
    

    출처 : 레디 스 센티넬 연결에이 블로그 게시물.

  3. ==============================

    3.당신은 Redisson를 시도? 그것은 모든 Redisson에 의해 이루어집니다 ... 그것은 감시 자동 마스터 / 슬레이브 / 감시 발견 및 토폴로지 업데이 트를 제공하고 당신은 데이터 인코딩, 연결을 처리 할 필요가 없습니다. 다음 코드 예제는 다음과 같습니다

    당신은 Redisson를 시도? 그것은 모든 Redisson에 의해 이루어집니다 ... 그것은 감시 자동 마스터 / 슬레이브 / 감시 발견 및 토폴로지 업데이 트를 제공하고 당신은 데이터 인코딩, 연결을 처리 할 필요가 없습니다. 다음 코드 예제는 다음과 같습니다

    Config config = new Config();
    config.useSentinelServers()
       .setMasterName("mymaster")
       .addSentinelAddress("127.0.0.1:26389", "127.0.0.1:26379")
    
    RedissonClient redisson = Redisson.create(config);
    
    RMap<MyKey, MyValue> map = redisson.getMap("myMap");
    map.put(new MyKey(), new MyValue());
    
  4. from https://stackoverflow.com/questions/19382065/looking-for-an-example-of-jedis-with-sentinel by cc-by-sa and MIT license