Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nouiWithSpringMVC
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
gechengyang
nouiWithSpringMVC
Commits
02d857a4
Commit
02d857a4
authored
Sep 25, 2020
by
tangzy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
改用redis的连接
parent
748970de
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
171 additions
and
30 deletions
+171
-30
RedisNode.java
src/main/java/org/sss/presentation/noui/redis/RedisNode.java
+76
-0
StatusTyp.java
src/main/java/org/sss/presentation/noui/redis/StatusTyp.java
+27
-0
RedisUtil.java
src/main/java/org/sss/presentation/noui/util/RedisUtil.java
+68
-30
No files found.
src/main/java/org/sss/presentation/noui/redis/RedisNode.java
0 → 100644
View file @
02d857a4
package
org
.
sss
.
presentation
.
noui
.
redis
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
java.io.Serializable
;
import
java.util.StringJoiner
;
public
class
RedisNode
implements
Serializable
{
private
String
host
;
private
String
master
;
private
Integer
status
;
private
Integer
order
;
private
RedisTemplate
<
String
,
Object
>
node
;
public
RedisNode
()
{
}
public
RedisNode
(
String
host
,
String
master
,
Integer
status
,
Integer
order
,
RedisTemplate
<
String
,
Object
>
node
)
{
this
.
host
=
host
;
this
.
master
=
master
;
this
.
status
=
status
;
this
.
order
=
order
;
this
.
node
=
node
;
}
public
String
getHost
()
{
return
host
;
}
public
void
setHost
(
String
host
)
{
this
.
host
=
host
;
}
public
String
getMaster
()
{
return
master
;
}
public
void
setMaster
(
String
master
)
{
this
.
master
=
master
;
}
public
Integer
getStatus
()
{
return
status
;
}
public
void
setStatus
(
Integer
status
)
{
this
.
status
=
status
;
}
public
Integer
getOrder
()
{
return
order
;
}
public
void
setOrder
(
Integer
order
)
{
this
.
order
=
order
;
}
public
RedisTemplate
<
String
,
Object
>
getNode
()
{
return
node
;
}
public
void
setNode
(
RedisTemplate
<
String
,
Object
>
node
)
{
this
.
node
=
node
;
}
@Override
public
String
toString
()
{
return
new
StringJoiner
(
", "
,
RedisNode
.
class
.
getSimpleName
()
+
"["
,
"]"
)
.
add
(
"host='"
+
host
+
"'"
)
.
add
(
"master='"
+
master
+
"'"
)
.
add
(
"status="
+
status
)
.
add
(
"order="
+
order
)
.
add
(
"node="
+
node
)
.
toString
();
}
}
src/main/java/org/sss/presentation/noui/redis/StatusTyp.java
0 → 100644
View file @
02d857a4
package
org
.
sss
.
presentation
.
noui
.
redis
;
public
enum
StatusTyp
{
/**
* 可用
*/
AVAILABLE
(
0
),
/**
* 不可用
*/
UNAVAILABLE
(
1
),
/**
* 待连接
*/
UNCONNECTED
(
2
);
private
Integer
code
;
StatusTyp
(
Integer
code
)
{
this
.
code
=
code
;
}
public
Integer
getCode
()
{
return
code
;
}
}
src/main/java/org/sss/presentation/noui/util/RedisUtil.java
View file @
02d857a4
...
...
@@ -2,12 +2,19 @@ package org.sss.presentation.noui.util;
import
log.Log
;
import
log.LogFactory
;
import
org.apache.commons.lang.StringUtils
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.util.ObjectUtils
;
import
org.sss.presentation.noui.redis.RedisBalanceProperties
;
import
org.sss.presentation.noui.redis.RedisNode
;
import
org.sss.presentation.noui.redis.StatusTyp
;
import
java.util.HashSet
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.TimeUnit
;
import
java.util.stream.Collectors
;
/**
* Redis连接池工具类
...
...
@@ -16,7 +23,9 @@ public class RedisUtil {
private
static
RedisTemplate
redisAPA
=
SpringContextHolder
.
getBean
(
"redisAPA"
);
private
static
RedisTemplate
redisAPB
=
SpringContextHolder
.
getBean
(
"redisAPB"
);
private
static
RedisTemplate
<
String
,
Object
>
masterNode
=
null
;
private
static
RedisBalanceProperties
redisProperties
=
SpringContextHolder
.
getBean
(
"redisProperties"
);
public
static
Map
<
String
,
RedisNode
>
redisNodes
=
new
ConcurrentHashMap
<>();
private
static
String
master
=
"redisAPA"
;
private
static
int
sessionTimeOut
=
1800
;
protected
static
final
Log
log
=
LogFactory
.
getLog
(
RedisUtil
.
class
);
...
...
@@ -24,6 +33,9 @@ public class RedisUtil {
static
{
sessionTimeOut
=
redisProperties
.
getSessionTimeOut
();
master
=
redisProperties
.
getMasterNode
();
redisNodes
.
put
(
"redisAPA"
,
new
RedisNode
(
redisProperties
.
getIp1
(),
master
,
StatusTyp
.
AVAILABLE
.
getCode
(),
0
,
redisAPA
));
redisNodes
.
put
(
"redisAPB"
,
new
RedisNode
(
redisProperties
.
getIp2
(),
null
,
StatusTyp
.
AVAILABLE
.
getCode
(),
1
,
redisAPB
));
masterNode
=
redisNodes
.
get
(
master
).
getNode
();
}
...
...
@@ -44,27 +56,31 @@ public class RedisUtil {
}
public
static
RedisTemplate
<
String
,
Object
>
getRedisConnection
()
{
if
(
master
.
equals
(
"redisAPA"
))
{
// log.info("getRedisMaster redisAPA1");
if
(
getRedisMaster
(
redisAPA
))
{
log
.
info
(
"getRedisMaster redisAPA"
);
setMaster
(
"redisAPA"
);
return
redisAPA
;
}
else
{
// log.info("getRedisMaster redisAPB");
setMaster
(
"redisAPB"
);
//设置当前节点不可用
RedisNode
node
=
redisNodes
.
get
(
master
);
node
.
setMaster
(
null
);
node
.
setStatus
(
StatusTyp
.
UNAVAILABLE
.
getCode
());
redisNodes
.
put
(
master
,
node
);
//查找当前所有的节点,选择可用的节点
for
(
Map
.
Entry
<
String
,
RedisNode
>
values
:
redisNodes
.
entrySet
())
{
RedisNode
redisNode
=
values
.
getValue
();
if
(
redisNode
.
getStatus
()
==
StatusTyp
.
AVAILABLE
.
getCode
())
{
setMaster
(
values
.
getKey
());
masterNode
=
redisNode
.
getNode
();
return
redisNode
.
getNode
();
}
}
if
(
master
.
equals
(
"redisAPB"
))
{
// log.info("getRedisMaster redisAPB1");
if
(
getRedisMaster
(
redisAPB
))
{
setMaster
(
"redisAPB"
);
log
.
info
(
"getRedisMaster redisAPB"
);
return
redisAPB
;
}
else
{
setMaster
(
"redisAPA"
);
// log.info("getRedisMaster redisAPA");
//当前所有的节点,都不可用的情况,尝试连接一次
for
(
Map
.
Entry
<
String
,
RedisNode
>
values
:
redisNodes
.
entrySet
())
{
RedisNode
redisNode
=
values
.
getValue
();
if
(
redisNode
.
getStatus
()
==
StatusTyp
.
UNAVAILABLE
.
getCode
())
{
if
(
getRedisMaster
(
redisNode
.
getNode
()))
{
setMaster
(
values
.
getKey
());
redisNode
.
setStatus
(
StatusTyp
.
AVAILABLE
.
getCode
());
masterNode
=
redisNode
.
getNode
();
return
redisNode
.
getNode
();
}
}
}
return
null
;
...
...
@@ -87,29 +103,50 @@ public class RedisUtil {
}
public
static
void
set
(
String
key
,
Object
value
)
throws
Exception
{
set
(
key
,
value
,
sessionTimeOut
);
set
(
key
,
value
,
sessionTimeOut
);
}
public
static
void
set
(
String
key
,
Object
value
,
int
sessionTimeOut
)
throws
Exception
{
public
static
void
set
(
String
key
,
Object
value
,
int
sessionTimeOut
)
throws
Exception
{
checkRedisNodeAvailable
();
getRedisConnection
().
opsForValue
().
set
(
key
,
value
);
getRedisConnection
().
expire
(
key
,
sessionTimeOut
,
TimeUnit
.
SECONDS
);
// checkRedisNodeAvailable();
try
{
masterNode
.
opsForValue
().
set
(
key
,
value
);
masterNode
.
expire
(
key
,
sessionTimeOut
,
TimeUnit
.
SECONDS
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
checkRedisNodeAvailable
();
}
}
public
static
Object
get
(
String
key
)
throws
Exception
{
checkRedisNodeAvailable
();
return
getRedisConnection
().
opsForValue
().
get
(
key
);
// checkRedisNodeAvailable();
// Object values=null;
try
{
return
masterNode
.
opsForValue
().
get
(
key
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
public
static
Boolean
delete
(
String
key
)
throws
Exception
{
checkRedisNodeAvailable
();
//
checkRedisNodeAvailable();
return
getRedisConnection
().
delete
(
key
);
try
{
return
masterNode
.
delete
(
key
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
false
;
}
public
static
Set
keys
(
String
key
)
throws
Exception
{
checkRedisNodeAvailable
();
return
getRedisConnection
().
keys
(
key
+
"*"
);
// checkRedisNodeAvailable();
try
{
return
masterNode
.
keys
(
key
+
"*"
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
new
HashSet
();
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment