代码拉取完成,页面将自动刷新
a.默认情况下,是获取yml配置文件中${spring.redis.username}:${spring.profiles.active}:${spring.application.name}作为key前缀,如果yml中未配置,默认会是default:dev:rediscover
b.如果项目侧自定义redisTemplate对象,则序列化key时,则将StringRedisSerializer交于spring管理
c.如果用户key以“global:"开头,则定制的key为${spring.redis.username}:${spring.profiles.active}:key,去掉项目名称,代表多个项目可以操作,获取此全局key的值,也需要加上global:
a.本地缓存一致性用redis发布订阅完成,是否开启发布订阅缓存消息,配置tcl.rediscover.redis.cache.publish.enable默认false,渠道topic配置为tcl.rediscover.redis.cache.topic,默认值jetCacheSyncTopic
b.redis订阅消息不可靠,对于强一致性项目,建议用专业MQ实现,配置tcl.rediscover.redis.cache.publish.enable为false,发布消息类,继承AbstractCacheMessagePublisher即可完成,缓存变更后的消息发送
<dependency>
<groupId>com.tcl.rediscover</groupId>
<artifactId>rediscover-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
例如:如果只需要redis基本功能,则maven引入如下配置
<dependency>
<groupId>com.tcl.rediscover</groupId>
<artifactId>rediscover-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>com.alicp.jetcache</groupId>
<artifactId>jetcache-starter-redis-springdata</artifactId>
</exclusion>
<exclusion>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
# 集群模式 jedis
spring:
redis:
client-type: jedis
timeout: 6000ms
password: 123456
cluster:
max-redirects: 3 # 获取失败 最大重定向次数
nodes:
- 192.168.220.101:6380
- 192.168.220.101:6381
- 192.168.220.102:6380
- 192.168.220.102:6381
- 192.168.220.103:6380
- 192.168.220.103:6381
jedis:
pool:
max-active: 1000 #连接池最大连接数(使用负值表示没有限制)
max-idle: 90 # 连接池中的最大空闲连接
min-idle: 15 # 连接池中的最小空闲连接
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
# 集群模式lettuce
spring:
redis:
client-type: lettuce
timeout: 6000ms
password: 123456
cluster:
max-redirects: 3 # 获取失败 最大重定向次数
nodes:
- 192.168.220.101:6380
- 192.168.220.101:6381
- 192.168.220.102:6380
- 192.168.220.102:6381
- 192.168.220.103:6380
- 192.168.220.103:6381
lettuce:
pool:
max-active: 1000 #连接池最大连接数(使用负值表示没有限制)
max-idle: 90 # 连接池中的最大空闲连接
min-idle: 15 # 连接池中的最小空闲连接
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
# 哨兵模式jedis
spring:
redis:
client-type: jedis
timeout: 6000ms
password: 123456
jedis:
pool:
max-active: 8
max-wait: -1ms
max-idle: 8
min-idle: 0
sentinel:
master: mymaster
password: 123456
nodes: 192.168.220.101:26379, 192.168.220.102:26379, 192.168.220.103:26379
# 哨兵模式lettuce
spring:
redis:
client-type: lettuce
timeout: 6000ms
password: 123456
lettuce:
pool:
max-active: 8
max-wait: -1ms
max-idle: 8
min-idle: 0
sentinel:
master: mymaster
password: 123456
nodes: 192.168.220.101:26379, 192.168.220.102:26379, 192.168.220.103:26379
jetcache:
statIntervalMinutes: 15
areaInCacheName: false
local:
default:
type: caffeine
keyConvertor: fastjson
limit: 100
remote:
default:
type: redis.springdata
keyConvertor: fastjson
keyPrefix: projectPrefix # 此处为项目定制key前缀
valueEncoder: kryo
valueDecoder: kryo
poolConfig:
minIdle: 5
maxIdle: 20
maxTotal: 50
1 maven引入jar包(通过maven引入的方式实现三大功能板块的可插拔)
<dependency>
<groupId>com.tcl.rediscover</groupId>
<artifactId>rediscover-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>6.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.6.3</version>
</dependency>
例如:如果只需要redis基本功能,则maven引入如下配置
<dependency>
<groupId>com.tcl.rediscover</groupId>
<artifactId>rediscover-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>com.alicp.jetcache</groupId>
<artifactId>jetcache-starter-redis-springdata</artifactId>
</exclusion>
<exclusion>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>6.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.6.3</version>
</dependency>
2 yml配置修改(主要是redis配置,对于已有的在原有基础上做修改)
# 集群模式 jedis
spring:
redis:
client-type: jedis
timeout: 6000ms
username: rediscover # 与低版本相比增加了用户隔离,如果redis未配置用户,默认配default
password: 123456
cluster:
max-redirects: 3 # 获取失败 最大重定向次数
nodes:
- 192.168.220.101:6380
- 192.168.220.101:6381
- 192.168.220.102:6380
- 192.168.220.102:6381
- 192.168.220.103:6380
- 192.168.220.103:6381
jedis:
pool:
max-active: 1000 #连接池最大连接数(使用负值表示没有限制)
max-idle: 90 # 连接池中的最大空闲连接
min-idle: 15 # 连接池中的最小空闲连接
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
# 集群模式lettuce
spring:
redis:
client-type: lettuce
timeout: 6000ms
username: rediscover # 与低版本相比增加了用户隔离,如果redis未配置用户,默认配default
password: 123456
cluster:
max-redirects: 3 # 获取失败 最大重定向次数
nodes:
- 192.168.220.101:6380
- 192.168.220.101:6381
- 192.168.220.102:6380
- 192.168.220.102:6381
- 192.168.220.103:6380
- 192.168.220.103:6381
lettuce:
pool:
max-active: 1000 #连接池最大连接数(使用负值表示没有限制)
max-idle: 90 # 连接池中的最大空闲连接
min-idle: 15 # 连接池中的最小空闲连接
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
# 哨兵模式jedis
spring:
redis:
client-type: jedis
timeout: 6000ms
username: rediscover # 与低版本相比增加了用户隔离,如果redis未配置用户,默认配default
password: 123456
jedis:
pool:
max-active: 8
max-wait: -1ms
max-idle: 8
min-idle: 0
sentinel:
master: mymaster
username: sentinelUser
password: 123456
nodes: 192.168.220.101:26379, 192.168.220.102:26379, 192.168.220.103:26379
# 哨兵模式lettuce
spring:
redis:
client-type: lettuce
timeout: 6000ms
username: rediscover # 与低版本相比增加了用户隔离,如果redis未配置用户,默认配default
password: 123456
lettuce:
pool:
max-active: 8
max-wait: -1ms
max-idle: 8
min-idle: 0
sentinel:
master: mymaster
username: sentinelUser
password: 123456
nodes: 192.168.220.101:26379, 192.168.220.102:26379, 192.168.220.103:26379
3.如果需要使用jetCache缓存组件,则放开maven的exclusion,同时yml添加如下配置
jetcache:
statIntervalMinutes: 15
areaInCacheName: false
local:
default:
type: caffeine
keyConvertor: fastjson
limit: 100
remote:
default:
type: redis.springdata
keyConvertor: fastjson
keyPrefix: ${spring.redis.username}:${spring.profiles.active} # 此处为项目定制key前缀,默认为此配置
valueEncoder: kryo
valueDecoder: kryo
poolConfig:
minIdle: 5
maxIdle: 20
maxTotal: 50
所有节点配置相同
aclfile /root/redis-6.2.6/conf/users.acl # 文件方式配置用户权限
masteruser "default" #用default用户用作集群间的交互
masterauth "123456"
user default on #8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 ~* &* +@all
user rediscover on #8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 ~rediscover:uat* &* +@read +@write -@dangerous
所有数据节点的配置
aclfile /root/redis-6.2.6/conf/users.acl # 文件方式配置用户权限
masteruser "default" #用default用户用作节点间的交互
masterauth "123456"
user default on #8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 ~* &* +@all
user rediscover on #8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 ~rediscover:uat* &* +@read +@write -@dangerous
所有哨兵节点配置相同
sentinel auth-pass mymaster 123456
sentinel auth-user mymaster default #用default用户进行数据节点间的交互
aclfile "/root/redis-6.2.6/conf/sentinel-users.acl"
sentinel sentinel-user default #用default用户进行哨兵节点间的交互
sentinel sentinel-pass 123456
user default on #8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 ~* &* +@all
user sentinelUser on #8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 ~* &* +@connection
具体使用参考 ACL使用
如果工具类里面没有的,则
RedisUtils.getRedisTemplate.opsForValue.set("aa","bba")
或者
@Autowired
private RedisTemplate redisTemplate;
redisTemplate.opsForValue.set("aa","bba")
@PostMapping("/submit")
@MultiSubmit
public String submit(@RequestBody User user) {
}
@Autowired
private RedissonClient redissonClient;
RLock lock = redissonClient.getLock(key);
try {
lock.lock();
//业务代码
} catch (Exception e) {
log.error("线程被打断",e);
} finally {
lock.unlock();
}
RRateLimiter rateLimiter = redissonClient.getRateLimiter(key);
// 最大流速 = 每10秒钟产生1个令牌
rateLimiter.trySetRate(RateType.OVERALL, 1, 10, RateIntervalUnit.SECONDS);
//需要1个令牌
if(rateLimiter.tryAcquire(1)) {
return "访问成功";
} else {
return "系统繁忙,请稍后重试";
}
RMapCache<Object, Object> mapCache = redissonClient.getMapCache("cities", new StringCodec("utf-8"));
if(mapCache.size()<=0) {
mapCache.put(1,"libai",10,TimeUnit.SECONDS);
mapCache.put(2,"dufu",3, TimeUnit.SECONDS);
}
return mapCache.values().toString();
启动类上加入 @EnableCreateCacheAnnotation
@CreateCache(name = ":user:", expire = 3600, cacheType = CacheType.BOTH, localLimit = 50)
private Cache<Long, User> userCache;
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.3.RELEASE)
2021-11-30 11:08:10.258 [main] INFO com.tcl.rediscover.redis.demo.Application - Starting Application on sz-00194633 with PID 16500 (E:\proboot\demo\target\classes started by liangxi.zeng in E:\proboot)
2021-11-30 11:08:10.263 [main] INFO com.tcl.rediscover.redis.demo.Application - The following profiles are active: uat
2021-11-30 11:08:11.550 [main] INFO o.s.d.r.config.RepositoryConfigurationDelegate - Multiple Spring Data modules found, entering strict repository configuration mode!
2021-11-30 11:08:11.554 [main] INFO o.s.d.r.config.RepositoryConfigurationDelegate - Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2021-11-30 11:08:11.594 [main] INFO o.s.d.r.config.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 21ms. Found 0 Redis repository interfaces.
2021-11-30 11:08:12.623 [main] INFO o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat initialized with port(s): 9988 (http)
2021-11-30 11:08:12.633 [main] INFO org.apache.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-9988"]
2021-11-30 11:08:13.069 [main] INFO c.t.r.r.c.namespace.key.DefaultNameSpaceKeyHandler - ****redis加载默认DefaultNameSpaceKey权限key构造器****
2021-11-30 11:08:13.073 [main] INFO c.t.r.redis.core.redisson.NameSpaceKeyNameMapper - ****redisson加载默认NameSpaceKeyNameMapper权限key构造器****
2021-11-30 11:08:13.321 [main] INFO org.redisson.Version - Redisson 3.16.4
2021-11-30 11:08:15.387 [main] INFO c.a.jetcache.autoconfigure.AbstractCacheAutoInit - init cache area default , type= redis.springdata
2021-11-30 11:08:15.422 [main] INFO c.a.jetcache.autoconfigure.AbstractCacheAutoInit - init cache area default , type= caffeine
2021-11-30 11:08:15.429 [main] INFO c.t.r.r.c.n.aop.AspectStringRedisKeySerializer - ****redis权限key拦截启动****
2021-11-30 11:08:15.660 [main] INFO org.apache.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-9988"]
2021-11-30 11:08:15.695 [main] INFO o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 9988 (http) with context path ''
2021-11-30 11:08:16.200 [main] INFO com.tcl.rediscover.redis.demo.Application - Started Application in 6.423 seconds (JVM running for 7.073)
* 问题1:
项目启动:
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
com.tcl.digitization.proboot.redis.configuration.RedisConnectionConfiguration.getClusterConfiguration(RedisConnectionConfiguration.java:95)
The following method did not exist:
org.springframework.data.redis.connection.RedisClusterConfiguration.setUsername(Ljava/lang/String;)V
问题分析:1.升级redis到6.x版本,支持acl用户权限控制,spring-data-redis到2.5.6,lettuce-core到6.1.5.RELEASE或者jedis到3.6.3
2.去掉redis用户配置,低版本不支持此配置,redis不支持acl,namespace一样可以增加相应前缀
* 问题 2:
项目启动:`
Caused by: org.redisson.client.RedisException: ERR AUTH <password> called without any password configured for the default user. Are you sure your configuration is correct?. channel: [id: 0xfe21eabd, L:/10.88.84.96:63933 - R:10.74.151.20/10.74.151.20:32185] command: (AUTH), params: (password masked)
at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:370)
at org.redisson.client.handler.CommandDecoder.decodeCommand(CommandDecoder.java:198)
at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:137)
at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:113)
at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:501)
at io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:366)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
解决方案:redis配置中password错误,或者配置了password为空
* 问题 3:
@Cached的key、condition等表达式中使用参数名以后缓存没有生效
javac编译器需要指定-parameters参数以后才会把参数名信息写入到字节码中,然后才能被反射机制读取,默认情况下这个参数是没有指定的。
pom中的指定方式:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-parameters</compilerArgument>
</configuration>
</plugin>
</plugins>
* 问题 4:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
16:31:32.478 [restartedMain] ERROR o.s.b.SpringApplication - [reportFailure,837] - Application run failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.tcl.agp.server.web.ServerApplication]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'redisUtils' for bean class [com.tcl.rediscover.redis.core.util.RedisUtils] conflicts with existing, non-compatible bean definition of same name and class [com.tcl.agp.server.common.utils.redis.RedisUtils]
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:609)
at org.springframework.context.annotation.ConfigurationClassParser.access$800(ConfigurationClassParser.java:110)
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGroupingHandler.lambda$processGroupImports$1(ConfigurationClassParser.java:811)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'redisUtils' for bean class [com.tcl.rediscover.redis.core.util.RedisUtils] conflicts with existing, non-compatible bean definition of same name and class [com.tcl.agp.server.common.utils.redis.RedisUtils]
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:349)
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:287)
at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:132)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:295)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:249)
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:599)
... 25 common frames omitted
解决方案: 1.删除项目中的RedisUtils类,使用组件的工具类
2. @Component("redisUtilsCustomer")
public class RedisUtils
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。