關(guān)于我們
書單推薦
新書推薦
|
Spring Cloud、Nginx高并發(fā)核心編程
本書從動(dòng)態(tài)代理模式、Reactor模式、三大限流策略等知識(shí)入手,深入淺出地剖析Spring Cloud+Nginx系統(tǒng)架構(gòu)的核心原理以及Web高并發(fā)開發(fā)。全書從基礎(chǔ)設(shè)計(jì)模式和基礎(chǔ)原理出發(fā),理論與實(shí)戰(zhàn)相結(jié)合,系統(tǒng)和詳盡地介紹Spring Cloud + Nginx高并發(fā)核心編程。
本書共10章,前6章剖析Feign高并發(fā)RPC的底層原理,解析Hystrix高性能配置的核心選項(xiàng),闡述Hystrix滑動(dòng)窗口的核心原理。后4章介紹Nginx的核心原理及其配置,并結(jié)合秒殺場(chǎng)景實(shí)現(xiàn)Spring Cloud秒殺、Spring Cloud+Nginx Lua秒殺,為廣大Java開發(fā)者提供一個(gè)全面學(xué)習(xí)高并發(fā)開發(fā)的實(shí)戰(zhàn)案例。這些知識(shí)為廣大的Java工程師解決日常在后臺(tái)開發(fā)中遇到的高并發(fā)、高性能問題打下堅(jiān)實(shí)的技術(shù)基礎(chǔ)。
前言
第1章 Spring Cloud+Nginx 高并發(fā)核心編程的學(xué)習(xí)準(zhǔn)備 ........................ 1 1.1 Spring Cloud+Nginx架構(gòu)的主要組件 ......................................... 1 1.2 Spring Cloud和Spring Boot的版本選擇 .............................................. 3 1.3 Spring Cloud微服務(wù)開發(fā)所涉及的中間件..................................... 4 1.4 Spring Cloud微服務(wù)開發(fā)和自驗(yàn)證環(huán)境 .................................................... 6 1.4.1 開發(fā)和自驗(yàn)證環(huán)境的系統(tǒng)選項(xiàng)和環(huán)境變量配置.......................................... 6 1.4.2 使用Fiddler工具抓包和查看報(bào)文....................................................... 7 1.5 crazy-springcloud微服務(wù)開發(fā)腳手........................................................ 9 1.6 以秒殺作為Spring Cloud+Nginx的實(shí)戰(zhàn)案例 ............................................. 10 第2章 Spring Cloud入門實(shí)戰(zhàn) ...................................................... 12 2.1 Eureka服務(wù)注冊(cè)與發(fā)現(xiàn) .................................................... 13 2.1.1 什么是服務(wù)注冊(cè)與發(fā)現(xiàn) ......................................................... 13 2.1.2 Eureka Server注冊(cè)中心 ......................................................... 14 2.1.3 服務(wù)提供者的創(chuàng)建和配置 ......................................................... 19 2.1.4 服務(wù)提供者的續(xù)約(心跳) ....................................................... 22 2.1.5 服務(wù)提供者的健康狀態(tài) ............................................................ 24 2.1.6 Eureka自我保護(hù)模式與失效Provider的快速剔除 .................................... 28 2.2 Config配置中心 ............................................................... 32 2.2.1 config-server服務(wù)端組件 .......................................................... 32 2.2.2 config-client客戶端組件 .......................................................... 35 2.3 微服務(wù)的RPC遠(yuǎn)程調(diào)用 ................................................................ 36 2.3.1 RESTful風(fēng)格簡(jiǎn)介 ....................................................... 36 2.3.2 RestTemplate遠(yuǎn)程調(diào)用 ............................................................ 37 2.3.3 Feign遠(yuǎn)程調(diào)用 ................................................................... 38 2.4 Feign+Ribbon實(shí)現(xiàn)客戶端負(fù)載均衡 .................................................... 40 2.4.1 Spring Cloud Ribbon基礎(chǔ) ......................................................... 41 2.4.2 Spring Cloud Ribbon的負(fù)載均衡策略 ............................................... 43 2.4.3 Spring Cloud Ribbon的常用配置 ................................................... 45 2.5 Feign+Hystrix實(shí)現(xiàn)RPC調(diào)用保護(hù) ...................................................... 50 2.5.1 Spring Cloud Hystrix失敗回退 ..................................................... 51 2.5.2 分布式系統(tǒng)面臨的雪崩難題 ........................................................ 55 2.5.3 Spring Cloud Hystrix熔斷器 ...................................................... 56 第3章 Spring Cloud RPC遠(yuǎn)程調(diào)用核心原理 .................................................. 61 3.1 代理模式與RPC客戶端實(shí)現(xiàn)類 ............................................................ 61 3.1.1 客戶端RPC遠(yuǎn)程調(diào)用實(shí)現(xiàn)類的職責(zé) ................................................. 61 3.1.2 簡(jiǎn)單的RPC客戶端實(shí)現(xiàn)類 ........................................................ 63 3.1.3 從基礎(chǔ)原理講起:代理模式與RPC客戶端實(shí)現(xiàn)類 ......................................... 67 3.1.4 使用動(dòng)態(tài)代理模式實(shí)現(xiàn)RPC客戶端類 ............................................. 70 3.1.5 JDK動(dòng)態(tài)代理機(jī)制的原理 ........................................ 74 3.2 模擬Feign RPC動(dòng)態(tài)代理的實(shí)現(xiàn) ....................................................... 77 3.2.1 模擬Feign的方法處理器MethodHandler ................................................. 78 3.2.2 模擬Feign的調(diào)用處理器InvocationHandler ...................................... 80 3.2.3 模擬Feign的動(dòng)態(tài)代理RPC的執(zhí)行流程 ................................................. 83 3.2.4 模擬動(dòng)態(tài)代理RPC遠(yuǎn)程調(diào)用的測(cè)試 .................................................. 83 3.2.5 Feign彈性RPC客戶端實(shí)現(xiàn)類 ....................................................... 85 3.3 Feign彈性RPC客戶端的重要組件 ................................................... 86 3.3.1 演示用例說明 ................................................................. 86 3.3.2 Feign的動(dòng)態(tài)代理RPC客戶端實(shí)例 .................................................... 88 3.3.3 Feign的調(diào)用處理器InvocationHandler .......................................... 89 3.3.4 Feign的方法處理器MethodHandler ............................................... 91 3.3.5 Feign的客戶端組件 ........................................................... 94 3.4 Feign的RPC動(dòng)態(tài)代理實(shí)例的創(chuàng)建流程 ........................................ 97 3.4.1 Feign的整體運(yùn)作流程 ............................................................. 98 3.4.2 RPC動(dòng)態(tài)代理容器實(shí)例的FactoryBean工廠類 .......................................... 99 3.4.3 Feign.Builder建造者容器實(shí)例 ...................................................... 102 3.4.4 默認(rèn)的RPC動(dòng)態(tài)代理實(shí)例的創(chuàng)建流程 ................................................. 105 3.4.5 Contract遠(yuǎn)程調(diào)用協(xié)議規(guī)則類 .................................................. 108 3.5 Feign遠(yuǎn)程調(diào)用的執(zhí)行流程 ............................................................ 110 3.5.1 與FeignInvocationHandler相關(guān)的遠(yuǎn)程調(diào)用執(zhí)行流程 ................................... 110 3.5.2 與HystrixInvocationHandler相關(guān)的遠(yuǎn)程調(diào)用執(zhí)行流程 ................................. 111 3.5.3 Feign遠(yuǎn)程調(diào)用的完整流程及其特性 ............................................. 114 3.6 HystrixFeign動(dòng)態(tài)代理實(shí)例的創(chuàng)建流程 ............................................ 115 3.6.1 HystrixFeign.Builder建造者容器實(shí)例 .......................................... 115 3.6.2 配置HystrixFeign.Builder建造者容器實(shí)例 ...................................... 116 3.7 feign.Client客戶端容器實(shí)例 ................................................... 117 3.7.1 裝配LoadBalancerFeignClient負(fù)載均衡容器實(shí)例 ................................ 117 3.7.2 裝配ApacheHttpClient負(fù)載均衡容器實(shí)例 .......................................... 119 3.7.3 裝配OkHttpClient負(fù)載均衡容器實(shí)例 ............................................... 121 3.7.4 裝配Client.Default負(fù)載均衡容器實(shí)例 ................................ 122 第4章 RxJava響應(yīng)式編程框架 ........................................................ 124 4.1 從基礎(chǔ)原理講起:觀察者模式 ...................................................... 124 4.1.1 觀察者模式的基礎(chǔ)原理 ......................................................... 124 4.1.2 觀察者模式的經(jīng)典實(shí)現(xiàn) ....................................................... 125 4.1.3 RxJava中的觀察者模式 ....................................................... 127 4.1.4 RxJava的不完整回調(diào).......................................................... 130 4.1.5 RxJava的函數(shù)式編程....................................................... 132 4.1.6 RxJava的操作符............................................................ 134 4.2 創(chuàng)建型操作符 ................................................................... 134 4.2.1 just操作符 ............................................................... 135 4.2.2 from操作符 .............................................................. 135 4.2.3 range操作符 ............................................................. 136 4.2.4 interval操作符 .............................................................. 137 4.2.5 defer操作符 .......................................................... 138 4.3 過濾型操作符 ............................................................... 139 4.3.1 filter操作符 ............................................................ 139 4.3.2 distinct操作符 .......................................................... 140 4.4 轉(zhuǎn)換型操作符 ............................................................... 141 4.4.1 map操作符 ................................................................ 141 4.4.2 flatMap操作符 ............................................................ 142 4.4.3 scan操作符 ................................................... 144 4.5 聚合操作符 ..............................................................146 4.5.1 count操作符 .................................................... 146 4.5.2 reduce操作符 ......................................................... 147 4.6 其他操作符 .................................................................. 149 4.6.1 take操作符 ............................................................. 149 4.6.2 window操作符 .................................................................. 150 4.7 RxJava的Scheduler調(diào)度器................................................. 153 4.8 背壓 ........................................................................ 155 4.8.1 什么是背壓?jiǎn)栴} ............................................................... 155 4.8.2 背壓?jiǎn)栴}的幾種應(yīng)對(duì)模式 ...................................................... 158 第5章 Hystrix RPC保護(hù)的原理 ....................................................... 162 5.1 RPC保護(hù)的目標(biāo) ................................................................. 162 5.2 HystrixCommand簡(jiǎn)介 ............................................................ 162 5.2.1 HystrixCommand的使用 ........................................................ 163 5.2.2 HystrixCommand的配置內(nèi)容和方式 .............................................. 166 5.3 HystrixCommand命令的執(zhí)行方法 ................................................. 168 5.3.1 execute()方法 ................................................................ 168 5.3.2 queue()方法 .................................................................. 170 5.3.3 observe()方法 ........................................................... 172 5.3.4 toObservable()方法 ............................................................. 173 5.3.5 HystrixCommand的執(zhí)行方法之間的關(guān)系 ............................................. 175 5.4 RPC保護(hù)之艙壁模式 ............................................................... 176 5.4.1 什么是艙壁模式 ................................................................. 176 5.4.2 Hystrix線程池隔離 .............................................................. 178 5.4.3 Hystrix線程池隔離配置 .......................................................... 182 5.4.4 Hystrix信號(hào)量隔離 .............................................................. 184 5.5 RPC保護(hù)之熔斷器模式 .............................................................. 188 5.5.1 熔斷器狀態(tài)變化的演示實(shí)例 ....................................................... 189 5.5.2 熔斷器和滑動(dòng)窗口的配置屬性 ..................................................... 193 5.5.3 Hystrix命令的執(zhí)行流程 .......................................................... 198 5.6 RPC監(jiān)控之滑動(dòng)窗口的實(shí)現(xiàn)原理 ...................................................... 199 5.6.1 Hystrix健康統(tǒng)計(jì)滑動(dòng)窗口的模擬實(shí)現(xiàn)............................................... 200 5.6.2 Hystrix滑動(dòng)窗口的核心實(shí)現(xiàn)原理 ................................................ 205 第6章 微服務(wù)網(wǎng)關(guān)與用戶身份識(shí)別 ....................................................... 210 6.1 Zuul的基礎(chǔ)使用 .................................................................. 211 6.2 創(chuàng)建Zuul網(wǎng)關(guān)服務(wù) .............................................................. 211 6.2.1 Zuul路由規(guī)則配置 ............................................................ 212 6.2.2 過濾敏感請(qǐng)求頭部 ............................................................ 214 6.2.3 路徑前綴的處理 .............................................................. 215 6.3 Zuul過濾器 .................................................................... 216 6.3.1 Zuul網(wǎng)關(guān)的過濾器類型 ....................................................... 216 6.3.2 實(shí)戰(zhàn):用戶的黑名單過濾 ..................................................... 217 6.4 Spring Security原理和實(shí)戰(zhàn) ..................................................... 221 6.4.1 Spring Security核心組件 ..................................................... 221 6.4.2 Spring Security的請(qǐng)求認(rèn)證處理流程 ........................................... 224 6.4.3 基于數(shù)據(jù)源的認(rèn)證流程 ........................................................ 231 6.5 JWT+Spring Security進(jìn)行網(wǎng)關(guān)安全認(rèn)證 .............................................. 237 6.5.1 JWT安全令牌規(guī)范詳解 ........................................................... 238 6.5.2 JWT+Spring Security認(rèn)證處理流程 .......................................... 243 6.5.3 Zuul網(wǎng)關(guān)與UAA微服務(wù)的配合 ................................................ 250 6.5.4 使用Zuul過濾器添加代理請(qǐng)求的用戶標(biāo)識(shí) ................................................... 254 6.6 服務(wù)提供者之間的會(huì)話共享關(guān)系 .................................................... 256 6.6.1 分布式Session的起源和實(shí)現(xiàn)方案 ......................................... 257 6.6.2 Spring Session的核心組件和存儲(chǔ)細(xì)節(jié) .............................................. 258 6.6.3 Spring Session的使用和定制..................................................... 259 6.6.4 通過用戶身份標(biāo)識(shí)查找Session ID ..................................... 261 6.6.5 查找或創(chuàng)建分布式Session ....................................................... 263 6.6.6 加載高速訪問數(shù)據(jù)到分布式Session ......................................... 265 第7章 Nginx/OpenResty詳解 ...................................................... 268 7.1 Nginx簡(jiǎn)介 ...................................................................... 268 7.1.1 正向代理與反向代理 ................................................... 269 7.1.2 Nginx的啟動(dòng)與停止 ......................................................... 270 7.1.3 Nginx的啟動(dòng)命令和參數(shù)詳解 .................................................. 271 7.1.4 Linux下OpenResty的啟動(dòng)、停止腳本 ................................................. 271 7.1.5 Windows下OpenResty的啟動(dòng)、停止腳本 ............................................... 273 7.2 Nginx的核心原理 ............................................................. 274 7.2.1 Reactor模型 ........................................................... 274 7.2.2 Nginx的兩類進(jìn)程 ..................................................................... 275 7.2.3 Nginx的模塊化設(shè)計(jì) ............................................................. 276 7.2.4 Nginx配置文件上下文結(jié)構(gòu) ....................................................... 278 7.2.5 Nginx的請(qǐng)求處理流程 ................................................... 280 7.2.6 HTTP請(qǐng)求處理的11個(gè)階段 ................................................. 281 7.3 Nginx的基礎(chǔ)配置 ......................................................... 286 7.3.1 events事件驅(qū)動(dòng)配置 ................................................. 286 7.3.2 虛擬主機(jī)配置 .............................................................. 287 7.3.3 錯(cuò)誤頁面配置 ........................................................... 289 7.3.4 長連接相關(guān)配置 ............................................................. 290 7.3.5 訪問日志配置 .................................................... 290 7.3.6 Nginx核心模塊內(nèi)置變量 ........................................... 291 7.4 location路由規(guī)則配置詳解 ................................................... 293 7.4.1 location語法詳解 ....................................................... 293 7.4.2 常用的location路由配置 ................................................... 296 7.5 Nginx的rewrite模塊指令 .......................................................... 297 7.5.1 set指令 .................................................................. 298 7.5.2 rewrite指令 ............................................................... 298 7.5.3 if條件指令 ............................................................ 301 7.5.4 add_header指令 ............................................................... 303 7.5.5 指令的執(zhí)行順序 ........................................................... 304 7.6 反向代理與負(fù)載均衡配置 ............................................... 305 7.6.1 演示環(huán)境說明 ................................................. 305 7.6.2 proxy_pass反向代理指令 ............................................... 306 7.6.3 proxy_set_header請(qǐng)求頭設(shè)置指令 ................................... 308 7.6.4 upstream上游服務(wù)器組 ......................................................... 309 7.6.5 upstream的上游服務(wù)器配置 ........................................... 311 7.6.6 upstream的負(fù)載分配方式 .................................................... 313 第8章 Nginx Lua編程 ............................................................ 315 8.1 Nginx Lua編程的主要應(yīng)用場(chǎng)景 ................................................ 315 8.2 Nginx Lua編程簡(jiǎn)介 ....................................................... 315 8.2.1 ngx_lua簡(jiǎn)介 ....................................................... 315 8.2.2 Nginx Lua項(xiàng)目的創(chuàng)建 .................................................... 316 8.2.3 Lua項(xiàng)目的工程結(jié)構(gòu) ....................................................... 317 8.2.4 Lua項(xiàng)目的啟動(dòng) .......................................................... 318 8.3 Lua開發(fā)基礎(chǔ) .............................................................. 319 8.3.1 Lua模塊的定義和使用 ........................................................... 319 8.3.2 Lua模塊的使用 ................................................................. 320 8.3.3 Lua的數(shù)據(jù)類型 ................................................................. 322 8.3.4 Lua的字符串 ................................................................. 325 8.3.5 Lua的數(shù)組容器 ............................................................. 326 8.3.6 Lua的控制結(jié)構(gòu) ...................................................... 328 8.3.7 Lua的函數(shù)定義 ............................................................ 331 8.3.8 Lua的面向?qū)ο缶幊?........................................................ 332 8.4 Nginx Lua編程基礎(chǔ) ............................................................ 333 8.4.1 Nginx Lua的執(zhí)行原理 ................................................. 334 8.4.2 Nginx Lua的配置指令 ...................................................... 335 8.4.3 Nginx Lua的內(nèi)置常量和變量 .............................................. 339 8.5 Nginx Lua編程實(shí)例 ........................................................... 341 8.5.1 Lua腳本獲取URL中的參數(shù) ....................................................... 341 8.5.2 Nginx Lua的內(nèi)置方法 ..................................................... 342 8.5.3 通過ngx.header設(shè)置HTTP響應(yīng)頭 ........................................ 344 8.5.4 Lua訪問Nginx變量 .......................................................... 347 8.5.5 Lua訪問請(qǐng)求上下文變量 ..................................................... 349 8.6 重定向與內(nèi)部子請(qǐng)求 ................................................................ 350 8.6.1 Nginx Lua內(nèi)部重定向 ........................................................ 350 8.6.2 Nginx Lua外部重定向 ..................................................... 352 8.6.3 ngx.location.capture子請(qǐng)求 .............................................. 355 8.6.4 ngx.location.capture_multi并發(fā)子請(qǐng)求 .......................................359 8.7 Nginx Lua操作Redis .......................................................... 361 8.7.1 Redis的CRUD基本操作 ......................................................... 361 8.7.2 實(shí)戰(zhàn):封裝一個(gè)操作Redis的基礎(chǔ)類 ............................................. 364 8.7.3 在Lua中使用Redis連接池 .......................................................... 367 8.8 Nginx Lua編程實(shí)戰(zhàn)案例 ............................................................... 369 8.8.1 Nginx+Redis進(jìn)行分布式訪問統(tǒng)計(jì) .................................................... 369 8.8.2 Nginx+Redis+Java容器實(shí)現(xiàn)高并發(fā)訪問 ............................................. 370 8.8.3 Nginx+Redis實(shí)現(xiàn)黑名單攔截 ...................................................... 375 8.8.4 使用Nginx Lua共享內(nèi)存 ........................................................ 378 第9章 限流原理與實(shí)戰(zhàn) ................................................................. 380 9.1 限流策略原理與參考實(shí)現(xiàn) ................................................... 380 9.1.1 3種限流策略:計(jì)數(shù)器、漏桶和令牌桶 ........................................... 380 9.1.2 計(jì)數(shù)器限流原理和Java參考實(shí)現(xiàn) ................................................ 381 9.1.3 漏桶限流原理和Java參考實(shí)現(xiàn) .................................................. 384 9.1.4 令牌桶限流原理和Java參考實(shí)現(xiàn) ................................................ 386 9.2 分布式計(jì)數(shù)器限流 .............................................................. 390 9.2.1 實(shí)戰(zhàn):Nginx Lua分布式計(jì)數(shù)器限流 ............................................. 390 9.2.2 實(shí)戰(zhàn):Redis Lua分布式計(jì)數(shù)器限流 ........................................... 394 9.3 Nginx漏桶限流詳解 .................................................. 397 9.4 實(shí)戰(zhàn):分布式令牌桶限流 ....................................................... 399 9.4.1 分布式令牌桶限流Lua腳本 .................................................... 399 9.4.2 Java分布式令牌桶限流 .................................................... 402 9.4.3 Java分布式令牌桶限流的自驗(yàn)證 ......................................... 406 第10章 Spring Cloud+Nginx秒殺實(shí)戰(zhàn) ............................................. 409 10.1 秒殺系統(tǒng)的業(yè)務(wù)功能和技術(shù)難點(diǎn) ............................................. 409 10.1.1 秒殺系統(tǒng)的業(yè)務(wù)功能 ............................................ 409 10.1.2 秒殺系統(tǒng)面臨的技術(shù)難題 ............................................... 410 10.2 秒殺系統(tǒng)的系統(tǒng)架構(gòu) ................................................. 411 10.2.1 秒殺的分層架構(gòu) ..................................................... 411 10.2.2 秒殺的限流架構(gòu) ..................................................... 413 10.2.3 秒殺的分布式鎖架構(gòu) ................................................. 414 10.2.4 秒殺的削峰架構(gòu) ................................................... 415 10.3 秒殺業(yè)務(wù)的參考實(shí)現(xiàn) .............................................. 416 10.3.1 秒殺的功能模塊和接口設(shè)計(jì) ........................................... 416 10.3.2 數(shù)據(jù)表和PO實(shí)體類設(shè)計(jì) .................................................... 420 10.3.3 使用分布式ID生成器 ...................................................... 423 10.3.4 秒殺的控制層設(shè)計(jì) ....................................................... 424 10.3.5 service層邏輯:獲取秒殺令牌 ........................................... 426 10.3.6 service層邏輯:執(zhí)行秒殺下單 ........................................... 429 10.3.7 秒殺的Lua腳本設(shè)計(jì) ................................................... 434 10.3.8 BusinessException定義 ................................................... 436 10.4 Zuul內(nèi)部網(wǎng)關(guān)實(shí)現(xiàn)秒殺限流 ......................................... 439 10.5 Nginx高性能秒殺和限流 ...................................... 444 10.5.1 Lua腳本:獲取秒殺令牌 ................................................. 445 10.5.2 Lua腳本:執(zhí)行令牌桶限流 ........................................... 448
你還可能感興趣
我要評(píng)論
|