Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
aserver
Project
Project
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
15所TongWeb
aserver
Commits
a681395e
Commit
a681395e
authored
Mar 04, 2021
by
李德才
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加Redis哨兵集群的配置,自动切换连接方式
parent
3c8967e3
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
230 additions
and
10 deletions
+230
-10
MainConfig.java
...in/java/com/archser/aserver/common/config/MainConfig.java
+6
-9
RedisConfig.java
...om/archser/aserver/common/config/plugins/RedisConfig.java
+47
-0
ICache.java
src/main/java/com/archser/aserver/plugin/redis/ICache.java
+46
-0
RedisSentinelPlugin.java
...com/archser/aserver/plugin/redis/RedisSentinelPlugin.java
+118
-0
AppConfig-pro.properties
src/main/resources/AppConfig-pro.properties
+13
-1
No files found.
src/main/java/com/archser/aserver/common/config/MainConfig.java
View file @
a681395e
package
com
.
archser
.
aserver
.
common
.
config
;
import
com.alibaba.druid.filter.stat.StatFilter
;
import
com.archser.aserver.common.config.plugins.RedisConfig
;
import
com.archser.aserver.controller.*
;
import
com.archser.aserver.interceptor.JwtInterceptor
;
import
com.archser.aserver.itask.GoodSync
;
...
...
@@ -19,7 +20,6 @@ import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import
com.jfinal.plugin.activerecord.dialect.*
;
import
com.jfinal.plugin.cron4j.Cron4jPlugin
;
import
com.jfinal.plugin.druid.DruidPlugin
;
import
com.jfinal.plugin.redis.RedisPlugin
;
import
com.jfinal.render.ViewType
;
import
com.jfinal.server.undertow.UndertowServer
;
import
com.jfinal.server.undertow.WebBuilder
;
...
...
@@ -109,7 +109,8 @@ public class MainConfig extends JFinalConfig {
p
=
PropKit
.
use
(
"AppConfig.properties"
).
appendIfExists
(
"AppConfig-pro.properties"
);
ClassLoader
path
=
Thread
.
currentThread
().
getContextClassLoader
();
try
{
String
resources
=
path
.
getResource
(
"AppConfig.properties"
).
toURI
().
getPath
().
replace
(
"AppConfig.properties"
,
""
);
String
resources
=
path
.
getResource
(
"AppConfig.properties"
).
toURI
().
getPath
()
.
replace
(
"AppConfig.properties"
,
""
);
p
.
getProperties
().
put
(
"resources"
,
resources
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
...
...
@@ -119,9 +120,9 @@ public class MainConfig extends JFinalConfig {
}
/**
* @return void
* @Description: 确认中间件类型获取打包后的resource
* @authorAndDate: ChengYaqing create on 2021/1/14 9:23
* @return void
*/
public
static
void
ConfirmMiddlewareType
(
Prop
p
)
{
String
basePath
;
...
...
@@ -171,10 +172,8 @@ public class MainConfig extends JFinalConfig {
me
.
add
(
dbPlugin
);
me
.
add
(
arp
);
// 添加Redis 配置
RedisPlugin
redis
=
new
RedisPlugin
(
"redis"
,
PropKit
.
get
(
"redis.url"
),
PropKit
.
getInt
(
"redis.port"
,
6379
),
PropKit
.
get
(
"redis.password"
));
me
.
add
(
redis
);
// 配置Redis信息,自动判断单实例和集群并加入JFinal插件列表
new
RedisConfig
(
me
,
p
);
// 添加定时任务
me
.
add
(
new
Cron4jPlugin
(
PropKit
.
append
(
"task.properties"
)));
...
...
@@ -242,7 +241,6 @@ public class MainConfig extends JFinalConfig {
/**
* 添加websocket
*
* @param builder
*/
public
static
void
addWebSocket
(
WebBuilder
builder
)
{
...
...
@@ -253,7 +251,6 @@ public class MainConfig extends JFinalConfig {
/**
* 添加消息中心的websocket
*
* @param builder
*/
public
static
void
addMessageWebSocket
(
WebBuilder
builder
)
{
...
...
src/main/java/com/archser/aserver/common/config/plugins/RedisConfig.java
0 → 100644
View file @
a681395e
package
com
.
archser
.
aserver
.
common
.
config
.
plugins
;
import
com.archser.aserver.plugin.redis.RedisSentinelPlugin
;
import
com.jfinal.config.Plugins
;
import
com.jfinal.kit.Prop
;
import
com.jfinal.plugin.redis.RedisPlugin
;
import
java.util.HashSet
;
import
java.util.Set
;
import
redis.clients.jedis.HostAndPort
;
/**
* TODO 自定义Redis配置,提取逻辑判断,避免在主类中掺杂大量代码
* @author 李德才
* @version V2.0
* @date 2021/3/4 9:02
*/
public
class
RedisConfig
{
public
RedisConfig
(
Plugins
plugins
,
Prop
prop
)
{
boolean
openRedisSentinel
=
prop
.
getBoolean
(
"openRedisSentinel"
);
// 哨兵配置
if
(
openRedisSentinel
)
{
String
redisSentinel
=
prop
.
get
(
"redisSentinel.url"
);
Set
<
HostAndPort
>
sentinels
=
new
HashSet
<
HostAndPort
>();
String
[]
split
=
redisSentinel
.
split
(
","
);
for
(
String
item
:
split
)
{
sentinels
.
add
(
new
HostAndPort
(
item
.
split
(
":"
)[
0
],
Integer
.
parseInt
(
item
.
split
(
":"
)[
1
])));
}
RedisSentinelPlugin
redisSentinelPlugin
=
new
RedisSentinelPlugin
(
prop
.
get
(
"redisSentinel.CacheName"
,
"redisSentinel"
),
prop
.
get
(
"redisSentinel.masterName"
,
"mymaster"
),
sentinels
,
prop
.
get
(
"redisSentinel.password"
,
"archser_redis"
));
plugins
.
add
(
redisSentinelPlugin
);
}
// 普通Redis单实例配置
if
(!
openRedisSentinel
)
{
RedisPlugin
redis
=
new
RedisPlugin
(
"redis"
,
prop
.
get
(
"redis.url"
),
prop
.
getInt
(
"redis.port"
,
6379
),
prop
.
get
(
"redis.password"
));
plugins
.
add
(
redis
);
}
}
}
src/main/java/com/archser/aserver/plugin/redis/ICache.java
0 → 100644
View file @
a681395e
package
com
.
archser
.
aserver
.
plugin
.
redis
;
/**
* TODO
* @author 李德才
* @version V2.0
* @date 2021/3/3 14:52
*/
import
com.jfinal.plugin.redis.Cache
;
import
com.jfinal.plugin.redis.IKeyNamingPolicy
;
import
com.jfinal.plugin.redis.serializer.ISerializer
;
import
redis.clients.jedis.Jedis
;
import
redis.clients.jedis.JedisSentinelPool
;
public
class
ICache
extends
Cache
{
private
JedisSentinelPool
jedisSentinelPool
;
/**
* TODO JFinal 自带的 Cache要求传入 JedisPool,无法满足哨兵需求,重写连接属性和获取Jedis对象方法
* @author 李德才
* @date 2021/3/3 16:41
* @return
*/
public
ICache
(
String
name
,
JedisSentinelPool
jedisSentinelPool
,
ISerializer
serializer
,
IKeyNamingPolicy
keyNamingPolicy
)
{
try
{
super
.
name
=
name
;
super
.
serializer
=
serializer
;
super
.
keyNamingPolicy
=
keyNamingPolicy
;
super
.
threadLocalJedis
.
set
(
jedisSentinelPool
.
getResource
());
this
.
jedisSentinelPool
=
jedisSentinelPool
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
@Override
public
Jedis
getJedis
()
{
Jedis
jedis
=
threadLocalJedis
.
get
();
return
jedis
!=
null
?
jedis
:
jedisSentinelPool
.
getResource
();
}
}
src/main/java/com/archser/aserver/plugin/redis/RedisSentinelPlugin.java
0 → 100644
View file @
a681395e
package
com
.
archser
.
aserver
.
plugin
.
redis
;
import
com.jfinal.kit.StrKit
;
import
com.jfinal.plugin.IPlugin
;
import
com.jfinal.plugin.redis.IKeyNamingPolicy
;
import
com.jfinal.plugin.redis.Redis
;
import
com.jfinal.plugin.redis.serializer.FstSerializer
;
import
com.jfinal.plugin.redis.serializer.ISerializer
;
import
java.util.HashSet
;
import
java.util.Set
;
import
org.apache.commons.pool2.impl.GenericObjectPoolConfig
;
import
redis.clients.jedis.HostAndPort
;
import
redis.clients.jedis.JedisSentinelPool
;
import
redis.clients.jedis.Protocol
;
/**
* TODO 自定义JFinal sentinel 集群下的 Redis 插件
* @author 李德才
* @version V2.0
* @date 2021/3/2 17:14
*/
public
class
RedisSentinelPlugin
implements
IPlugin
{
private
String
cacheName
;
private
String
masterName
=
null
;
private
int
connectionTimeout
=
Protocol
.
DEFAULT_TIMEOUT
;
private
String
password
=
null
;
private
int
database
=
Protocol
.
DEFAULT_DATABASE
;
private
Set
<
String
>
sentinels
=
new
HashSet
<
String
>();
private
ISerializer
serializer
;
private
IKeyNamingPolicy
keyNamingPolicy
;
private
GenericObjectPoolConfig
poolConfig
;
ICache
iCache
;
public
RedisSentinelPlugin
(
String
cacheName
,
String
masterName
,
Set
<
HostAndPort
>
sentinels
,
String
password
)
{
this
(
cacheName
,
masterName
,
sentinels
,
new
GenericObjectPoolConfig
(),
Protocol
.
DEFAULT_TIMEOUT
,
password
);
}
public
RedisSentinelPlugin
(
String
cacheName
,
String
masterName
,
Set
<
HostAndPort
>
sentinels
,
final
GenericObjectPoolConfig
poolConfig
,
int
timeout
,
final
String
password
)
{
this
(
cacheName
,
masterName
,
sentinels
,
poolConfig
,
timeout
,
password
,
Protocol
.
DEFAULT_DATABASE
);
}
public
RedisSentinelPlugin
(
String
cacheName
,
String
masterName
,
Set
<
HostAndPort
>
sentinels
,
final
GenericObjectPoolConfig
poolConfig
,
int
timeout
,
final
String
password
,
final
int
database
)
{
this
(
cacheName
,
masterName
,
sentinels
,
poolConfig
,
timeout
,
timeout
,
password
,
database
);
}
public
RedisSentinelPlugin
(
String
cacheName
,
String
masterName
,
Set
<
HostAndPort
>
sentinels
,
final
GenericObjectPoolConfig
poolConfig
,
final
int
timeout
,
final
int
soTimeout
,
final
String
password
,
final
int
database
)
{
this
(
cacheName
,
masterName
,
sentinels
,
poolConfig
,
timeout
,
soTimeout
,
password
,
database
,
null
);
}
public
RedisSentinelPlugin
(
String
cacheName
,
String
masterName
,
Set
<
HostAndPort
>
sentinels
,
final
GenericObjectPoolConfig
poolConfig
,
final
int
connectionTimeout
,
final
int
soTimeout
,
final
String
password
,
final
int
database
,
final
String
clientName
)
{
if
(
StrKit
.
isBlank
(
cacheName
))
{
throw
new
IllegalArgumentException
(
"cacheName can not be blank."
);
}
if
(
StrKit
.
isBlank
(
masterName
))
{
throw
new
IllegalArgumentException
(
"masterName can not be blank."
);
}
if
(
null
==
sentinels
||
sentinels
.
isEmpty
())
{
throw
new
IllegalArgumentException
(
"sentinels can not be blank."
);
}
if
(
null
==
poolConfig
)
{
throw
new
IllegalArgumentException
(
"poolConfig can not be null."
);
}
for
(
HostAndPort
hp
:
sentinels
)
{
this
.
sentinels
.
add
(
hp
.
toString
());
}
this
.
cacheName
=
cacheName
.
trim
();
this
.
masterName
=
masterName
.
trim
();
this
.
poolConfig
=
poolConfig
;
this
.
connectionTimeout
=
connectionTimeout
;
this
.
password
=
password
;
this
.
database
=
database
;
}
public
boolean
start
()
{
JedisSentinelPool
jedisSentinelPool
=
new
JedisSentinelPool
(
masterName
,
sentinels
,
poolConfig
,
connectionTimeout
,
password
,
database
);
if
(
serializer
==
null
)
{
serializer
=
FstSerializer
.
me
;
}
if
(
keyNamingPolicy
==
null
)
{
keyNamingPolicy
=
IKeyNamingPolicy
.
defaultKeyNamingPolicy
;
}
iCache
=
new
ICache
(
cacheName
,
jedisSentinelPool
,
serializer
,
keyNamingPolicy
);
Redis
.
addCache
(
iCache
);
return
true
;
}
public
boolean
stop
()
{
try
{
iCache
.
getJedis
().
close
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
true
;
}
}
src/main/resources/AppConfig-pro.properties
View file @
a681395e
...
...
@@ -38,11 +38,23 @@ jdbc.password=archser_hywdtest
#\u4E2D\u95F4\u4EF6\u7C7B\u578B \u6C49\u8BED\u62FC\u97F3 zhongchaung\u3001dongfangtong\u3001baolande\u3001jindie\u3001dev
MiddlewareType
=
dev
# redis\u914D\u7F6E
### don't open redis and redisSentinel At the same time
### \u4E0D\u8981\u540C\u65F6\u6253\u5F00 redis \u5355\u5B9E\u4F8B \u548C redisSentinel \u54E8\u5175\u96C6\u7FA4
## open the redis sentinel ? \u662F\u5426\u5F00\u542F redis \u54E8\u5175\u96C6\u7FA4
openRedisSentinel
=
false
# redis \u5355\u5B9E\u4F8B
redis.url
=
127.0.0.1
redis.port
=
6379
redis.password
=
archser_redis
# redisSentinel \u54E8\u5175\u96C6\u7FA4
redisSentinel.url
=
192.168.31.151:26379,192.168.31.154:26379,192.168.31.157:26379
redisSentinel.password
=
archser_redis
redisSentinel.CacheName
=
redisSentinel
redisSentinel.masterName
=
mymaster
#\u5355\u70B9\u767B\u9646\u5730\u5740
basic.url
=
http://39.104.21.218:11038
## \u662F\u5426\u540C\u6B65Redis\u4E2D\u7684\u6570\u636E
...
...
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