I. environment
Redis:4.0.9
SpringBoot:2.0.1
Redis installation: Linux(Redhat) Installation of Redis
2. SpringBook Integration Redis
1. Basic construction of the project:
We are based on (5) SpringBoot 2.0 Foundation - Mybatis and Plug-in Generation Code Redis integration for this project;
2. Add maven-related dependencies and Redis connection information:
Pom.xml
<!-- Redis Dependency Library --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.7.RELEASE</version> </dependency> <!-- fastJson Dependency Library --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28</version> </dependency> <!-- aop Dependency Library--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
application.properties:
spring.redis.cluster.nodes=192.168.1.124:7001
3. Create RedisProperties attribute class and RedisConfig configuration class, and put JedisCluster into Spring container:
RedisConfigurationProperties:
package com.cn.common.redis; import java.util.ArrayList; import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; /** * @program: spring-boot-example * @description: * @author: * @create: 2018-05-16 15:15 **/ @Configuration @ConfigurationProperties(prefix = "spring.redis.cluster") public class RedisConfigurationProperties { private List<String> nodes = new ArrayList<>(); public List<String> getNodes() { return nodes; } public void setNodes(List<String> nodes) { this.nodes = nodes; } }
RedisConfig:
package com.cn.common.redis; import java.util.HashSet; import java.util.Set; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisCluster; /** * @program: spring-boot-example * @description: Redis Configuration file * @author: * @create: 2018-05-16 15:01 **/ @Configuration public class RedisConfig { @Autowired private RedisConfigurationProperties redisConfigurationProperties; @Bean public JedisCluster jedisCluster() { Set<HostAndPort> nodeSet = new HashSet<>(); for(String node :redisConfigurationProperties.getNodes()) { String[] split = node.split(":"); nodeSet.add(new HostAndPort(split[0],Integer.valueOf(split[1]))); } return new JedisCluster(nodeSet); } }
4. Create a Jedis Service (Jedis Service Impl) encapsulating Redis:
JedisService.javapackage com.cn.common.service; import java.util.List; import java.util.Map; import redis.clients.jedis.GeoRadiusResponse; /** * @program: spring-boot-example * @description: * @author: * @create: 2018-05-16 15:27 **/ public interface JedisService { /** * @Description: Does it exist? * @Param: * @return: * @Author: * @Date: 2018/5/16 */ boolean exists(String key); /** * @Description:Cache set value * @Param: seconds:Cache time, not set to 0 * @return: * @Author: * @Date: 2018/5/16 */ String set(String key,String value,int seconds); /** * @Description: Recaching getSet values * @Param: * @return: * @Author: * @Date: 2018/5/16 */ String getSet(String key,String value, int seconds); /** * @Description: Get set value * @Param: * @return: * @Author: * @Date: 2018/5/16 */ String get(String key); /** * @Description: Adding Geographical Location * @Param: * @return: * @Author: * @Date: 2018/5/16 */ Long geoadd(String key,double longitude,double latitude,byte[] obj); /** * @Description: Geographical Location Query * @Param: * @return: * @Author: * @Date: 2018/5/16 */ List<GeoRadiusResponse> georadius(String key,double longitude,double latitude); /** * @Description: Delete key * @Param: * @return: * @Author: * @Date: 2018/5/16 */ void delKey(String key); /** * @Description: Delete native key * @Param: * @return: * @Author: * @Date: 2018/5/16 */ void delNativeKey(String key); /** * @Description: Get data in map format * @Param: * @return: * @Author: * @Date: 2018/5/16 */ Map<String ,Object> getMapData(String key); /** * @Description: Lock to avoid duplicate submission * @Param: * @return: * @Author: * @Date: 2018/5/16 */ boolean lock(String key,int seconds); /** * @Description: Unlock * @Param: * @return: * @Author: * @Date: 2018/5/16 */ void unlock(String key); /** * @Description: Statistical lockout times * @Param: * @return: * @Author: * @Date: 2018/5/16 */ String getLocakValue(String key); }