初始化提交
This commit is contained in:
31
tulingmall-redis-comm/.gitignore
vendored
Normal file
31
tulingmall-redis-comm/.gitignore
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
HELP.md
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**
|
||||
!**/src/test/**
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
55
tulingmall-redis-comm/pom.xml
Normal file
55
tulingmall-redis-comm/pom.xml
Normal file
@@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>tulingmall-redis-comm</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>tulingmall-redis-comm</name>
|
||||
<description>图灵商城redis通用模块</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.tuling</groupId>
|
||||
<artifactId>tuling-mall</artifactId>
|
||||
<version>0.0.5</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.5.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-pool2</artifactId>
|
||||
</dependency>
|
||||
<!--加入redisson-->
|
||||
<dependency>
|
||||
<groupId>org.redisson</groupId>
|
||||
<artifactId>redisson</artifactId>
|
||||
<version>3.6.5</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.3.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.tuling.tulingmall.rediscomm.config;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.tuling.tulingmall.rediscomm.util.RedisOpsExtUtil;
|
||||
import org.redisson.Redisson;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.config.ClusterServersConfig;
|
||||
import org.redisson.config.Config;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
@Configuration
|
||||
public class RedisExtConifg {
|
||||
|
||||
@Value("${spring.redis.cluster.nodes}")
|
||||
String redisNodes;
|
||||
|
||||
@Value("${spring.redis.password:nopwd}")
|
||||
String redisPass;
|
||||
|
||||
@Autowired
|
||||
private RedisConnectionFactory connectionFactory;
|
||||
|
||||
@Bean("redisCluster")
|
||||
@Primary
|
||||
public RedisTemplate<String,Object> redisTemplate(){
|
||||
RedisTemplate<String,Object> template = new RedisTemplate();
|
||||
template.setConnectionFactory(connectionFactory);
|
||||
// 序列化工具
|
||||
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer
|
||||
= new Jackson2JsonRedisSerializer<>(Object.class);
|
||||
ObjectMapper om = new ObjectMapper();
|
||||
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
||||
jackson2JsonRedisSerializer.setObjectMapper(om);
|
||||
|
||||
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
|
||||
template.setKeySerializer(stringRedisSerializer);
|
||||
template.setValueSerializer(jackson2JsonRedisSerializer);
|
||||
|
||||
template.setHashKeySerializer(jackson2JsonRedisSerializer);
|
||||
template.setHashValueSerializer(jackson2JsonRedisSerializer);
|
||||
|
||||
template.afterPropertiesSet();
|
||||
return template;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RedisOpsExtUtil redisOpsUtil(){
|
||||
return new RedisOpsExtUtil();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RedissonClient redissonClient(){
|
||||
Config config = new Config();
|
||||
ClusterServersConfig clusterServersConfig = config.useClusterServers();
|
||||
for (String node: redisNodes.split(",")){
|
||||
clusterServersConfig.addNodeAddress("redis://"+node);
|
||||
}
|
||||
if(!"nopwd".equals(redisPass)){
|
||||
clusterServersConfig.setPassword(redisPass);
|
||||
}
|
||||
return Redisson.create(config);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.tuling.tulingmall.rediscomm.util;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Slf4j
|
||||
public class RedisOpsExtUtil {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("redisCluster")
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
public void set(String key,Object value){
|
||||
redisTemplate.opsForValue().set(key,value);
|
||||
}
|
||||
|
||||
public void set(String key, Object value, long timeout, TimeUnit unit){
|
||||
redisTemplate.opsForValue().set(key,value,timeout,unit);
|
||||
}
|
||||
|
||||
public <V> void putListAllRight(String key, Collection<V> values){
|
||||
if(CollectionUtils.isEmpty(values)){
|
||||
log.warn("{}没有数据可放入Redis",key);
|
||||
}else{
|
||||
redisTemplate.opsForList().rightPushAll(key,values);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> T getListAll(String key,Class<?> T){
|
||||
return (T)redisTemplate.opsForList().range(key,0,-1);
|
||||
}
|
||||
|
||||
public <T> T get(String key,Class<?> T){
|
||||
return (T)redisTemplate
|
||||
.opsForValue().get(key);
|
||||
}
|
||||
|
||||
public String get(String key){
|
||||
return (String) redisTemplate
|
||||
.opsForValue().get(key);
|
||||
}
|
||||
|
||||
public Long decr(String key){
|
||||
return redisTemplate
|
||||
.opsForValue().decrement(key);
|
||||
}
|
||||
|
||||
public Long decr(String key,long delta){
|
||||
return redisTemplate
|
||||
.opsForValue().decrement(key,delta);
|
||||
}
|
||||
|
||||
public Long incr(String key){
|
||||
return redisTemplate
|
||||
.opsForValue().increment(key);
|
||||
}
|
||||
|
||||
public Long incr(String key,long delta){
|
||||
return redisTemplate
|
||||
.opsForValue().increment(key,delta);
|
||||
}
|
||||
|
||||
public boolean expire(String key,long timeout,TimeUnit unit){
|
||||
return redisTemplate.expire(key,timeout, unit);
|
||||
}
|
||||
|
||||
public boolean delete(String key){
|
||||
return redisTemplate.delete(key);
|
||||
}
|
||||
|
||||
public boolean hasKey(String key){
|
||||
return redisTemplate.hasKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布channel信息
|
||||
* @param channel
|
||||
* @param message
|
||||
*/
|
||||
public void publish(String channel,Object message){
|
||||
redisTemplate.convertAndSend(channel,message);
|
||||
}
|
||||
|
||||
}
|
||||
16
tulingmall-redis-comm/src/main/resources/rebel.xml
Normal file
16
tulingmall-redis-comm/src/main/resources/rebel.xml
Normal file
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<!--
|
||||
This is the JRebel configuration file. It maps the running application to your IDE workspace, enabling JRebel reloading for this project.
|
||||
Refer to https://manuals.jrebel.com/jrebel/standalone/config.html for more information.
|
||||
-->
|
||||
<application generated-by="intellij" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.zeroturnaround.com" xsi:schemaLocation="http://www.zeroturnaround.com http://update.zeroturnaround.com/jrebel/rebel-2_3.xsd">
|
||||
|
||||
<id>tulingmall-common</id>
|
||||
|
||||
<classpath>
|
||||
<dir name="D:/GitSource/tmallV5/tulingmall-common/target/classes">
|
||||
</dir>
|
||||
</classpath>
|
||||
|
||||
</application>
|
||||
Reference in New Issue
Block a user