-
緩存
- 用途
- 經常使用的數據,查詢後加入緩存,再次查詢不需要從數據庫獲取
- 一次性使用的數據,用完刪除,比如發送短信驗證碼
- Java caching的核心接口(jsr107)
- CachingProvider: 緩存提供者.創建,配置,獲取,管理,控制CacheManager
- CacheManager: 緩存管理器.創建,配置,獲取,管理,控制Cache
- Cache: 緩存.一個類似Map的數據結構
- Entry: 緩存條目.一個存儲在Cache中的key-value對
- Expiry: 緩存條目過期時間.存儲在Cache中的條目的有效期
- 使用jsr107需要導入該包
- spring緩存抽象
- Cache: 緩存接口,有RedisCache,ConcurrentMapCache等實現
- CacheManager: 緩存管理器,管理Cache組件
- @Cacheable: 增加緩存,可使用spEL表達式
- @CacheEvict: 清除緩存
- @CachePut: 修改緩存
- @EnableCaching: 開啓基於註解的緩存
- keyGenerator: 緩存的key的生成策略
- serialize: 緩存的value的序列化策略
- @Caching是@Cacheable,@CachePut,@CacheEvict的組合註解
- @CacheConfig抽取公共配置
- @Cacheable/@CachePut/@CacheEvict的主要參數
- cacheNames/value: 指定緩存組件的名字,是一個數組
- key: 指定緩存的key值,默認是參數的值
- KenGenerator: key值生成器,key/keyGenerator二選一
- CacheManager: 指定緩存管理器,或cacheResolver指定緩存解析器
- condition: 滿足條件緩存
- unless: 否定條件緩存(異步模式不支持)
- sync: 使用異步模式
- 緩存原理
- CacheAutoConfiguration: 自動配置類
- 默認SimpleCacheConfiguration生效
- 給容器中注入了一個ConcurrentMapCacheManager組件
- 創建和獲取一個ConcurrentMapCache類型的組件
- 運行流程
- 查詢語句運行之前,先獲取Cache組件(CacheManager.getCache()),如果沒有獲取到,則創建一個並放入CurrentMap中
- 查詢Cache(緩存組件),按照cacheNames指定的名字獲取
- key默認使用SimpleKeyGenerator生成
- 沒有查到緩存,調用目標方法,將結果加入緩存
- 查到緩存,直接返回,不調用目標方法
- 整合redis緩存
- 導入redis的啓動器
- 操作五大數據類型(redisTemplate,stringRedisTemplate)
- stringRedisTemplate.opsForValue();
- stringRedisTemplate.opsForList();
- stringRedisTemplate.opsForHash();
- stringRedisTemplate.opsForSet();
- stringRedisTemplate.opsForZSet();
- 通過CacheManager獲取
@Autowired private RedisCacheManager redisCacheManager; Cache cache = redisCacheManager.getCache("com.version.controller.DriverInfoController"); cache.put("name", "jiyu"); cache.get("name");
- 用途
-
消息隊列
- 重要概念:消息代理和目的地,當消息發送者發送消息後,由消息代理接管,保證消息送達指定目的地
- 目的地形式
- 隊列(queue): 點對點通信.一個消息只有一個發送者和接收者
- 主題(topic): 發佈(publish)/訂閱(subscribe)消息.一個消息只有一個發送者,但可以有多個接收者
- 用途
- 異步處理: 註冊完畢後,立即返回消息,發送郵件,統計數據等操作先寫入消息隊列,稍後讀取
- 應用解耦: 訂單和庫存之間使用微服務解耦,下單時訂單模塊向消息隊列寫入消息,然後庫存模塊讀取消息
- 秒殺任務: 定長消息,先搶佔位置,再執行實際操作
- 服務協議
- JMS(Java message service): Java消息服務,基於jvm消息代理規範.有ActiveMQ等實現
- AMQP(Advanced Message Queuing Protocol): 高級消息隊列.有RabbitMQ等實現
- 比較
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[]
消息需要序列化後發送
- spring支持
- spring-jms提供了對jms的支持
- spring-rabbit提供了對amqp的支持
- 需要ConnectionFactory的實現來連接消息隊列
- 提供JmsTemplate和RabbitTemplate來發送消息
- 在方法上使用註解@JmsListener,@RabbitListener監聽消息代理發佈的消息
- @EnableJms,@EnableRabbit開啓消息隊列支持
- JmsAutoConfiguration, RabbitAutoConfiguration自動配置類
- rabbit簡介
- 核心概念
- Message: 消息.消息是不具名的,由消息頭消息體組成.消息體是不透明的(程序根據需要自定義的),消息頭由一系列可選屬性組成,這些屬性包括routing-key(路由鍵),priority(優先權),delivery-mode(消息是否需要持久化存儲)等
- Publisher: 消息發送者.向交換器發(Exchange)送消息(Message)的客戶端程序
- Exchange: 交換器.接收消息發送者(Publish)發送的消息並將這些消息路由給服務器中的隊列(Queue).有四種交換器
- direct(默認): 消息的路由鍵和綁定中的綁定鍵完全一致才能接收消息(單播模式)
- fanout: 交換器收到消息時,會給綁定該交換器的消息隊列都發消息(廣播模式)
- topic: 按照匹配模式轉發消息到消息隊列.以路由鍵和綁定鍵的字符串切分單詞,使用逗號分隔.#匹配0個或多個單詞,*匹配一個單詞
- headers: 和direct一致,性能較差,基本不用
- Queue: 消息隊列.用來保存消息直到發給消費者.它是消息的容器,也是消息的終點,一個消息可以投入一個或多個隊列
- Binding: 綁定.用於關聯隊列(Queue)和交換器(Exchange).一個綁定就是基於路由鍵將交換器和消息隊列連接起來的路由規則,可以將交換器理解爲一個有綁定(Binging)構成的路由表.Exchange和Queue可以是多對多關係
- Connection: 網絡連接,比如一個tcp連接
- Channel: 信道.多路複用連接中的一條獨立的雙向數據流通道,信道是建立在真實的tcp連接內的虛擬連接.AMQP命令都是通過信道發出去的,因爲tcp的建立和銷燬開銷較大,所以引入信道(Channel)複用一條tcp連接
- Consumer: 消息消費者.從消息隊列(Queue)中取出消息的客戶端程序
- VirtualHost: 虛擬主機,每個虛擬主機都是一個迷你版的RabbitMQ服務器,擁有自己的消息隊列,交換器,綁定和權限機制,vhost是amqp的基礎,必須在連接時指定.默認是的vhost是/
- Broker: 消息隊列服務器實體
- 關係圖
- springboot整合rabbitmq
- 導入starter啓動器
- RabbitAutoConfiguration自動配置類
- CachingConnectionFactory自動部署連接工廠
- RabbitProperties綁定rabbitmq的配置
- RabbitTemplate給rabbitmq發送和接收消息
- AmqpAdmin系統功能管理組件,新建交換器,消息隊列等
- 核心概念
-
elasticsearch
- 類比關係型數據庫
mysql elasticsearch 數據庫(database) 索引(index)(名詞,動詞索引是存儲數據的意思) 數據表(table) 類型(type) 記錄(record) 文檔(document) 字段(field) 屬性(property) - 整合springboot項目
- springdata方式(默認方式)
- Client節點信息clusterNodes,clusterName
- ElasticsearchTemplate
- 編寫ElasticsearchRepository的子接口操作es數據庫
- jest方式
- 導入jest工具包(io.searchbox.client.JestClient)
- springdata方式(默認方式)
- 類比關係型數據庫
-
任務調度
- 異步任務:使用@EnableAsync開啓異步任務,在需要異步的方法上加@Async註解
- 定時任務: 使用@EnableScheduling開啓異步任務,在需要異步的方法上加@Scheduled(cron = "0 * 1/24 * * ?")註解
- cron表達式
- 郵件任務
-
spring-security
- 引入啓動器
- 編寫配置文件
@EnableWebSecurity public class CustomerSecurityConfig extends WebSecurityConfigurerAdapter { }
- 控制請求訪問權限
-
整合springcloud服務發現
- spring cloud是一個分佈式的整體解決方案,能夠快速構建應用,對接雲平臺資源
- 配置管理
- 服務發現
- 熔斷
- 路由
- 微代理
- 控制總線
- 一次性token驗證
- 全局鎖
- leader選舉
- 分佈式session管理
- 集羣狀態
- spring cloud五大組件
- 服務發現--netflix eureka
- 客戶端負載均衡--netflix ribbon
- 斷路器--netflix hystrix
- 服務網關--netflix zuul
- 分佈式配置--spring cloud config
- 監控管理,加入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]}
- 健康指示器: 實現HealthIndicator接口的xxxHealthIndicator類加入到容器中
- spring cloud是一個分佈式的整體解決方案,能夠快速構建應用,對接雲平臺資源