Redis sentinel java example
Redis Sentinel – HAProxy을 이용한 Client 통신에 대한 java example입니다.
Master(5000 port) 는 set(write) 용도
Slave(5001 port)는 get(read) 용도
JedisSentinalTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.time.Duration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class JedisSentinalTest { private static final Logger log = LoggerFactory.getLogger(JedisSentinalTest.class); private static JedisPoolConfig jedisPoolConfig = buildPoolConfig(); private static JedisPool jedisMasterPool; private static Jedis jedisMasterClient; private static JedisPool jedisSlavePool; private static Jedis jedisSlaveClient; public static boolean isMasterConnectSuccess = false; public static boolean isSlaveConnectSuccess = false; private static String redisHost = "lcoalhost"; private static int redisMasterPort = 5000; private static int redisSlavePort = 5001; private static int redisDefaultExpireTime = 60; private static JedisPoolConfig buildPoolConfig() { final JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMaxTotal(4096); poolConfig.setMaxIdle(500); poolConfig.setMinIdle(200); poolConfig.setTestOnBorrow(true); poolConfig.setTestOnReturn(true); poolConfig.setTestWhileIdle(true); poolConfig.setMinEvictableIdleTimeMillis(Duration.ofSeconds(60).toMillis()); poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofSeconds(30).toMillis()); poolConfig.setNumTestsPerEvictionRun(3); poolConfig.setBlockWhenExhausted(true); return poolConfig; } public static void getMasterConnection() { log.info("■ RedisUtil getConnection start..."); try { /** * jedisPool */ jedisMasterPool = new JedisPool(jedisPoolConfig, redisHost, redisMasterPort, 60000, "password"); jedisMasterClient = jedisMasterPool.getResource(); log.info("■ RedisClientPool created..."); if (jedisMasterPool != null) { isMasterConnectSuccess = true; } } catch (Exception e) { log.error("■ 에러 : " + e.getMessage(), e); isMasterConnectSuccess = false; } } public static void getSlaveConnection() { log.info("■ RedisUtil getConnection start..."); try { /** * jedisPool */ jedisSlavePool = new JedisPool(jedisPoolConfig, redisHost, redisSlavePort, 60000, "password"); jedisSlaveClient = jedisSlavePool.getResource(); log.info("■ RedisClientPool created..."); if (jedisSlavePool != null) { isSlaveConnectSuccess = true; } } catch (Exception e) { log.error("■ 에러 : " + e.getMessage(), e); isSlaveConnectSuccess = false; } } /** * Master 연결종료 * * @throws Exception */ public static void closeMasterConnection() { log.info("■ RedisClientPool closeConnection start..."); if (jedisMasterPool == null) { log.error("■ RedisClientPool is null."); return; } try { jedisMasterClient.close(); jedisMasterPool.destroy(); log.error("■ RedisMasterClientPool closed..."); } catch (Exception e) { log.error("■ 에러 : " + e.getMessage(), e); } } /** * Slave 연결종료 * * @throws Exception */ public static void closeSlaveConnection() { log.info("■ RedisClientPool closeConnection start..."); if (jedisSlavePool == null) { log.error("■ RedisClientPool is null."); return; } try { jedisSlaveClient.close(); jedisSlavePool.destroy(); log.error("■ RedisSlaveClientPool closed..."); } catch (Exception e) { log.error("■ 에러 : " + e.getMessage(), e); } } public static boolean setKeyValue(String key, Object obj, int expireTime) { return setKeyValue(key, obj, expireTime, 0); } /** * Key-Value Item 저장 */ public static boolean setKeyValue(String key, Object obj, int expireTime, int selectDb) { boolean result = true; if (obj == null) { if (log.isDebugEnabled()) { log.debug("■■ RedisCache set value is null."); } return false; } if (!isMasterConnectSuccess) { return false; } if (expireTime == 0) { expireTime = redisDefaultExpireTime; } String statusCodeReply = null; try { byte[] svalue = serialize(obj); jedisMasterClient.select(selectDb); statusCodeReply = jedisMasterClient.setex(key.getBytes(), expireTime, svalue); if (statusCodeReply == null) { if (log.isDebugEnabled()) { log.debug("■ RedisCache set fail."); } return false; } if (log.isDebugEnabled()) { log.debug("■ RedisCache set success."); } } catch (Exception e) { log.error("■ Error : " + e.getMessage(), e); return false; } return result; } /** * Key-Value Item 조회 */ public static Object getKeyValue(String key) { return getKeyValue(key, 0); } /** * Key-Value Item 조회 */ public static Object getKeyValue(String key, int selectDb) { Object result = null; if (!isSlaveConnectSuccess) { if (log.isDebugEnabled()) { log.debug("■ RedisCache Connection value is false."); } return null; } try { jedisSlaveClient.select(selectDb); byte [] tmp = jedisSlaveClient.get(key.getBytes()); if (tmp == null) { if (log.isDebugEnabled()) { log.debug("■ RedisCache get key[ " + key + " ] value is null"); } return null; } result = deserialize(tmp); if (result == null) { if (log.isDebugEnabled()) { log.debug("■ RedisCache get key[ " + key + " ] value is null"); } return null; } if (log.isDebugEnabled()) { log.debug("■ RedisCache Result : " + result); } } catch (Exception e) { log.error("■ Error : " + e.getMessage(), e); return null; } return result; } public static byte[] serialize(Object value) { if (value == null) { throw new NullPointerException("Can't serialize null"); } byte[] rv = null; ByteArrayOutputStream bos = null; ObjectOutputStream os = null; try { bos = new ByteArrayOutputStream(); os = new ObjectOutputStream(bos); os.writeObject(value); rv = bos.toByteArray(); } catch (Exception e) { throw new RuntimeException("Object can't be serialized", e); } finally { close(os); close(bos); } return rv; } public static Object deserialize(byte[] in) { return deserialize(in, new Object()); } @SuppressWarnings("unchecked") public static <T> T deserialize(byte[] in, Object requiredType) { Object rv = null; ByteArrayInputStream bis = null; ObjectInputStream is = null; try { if (in != null) { bis = new ByteArrayInputStream(in); is = new ObjectInputStream(bis); rv = is.readObject(); } } catch (Exception e) { throw new RuntimeException("Object can't be deserialized", e); } finally { close(is); close(bis); } return (T) rv; } private static void close(Closeable closeable) { if (closeable != null) try { closeable.close(); } catch (IOException e) { throw new RuntimeException("close stream error"); } } public static void main(String[] args) { getMasterConnection(); //Redis Sentinal Master(5000 port) Connection getSlaveConnection(); //Redis Sentinal Salve(5001 port) Connection String key = "testKey"; setKeyValue(key, "testValue", 60); log.info("getKeyValue : " + getKeyValue(key)); } } |