天天看點

查詢優化技術方案

查詢優化技術之多級緩存  

配置Jedis不需要序列化存入json
查詢優化技術方案
查詢優化技術方案

  一級緩存             二級緩存                              三級緩存                            四級緩存

redis緩存  --> 熱點記憶體本地緩存JVM  --> nginx proxy chache 緩存 --> nginx lua緩存

    redis緩存 : 

        單機版 : 存在單點故障,存在容量上限

          sentinal 哨兵模式 :  解決單機版問題,解決redis哪台壞了,那個redis備份是最新的。單點機器。

                                            master機器通過心跳機制和sentinal建立連結,當master出現異常,sentinal則将                                                master機器改為slave,slave變成master進行切換。

          叢集cluster模式:不需要知道redis叢集的狀态 

          jedis已經內建三種模式支援,隻需配制

Redis 叢集模式cluster配置及搭建

          Springboot整合luttuce 當叢集主節點當機之後需要重新拓撲重新整理

        查詢資料 

https://blog.csdn.net/qq_30062125/article/details/101689894 https://www.twblogs.net/a/5c9edae4bd9eee75238884b8/zh-cn

package cn.duckerkj.springbootguide.config.redis;

import java.time.Duration;

import org.joda.time.DateTime;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import  org.springframework.boot.autoconfigure.data.redis.RedisProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import  org.springframework.data.redis.connection.RedisClusterConfiguration;

import  org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;

import  org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;

import org.springframework.data.redis.core.RedisTemplate;

import  org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import  org.springframework.data.redis.serializer.StringRedisSerializer;

import com.fasterxml.jackson.annotation.JsonAutoDetect;

import com.fasterxml.jackson.annotation.PropertyAccessor;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.databind.module.SimpleModule;

import io.lettuce.core.ReadFrom;

import io.lettuce.core.cluster.ClusterClientOptions;

import io.lettuce.core.cluster.ClusterTopologyRefreshOptions;

/**

* @author pgig

* @date 2019/3/21

*/

@Configuration

public class RedisConfiguration {

    @Autowired

    private RedisProperties redisProperties;

    @Value("${redis.maxRedirects:3}")

    private int maxRedirects;

    @Value("${redis.refreshTime:5}")

    private int refreshTime;

    @Bean

    public LettuceConnectionFactory redisConnectionFactory() {

        RedisClusterConfiguration redisClusterConfiguration =  new  RedisClusterConfiguration(redisProperties.getCluster().getNodes());

        redisClusterConfiguration.setMaxRedirects(maxRedirects);

        //支援自适應叢集拓撲重新整理和靜态重新整理源

        ClusterTopologyRefreshOptions  clusterTopologyRefreshOptions =   ClusterTopologyRefreshOptions.builder()

                .enablePeriodicRefresh()

                .enableAllAdaptiveRefreshTriggers()

                .refreshPeriod(Duration.ofSeconds(refreshTime))

                .build();

        ClusterClientOptions clusterClientOptions =  ClusterClientOptions.builder()

                 .topologyRefreshOptions(clusterTopologyRefreshOptions).build();

        //從優先,讀寫分離,讀從可能存在不一緻,最終一緻性CP

        LettuceClientConfiguration lettuceClientConfiguration =  LettuceClientConfiguration.builder()

                .readFrom(ReadFrom.SLAVE_PREFERRED)

                .clientOptions(clusterClientOptions).build();

        return new  LettuceConnectionFactory(redisClusterConfiguration,  lettuceClientConfiguration);

    }

    public RedisTemplate<Object, Object>  redisTemplate(LettuceConnectionFactory redisConnectionFactory) {

        RedisTemplate<Object, Object> redisTemplate = new  RedisTemplate<>();

         redisTemplate.setConnectionFactory(redisConnectionFactory);

        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new  Jackson2JsonRedisSerializer(Object.class);       

        ObjectMapper om = new ObjectMapper();

        SimpleModule simpleModule = new SimpleModule();

        simpleModule.addSerializer(DateTime.class,new  JodaDateTimeJsonSerializer());

        simpleModule.addDeserializer(DateTime.class,new  JodaDateTimeJsonDeserializer());

        om.registerModule(simpleModule);

         om.setVisibility(PropertyAccessor.ALL,JsonAutoDetect.Visibility.ANY);

         om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        jackson2JsonRedisSerializer.setObjectMapper(om);

         redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);

        redisTemplate.setKeySerializer(new  StringRedisSerializer());

        redisTemplate.afterPropertiesSet();

        return redisTemplate;

}

本地資料熱點緩存 (二級)

        不叫本地緩存是因為: 1、熱點資料  2、髒讀非常不敏感 3、記憶體可控 。使用的是JVM緩存。本地緩存生命周期比較短

          方式一 : Guava cache   

Guava深入學習了解

                    可以控制的大小和逾時時間

                    可配置的lru政策

                    線程安全

        <dependency>

            <groupId>com.google.guava</groupId>

            <artifactId>guava</artifactId>

            <version>23.0</version>

        </dependency>

          問題 : 存在資料更新,本地緩存沒有更新的問題???

nginx proxy cache 緩存 (三級)  推薦nginx lua

        1、nginx反向代理 前置條件

         2、依靠檔案系統存索引級的檔案

         3、依靠記憶體緩存檔案位址 就是key

        配置 : 

    proxy_cache_path  

    key記憶體100m 儲存時間7天 存在目錄一共10G

    location 配置

查詢優化技術方案

        問題 : 不應該通路本地檔案磁盤,降低讀本地檔案性能降低 應該讀取遠端NAS,檔案應該無線大。

    16、nginx lua 推薦 代替 nginx proxy cache

            1、lua 協程機制 。

https://coding.imooc.com/lesson/338.html#mid=24955

                依托于線程 無需考慮異步機制 。如果阻塞 會想epoll放棄自己的調用權限

查詢優化技術方案

          2、nginx 協程機制 

https://coding.imooc.com/lesson/338.html#mid=24956
查詢優化技術方案
查詢優化技術方案
查詢優化技術方案

          3、nginx lua 插槽點

查詢優化技術方案
查詢優化技術方案

          實戰 

https://coding.imooc.com/lesson/338.html#mid=24958

            主要常用在content_by_lua

查詢優化技術方案

              好處: 使用lua腳本直接處理業務邏輯不用通路後端

            4、openresty

查詢優化技術方案
查詢優化技術方案

                4.1 openresty hellow world  注意路徑位址

                4.2 share dic

                    4.2.1 配置share dic 

https://coding.imooc.com/lesson/338.html#mid=24961
查詢優化技術方案
查詢優化技術方案

             4.4、openresty redis 可以代替share dic

https://coding.imooc.com/lesson/338.html#mid=24962
查詢優化技術方案