- Scheduled jobs (nightly cleanup, moderation, search dictionary rebuild) take a ShedLock lock in Redis, so only one instance runs each; the moderation run stops before its lock can expire. - STOMP events (chat messages, unread counters, transcoding status) are published to the Redis channel ws:broadcast and every instance hands them to its local simple broker, so subscribers on other instances receive them. Falls back to local delivery if Redis is down; app.websocket.cluster-broadcast switches it off. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
72 lines
3.6 KiB
Java
72 lines
3.6 KiB
Java
package com.krylov.refound.config;
|
||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||
import com.krylov.refound.dto.CachedPostPage;
|
||
import com.krylov.refound.dto.MapMarkerDto;
|
||
import java.util.List;
|
||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||
import org.springframework.context.annotation.Bean;
|
||
import org.springframework.context.annotation.Configuration;
|
||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||
import org.springframework.data.redis.core.RedisTemplate;
|
||
import org.springframework.data.redis.listener.ChannelTopic;
|
||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||
|
||
@Configuration
|
||
public class RedisConfig {
|
||
|
||
@Bean
|
||
public RedisTemplate<String, CachedPostPage> postCacheRedisTemplate(RedisConnectionFactory connectionFactory, ObjectMapper objectMapper) {
|
||
return buildTemplate(connectionFactory, objectMapper, CachedPostPage.class);
|
||
}
|
||
|
||
private static <T> RedisTemplate<String, T> buildTemplate(RedisConnectionFactory connectionFactory, ObjectMapper objectMapper, Class<T> type) {
|
||
// Своя копия: изменения веб-настроек ObjectMapper не должны ломать формат кэша,
|
||
// а неизвестные поля в старых записях не должны валить десериализацию
|
||
ObjectMapper cacheMapper = objectMapper.copy().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||
|
||
StringRedisSerializer keySerializer = new StringRedisSerializer();
|
||
Jackson2JsonRedisSerializer<T> valueSerializer = new Jackson2JsonRedisSerializer<>(cacheMapper, type);
|
||
|
||
RedisTemplate<String, T> template = new RedisTemplate<>();
|
||
template.setConnectionFactory(connectionFactory);
|
||
template.setKeySerializer(keySerializer);
|
||
template.setValueSerializer(valueSerializer);
|
||
template.setHashKeySerializer(keySerializer);
|
||
template.setHashValueSerializer(valueSerializer);
|
||
return template;
|
||
}
|
||
|
||
@Bean
|
||
public RedisTemplate<String, List<MapMarkerDto>> mapRedisTemplate(RedisConnectionFactory connectionFactory, ObjectMapper objectMapper) {
|
||
|
||
ObjectMapper cacheMapper = objectMapper.copy()
|
||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||
|
||
StringRedisSerializer keySerializer = new StringRedisSerializer();
|
||
Jackson2JsonRedisSerializer<List<MapMarkerDto>> valueSerializer =
|
||
new Jackson2JsonRedisSerializer<>(cacheMapper,
|
||
objectMapper.getTypeFactory().constructCollectionType(List.class, MapMarkerDto.class));
|
||
|
||
RedisTemplate<String, List<MapMarkerDto>> template = new RedisTemplate<>();
|
||
template.setConnectionFactory(connectionFactory);
|
||
template.setKeySerializer(keySerializer);
|
||
template.setValueSerializer(valueSerializer);
|
||
return template;
|
||
}
|
||
|
||
/** Подписка на канал рассылки WebSocket-сообщений между экземплярами (см. ClusterMessaging). */
|
||
@Bean
|
||
@ConditionalOnProperty(name = "app.websocket.cluster-broadcast", havingValue = "true", matchIfMissing = true)
|
||
public RedisMessageListenerContainer wsBroadcastListenerContainer(RedisConnectionFactory connectionFactory,
|
||
ClusterMessaging clusterMessaging) {
|
||
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
||
container.setConnectionFactory(connectionFactory);
|
||
container.addMessageListener(clusterMessaging, new ChannelTopic(ClusterMessaging.CHANNEL));
|
||
return container;
|
||
}
|
||
}
|