前言
本文假使你了解spring boot并实践过,非spring boot用户可跳过也可借此研究一下。
redisson是redis的java客户端程序,国内外很多公司都有在用,如下,
和spring的集成中官方给出的实例也是比较多,比较方便。
集成jedis实例,xml方式
集成前引用的jar
1
2
3
4
5
6
7
8
9
10
11
12
|
<!--kl add redis client--> < dependency > < groupId >redis.clients</ groupId > < artifactId >jedis</ artifactId > < version >2.9.0</ version > </ dependency > <!-- https://mvnrepository.com/artifact/commons-pool/commons-pool --> < dependency > < groupId >org.apache.commons</ groupId > < artifactId >commons-pool2</ artifactId > < version >2.2</ version > </ dependency > |
spring bean配置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
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <!-- POOL配置 --> < bean id = "jedisPoolConfig" class = "redis.clients.jedis.JedisPoolConfig" > < property name = "maxTotal" value = "300" /> < property name = "maxIdle" value = "10" /> < property name = "maxWaitMillis" value = "1000" /> < property name = "testOnBorrow" value = "true" /> </ bean > <!-- jedis shard信息配置 --> < bean id = "jedisShardInfo" class = "redis.clients.jedis.JedisShardInfo" > < constructor-arg index = "0" value = "${redis.host}" /> < constructor-arg index = "1" value = "${redis.port}" type = "int" /> </ bean > <!-- jedis shard pool配置 --> < bean id = "shardedJedisPool" class = "redis.clients.jedis.ShardedJedisPool" > < constructor-arg index = "0" ref = "jedisPoolConfig" /> < constructor-arg index = "1" > < list > < ref bean = "jedisShardInfo" /> </ list > </ constructor-arg > </ bean > < bean id = "shardedJedis" factory-bean = "shardedJedisPool" factory-method = "getResource" /> </ beans > |
集成redisson实例,java bean的方式
集成前引入的jar
1
2
3
4
5
6
|
<!--kl add redis client--> < dependency > < groupId >org.redisson</ groupId > < artifactId >redisson</ artifactId > < version >2.5.0</ version > </ dependency > |
javabean配置如下
1
2
3
4
5
6
7
8
9
10
11
12
|
/** * Created by kl on 2016/10/21. */ @Configuration @ComponentScan public class RedsissonConfig { @Bean (destroyMethod= "shutdown" ) RedissonClient redisson( @Value ( "classpath:/conf/redisson.yaml" ) Resource configFile) throws IOException { Config config = Config.fromYAML(configFile.getInputStream()); return Redisson.create(config); } } |
spring集成redis客户端jedis以及redisson,可以提供yaml,json配置文件来实例化redissonClient,也可以使用spring的xml来配置,redisson官方给出了诸如<redisson:client>等标签来简化在xml中的配置,但是如果我们的程序是spring boot,一般都是使用application.properties来配置我们应用配置参数,不想提供额外的yaml,json,xml等配置文件,虽然spring boot也支持这么做。所以如何使用application.properties的方式配置redisson呢,请看下文?
提供实例化javabean
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
|
/** * Created by kl on 2017/09/26. * redisson 客户端配置 */ @ConfigurationProperties (prefix = "spring.redisson" ) @Configuration public class RedissonConfig{ private String address; private int connectionMinimumIdleSize = 10 ; private int idleConnectionTimeout= 10000 ; private int pingTimeout= 1000 ; private int connectTimeout= 10000 ; private int timeout= 3000 ; private int retryAttempts= 3 ; private int retryInterval= 1500 ; private int reconnectionTimeout= 3000 ; private int failedAttempts= 3 ; private String password = null ; private int subscriptionsPerConnection= 5 ; private String clientName= null ; private int subscriptionConnectionMinimumIdleSize = 1 ; private int subscriptionConnectionPoolSize = 50 ; private int connectionPoolSize = 64 ; private int database = 0 ; private boolean dnsMonitoring = false ; private int dnsMonitoringInterval = 5000 ; private int thread; //当前处理核数量 * 2 private String codec= "org.redisson.codec.JsonJacksonCodec" ; @Bean (destroyMethod = "shutdown" ) RedissonClient redisson() throws Exception { Config config = new Config(); config.useSingleServer().setAddress(address) .setConnectionMinimumIdleSize(connectionMinimumIdleSize) .setConnectionPoolSize(connectionPoolSize) .setDatabase(database) .setDnsMonitoring(dnsMonitoring) .setDnsMonitoringInterval(dnsMonitoringInterval) .setSubscriptionConnectionMinimumIdleSize(subscriptionConnectionMinimumIdleSize) .setSubscriptionConnectionPoolSize(subscriptionConnectionPoolSize) .setSubscriptionsPerConnection(subscriptionsPerConnection) .setClientName(clientName) .setFailedAttempts(failedAttempts) .setRetryAttempts(retryAttempts) .setRetryInterval(retryInterval) .setReconnectionTimeout(reconnectionTimeout) .setTimeout(timeout) .setConnectTimeout(connectTimeout) .setIdleConnectionTimeout(idleConnectionTimeout) .setPingTimeout(pingTimeout) .setPassword(password); Codec codec=(Codec)ClassUtils.forName(getCodec(),ClassUtils.getDefaultClassLoader()).newInstance(); config.setCodec(codec); config.setThreads(thread); config.setEventLoopGroup( new NioEventLoopGroup()); config.setUseLinuxNativeEpoll( false ); return Redisson.create(config); } |
注意:以上代码不是完整的,省略了get set代码
application.properties添加如下配置
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
|
#redis链接地址 spring.redisson.address=192.168.1.204:6379 #当前处理核数量 * 2 spring.redisson.thread=4 #指定编解码 spring.redisson.codec=org.redisson.codec.JsonJacksonCodec; #最小空闲连接数,默认值:10,最小保持连接数(长连接) spring.redisson.connectionMinimumIdleSize=12 #连接空闲超时,单位:毫秒 默认10000;当前连接池里的连接数量超过了最小空闲连接数, #而连接空闲时间超过了该数值,这些连接将会自动被关闭,并从连接池里去掉 spring.redisson.idleConnectionTimeout=10000 #ping节点超时,单位:毫秒,默认1000 spring.redisson.pingTimeout=1000 #连接等待超时,单位:毫秒,默认10000 spring.redisson.connectTimeout=10000 #命令等待超时,单位:毫秒,默认3000;等待节点回复命令的时间。该时间从命令发送成功时开始计时 spring.redisson.timeout=3000 #命令失败重试次数,默认值:3 spring.redisson.retryAttempts=2 #命令重试发送时间间隔,单位:毫秒,默认值:1500 spring.redisson.retryInterval=1500 #重新连接时间间隔,单位:毫秒,默认值:3000;连接断开时,等待与其重新建立连接的时间间隔 spring.redisson.reconnectionTimeout=3000 #执行失败最大次数, 默认值:3;失败后直到 reconnectionTimeout超时以后再次尝试。 spring.redisson.failedAttempts=2 #身份验证密码 #spring.redisson.password= #单个连接最大订阅数量,默认值:5 spring.redisson.subscriptionsPerConnection=5 #客户端名称 #spring.redisson.clientName= #发布和订阅连接的最小空闲连接数,默认值:1;Redisson内部经常通过发布和订阅来实现许多功能。 #长期保持一定数量的发布订阅连接是必须的 spring.redisson.subscriptionConnectionMinimumIdleSize=1 #发布和订阅连接池大小,默认值:50 spring.redisson.subscriptionConnectionPoolSize=50 #连接池最大容量。默认值:64;连接池的连接数量自动弹性伸缩 spring.redisson.connectionPoolSize=64 #数据库编号,默认值:0 spring.redisson.database=0 #是否启用DNS监测,默认值:false spring.redisson.dnsMonitoring= false #DNS监测时间间隔,单位:毫秒,默认值:5000 spring.redisson.dnsMonitoringInterval=5000 |
java bean中已经给所有需要配置的值写上了官方默认的初始值,如果你不考虑更改默认值,实际上你只需要在你的application.properties添加如下配置就好
1
2
|
#redis链接地址 spring.redisson.address=192.168.1.204:6379 |
注意:这里配置连接的模式是单机模式,如果你想通过这种配置方式配置集群模式和哨兵模式,请参考官方wiki,修改下java bean就好
githubwiki:https://github.com/redisson/redisson/wiki/
以上就是spring boot集成redisson的最佳实践示例的详细内容,更多关于spring boot集成redisson实践的资料请关注服务器之家其它相关文章!
原文链接:http://www.kailing.pub/article/index/arcid/162.html