Springboot整合AOP和redis的示例詳解
aop
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
開(kāi)啟自動(dòng)代理
注意:在完成了引入AOP依賴(lài)包后,一般來(lái)說(shuō)并不需要去做其他配置。使用過(guò)Spring注解配置方式的人會(huì)問(wèn)是否需要在程序主類(lèi)中增加@EnableAspectJAutoProxy來(lái)啟用,實(shí)際并不需要。
因?yàn)樵贏OP的默認(rèn)配置屬性中,spring.aop.auto屬性默認(rèn)是開(kāi)啟的,也就是說(shuō)只要引入了AOP依賴(lài)后,默認(rèn)已經(jīng)增加了@EnableAspectJAutoProxy。
日志切面
@Component @Aspect @Slf4j public class WebLogAspect { @Pointcut("(execution(public * com.zking.ssm.web..*.*(..))) || (execution(public * com.zking.ssm.controller..*.*(..)))") public void pointcut(){ ? } @Before("pointcut()") public void before(JoinPoint joinPoint){ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); if(attributes!=null){ HttpServletRequest request = attributes.getRequest(); log.info("請(qǐng)求地址URL:"+request.getRequestURL().toString()); log.info("請(qǐng)求方式HTTP_METHOD:"+request.getMethod()); log.info("客戶(hù)端地址IP:"+request.getRemoteAddr()); log.info("訪問(wèn)方法CLASS_METHOD:"+joinPoint.getSignature().getDeclaringTypeName()); log.info("訪問(wèn)方法中的參數(shù)ARGS:"+ Arrays.toString(joinPoint.getArgs())); } } }
格式:
- execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)
- 修飾符匹配(modifier-pattern?)
- 返回值匹配(ret-type-pattern)可以為*表示任何返回值,全路徑的類(lèi)名等
- 類(lèi)路徑匹配(declaring-type-pattern?)
- 方法名匹配(name-pattern)可以指定方法名 或者 代表所有, set 代表以set開(kāi)頭的所有方法
- 參數(shù)匹配((param-pattern))可以指定具體的參數(shù)類(lèi)型,多個(gè)參數(shù)間用“,”隔開(kāi),各個(gè)參數(shù)也可以用“”來(lái)表示匹配任意類(lèi)型的參數(shù),如(String)表示匹配一個(gè)String參數(shù)的方法;(,String) 表示匹配有兩個(gè)參數(shù)的方法,第一個(gè)參數(shù)可以是任意類(lèi)型,而第二個(gè)參數(shù)是String類(lèi)型;可以用(..)表示零個(gè)或多個(gè)任意參數(shù)
- 異常類(lèi)型匹配(throws-pattern?)
- 其中后面跟著“?”的是可選項(xiàng)
druid數(shù)據(jù)庫(kù)連接池
參考springboot03-mybatis.md
redis springboot整合redis pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!--lettuce依賴(lài)commons-pool2--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.8.0</version> </dependency>
yaml
spring: redis: host: 127.0.0.1 port: 6379 password: lettuce: pool: max-active: 8 #連接池最大連接數(shù)(使用負(fù)值表示沒(méi)有限制)默認(rèn)為8 max-wait: -1ms #連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制)默認(rèn)為-1 max-idle: 8 #連接池中的最大空閑連接 默認(rèn)為8 min-idle: 5 # 連接池中的最小空閑連接 默認(rèn)為0
自動(dòng)配置,參考:org.springframework.boot.autoconfigure.data.redis.RedisProperties
RedisConfig
@Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { // 配置redisTemplate RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); redisTemplate.setConnectionFactory(redisConnectionFactory); RedisSerializer stringSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringSerializer); // key序列化 redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // value序列化 redisTemplate.setHashKeySerializer(stringSerializer); // Hash key序列化 redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); // Hash value序列化 redisTemplate.afterPropertiesSet(); return redisTemplate; } ? }
RedisService
public interface RedisService { void setObj(String key, Object obj, long timeout); void setObj(String key, Object obj); Object getObj(String key); } @Service("redisService") public class RedisServiceImpl implements RedisService { @Resource private RedisTemplate redisTemplate; @Override public void setObj(final String key, Object obj, long timeout) { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.set(key, obj, timeout, TimeUnit.SECONDS); } ? @Override public void setObj(String key, Object obj) { setObj(key,obj,60*60*15); } ? @Override public Object getObj(final String key) { Object o = redisTemplate.opsForValue().get(key); return o; } ? }
使用
@Service public class CustomerServiceImpl implements CustomerService { @Autowired private CustomerMapper customerMapper; @Autowired private RedisService redisService; @Override public Customer selectByPrimaryKey(Integer id) { Customer customer = (Customer)redisService.getObj("springboot:ssm:customer:"+id); if(customer==null){ customer = customerMapper.selectByPrimaryKey(id); redisService.setObj("springboot:ssm:customer:"+id,customer); } return customer; } }
客戶(hù)端工具
在后面 springboot 整合 redis 的時(shí)候會(huì)用到連接池,所以這里先來(lái)介紹下 Redis中的連接池:
客戶(hù)端連接 Redis 使用的是 TCP協(xié)議,直連的方式每次需要建立 TCP連接,而連接池的方式是可以預(yù)先初始化好客戶(hù)端連接,所以每次只需要從 連接池借用即可,而借用和歸還操作是在本地進(jìn)行的,只有少量的并發(fā)同步開(kāi)銷(xiāo),遠(yuǎn)遠(yuǎn)小于新建TCP連接的開(kāi)銷(xiāo)。另外,直連的方式無(wú)法限制 redis客戶(hù)端對(duì)象的個(gè)數(shù),在極端情況下可能會(huì)造成連接泄漏,而連接池的形式可以有效的保護(hù)和控制資源的使用
下面以Jedis客戶(hù)端為例,再來(lái)總結(jié)下 客戶(hù)端直連方式和連接池方式的對(duì)比
優(yōu)點(diǎn) | 缺點(diǎn) | |
---|---|---|
直連 | 簡(jiǎn)單方便,適用于少量長(zhǎng)期連接的場(chǎng)景 | 1. 存在每次新建/關(guān)閉TCP連接開(kāi)銷(xiāo) 2. 資源無(wú)法控制,極端情況下出現(xiàn)連接泄漏 3. Jedis對(duì)象線程不安全(Lettuce對(duì)象是線程安全的) |
連接池 | 1. 無(wú)需每次連接生成Jedis對(duì)象,降低開(kāi)銷(xiāo) 2. 使用連接池的形式保護(hù)和控制資源的使用 | 相對(duì)于直連,使用更加麻煩,尤其在資源的管理上需要很多參數(shù)來(lái)保證,一旦規(guī)劃不合理也會(huì)出現(xiàn)問(wèn)題 |
Jedis vs Lettuce
Jedis 和 Lettuce 是 Java 操作 Redis 的客戶(hù)端。在 Spring Boot 1.x 版本默認(rèn)使用的是 jedis ,而在 Spring Boot 2.x 版本默認(rèn)使用的就是Lettuce。關(guān)于 Jedis 跟 Lettuce 的區(qū)別如下:
Jedis在實(shí)現(xiàn)上是直接連接的redis server,如果在多線程環(huán)境下是非線程安全的,這個(gè)時(shí)候只有使用連接池,為每個(gè)Jedis實(shí)例增加物理連接 Lettuce的連接是基于Netty的,連接實(shí)例(StatefulRedisConnection)可以在多個(gè)線程間并發(fā)訪問(wèn),應(yīng)為StatefulRedisConnection是線程安全的,所以一個(gè)連接實(shí)例(StatefulRedisConnection)就可以滿(mǎn)足多線程環(huán)境下的并發(fā)訪問(wèn),當(dāng)然這個(gè)也是可伸縮的設(shè)計(jì),一個(gè)連接實(shí)例不夠的情況也可以按需增加連接實(shí)例。
Lettuce
同4.1
jedis pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <exclusions> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency> ? <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
yml
spring: redis: host: 127.0.0.1 port: 6379 password: jedis: pool: max-active: 8 #連接池最大連接數(shù)(使用負(fù)值表示沒(méi)有限制)默認(rèn)為8 max-wait: -1ms #連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制)默認(rèn)為-1 max-idle: 8 #連接池中的最大空閑連接 默認(rèn)為8 min-idle: 5 # 連接池中的最小空閑連接 默認(rèn)為0
到此這篇關(guān)于Springboot整合AOP和redis的文章就介紹到這了,更多相關(guān)Springboot整合AOP和redis內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Springboot中使用Redisson+AOP+自定義注解實(shí)現(xiàn)訪問(wèn)限流與黑名單攔截
- SpringBoot整合redis+Aop防止重復(fù)提交的實(shí)現(xiàn)
- SpringBoot+Redis使用AOP防止重復(fù)提交的實(shí)現(xiàn)
- SpringBoot?使用AOP?+?Redis?防止表單重復(fù)提交的方法
- SpringBoot使用自定義注解+AOP+Redis實(shí)現(xiàn)接口限流的實(shí)例代碼
- SpringBoot?AOP?Redis實(shí)現(xiàn)延時(shí)雙刪功能實(shí)戰(zhàn)
- SpringBoot AOP控制Redis自動(dòng)緩存和更新的示例
- 淺談SpringBoot集成Redis實(shí)現(xiàn)緩存處理(Spring AOP實(shí)現(xiàn))
相關(guān)文章
并發(fā)編程之Java內(nèi)存模型volatile的內(nèi)存語(yǔ)義
這篇文章主要介紹了并發(fā)編程之Java內(nèi)存模型volatile的內(nèi)存語(yǔ)義,理解volatile特性的一個(gè)好辦法是把對(duì)volatile變量的單個(gè)讀/寫(xiě),看成是使用同一個(gè)鎖對(duì)單個(gè)讀/寫(xiě)操作做了同步。下面我們一起進(jìn)入文章看看具體例子吧,需要的小伙伴可以參考下2021-11-11Springboot集成Tika實(shí)現(xiàn)文檔解析
Tika是一款A(yù)pache開(kāi)源的,跨平臺(tái),支持多品種文本類(lèi)型的內(nèi)容檢測(cè)和提取工具,本文將介紹Springboot如何集成Tika實(shí)現(xiàn)文檔解析,需要的可以參考下2024-11-11Java如何實(shí)現(xiàn)http接口參數(shù)和返回值加密
這篇文章主要介紹了Java如何實(shí)現(xiàn)http接口參數(shù)和返回值加密問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11Springboot整合spring-boot-starter-data-elasticsearch的過(guò)程
本文詳細(xì)介紹了Springboot整合spring-boot-starter-data-elasticsearch的過(guò)程,包括版本要求、依賴(lài)添加、實(shí)體類(lèi)添加、索引的名稱(chēng)、分片、副本設(shè)置等,同時(shí),還介紹了如何使用ElasticsearchRepository類(lèi)進(jìn)行增刪改查操作2024-10-10IntelliJ?IDEA無(wú)公網(wǎng)遠(yuǎn)程Linux服務(wù)器環(huán)境開(kāi)發(fā)過(guò)程(推薦收藏)
下面介紹如何在IDEA中設(shè)置遠(yuǎn)程連接服務(wù)器開(kāi)發(fā)環(huán)境并結(jié)合Cpolar內(nèi)網(wǎng)穿透工具實(shí)現(xiàn)無(wú)公網(wǎng)遠(yuǎn)程連接,然后實(shí)現(xiàn)遠(yuǎn)程Linux環(huán)境進(jìn)行開(kāi)發(fā),感興趣的朋友跟隨小編一起看看吧2023-12-12JAVA實(shí)現(xiàn)redis分布式雙重加鎖的示例代碼
在高并發(fā)環(huán)境下,通過(guò)Redis分布式鎖實(shí)現(xiàn)數(shù)據(jù)唯一性校驗(yàn)非常關(guān)鍵,為避免用戶(hù)數(shù)據(jù)重復(fù),可使用Redis鎖或集合數(shù)據(jù)結(jié)構(gòu)進(jìn)行前置檢查,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-10-10Android中Parcelable的作用實(shí)例解析
這篇文章主要介紹了Android中Parcelable的作用,對(duì)于Android初學(xué)者有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2014-08-08Java實(shí)現(xiàn)兩個(gè)隨機(jī)數(shù)組合并進(jìn)行排序的方法
本文主要介紹了Java實(shí)現(xiàn)兩個(gè)隨機(jī)數(shù)組合并進(jìn)行排序的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09