springboot整合-學習筆記

  1. 緩存

    1. 用途
      1. 經常使用的數據,查詢後加入緩存,再次查詢不需要從數據庫獲取
      2. 一次性使用的數據,用完刪除,比如發送短信驗證碼
    2. Java caching的核心接口(jsr107)
      1. CachingProvider: 緩存提供者.創建,配置,獲取,管理,控制CacheManager
      2. CacheManager: 緩存管理器.創建,配置,獲取,管理,控制Cache
      3. Cache: 緩存.一個類似Map的數據結構
      4. Entry: 緩存條目.一個存儲在Cache中的key-value對
      5. Expiry: 緩存條目過期時間.存儲在Cache中的條目的有效期
    3. 使用jsr107需要導入該包
    4. spring緩存抽象
      1. Cache: 緩存接口,有RedisCache,ConcurrentMapCache等實現
      2. CacheManager: 緩存管理器,管理Cache組件
      3. @Cacheable: 增加緩存,可使用spEL表達式
      4. @CacheEvict: 清除緩存
      5. @CachePut: 修改緩存
      6. @EnableCaching: 開啓基於註解的緩存
      7. keyGenerator: 緩存的key的生成策略
      8. serialize: 緩存的value的序列化策略
      9. @Caching是@Cacheable,@CachePut,@CacheEvict的組合註解
      10. @CacheConfig抽取公共配置
    5. @Cacheable/@CachePut/@CacheEvict的主要參數
      1. cacheNames/value: 指定緩存組件的名字,是一個數組
      2. key: 指定緩存的key值,默認是參數的值
      3. KenGenerator: key值生成器,key/keyGenerator二選一
      4. CacheManager: 指定緩存管理器,或cacheResolver指定緩存解析器
      5. condition: 滿足條件緩存
      6. unless: 否定條件緩存(異步模式不支持)
      7. sync: 使用異步模式
    6. 緩存原理
      1. CacheAutoConfiguration: 自動配置類
      2. 默認SimpleCacheConfiguration生效
        1. 給容器中注入了一個ConcurrentMapCacheManager組件
        2. 創建和獲取一個ConcurrentMapCache類型的組件
      3. 運行流程
        1. 查詢語句運行之前,先獲取Cache組件(CacheManager.getCache()),如果沒有獲取到,則創建一個並放入CurrentMap中
        2. 查詢Cache(緩存組件),按照cacheNames指定的名字獲取
        3. key默認使用SimpleKeyGenerator生成
        4. 沒有查到緩存,調用目標方法,將結果加入緩存
        5. 查到緩存,直接返回,不調用目標方法
      4. 整合redis緩存
        1. 導入redis的啓動器
        2. 操作五大數據類型(redisTemplate,stringRedisTemplate)
          1. stringRedisTemplate.opsForValue();
          2. stringRedisTemplate.opsForList();
          3. stringRedisTemplate.opsForHash();
          4. stringRedisTemplate.opsForSet();
          5. stringRedisTemplate.opsForZSet();
        3. 通過CacheManager獲取
          @Autowired
          private RedisCacheManager redisCacheManager;
              
          Cache cache = redisCacheManager.getCache("com.version.controller.DriverInfoController");
          cache.put("name", "jiyu");
          cache.get("name");

           

  2. 消息隊列

    1. 重要概念:消息代理目的地,當消息發送者發送消息後,由消息代理接管,保證消息送達指定目的地
    2. 目的地形式
      1. 隊列(queue): 點對點通信.一個消息只有一個發送者和接收者
      2. 主題(topic): 發佈(publish)/訂閱(subscribe)消息.一個消息只有一個發送者,但可以有多個接收者
    3. 用途
      1. 異步處理: 註冊完畢後,立即返回消息,發送郵件,統計數據等操作先寫入消息隊列,稍後讀取
      2. 應用解耦: 訂單和庫存之間使用微服務解耦,下單時訂單模塊向消息隊列寫入消息,然後庫存模塊讀取消息
      3. 秒殺任務: 定長消息,先搶佔位置,再執行實際操作
    4. 服務協議
      1. JMS(Java message service): Java消息服務,基於jvm消息代理規範.有ActiveMQ等實現
      2. AMQP(Advanced Message Queuing Protocol): 高級消息隊列.有RabbitMQ等實現
      3. 比較
          JMS AMQP
        定義 Java API 網絡級協議
        跨語言跨平臺
        model

        peer-2-peer(點對點)

        pub/sub(發佈/訂閱)

        direct exchange(點對點)

        fanout exchange(發佈/訂閱)

        topic exchange(發佈/訂閱)

        headers exchange(發佈/訂閱)

        system exchange(發佈/訂閱)

        支持消息類型

        TextMessage

        MapMessage

        BytesMessage

        StreamMessage

        ObjectMessage

        Message(只有消息頭和屬性)

        byte[]

        消息需要序列化後發送

         

    5. spring支持
      1. spring-jms提供了對jms的支持
      2. spring-rabbit提供了對amqp的支持
      3. 需要ConnectionFactory的實現來連接消息隊列
      4. 提供JmsTemplate和RabbitTemplate來發送消息
      5. 在方法上使用註解@JmsListener,@RabbitListener監聽消息代理發佈的消息
      6. @EnableJms,@EnableRabbit開啓消息隊列支持
      7. JmsAutoConfiguration, RabbitAutoConfiguration自動配置類
    6. rabbit簡介
      1. 核心概念
        1. Message: 消息.消息是不具名的,由消息頭消息體組成.消息體是不透明的(程序根據需要自定義的),消息頭由一系列可選屬性組成,這些屬性包括routing-key(路由鍵),priority(優先權),delivery-mode(消息是否需要持久化存儲)等
        2. Publisher: 消息發送者.向交換器發(Exchange)送消息(Message)的客戶端程序
        3. Exchange: 交換器.接收消息發送者(Publish)發送的消息並將這些消息路由給服務器中的隊列(Queue).有四種交換器
          1. direct(默認): 消息的路由鍵和綁定中的綁定鍵完全一致才能接收消息(單播模式)
          2. fanout: 交換器收到消息時,會給綁定該交換器的消息隊列都發消息(廣播模式)
          3. topic: 按照匹配模式轉發消息到消息隊列.以路由鍵和綁定鍵的字符串切分單詞,使用逗號分隔.#匹配0個或多個單詞,*匹配一個單詞
          4. headers: 和direct一致,性能較差,基本不用
        4. Queue: 消息隊列.用來保存消息直到發給消費者.它是消息的容器,也是消息的終點,一個消息可以投入一個或多個隊列
        5. Binding: 綁定.用於關聯隊列(Queue)和交換器(Exchange).一個綁定就是基於路由鍵將交換器和消息隊列連接起來的路由規則,可以將交換器理解爲一個有綁定(Binging)構成的路由表.Exchange和Queue可以是多對多關係
        6. Connection: 網絡連接,比如一個tcp連接
        7. Channel: 信道.多路複用連接中的一條獨立的雙向數據流通道,信道是建立在真實的tcp連接內的虛擬連接.AMQP命令都是通過信道發出去的,因爲tcp的建立和銷燬開銷較大,所以引入信道(Channel)複用一條tcp連接
        8. Consumer: 消息消費者.從消息隊列(Queue)中取出消息的客戶端程序
        9. VirtualHost: 虛擬主機,每個虛擬主機都是一個迷你版的RabbitMQ服務器,擁有自己的消息隊列,交換器,綁定和權限機制,vhost是amqp的基礎,必須在連接時指定.默認是的vhost是/
        10. Broker: 消息隊列服務器實體
        11. 關係圖
      2. springboot整合rabbitmq
        1. 導入starter啓動器
        2. RabbitAutoConfiguration自動配置類
        3. CachingConnectionFactory自動部署連接工廠
        4. RabbitProperties綁定rabbitmq的配置
        5. RabbitTemplate給rabbitmq發送和接收消息
        6. AmqpAdmin系統功能管理組件,新建交換器,消息隊列等
  3. elasticsearch

    1. 類比關係型數據庫
      mysql elasticsearch
      數據庫(database) 索引(index)(名詞,動詞索引是存儲數據的意思)
      數據表(table) 類型(type)
      記錄(record) 文檔(document)
      字段(field) 屬性(property)

       

    2. 整合springboot項目
      1. springdata方式(默認方式)
        1. Client節點信息clusterNodes,clusterName
        2. ElasticsearchTemplate
        3. 編寫ElasticsearchRepository的子接口操作es數據庫
      2. jest方式
        1. 導入jest工具包(io.searchbox.client.JestClient)
  4. 任務調度

    1. 異步任務:使用@EnableAsync開啓異步任務,在需要異步的方法上加@Async註解
    2. 定時任務: 使用@EnableScheduling開啓異步任務,在需要異步的方法上加@Scheduled(cron = "0 * 1/24 * * ?")註解
      1. cron表達式
    3. 郵件任務
  5. spring-security

    1. 引入啓動器
    2. 編寫配置文件
      @EnableWebSecurity
      public class CustomerSecurityConfig extends WebSecurityConfigurerAdapter {
      }

       

    3. 控制請求訪問權限
  6. 整合springcloud服務發現

    1. spring cloud是一個分佈式的整體解決方案,能夠快速構建應用,對接雲平臺資源
      1. 配置管理
      2. 服務發現
      3. 熔斷
      4. 路由
      5. 微代理
      6. 控制總線
      7. 一次性token驗證
      8. 全局鎖
      9. leader選舉
      10. 分佈式session管理
      11. 集羣狀態
    2. spring cloud五大組件
      1. 服務發現--netflix eureka
      2. 客戶端負載均衡--netflix ribbon
      3. 斷路器--netflix hystrix
      4. 服務網關--netflix zuul
      5. 分佈式配置--spring cloud config
    3. 監控管理,加入actuator的啓動器即可,通過在項目的/actuator/**下訪問
      {GET /actuator/archaius, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {GET /actuator/auditevents, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {GET /actuator/beans, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {GET /actuator/caches/{cache}, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {GET /actuator/caches, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {DELETE /actuator/caches/{cache}, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {DELETE /actuator/caches}
      {GET /actuator/health, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {GET /actuator/health/{component}, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {GET /actuator/health/{component}/{instance}, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {GET /actuator/conditions, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {GET /actuator/configprops, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {GET /actuator/env, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {GET /actuator/env/{toMatch}, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {POST /actuator/env, consumes [application/vnd.spring-boot.actuator.v2+json || application/json], produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {DELETE /actuator/env, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {GET /actuator/info, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {GET /actuator/loggers, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {GET /actuator/loggers/{name}, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {POST /actuator/loggers/{name}, consumes [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {GET /actuator/heapdump, produces [application/octet-stream]}
      {GET /actuator/threaddump, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {GET /actuator/metrics/{requiredMetricName}, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {GET /actuator/metrics, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {GET /actuator/scheduledtasks, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {GET /actuator/httptrace, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {GET /actuator/mappings, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {POST /actuator/refresh, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {GET /actuator/features, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {POST /actuator/service-registry, consumes [application/vnd.spring-boot.actuator.v2+json || application/json], produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {GET /actuator/service-registry, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}
      {GET /actuator, produces [application/vnd.spring-boot.actuator.v2+json || application/json]}

       

    4. 健康指示器: 實現HealthIndicator接口的xxxHealthIndicator類加入到容器中

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章