Springboot整合AOP和redis的示例详解
作者:小熊哦呜
aop
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>开启自动代理
注意:在完成了引入AOP依赖包后,一般来说并不需要去做其他配置。使用过Spring注解配置方式的人会问是否需要在程序主类中增加@EnableAspectJAutoProxy来启用,实际并不需要。
因为在AOP的默认配置属性中,spring.aop.auto属性默认是开启的,也就是说只要引入了AOP依赖后,默认已经增加了@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("请求地址URL:"+request.getRequestURL().toString());
log.info("请求方式HTTP_METHOD:"+request.getMethod());
log.info("客户端地址IP:"+request.getRemoteAddr());
log.info("访问方法CLASS_METHOD:"+joinPoint.getSignature().getDeclaringTypeName());
log.info("访问方法中的参数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)可以为*表示任何返回值,全路径的类名等
- 类路径匹配(declaring-type-pattern?)
- 方法名匹配(name-pattern)可以指定方法名 或者 代表所有, set 代表以set开头的所有方法
- 参数匹配((param-pattern))可以指定具体的参数类型,多个参数间用“,”隔开,各个参数也可以用“”来表示匹配任意类型的参数,如(String)表示匹配一个String参数的方法;(,String) 表示匹配有两个参数的方法,第一个参数可以是任意类型,而第二个参数是String类型;可以用(..)表示零个或多个任意参数
- 异常类型匹配(throws-pattern?)
- 其中后面跟着“?”的是可选项
druid数据库连接池
参考springboot03-mybatis.md
redis springboot整合redis pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--lettuce依赖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 #连接池最大连接数(使用负值表示没有限制)默认为8
max-wait: -1ms #连接池最大阻塞等待时间(使用负值表示没有限制)默认为-1
max-idle: 8 #连接池中的最大空闲连接 默认为8
min-idle: 5 # 连接池中的最小空闲连接 默认为0自动配置,参考: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;
}
}客户端工具
在后面 springboot 整合 redis 的时候会用到连接池,所以这里先来介绍下 Redis中的连接池:
客户端连接 Redis 使用的是 TCP协议,直连的方式每次需要建立 TCP连接,而连接池的方式是可以预先初始化好客户端连接,所以每次只需要从 连接池借用即可,而借用和归还操作是在本地进行的,只有少量的并发同步开销,远远小于新建TCP连接的开销。另外,直连的方式无法限制 redis客户端对象的个数,在极端情况下可能会造成连接泄漏,而连接池的形式可以有效的保护和控制资源的使用
下面以Jedis客户端为例,再来总结下 客户端直连方式和连接池方式的对比
| 优点 | 缺点 | |
|---|---|---|
| 直连 | 简单方便,适用于少量长期连接的场景 | 1. 存在每次新建/关闭TCP连接开销 2. 资源无法控制,极端情况下出现连接泄漏 3. Jedis对象线程不安全(Lettuce对象是线程安全的) |
| 连接池 | 1. 无需每次连接生成Jedis对象,降低开销 2. 使用连接池的形式保护和控制资源的使用 | 相对于直连,使用更加麻烦,尤其在资源的管理上需要很多参数来保证,一旦规划不合理也会出现问题 |
Jedis vs Lettuce
Jedis 和 Lettuce 是 Java 操作 Redis 的客户端。在 Spring Boot 1.x 版本默认使用的是 jedis ,而在 Spring Boot 2.x 版本默认使用的就是Lettuce。关于 Jedis 跟 Lettuce 的区别如下:
Jedis在实现上是直接连接的redis server,如果在多线程环境下是非线程安全的,这个时候只有使用连接池,为每个Jedis实例增加物理连接 Lettuce的连接是基于Netty的,连接实例(StatefulRedisConnection)可以在多个线程间并发访问,应为StatefulRedisConnection是线程安全的,所以一个连接实例(StatefulRedisConnection)就可以满足多线程环境下的并发访问,当然这个也是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。
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 #连接池最大连接数(使用负值表示没有限制)默认为8
max-wait: -1ms #连接池最大阻塞等待时间(使用负值表示没有限制)默认为-1
max-idle: 8 #连接池中的最大空闲连接 默认为8
min-idle: 5 # 连接池中的最小空闲连接 默认为0到此这篇关于Springboot整合AOP和redis的文章就介绍到这了,更多相关Springboot整合AOP和redis内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
