初始化提交
This commit is contained in:
31
tulingmall-redis-multi/.gitignore
vendored
Normal file
31
tulingmall-redis-multi/.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/
|
||||
59
tulingmall-redis-multi/pom.xml
Normal file
59
tulingmall-redis-multi/pom.xml
Normal file
@@ -0,0 +1,59 @@
|
||||
<?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-multi</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>tulingmall-redis-multi</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>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</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,156 @@
|
||||
package com.tuling.tulingmall.rediscomm.config;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.tuling.tulingmall.rediscomm.util.RedisClusterUtil;
|
||||
import com.tuling.tulingmall.rediscomm.util.RedisSingleUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.data.redis.connection.RedisClusterConfiguration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
||||
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
|
||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class RedisConfig {
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Value("${spring.redisSingle.host}")
|
||||
private String host;
|
||||
@Value("${spring.redisSingle.port}")
|
||||
private String port;
|
||||
@Value("${spring.redisSingle.lettuce.pool.max-active}")
|
||||
private String max_active;
|
||||
@Value("${spring.redisSingle.lettuce.pool.max-idle}")
|
||||
private String max_idle;
|
||||
@Value("${spring.redisSingle.lettuce.pool.max-wait}")
|
||||
private String max_wait;
|
||||
@Value("${spring.redisSingle.lettuce.pool.min-idle}")
|
||||
private String min_idle;
|
||||
|
||||
@Bean("redisClusterPool")
|
||||
@Primary
|
||||
@ConfigurationProperties(prefix = "spring.redis.cluster.lettuce.pool")
|
||||
public GenericObjectPoolConfig redisClusterPool() {
|
||||
return new GenericObjectPoolConfig();
|
||||
}
|
||||
|
||||
@Bean("redisClusterConfig")
|
||||
@Primary
|
||||
public RedisClusterConfiguration redisClusterConfig() {
|
||||
|
||||
Map<String, Object> source = new HashMap<>(8);
|
||||
source.put("spring.redis.cluster.nodes", environment.getProperty("spring.redis.cluster.nodes"));
|
||||
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));
|
||||
redisClusterConfiguration.setPassword(environment.getProperty("spring.redis.password"));
|
||||
return redisClusterConfiguration;
|
||||
|
||||
}
|
||||
|
||||
@Bean("redisFactoryCluster")
|
||||
@Primary
|
||||
public LettuceConnectionFactory lettuceConnectionFactory(
|
||||
@Qualifier("redisClusterPool") GenericObjectPoolConfig redisPool,
|
||||
@Qualifier("redisClusterConfig") RedisClusterConfiguration redisClusterConfig) {
|
||||
LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(redisPool).build();
|
||||
return new LettuceConnectionFactory(redisClusterConfig, clientConfiguration);
|
||||
}
|
||||
|
||||
@Bean("redisClusterTemplate")
|
||||
@Primary
|
||||
public RedisTemplate redisClusterTemplate(
|
||||
@Qualifier("redisFactoryCluster") RedisConnectionFactory redisConnectionFactory) {
|
||||
return redisTemplateSerializer(redisConnectionFactory);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RedisClusterUtil redisOpsUtil(){
|
||||
return new RedisClusterUtil();
|
||||
}
|
||||
|
||||
@Bean("redisSinglePool")
|
||||
public GenericObjectPoolConfig redisSinglePool() {
|
||||
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
|
||||
config.setMinIdle(Convert.toInt(min_idle));
|
||||
config.setMaxIdle(Convert.toInt(max_idle));
|
||||
config.setMaxTotal(Convert.toInt(max_active));
|
||||
config.setMaxWaitMillis(Convert.toInt(max_wait));
|
||||
return config;
|
||||
}
|
||||
|
||||
@Bean("redisSingleConfig")
|
||||
public RedisStandaloneConfiguration redisSingleConfig() {
|
||||
RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration(host,Convert.toInt(port));
|
||||
return redisConfig;
|
||||
}
|
||||
|
||||
@Bean("redisFactorySingle")
|
||||
public LettuceConnectionFactory redisFactorySingle(@Qualifier("redisSinglePool") GenericObjectPoolConfig config,
|
||||
@Qualifier("redisSingleConfig") RedisStandaloneConfiguration redisConfig) {//注意传入的对象名和类型RedisStandaloneConfiguration
|
||||
LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build();
|
||||
return new LettuceConnectionFactory(redisConfig, clientConfiguration);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 单实例redis数据源
|
||||
*
|
||||
* @param connectionFactory
|
||||
* @return
|
||||
*/
|
||||
@Bean("redisSingleTemplate")
|
||||
public RedisTemplate<String, Object> redisSingleTemplate(
|
||||
@Qualifier("redisFactorySingle")LettuceConnectionFactory connectionFactory) {
|
||||
return redisTemplateSerializer(connectionFactory);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RedisSingleUtil redisSingleUtil(){
|
||||
return new RedisSingleUtil();
|
||||
}
|
||||
|
||||
private RedisTemplate<String, Object> redisTemplateSerializer(RedisConnectionFactory connectionFactory){
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
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.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisConnectionUtils;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Slf4j
|
||||
public class RedisClusterUtil {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("redisClusterTemplate")
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
@PostConstruct
|
||||
public void init(){
|
||||
RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
|
||||
RedisConnection conn = RedisConnectionUtils.getConnection(factory);
|
||||
log.info("RedisCluster信息:{}",conn.info().toString());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
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.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisConnectionUtils;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Slf4j
|
||||
public class RedisSingleUtil {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("redisSingleTemplate")
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
@PostConstruct
|
||||
public void init(){
|
||||
RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
|
||||
RedisConnection conn = RedisConnectionUtils.getConnection(factory);
|
||||
log.info("Redis服务器信息:{}",conn.info().toString());
|
||||
}
|
||||
|
||||
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-multi/src/main/resources/rebel.xml
Normal file
16
tulingmall-redis-multi/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