所需E币: 0
时间: 2023-12-14 10:32
大小: 2.41KB
今天给大家讲讲关于多级网关与多级缓存架构的相关知识,在文章里面,我将从0到1带着大家构建基础服务接口,通过层层递进优化服务,使得服务具备多级缓存的特性,并融合OpenResty拓展一个强大的多级网关+多级缓存的技术架构。以下就是代码实战展示:引入springboot3的maven依赖,本质上作为pom引入,直接管理他的版本号,后续用到啥组件直接拿来即用:<dependencies> <!--引入SpringBoot依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency></dependencies>dependencyManagement依赖管理全代码依赖配置如下:<!-- 使用dependencyManagement的目的是为了保证当前父工程的干净, 也就是说父工程他只负责对依赖(坐标)的管理,以及依赖的版本管理,而不会去引入额外的jar依赖 如此一来,父工程的职责就相当的单一了,而且也符合面向对象的理念,是一种父子一来继承的关系 依赖的导入只有在各自的子工程中才会导入。--><dependencyManagement> <dependencies> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> <!--持久层mybatis--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.0</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.4.1</version> </dependency> <!--jackson--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.14.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.14.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.14.2</version> </dependency> <!--apache工具类--> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.15</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.12.0</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.11.0</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency> <!--google工具类--> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>28.2-jre</version> </dependency> </dependencies></dependencyManagement>使用zaddzset10value120value230value3:设置member和对应的分数zrangezset0-1:查看所有zset中的内容zrangezset0-1withscores:带有分数zrankzsetvalue:获得对应的下标zscorezsetvalue:获得对应的分数zcardzset:统计个数zcountzset分数1分数2:统计个数zrangebyscorezset分数1分数2:查询分数之间的member(包含分数1分数2)zrangebyscorezset(分数1(分数2:查询分数之间的member(不包含分数1和分数2)zrangebyscorezset分数1分数2limitstartend:查询分数之间的member(包含分数1分数2),获得的结果集再次根据下标区间做查询zremzsetvalue:删除member在common中引入的坐标依赖<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId></dependency><dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId></dependency><dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId></dependency><dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId></dependency><dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId></dependency>迭代查询代码,一级缓存与二级缓存结合:@GetMapping("query")publicObjectquery(Stringid){ StringarticleKey="article:"+id; StringarticleKeyRedis="REDIS_ARTICLE:"+id; Articlearticle=cache.get(articleKey,s->{ System.out.println("文章id为"+id+"的没有查询到,则从Redis中查询后返回..."); ArticlearticleReal=null; StringarticleJsonStr=redis.get(articleKeyRedis); //判断从redis中查询到的文章数据是否为空 if(StringUtils.isBlank(articleJsonStr)){ System.out.println("Redis中不存在该文章,将从数据库中查询..."); //如果为空,则进入本条件,则从数据库中查询数据 articleReal=articleService.queryArticleDetail(id); //手动把文章数据设置到redis中,后续再次查询则有值 StringarticleJson=JsonUtils.objectToJson(articleReal); redis.set(articleKeyRedis,articleJson); }else{ System.out.println("Redis中存在该文章,将直接返回..."); //如果不为空,则直接转换json类型article再返回即可 articleReal=JsonUtils.jsonToPojo(articleJsonStr,Article.class); } returnarticleReal; }); returnarticle;}本文到此结束,感谢大家的阅读!