pom.xml配置
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
|
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version> 1.0 . 2 .RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version> 2.7 . 0 </version> <type>jar</type> <scope>compile</scope> </dependency> ? 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 public class JedisPoolUtil { private static JedisSentinelPool pool = null ; public static Properties getJedisProperties() { Properties config = new Properties(); InputStream is = null ; try { is = JedisPoolUtil. class .getClassLoader().getResourceAsStream( "cacheConfig.properties" ); config.load(is); } catch (IOException e) { logger.error( "" , e); } finally { if (is != null ) { try { is.close(); } catch (IOException e) { logger.error( "" , e); } } } return config; } /** * 創建連接池 * */ private static void createJedisPool() { // 建立連接池配置參數 JedisPoolConfig config = new JedisPoolConfig(); Properties prop = getJedisProperties(); // 設置最大連接數 config.setMaxTotal(StringUtil.nullToInteger(prop.getProperty( "MAX_ACTIVE" ))); // 設置最大阻塞時間,記住是毫秒數milliseconds config.setMaxWaitMillis(StringUtil.nullToInteger(prop.getProperty( "MAX_WAIT" ))); // 設置空間連接 config.setMaxIdle(StringUtil.nullToInteger(prop.getProperty( "MAX_IDLE" ))); // jedis實例是否可用 boolean borrow = prop.getProperty( "TEST_ON_BORROW" ) == "false" ? false : true ; config.setTestOnBorrow(borrow); // 創建連接池 // pool = new JedisPool(config, prop.getProperty("ADDR"), StringUtil.nullToInteger(prop.getProperty("PORT")), StringUtil.nullToInteger(prop.getProperty("TIMEOUT")));// 線程數量限制,IP地址,端口,超時時間 //獲取redis密碼 String password = StringUtil.nullToString(prop.getProperty( "PASSWORD" )); String masterName = "mymaster" ; Set<String> sentinels = new HashSet<String>(); sentinels.add( "192.168.137.128:26379" ); sentinels.add( "192.168.137.128:26380" ); sentinels.add( "192.168.137.128:26381" ); pool = new JedisSentinelPool(masterName, sentinels, config); } /** * 在多線程環境同步初始化 */ private static synchronized void poolInit() { if (pool == null ) createJedisPool(); } /** * 獲取一個jedis 對象 * * @return */ public static Jedis getJedis() { if (pool == null ) poolInit(); return pool.getResource(); } /** * 釋放一個連接 * * @param jedis */ public static void returnRes(Jedis jedis) { pool.returnResource(jedis); } /** * 銷毀一個連接 * * @param jedis */ public static void returnBrokenRes(Jedis jedis) { pool.returnBrokenResource(jedis); } public static void main(String[] args){ Jedis jedis=getJedis(); } } |
以上這篇java客戶端Jedis操作Redis Sentinel 連接池的實現方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。