簡體   English   中英

spring啟動微服務中的分布式redis緩存

[英]Distributed redis cache in spring boot micro services

我有兩個微服務。 項目微服務將數據填充到 redis 緩存中。 它是成功的,我也能夠在同一個微服務中檢索數據。 另一個微服務是訂單服務。 為了服務,我需要從 redis 緩存中獲取項目服務數據,因為我需要使用分布式緩存。 但是,我無法從 redis 緩存中訪問緩存數據以提供服務。

這是我的實現代碼

物品服務

RedisConfig.java

@Configuration
public class RedisConfig {

    /** The redis host name. */
    @Value("${spring.redis.host}")
    private transient String redisHostName;

    /** The redis port. */
    @Value("${spring.redis.port}")
    private transient int redisPort;

    /**
     * Jedis connection factory.
     *
     * @return the jedis connection factory
     */
    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        final RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(redisHostName,
                redisPort);
        return new JedisConnectionFactory(redisStandaloneConfiguration);
    }

    /**
     * Redis template.
     *
     * @return the redis template
     */
    @Bean
    public RedisTemplate<String, ItemEntity> redisTemplate() {
        final RedisTemplate<String, ItemEntity> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        return template;
    }
}

RedisRepoImpl.java

 /** The redis template. */
    private transient RedisTemplate<String, ItemEntity> redisTemplate;

    /** The hash operations. */
    private transient HashOperations<String, Long, ItemEntity> hashOperations;
    
    /** The Constant CACHE_KEY. */
    private static final String CACHE_KEY ="ITEM_MASTER";


    public RedisRepositoryImpl(final RedisTemplate<String, ItemEntity> redisTemplate) {
        this.redisTemplate = redisTemplate;
        this.hashOperations = redisTemplate.opsForHash();
    }


    @Override
    public void save(final ItemEntity item) {
        hashOperations.put(CACHE_KEY, item.getItemEntityId(), item);
    }

    /**
     * Find all.
     *
     * @return the map
     */
    @Override
    public List<ItemEntity> findAll() {
        final Map<Long, ItemEntity> itemEntities = hashOperations.entries(CACHE_KEY);
        return new ArrayList<>(itemEntities.values());
    } 

    //other code

訂單服務

RedisConfig.java

我在訂單服務中創建了 ItemEntity class 以及與項目服務相同的。

@Configuration
public class RedisConfig {
    
    /** The redis host name. */
    @Value("${spring.redis.host}")
    private transient String redisHostName;

    /** The redis port. */
    @Value("${spring.redis.port}")
    private transient int redisPort;

    /**
     * Jedis connection factory.
     *
     * @return the jedis connection factory
     */
    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        final RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(redisHostName,
                redisPort);
        return new JedisConnectionFactory(redisStandaloneConfiguration);
    }

    /**
     * Redis template.
     *
     * @return the redis template
     */
    @Bean
    public RedisTemplate<String, ItemEntity> redisTemplate() {
        final RedisTemplate<String, ItemEntity> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new GenericToStringSerializer<Long>(Long.class));
        template.setHashValueSerializer(new GenericToStringSerializer<ItemEntity>(ItemEntity.class));
        return template;
    }

RedisRepoImpl.java

@Repository
public class RedisRepositoryImpl implements RedisRepository {
    
    /** The Constant CACHE_KEY. */
    private static final String CACHE_KEY ="ITEM_MASTER";

    /** The hash operations. */
    private transient HashOperations<String, Long, ItemEntity> hashOperations;

    /**
     * Instantiates a new redis repository impl.
     *
     * @param redisTemplate the redis template
     */
    public RedisRepositoryImpl(final RedisTemplate<String, ItemEntity> redisTemplate) {
        this.hashOperations = redisTemplate.opsForHash();
    }

    /**
     * Find all.
     *
     * @return the map
     */
    @Override
    public List<ItemEntity> findAll() {
        final Map<Long, ItemEntity> itemEntities = hashOperations.entries(CACHE_KEY);
        return new ArrayList<>(itemEntities.values());
    }
}

我在訂單服務中將緩存作為空列表。 你能告訴我我在這里犯了什么錯誤嗎?

在瀏覽了更多在線資料后,我能夠解決這個問題。

我所做的額外更改是更新了兩個微服務中的 RedisTemplate bean,如下所示

    @Bean
    public RedisTemplate<String, ItemEntity> redisTemplate() {
        final RedisTemplate<String, ItemEntity> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        Jackson2JsonRedisSerializer<ItemEntity> values = new Jackson2JsonRedisSerializer<>(ItemEntity.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.registerModule(new JavaTimeModule());
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        values.setObjectMapper(objectMapper);
        template.setHashValueSerializer(values);
        return template;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM