当前位置: 首页 > news >正文

淄博论坛网站建设网站注册查询

淄博论坛网站建设,网站注册查询,点击网站排名,网站公司查询文章目录 前言Redission详细配置步骤pom依赖application.yaml配置类CacheConfigEnvironmentContext RedissionController单测 前言 本篇博客是SpringBoot整合Redission,若文章中出现相关问题,请指出! 所有博客文件目录索引:博客…

文章目录

  • 前言
  • Redission详细配置步骤
    • pom依赖
    • application.yaml
    • 配置类
      • CacheConfig
      • EnvironmentContext
    • RedissionController
    • 单测

最新消息资讯热点围观公众号首图

前言

本篇博客是SpringBoot整合Redission,若文章中出现相关问题,请指出!

所有博客文件目录索引:博客目录索引(持续更新)

Redission集成到springboot是有两种场景的,第一个场景是针对单台节点,第二个场景是针对多台节点。

当前配置是单台节点

配套源码地址:

  • gitee:https://gitee.com/changluJava/demo-exer/tree/master/SpringBoot/springboot-redission
  • github:https://github.com/changluya/Java-Demos/tree/master/SpringBoot/springboot-redission

Redission详细配置步骤

image-20241218170628525

pom依赖

    <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.redisson</groupId><artifactId>redisson-spring-boot-starter</artifactId><version>3.17.7</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency></dependencies>

application.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 Map<String, 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);}
}
http://www.hkea.cn/news/522955/

相关文章:

  • 360极速怎么屏蔽网站新闻热点大事件
  • 购物app开发价格表站长工具seo排名
  • 微餐饮网站建设营销型网站建设方案
  • 高端网站建设公司好不好2020国内搜索引擎排行榜
  • 网站建设服务公司选哪家比较好?苏州优化收费
  • 中国建设银行河南省分行网站推广信息哪个平台好
  • 网站建设官网免费模板杭州seo优化
  • 绍兴网站建设谷歌搜索引擎在线
  • 网站的会员认证怎么做黑龙江新闻头条最新消息
  • 做网站如何分工百度推广登录平台客服
  • 网站建设如何提案万网域名注册信息查询
  • 创意二维码制作网站企业网络营销推广案例
  • 论坛型网站怎么做百度高级检索入口
  • 做百度移动网站排搜素引擎优化
  • 公司创建一个网站需要多少钱想做百度推广找谁
  • 做文献ppt模板下载网站有哪些常德政府网站
  • 青岛网站建设公司排行外链工具在线
  • 网站怎么做显得简洁美观seo数据是什么意思
  • 阿里巴巴开通诚信通后网站怎么做网络优化网站
  • 东莞手机网站价格便宜个人免费建站软件
  • 电子商务网站建设的步骤一般为百度100%秒收录
  • 做企业网站怎么样免费的推广软件下载
  • 拓普网站建设美国搜索引擎
  • 网站开发者工资冯耀宗seo视频教程
  • 软件开发各阶段工作量比例搜索引擎优化的基础是什么
  • 网站怎么做才能将名声打响云搜索app
  • 南阳做网站优化哪家好一级域名生成二级域名
  • 3322动态域名官网郑州seo联系搜点网络效果好
  • 网络营销渠道的类型河北seo基础教程
  • 做微信网站多少钱seo内部优化包括哪些内容