Redisson
Redisson是架设在Redis基础上的⼀个Java驻内存数据⽹格(In-Memory Data Grid)。 Redisson在基于NIO的Netty框架上,充分的利⽤了Redis键值数据库提供的⼀系列优势,在Java实⽤⼯具包中常⽤ 接⼝的基础上,为使⽤者提供了⼀系列具有分布式特性的常⽤⼯具类。使得原本作为协调单机多线程并发程序的⼯ 具包获得了协调分布式多机多线程并发系统的能⼒,⼤⼤降低了设计和研发⼤规模分布式系统的难度。同时结合各 富特⾊的分布式服务,更进⼀步简化了分布式环境中程序相互之间的协作。
Redisson使⽤
1.引⼊分布式锁的⼯具类
redisson
依赖。
<!-- 实现分布式锁的工具类 --> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>3.17.7</version> </dependency> <!-- Spring操作Redis的工具类 --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.3.2.RELEASE</version> </dependency> <!-- Redis客户端 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.9.0</version> </dependency> <!-- JSON解析工具 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.4</version> </dependency>
2.在com.yx.config包下创建RedissonConfig配置类,初始化RedissonClient客户端。
package com . yx . config ;import org . redisson . Redisson ;import org . redisson . api . RedissonClient ;import org . redisson . config . Config ;import org . springframework . context . annotation . Bean ;import org . springframework . context . annotation . Configuration ;@Configurationpublic class RedissonConfig {/** Redis 单机模式 */@Beanpublic RedissonClient singletonModeRedisson () {Config config = new Config ();// 使⽤ "redis://" 来启⽤ SSL 连接config . useSingleServer (). setAddress ( "redis://192.168.230.131:6379" ). setDatabase ( 0 );return Redisson . create ( config );}/** Redis 集群模式 *//*@Beanpublic RedissonClient clusterModeRedisson() {Config config = new Config();config.useClusterServers().setScanInterval(2000) // 群集状态扫描间隔(毫秒).addNodeAddress("redis://192.168.230.131:6379","redis://192.168.230.132:6379", "redis://192.168.230.133:6379");return Redisson.create(config);}*//** Redis 哨兵模式 *//*@Beanpublic RedissonClient sentinelModeRedisson() {Config config = new Config();config.useSentinelServers().setMasterName("myMaster")
在
com.yx.config
包下创建
RedissonConfig
配置类,初始化
RedissonClient
客户端。
以下提供了4种方式配置
package com;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RedissonConfig {
/** Redis单机模式 */
@Bean
public RedissonClient singletonModeRedisson() {
Config config = new Config();
// 使⽤"redis://"来启⽤SSL连接 //使用的是哪个数据库
config.useSingleServer().setAddress("redis://192.168.48.67:6379").setDatabase(0);
return Redisson.create(config);
}
/** Redis集群模式 */
/*
@Bean
public RedissonClient clusterModeRedisson() {
Config config = new Config();
config.useClusterServers()
.setScanInterval(2000) // 群集状态扫描间隔(毫秒)
.addNodeAddress("redis://192.168.230.131:6379",
"redis://192.168.230.132:6379", "redis://192.168.230.133:6379");
return Redisson.create(config);
}
*/
/** Redis哨兵模式 */
// @Bean
// public RedissonClient sentinelModeRedisson() {
// Config config = new Config();
// config.useSentinelServers()
// .setMasterName("myMaster").addSentinelAddress("redis://192.168.230.131:6379",
//"redis://192.168.230.132:6379", "redis://192.168.230.133:6379");
// return Redisson.create(config);
// }
/** Redis主从模式 */
// @Bean
// public RedissonClient masterSlaveModeRedisson() {
// Config config = new Config();
// config.useMasterSlaveServers()
// // 主服务器IP
// .setMasterAddress("redis://192.168.230.131:6379")
// // 从服务器IP
// .addSlaveAddress("redis://192.168.230.132:6379",
//"redis://192.168.230.133:6379");
// return Redisson.create(config);
// }
}
在maven环境中还要如下配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 包扫面-->
<context:component-scan base-package="com"></context:component-scan>
<!--连接工厂-->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="192.168.48.67"></property>
<property name="port" value="6379"></property>
</bean>
<!--Redis操作模板类-->
<bean id="stringRedisTemplate"
class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
</beans>
在web.xml中进行如下配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/Spring/Spring.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 处理中文乱码的问题 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>