栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot项目进行Redis配置

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot项目进行Redis配置

NoSQL

Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 与范围查询, bitmaps, hyperloglogs 和 地理空间(geospatial) 索引半径查询。 Redis 内置了 复制(replication),LUA脚本(Lua scripting), LRU驱动事件(LRU eviction),事务(transactions) 和不同级别的 磁盘持久化(persistence), 并通过 Redis哨兵(Sentinel)和自动 分区(Cluster)提供高可用性(high availability)。

1.1、Redis自动配置
        
            org.springframework.boot
            spring-boot-starter-data-redis
        

  • RedisAutoConfiguration 自动配置类;RedisProperties 属性类;spring.redis.xxx :对redis配置
  • @Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class}):LettuceConnectionConfiguration.class 导入客户端类型 LettuceConnectionFactory 连接工厂也是配置好的;
    JedisConnectionConfiguration.class 导入客户端类型 JedisConnectionFactory 连接工厂也是配置好的;
  • 容器自动注入 RedisTemplate
  • 容器自动注入 StringRedisTemplate,key:value,二者都是 String类型
  • 允许key:value 二者都是object类型
  • 在这底层中只有我们使用StringRedisTemplate、RedisTemplate就可以操作redis

redis环境搭建

1、阿里云按量付费redis。经典网络

2、申请redis的公网连接地址

3、白名单设置,127.0.0.1仅仅本机访问,修改白名单允许0.0.0.0/0 允许所有地址访问

1.2、RedisTemplate与Lettuce
@Test
void testRedis(){
    ValueOperations operations = stringRedisTemplate.opsForValue();
    operations.set("hello","world");
    String hello = operations.get("hello");
    System.out.println("hello:"+hello);
}
1.3、切换至jedis
    	
        	org.springframework.boot
        	spring-boot-starter-data-redis
    	
            
        
            redis.clients
            jedis
        
#      redis配置
spring:
  redis:
    host: r-7xvnzvfphwycfwzzi7pd.redis.rds.aliyuncs.com #公网地址
    port: 6379 #端口号
    password: admin:admin  #账号:密码
    client-type: jedis #切换客户机为 jedis 或者 lettuce
    jedis:
      pool:
        max-active: 10 #最大活动数
        min-idle: 5 #最小空闲

实例:

@Component
public class RedisUrlCountInterceptor implements HandlerInterceptor {

    @Autowired
    StringRedisTemplate redisTemplate;

    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        //默认每次访问,当前requestURI就会计数加1
        redisTemplate.opsForValue().increment(requestURI);
        return true;
    }
}

//@EnableWebMvc
@Configuration
public class AdminConfig implements WebMvcConfigurer {

    
    @Autowired
    RedisUrlCountInterceptor redisUrlCountInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册一个自定义拦截器
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("
@Slf4j
@Controller
public class IndexController {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    
    @GetMapping({"/main.html"})
    public String mainPage(HttpSession session,Model model){
        log.info("当前方法是:"+"mainPage");
        // 是否登录,使用拦截器、过滤器,若是做了拦截器,可以注释掉
//        Object loginUser = session.getAttribute("loginUser");
//        if (loginUser != null) {
//            return "main";
//        }else {
//            model.addAttribute("msg","请重新登录");
//            //回到登录页
//            return "login";
//        }
        ValueOperations opsForValue = stringRedisTemplate.opsForValue();
        String s = opsForValue.get("/main.html");
        String s1 = opsForValue.get("/dynamic_table");
        model.addAttribute("mainCount",s);
        model.addAttribute("dynamicCount",s1);
        return "main";
    }
}
[[${mainCount}]]
/main.html
[[${dynamicCount}]]
/dynamicCount.html
[[${mainCount}]]
/main.html
[[${dynamicCount}]]
/dynamicCount.html
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/963521.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号