月付购物网站建站,个人做排行网站,seo外链是什么,连锁店网站建设文章目录 前言Redission详细配置步骤pom依赖application.yaml配置类CacheConfigEnvironmentContext RedissionController单测 前言
本篇博客是SpringBoot整合Redission#xff0c;若文章中出现相关问题#xff0c;请指出#xff01;
所有博客文件目录索引#xff1a;博客… 文章目录 前言Redission详细配置步骤pom依赖application.yaml配置类CacheConfigEnvironmentContext RedissionController单测 前言
本篇博客是SpringBoot整合Redission若文章中出现相关问题请指出
所有博客文件目录索引博客目录索引(持续更新)
Redission集成到springboot是有两种场景的第一个场景是针对单台节点第二个场景是针对多台节点。
当前配置是单台节点
配套源码地址
giteehttps://gitee.com/changluJava/demo-exer/tree/master/SpringBoot/springboot-redissiongithubhttps://github.com/changluya/Java-Demos/tree/master/SpringBoot/springboot-redission
Redission详细配置步骤
。
pom依赖 dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.redisson/groupIdartifactIdredisson-spring-boot-starter/artifactIdversion3.17.7/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.13.2/versionscopetest/scope/dependency/dependenciesapplication.yaml
server:port: 8055
spring:redis:host: 127.0.0.1port: 6379database: 1password: 123456
# 直接配置参数
#redisson:
# codec: org.redisson.codec.JsonJacksonCodec
# threads: 4
# netty:
# threads: 4
# single-server-config:
# address: redis://localhost:6379
# password: 123456
# database: 0配置类
CacheConfig
package com.changlu.redission.config;import io.micrometer.core.instrument.util.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.redisson.config.TransportMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
public class CacheConfig {Autowiredprivate EnvironmentContext environmentContext;Beanpublic RedissonClient redissonClient(){Config config new Config();config.setTransportMode(TransportMode.NIO);String redisPassword getRedisPassword();int redisDB environmentContext.getRedisDB();// 单节点服务器SingleServerConfig singleServerConfig config.useSingleServer();singleServerConfig.setAddress(getRedissonAddress());singleServerConfig.setDatabase(redisDB);if (StringUtils.isNotBlank(redisPassword)) {singleServerConfig.setPassword(redisPassword);}return Redisson.create(config);}private String getRedissonAddress() {return redis:// environmentContext.getRedisUrl() : environmentContext.getRedisPort();}public String getRedisPassword() {String redisPassword;try {redisPassword environmentContext.getRedisPassword();} catch (Exception e) {redisPassword environmentContext.getRedisPassword();}return redisPassword;}}EnvironmentContext
package com.changlu.redission.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;Component
public class EnvironmentContext {Autowiredprivate Environment environment;public String getRedisSentinel() {return environment.getProperty(spring.redis.sentinel.nodes, );}public int getRedisDB() {return Integer.parseInt(environment.getProperty(spring.redis.database, 1));}public String getRedisUrl() {return environment.getProperty(spring.redis.host, 127.0.0.1);}public String getRedisPassword() {return environment.getProperty(spring.redis.password);}public int getRedisPort() {return Integer.parseInt(environment.getProperty(spring.redis.port, 6379));}
}RedissionController
package com.changlu.redission.controller;import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;RestController
RequestMapping(/redission)
public class RedissionController {Autowiredprivate RedissonClient redissonClient;GetMapping(/key/{key})public MapString, String redission(PathVariable(key)String key) {RLock rLock redissonClient.getLock(key);try {boolean lock rLock.tryLock(10, 20, TimeUnit.SECONDS);System.out.println(lock: lock);if (lock) {//业务Thread.sleep(1000 * 10);}} catch (Throwable e) {e.printStackTrace();} finally {if (rLock.isLocked() rLock.isHeldByCurrentThread()) {rLock.unlock();}System.out.println(解锁);}return new HashMap();}}单测
SpringBootTest(classes SpringbootRedissionApplication.class)
RunWith(SpringRunner.class)
public class TestApplication {AutowiredApplicationContext context;// redisson客户端AutowiredRedissonClient redissonClient;// 测试分布式锁Testpublic void terst1() throws InterruptedException {RLock lock redissonClient.getLock(anyLock);new Thread(() - {lock.lock();try {System.out.println(Thread.currentThread().getName() :\t 获得锁);Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();} finally {System.out.println(Thread.currentThread().getName() :\t 释放锁);lock.unlock();}}).start();new Thread(() - {lock.lock();try {System.out.println(Thread.currentThread().getName() :\t 获得锁);Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();} finally {System.out.println(Thread.currentThread().getName() :\t 释放锁);lock.unlock();}}).start();Thread.sleep(100000);}
}