springboot2.0 redis EnableCaching的配置和使用

一、前言

  关于EnableCaching最简单使用,个人感觉只需提供一个CacheManager的一个实例就好了。springboot为我们提供了cache相关的自动配置。引入cache模块,如下。

二、maven依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.integration</groupId><artifactId>spring-integration-redis</artifactId></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId></dependency>

 

三、缓存类型

   本人也仅仅使用了redis、guava、ehcache。更多详情请参考 spring cache官方文档。

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-caching.html

四、常用注解

  @Cacheable    触发缓存填充

  @CacheEvict    触发缓存驱逐

  @CachePut    更新缓存而不会干扰方法执行

  @Caching    重新组合要在方法上应用的多个缓存操作

  @CacheConfig    在类级别共享一些常见的缓存相关设置

五、Spring Cache提供的SpEL上下文数据

  下表直接摘自Spring官方文档:

名字
位置
描述
示例
methodName
root对象
当前被调用的方法名
#root.methodName
method
root对象
当前被调用的方法
#root.method.name
target
root对象
当前被调用的目标对象
#root.target
targetClass
root对象
当前被调用的目标对象类
#root.targetClass
args
root对象
当前被调用的方法的参数列表
#root.args[0]
caches
root对象
当前方法调用使用的缓存列表(如@Cacheable(value={"cache1", "cache2"})),则有两个cache
#root.caches[0].name
argument name
执行上下文
当前被调用的方法的参数,如findById(Long id),我们可以通过#id拿到参数
#user.id
result
执行上下文
方法执行后的返回值(仅当方法执行之后的判断有效,如‘unless’,'cache evict'的beforeInvocation=false)
#result

 六、RedisCacheManager配置

  基于jedis

@Configuration
@EnableCaching
public class RedisCacheConfig extends CachingConfigurerSupport {@Autowiredprivate RedisProperties redisProperties;@Beanpublic JedisConnectionFactory jedisConnectionFactory() {// 获取服务器数组(这里要相信自己的输入,所以没有考虑空指针问题)String[] serverArray = redisProperties.getClusterNodes().split(",");RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(Arrays.asList(serverArray));JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();// 最大空闲连接数, 默认8个jedisPoolConfig.setMaxIdle(100);// 最大连接数, 默认8个jedisPoolConfig.setMaxTotal(500);// 最小空闲连接数, 默认0jedisPoolConfig.setMinIdle(0);// 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间,// 默认-1jedisPoolConfig.setMaxWaitMillis(2000); // 设置2秒// 对拿到的connection进行validateObject校验jedisPoolConfig.setTestOnBorrow(true);return new JedisConnectionFactory(redisClusterConfiguration,jedisPoolConfig);}/** * 注入redis template * * @return */@Bean@Qualifier("redisTemplate")public RedisTemplate redisTemplate(JedisConnectionFactoryjedisConnectionFactory, Jackson2JsonRedisSerializer jackson2JsonRedisSerializer) {RedisTemplate template = new RedisTemplate();template.setConnectionFactory(jedisConnectionFactory);template.setKeySerializer(new JdkSerializationRedisSerializer());template.setValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();return template;}/** * redis cache manager * * @return */@Bean@Primarypublic RedisCacheManager redisCacheManager(JedisConnectionFactory jedisConnectionFactory, ObjectProvider<List<RedisCacheConfigurationProvider>> configurationProvider) {Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = Maps.newHashMap();List<RedisCacheConfigurationProvider> configurations= configurationProvider.getIfAvailable();if (!CollectionUtils.isEmpty(configurations)) {for (RedisCacheConfigurationProvider configuration : configurations) {redisCacheConfigurationMap.putAll(configuration.resolve());}}RedisCacheManager cacheManager = RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory).cacheDefaults(resovleRedisCacheConfiguration(Duration.ofSeconds(300), JacksonHelper.genJavaType(Object.class))).withInitialCacheConfigurations(redisCacheConfigurationMap).build();return cacheManager;}private static RedisCacheConfiguration resovleRedisCacheConfiguration(Duration duration, JavaType javaType) {return RedisCacheConfiguration.defaultCacheConfig().serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new Jackson2JsonRedisSerializer<>(javaType))).entryTtl(duration);}/** * 配置一个序列器, 将对象序列化为字符串存储, 和将对象反序列化为对象 */@Beanpublic Jackson2JsonRedisSerializer jackson2JsonRedisSerializer() {Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);return jackson2JsonRedisSerializer;}public static abstract class RedisCacheConfigurationProvider {// key = 缓存名称, value = 缓存时间 和 缓存类型protected Map<String, Pair<Duration, JavaType>> configs;protected abstract void initConfigs();public Map<String, RedisCacheConfiguration> resolve() {initConfigs();Assert.notEmpty(configs, "RedisCacheConfigurationProvider 配置不能为空...");Map<String, RedisCacheConfiguration> result = Maps.newHashMap();configs.forEach((cacheName, pair) -> result.put(cacheName, resovleRedisCacheConfiguration(pair.getKey(), pair.getValue())));return result;}}}

  基于Lettuce

@Configuration
@EnableCaching
public class RedisCacheConfig extends CachingConfigurerSupport {@Autowiredprivate RedisProperties redisProperties;@Beanpublic LettuceConnectionFactory lettuceConnectionFactory() {String[] serverArray = redisProperties.getClusterNodes().split(",");// 获取服务器数组(这里要相信自己的输入,所以没有考虑空指针问题)RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(Arrays.asList(serverArray));GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();// 最大空闲连接数, 默认8个poolConfig.setMaxIdle(100);// 最大连接数, 默认8个poolConfig.setMaxTotal(500);// 最小空闲连接数, 默认0poolConfig.setMinIdle(0);LettuceClientConfiguration lettuceClientConfiguration = LettucePoolingClientConfiguration.builder().commandTimeout(Duration.ofSeconds(15)).poolConfig(poolConfig).shutdownTimeout(Duration.ZERO).build();return new LettuceConnectionFactory(redisClusterConfiguration, lettuceClientConfiguration);}/**     * 注入redis template     *     * @return     */@Bean@Qualifier("redisTemplate")public RedisTemplate redisTemplate(LettuceConnectionFactory lettuceConnectionFactory, Jackson2JsonRedisSerializer jackson2JsonRedisSerializer) {RedisTemplate template = new RedisTemplate();template.setConnectionFactory(lettuceConnectionFactory);template.setKeySerializer(new JdkSerializationRedisSerializer());template.setValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();return template;}/**     * redis cache manager     *     * @return     */@Bean@Primarypublic RedisCacheManager redisCacheManager(LettuceConnectionFactory lettuceConnectionFactory, ObjectProvider<List<RedisCacheConfigurationProvider>> configurationProvider) {Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = Maps.newHashMap();List<RedisCacheConfigurationProvider> configurations = configurationProvider.getIfAvailable();if (!CollectionUtils.isEmpty(configurations)) {for (RedisCacheConfigurationProvider configuration : configurations) {redisCacheConfigurationMap.putAll(configuration.resolve());}}RedisCacheManager cacheManager = RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(lettuceConnectionFactory).cacheDefaults(resovleRedisCacheConfiguration(Duration.ofSeconds(300), JacksonHelper.genJavaType(Object.class))).withInitialCacheConfigurations(redisCacheConfigurationMap).build();return cacheManager;}private static RedisCacheConfiguration resovleRedisCacheConfiguration(Duration duration, JavaType javaType) {return RedisCacheConfiguration.defaultCacheConfig().serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new Jackson2JsonRedisSerializer<>(javaType))).entryTtl(duration);}@Beanpublic RedisLockRegistry redisLockRegistry(LettuceConnectionFactory lettuceConnectionFactory) {return new RedisLockRegistry(lettuceConnectionFactory, "recharge-plateform", 60000 * 20);}/**     * 配置一个序列器, 将对象序列化为字符串存储, 和将对象反序列化为对象     */@Beanpublic Jackson2JsonRedisSerializer jackson2JsonRedisSerializer() {Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);return jackson2JsonRedisSerializer;}public static abstract class RedisCacheConfigurationProvider {// key = 缓存名称, value = 缓存时间 和 缓存类型protected Map<String, Pair<Duration, JavaType>> configs;protected abstract void initConfigs();public Map<String, RedisCacheConfiguration> resolve() {initConfigs();Assert.notEmpty(configs, "RedisCacheConfigurationProvider 配置不能为空...");Map<String, RedisCacheConfiguration> result = Maps.newHashMap();configs.forEach((cacheName, pair) -> result.put(cacheName, resovleRedisCacheConfiguration(pair.getKey(), pair.getValue())));return result;}}}

  Jedis和Lettuce比较

  Jedis 是直连模式,在多个线程间共享一个 Jedis 实例时是线程不安全的,如果想要在多线程环境下使用 Jedis,需要使用连接池,

  每个线程都去拿自己的 Jedis 实例,当连接数量增多时,物理连接成本就较高了。

 

  Lettuce的连接是基于Netty的,连接实例可以在多个线程间共享,

  所以,一个多线程的应用可以使用同一个连接实例,而不用担心并发线程的数量。当然这个也是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。通过异步的方式可以让我们更好的利用系统资源,而不用浪费线程等待网络或磁盘I/O。

  

  只在基于Lettuce的配置中,加入了RedisLockRegistry对应bean的配置,由于在集群的模式下,基于Jedis的配置下,通过RedisLockRegistry 获取分布式锁的时候报错:

EvalSha is not supported in cluster environment

  具体的解决方案就是切换至基于Lettuce的配置,请参考

https://stackoverflow.com/questions/47092475/spring-boot-redistemplate-execute-sc

  

  RedisCacheConfigurationProvider 作用为不同的cache提供特定的缓存时间以及key、value序列化和反序列化的方式。具体使用方式如下。

@Componentpublic class CouponRedisCacheConfigurationProvider extends RedisCacheConfig.RedisCacheConfigurationProvider {@Overrideprotected void initConfigs() {this.configs = Maps.newHashMap();this.configs.put(CouponConstants.COUPON_ALL_CACHE, new Pair<>(Duration.ofHours(1), JacksonHelper.genMapType(HashMap.class, Integer.class, Coupon.class)));this.configs.put(CouponConstants.COUPON_GOOD_CACHE, new Pair<>(Duration.ofHours(1), JacksonHelper.genCollectionType(List.class, String.class)));this.configs.put(CouponConstants.COUPON_HANDLE_TELEPHONE_STATUS_CACHE, new Pair<>(Duration.ofMinutes(10), JacksonHelper.genCollectionType(List.class, CouponHandle.class)));this.configs.put(CouponConstants.COUPON_HANDLE_TELEPHONE_GOOD_CACHE, new Pair<>(Duration.ofMinutes(10), JacksonHelper.genCollectionType(List.class, CouponHandle.class)));}}

  JacksonHelper 作用是根据不同的类型的对象获取对应的JavaType对象,在构造RedisTempalte序列化和反序列化器Jackson2JsonRedisSerializer对象需要。具体代码如下。

public class JacksonHelper {private static Logger LOGGER = LoggerFactory.getLogger(JacksonHelper.class);private static final SimpleModule module = initModule();private static final ObjectMapper objectMapper;private static final ObjectMapper prettyMapper;public JacksonHelper() {}private static SimpleModule initModule() {return (new SimpleModule()).addSerializer(BigDecimal.class, new BigDecimalSerializer()).addSerializer(LocalTime.class, new LocalTimeSerializer()).addDeserializer(LocalTime.class, new LocalTimeDeserializer()).addSerializer(LocalDate.class, new LocalDateSerializer()).addDeserializer(LocalDate.class, new LocalDateDeserializer()).addSerializer(LocalDateTime.class, new LocalDateTimeSerializer()).addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer()).addSerializer(Date.class, new DateSerializer()).addDeserializer(Date.class, new DateDeserializer());}public static JavaType genJavaType(TypeReference<?> typeReference) {return getObjectMapper().getTypeFactory().constructType(typeReference.getType());}public static JavaType genJavaType(Class<?> clazz) {return getObjectMapper().getTypeFactory().constructType(clazz);}public static JavaType genCollectionType(Class<? extends Collection> collectionClazz, Class<?> javaClazz) {return getObjectMapper().getTypeFactory().constructCollectionType(collectionClazz, javaClazz);}public static JavaType genMapType(Class<? extends Map> mapClazz, Class<?> keyClass, Class<?> valueClazz) {return getObjectMapper().getTypeFactory().constructMapType(mapClazz, keyClass, valueClazz);}public static ObjectMapper getObjectMapper() {return objectMapper;}public static ObjectMapper getPrettyMapper() {return prettyMapper;}static {objectMapper = (new ObjectMapper()).registerModule(module).configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true).configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);prettyMapper = objectMapper.copy().configure(SerializationFeature.INDENT_OUTPUT, true);}}class LocalDateDeserializer extends JsonDeserializer<LocalDate> {public LocalDateDeserializer() {}public LocalDate deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {String dateString = ((JsonNode) jp.getCodec().readTree(jp)).asText();return LocalDate.parse(dateString, DateTimeFormatter.ISO_LOCAL_DATE);}}class DateDeserializer extends JsonDeserializer<Date> {public DateDeserializer() {}public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {String dateTimeStr = ((JsonNode) jp.getCodec().readTree(jp)).asText();SimpleDateFormat sdf = new SimpleDateFormat(CouponConstants.DATE_TIME_FORMATER);ParsePosition pos = new ParsePosition(0);return sdf.parse(dateTimeStr, pos);}}class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {public LocalDateTimeDeserializer() {}public LocalDateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {String dateTimeStr = ((JsonNode) jp.getCodec().readTree(jp)).asText();return LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ISO_LOCAL_DATE_TIME);}}class LocalTimeDeserializer extends JsonDeserializer<LocalTime> {public LocalTimeDeserializer() {}public LocalTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {String dateString = ((JsonNode) jp.getCodec().readTree(jp)).asText();return LocalTime.parse(dateString, DateTimeFormatter.ISO_LOCAL_TIME);}}class BigDecimalSerializer extends JsonSerializer<BigDecimal> {public BigDecimalSerializer() {}public void serialize(BigDecimal value, JsonGenerator jgen, SerializerProvider provider) throws IOException {jgen.writeString(value.toString());}}class LocalDateSerializer extends JsonSerializer<LocalDate> {public LocalDateSerializer() {}public void serialize(LocalDate value, JsonGenerator jgen, SerializerProvider provider) throws IOException {jgen.writeString(DateTimeFormatter.ISO_LOCAL_DATE.format(value));}}class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {public LocalDateTimeSerializer() {}public void serialize(LocalDateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException {jgen.writeString(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(value));}}class DateSerializer extends JsonSerializer<Date> {public DateSerializer() {}public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException {SimpleDateFormat sdf = new SimpleDateFormat(CouponConstants.DATE_TIME_FORMATER);jgen.writeString(sdf.format(value));}}class LocalTimeSerializer extends JsonSerializer<LocalTime> {public LocalTimeSerializer() {}public void serialize(LocalTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException {jgen.writeString(DateTimeFormatter.ISO_LOCAL_TIME.format(value));}}

  业务代码

@Component
public class ServiceImpl {  @Override@CacheEvict(cacheNames = CouponConstants.COUPON_HANDLE_TELEPHONE_STATUS_CACHE, key = "#telephone+'#'+#status", beforeInvocation = true)public void evictCouponHandles(String telephone, Integer status) {}@Override@Cacheable(cacheNames = CouponConstants.COUPON_HANDLE_TELEPHONE_STATUS_CACHE, key = "#telephone+'#'+#status", sync = true)public List<CouponHandle> searchCouponHandles(String telephone, Integer status) {}}

  不同的缓存对应不同的存储类型,不同的存储类型对应着不同的序列化和反序列化器,这就保证了再调用注有@Cacheable注解的代码时获取到的对象不会发生类型转换错误。关于设置不同的cache下过期时间以及序列化和反序列器,请参考下面更直接明了的例子。

@Configurationpublic class RedisCacheConfig {@Beanpublic KeyGenerator simpleKeyGenerator() {return (o, method, objects) -> {StringBuilder stringBuilder = new StringBuilder();stringBuilder.append(o.getClass().getSimpleName());stringBuilder.append(".");stringBuilder.append(method.getName());stringBuilder.append("[");for (Object obj : objects) {stringBuilder.append(obj.toString());}stringBuilder.append("]");return stringBuilder.toString();};}@Beanpublic CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {return new RedisCacheManager(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),this.getRedisCacheConfigurationWithTtl(600), // 默认策略,未配置的 cache 会使用这个this.getRedisCacheConfigurationMap() // 指定 cache 策略
        );}private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();redisCacheConfigurationMap.put("UserInfoList", this.getRedisCacheConfigurationWithTtl(3000));redisCacheConfigurationMap.put("UserInfoListAnother", this.getRedisCacheConfigurationWithTtl(18000));return redisCacheConfigurationMap;}private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)).entryTtl(Duration.ofSeconds(seconds));return redisCacheConfiguration;}}
@Cacheable(value = "UserInfoList", keyGenerator = "simpleKeyGenerator") // 3000秒
@Cacheable(value = "UserInfoListAnother", keyGenerator = "simpleKeyGenerator") // 18000秒
@Cacheable(value = "DefaultKeyTest", keyGenerator = "simpleKeyGenerator") // 600秒,未指定的cache,使用默认策略

 

转载于:https://www.cnblogs.com/hujunzheng/p/9660681.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/531222.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

依赖配置中心实现注有@ConfigurationProperties的bean相关属性刷新

配置中心是什么 配置中心&#xff0c;通过keyvalue的形式存储环境变量。配置中心的属性做了修改&#xff0c;项目中可以通过配置中心的依赖&#xff08;sdk&#xff09;立即感知到。需要做的就是如何在属性发生变化时&#xff0c;改变带有ConfigurationProperties的bean的相关属…

java接口签名(Signature)实现方案

预祝大家国庆节快乐&#xff0c;赶快迎接美丽而快乐的假期吧&#xff01;&#xff01;&#xff01; 前言 在为第三方系统提供接口的时候&#xff0c;肯定要考虑接口数据的安全问题&#xff0c;比如数据是否被篡改&#xff0c;数据是否已经过时&#xff0c;数据是否可以重复提交…

Git rebase命令实战

一、前言 一句话&#xff0c;git rebase 可以帮助项目中的提交历史干净整洁&#xff01;&#xff01;&#xff01; 二、避免合并出现分叉现象 git merge操作 1、新建一个 develop 分支 2、在develop分支上新建两个文件 3、然后分别执行 add、commit、push 4、接着切换到master分…

windows系统nexus3安装和配置

一、前言 为什么要在本地开发机器上安装nexus&#xff1f;首先声明公司内部是有自己的nexus仓库&#xff0c;但是对上传jar包做了限制&#xff0c;不能畅快的上传自己测试包依赖。于是就自己在本地搭建了一个nexus私服&#xff0c;即可以使用公司nexus私服仓库中的依赖&#xf…

Springmvc借助SimpleUrlHandlerMapping实现接口开关功能

一、接口开关功能 1、可配置化&#xff0c;依赖配置中心 2、接口访问权限可控 3、springmvc不会扫描到&#xff0c;即不会直接的将接口暴露出去 二、接口开关使用场景 和业务没什么关系&#xff0c;主要方便查询系统中的一些状态信息。比如系统的配置信息&#xff0c;中间件的状…

log4j平稳升级到log4j2

一、前言 公司中的项目虽然已经用了很多的新技术了&#xff0c;但是日志的底层框架还是log4j&#xff0c;个人还是不喜欢用这个的。最近项目再生产环境上由于log4j引起了一场血案&#xff0c;于是决定升级到log4j2。 二、现象 虽然生产环境有多个结点分散高并发带来的压力&…

Springboot集成ES启动报错

报错内容 None of the configured nodes are available elasticsearch.yml配置 cluster.name: ftest node.name: node-72 node.master: true node.data: true network.host: 112.122.245.212 http.port: 39200 transport.tcp.port: 39300 discovery.zen.ping.unicast.hosts: [&…

kafka-manager配置和使用

kafka-manager配置 最主要配置就是用于kafka管理器状态的zookeeper主机。这可以在conf目录中的application.conf文件中找到。 kafka-manager.zkhosts"my.zookeeper.host.com:2181" 当然也可以声明为zookeeper集群。 kafka-manager.zkhosts"my.zookeeper.host.co…

kafka告警简单方案

一、前言 为什么要设计kafka告警方案&#xff1f;现成的监控项目百度一下一大堆&#xff0c;KafkaOffsetMonitor、KafkaManager、 Burrow等&#xff0c;具体参考&#xff1a;kafka的消息挤压监控。由于本小组的项目使用的kafka集群并没有被公司的kafka-manager管理&#xff0c;…

RedisCacheManager设置Value序列化器技巧

CacheManager基本配置 请参考博文&#xff1a;springboot2.0 redis EnableCaching的配置和使用 RedisCacheManager构造函数 /*** Construct a {link RedisCacheManager}.* * param redisOperations*/ SuppressWarnings("rawtypes") public RedisCacheManager(RedisOp…

HashMap 源码阅读

前言 之前读过一些类的源码&#xff0c;近来发现都忘了&#xff0c;再读一遍整理记录一下。这次读的是 JDK 11 的代码&#xff0c;贴上来的源码会去掉大部分的注释, 也会加上一些自己的理解。 Map 接口 这里提一下 Map 接口与1.8相比 Map接口又新增了几个方法&#xff1a;   …

SpringMvc接口中转设计(策略+模板方法)

一、前言 最近带着两个兄弟做支付宝小程序后端相关的开发&#xff0c;小程序首页涉及到很多查询的服务。小程序后端服务在我司属于互联网域&#xff0c;相关的查询服务已经在核心域存在了&#xff0c;查询这块所要做的工作就是做接口中转。参考了微信小程序的代码&#xff0c;发…

SpringSecurity整合JWT

一、前言 最近负责支付宝小程序后端项目设计&#xff0c;这里主要分享一下用户会话、接口鉴权的设计。参考过微信小程序后端的设计&#xff0c;会话需要依靠redis。相关的开发人员和我说依靠Redis并不是很靠谱&#xff0c;redis在业务高峰期不稳定&#xff0c;容易出现问题&…

Springboot定时任务原理及如何动态创建定时任务

一、前言 上周工作遇到了一个需求&#xff0c;同步多个省份销号数据&#xff0c;解绑微信粉丝。分省定时将销号数据放到SFTP服务器上&#xff0c;我需要开发定时任务去解析文件。因为是多省份&#xff0c;服务器、文件名规则、数据规则都不一定&#xff0c;所以要做成可配置是有…

转载:ThreadPoolExecutor 源码阅读

前言 之前研究了一下如何使用ScheduledThreadPoolExecutor动态创建定时任务(Springboot定时任务原理及如何动态创建定时任务)&#xff0c;简单了解了ScheduledThreadPoolExecutor相关源码。今天看了同学写的ThreadPoolExecutor 的源码解读&#xff0c;甚是NB&#xff0c;必须转…

使用pdfBox实现pdf转图片,解决中文方块乱码等问题

一、引入依赖 <dependency><groupId>org.apache.pdfbox</groupId><artifactId>fontbox</artifactId><version>2.0.13</version> </dependency> <dependency><groupId>org.apache.pdfbox</groupId><artif…

Spring异步调用原理及SpringAop拦截器链原理

一、Spring异步调用底层原理 开启异步调用只需一个注解EnableAsync Target(ElementType.TYPE) Retention(RetentionPolicy.RUNTIME) Documented Import(AsyncConfigurationSelector.class) public interface EnableAsync {/*** Indicate the async annotation type to be detec…

Spring MVC源码——Root WebApplicationContext

Spring MVC源码——Root WebApplicationContext 打算开始读一些框架的源码,先拿 Spring MVC 练练手,欢迎点击这里访问我的源码注释, SpringMVC官方文档一开始就给出了这样的两段示例: WebApplicationInitializer示例: public class MyWebApplicationInitializer implements Web…

Spring MVC源码——Servlet WebApplicationContext

上一篇笔记(Spring MVC源码——Root WebApplicationContext)中记录了下 Root WebApplicationContext 的初始化代码.这一篇来看 Servlet WebApplicationContext 的初始化代码 DispatcherServlet 是另一个需要在 web.xml 中配置的类, Servlet WebApplicationContext 就由它来创建…

Springboot源码——应用程序上下文分析

前两篇(Spring MVC源码——Root WebApplicationContext 和 Spring MVC源码——Servlet WebApplicationContext)讲述了springmvc项目创建上下文的过程&#xff0c;这一篇带大家了解一下springboot项目创建上下文的过程。 SpringApplication引导类 SpringApplication类用于启动或…