從企業級開發到雲原生微服務:Spring Boot實戰

從企業級開發到雲原生微服務:Spring Boot實戰

《從企業級開發到雲原生微服務:Spring Boot實戰》是2020年1月電子工業出版社出版的書籍。

基本介紹

  • 中文名:從企業級開發到雲原生微服務:SpringBoot實戰 
  • 作者:汪雲飛 沈永林 陳曉茜 
  • 出版社:電子工業出版社 
  • 出版時間:2020年1月 
  • 頁數:504 頁 
  • 定價:108 元 
  • 開本:16 開
  • ISBN:9787121377921 
內容提要,目錄,

內容提要

本書以Spring 5.2和Spring Boot 2.2為基礎,系統地講解了在日常企業級開發和微服務開發中面臨的大部分的問題,如函式式編程、Spring MVC、Spring Data、Spring Security、回響式編程、事件驅動、Spring Integration、Spring Batch、Spring Cloud、Kubernetes 與微服務等。本書內容由淺入深,適合有Java基礎的初級程式設計師學習。同時,本書還介紹了較深的理論及原理知識,可供中、高級工程師提升使用。本書講解的所有主題都附有實戰案例,讀者可快速將相關技術套用於工作實踐中。

目錄

第 1 章 初識 Spring Boot ............................................................................................... 1
1.1 Spring Boot 概述 ............................................................................................................ 1 1.2 快速建立 Spring Boot 套用 ........................................................................................... 2
1.2.1 安裝 Java ............................................................................................................. 2
1.2.2 使用 Spring Initializr ........................................................................................... 2
1.2.3 第一段代碼 ......................................................................................................... 3
1.3 體驗 Spring Boot ............................................................................................................ 4 1.3.1 Spring Boot 的套用結構 ..................................................................................... 4
1.3.2 build.gradle .......................................................................................................... 5
1.3.3 QuickStartApplication ......................................................................................... 5
1.3.4 application.properties .......................................................................................... 6
1.4 小結 ................................................................................................................................ 6
第 2 章 函式式編程 ........................................................................................................ 7
2.1 了解函式式編程 ............................................................................................................ 7
2.2 Lambda 表達式 .............................................................................................................. 7
2.2.1 了解Lambda 表達式 .......................................................................................... 7
2.2.2 把 Lambda 表達式作為參數 .............................................................................. 8
2.3 函式接口 ........................................................................................................................ 9
2.3.1 Predicate ............................................................................................................ 10
2.3.2 Function ............................................................................................................. 12
2.3.3 Consumer .......................................................................................................... 14
2.3.4 Supplier ............................................................................................................. 14
2.3.5 Operator ............................................................................................................. 15
2.3.6 Comparator ........................................................................................................ 15
2.3.7 自定義函式接口 ............................................................................................... 16
2.4 方法引用 ...................................................................................................................... 16
2.4.1 構造器方法引用 ............................................................................................... 16
2.4.2 靜態方法引用 ................................................................................................... 17
2.4.3 實例方法引用 ................................................................................................... 18
2.4.4 引用特定類的任意對象的方法 ....................................................................... 18
2.5 Stream ........................................................................................................................... 18
2.5.1 Stream 簡介 ....................................................................................................... 19
2.5.2 獲得 Stream ....................................................................................................... 19
2.5.3 中間操作 ........................................................................................................... 20
2.5.4 終結操作 ........................................................................................................... 23
2.6 Optional ........................................................................................................................ 27
2.6.1 獲得Optional .................................................................................................... 28
2.6.2 Optional 的用法 ................................................................................................ 28
2.7小結......................................................................................................................29
第 3 章 Spring 5.X 基礎 ............................................................................................... 30
3.1 IoC 容器 ....................................................................................................................... 30
3.2 Spring Bean 的配置 ..................................................................................................... 31
3.2.1 註解配置(@Component) ............................................................................. 31
3.2.2 Java 配置(@Configuration 和@Bean) ........................................................ 31
3.2.3 依賴注入(Dependency Injection) ................................................................ 32
3.2.4 運行檢驗(CommandLineRunner) ............................................................... 37
3.2.5 Bean 的 Scope ................................................................................................... 38
3.2.6 Bean 的生命周期 .............................................................................................. 40
3.2.7 套用環境 ........................................................................................................... 43
3.2.8 條件配置(@Conditional) ............................................................................ 46
3.2.9 開啟配置(@Enable*和@Import) ................................................................ 47
3.3 對 Bean 的處理(BeanPostProcessor) ...................................................................... 53
3.4 Spring Aware 容器 ....................................................................................................... 54
3.5 Bean 之間的事件通信 ................................................................................................. 57
3.6 Spring EL ...................................................................................................................... 59
3.7 AOP .............................................................................................................................. 61
3.8 註解工作原理 .............................................................................................................. 64
3.8.1 BeanPostProcessor ............................................................................................ 64
3.8.2 BeanFactoryPostProcessor ................................................................................ 66
3.8.3 使用AOP .......................................................................................................... 68
3.8.4 組合元註解 ....................................................................................................... 68
3.9小結......................................................................................................................68
第 4 章 深入 Spring Boot ............................................................................................. 69
4.1 Spring Boot 介紹 .......................................................................................................... 69
4.2 Spring Boot 的“魔法” .................................................................................................. 69
4.2.1 載入自動配置 ................................................................................................... 69
4.2.2 實現自動配置 ................................................................................................... 71
4.3 Spring Boot 的配置 ...................................................................................................... 73
4.3.1 套用配置 ........................................................................................................... 74
4.3.2 修改默認配置 ................................................................................................... 76
4.3.3 外部配置 ........................................................................................................... 79
4.4 日誌和報告 .................................................................................................................. 91
4.4.1 日誌 ................................................................................................................... 91
4.4.2 報告 ................................................................................................................... 93
4.5 多執行緒任務和計畫任務 .............................................................................................. 94
4.5.1 Task Executor .................................................................................................... 94
4.5.2 Task Scheduler .................................................................................................. 97
4.6 Gradle 外掛程式 .................................................................................................................. 99
4.6.1 依賴 ................................................................................................................... 99
4.6.2 Spring Boot Starter .......................................................................................... 100
4.6.3 外掛程式任務 ......................................................................................................... 100
4.7 自定義Starter ............................................................................................................. 100
4.7.1 包裝技術庫 ..................................................................................................... 101
4.7.2 Starter 的結構 ................................................................................................. 103
4.7.3 autoconfigure 模組 .......................................................................................... 105
4.7.4 Starter 模組 ..................................................................................................... 107
4.7.5 使用 Starter ..................................................................................................... 108
4.8 Spring Boot Actuator .................................................................................................. 110
4.8.1 常用端點 ......................................................................................................... 110
4.8.2 自定義 ............................................................................................................. 119
4.8.3 使用 Prometheus 和Grafana 監控指標 ......................................................... 124
4.9 小結 ............................................................................................................................ 128
第 5 章 Spring Web MVC .......................................................................................... 129
5.1 Spring Web MVC 簡介 .............................................................................................. 129
5.2 用 Spring Boot 學習Web MVC ................................................................................ 129
5.2.1 核心註解 ......................................................................................................... 129
5.2.2 RESTful 服務 .................................................................................................. 130
5.2.3 @ControllerAdvice ......................................................................................... 146
5.2.4 @RestControllerAdvice .................................................................................. 151
5.2.5 JSON 定製 ...................................................................................................... 156
5.2.6 RestTemplate ................................................................................................... 159
5.3 Web MVC 配置 .......................................................................................................... 161
5.3.1 Spring MVC 的工作原理 ............................................................................... 161
5.3.2 配置MVC ....................................................................................................... 161
5.3.3 Interceptor ....................................................................................................... 162
5.3.4 Formatter ......................................................................................................... 164
5.3.5 HttpMessageConverter .................................................................................... 166
5.3.6 方法參數和返回值處理設定 ......................................................................... 169
5.3.7 初始化數據綁定設定 ..................................................................................... 176
5.3.8 類型轉換原理與設定 ..................................................................................... 176
5.3.9 路徑匹配和內容協商 ..................................................................................... 183
5.3.10 JSON ............................................................................................................. 188
5.3.11 其他外部屬性配置 ......................................................................................... 189
5.4 Servlet 容器 ................................................................................................................ 191
5.4.1 註冊 Servlet、Filter 和 Listener ..................................................................... 191
5.4.2 配置 Servlet 容器 ............................................................................................ 196
5.5 異步請求 .................................................................................................................... 202
5.5.1 Servlet 3.0 異步返回 ...................................................................................... 202
5.5.2 HTTP Streaming.............................................................................................. 206
5.5.3 HTTP/2 ............................................................................................................ 212
5.6 小結 ............................................................................................................................ 214
第 6 章 數據訪問 ....................................................................................................... 215
6.1 Spring Data Repository ............................................................................................. 215
6.1.1 DDD 與 Spring Data Repository ..................................................................... 215
6.1.2 查詢方法 ......................................................................................................... 218
6.2 關係資料庫——Spring Data JPA .............................................................................. 218
6.2.1 JPA、Hibernate 和Spring Data JPA .............................................................. 218
6.2.2 環境準備 ......................................................................................................... 218
6.2.3 自動配置 ......................................................................................................... 220
6.2.4 定義聚合 ......................................................................................................... 221
6.2.5 定義聚合 Repository ...................................................................................... 224
6.2.6 查詢 ................................................................................................................. 226
6.2.7 事件監聽 ......................................................................................................... 238
6.2.8 領域事件 ......................................................................................................... 242
6.2.9 審計功能 ......................................................................................................... 245
6.2.10 Web 支持 ...................................................................................................... 246
6.2.11 資料庫初始化 ............................................................................................... 248
6.3 NoSQL——Spring Data Elasticsearch ....................................................................... 252
6.3.1 Elascticsearch 簡介 ......................................................................................... 252
6.3.2 環境準備 ......................................................................................................... 252
6.3.3 自動配置 ......................................................................................................... 253
6.3.4 定義聚合 ......................................................................................................... 254
6.3.5 定義聚合 Repository ...................................................................................... 255
6.3.6 查詢 ................................................................................................................. 256
6.4 數據快取 .................................................................................................................... 261
6.4.1 Spring Boot 與快取......................................................................................... 261
6.4.2 環境準備 ......................................................................................................... 262
6.4.3 使用快取註解 ................................................................................................. 264
6.5 小結 ............................................................................................................................ 266
第 7 章 安全控制 ....................................................................................................... 267
7.1 Spring Security 的套用 .............................................................................................. 267
7.1.1 Spring Boot 的自動配置 ................................................................................. 267
7.1.2 開啟Web 安全配置 ....................................................................................... 268
7.1.3 定製Web 安全配置 ....................................................................................... 268
7.1.4 Authentication ................................................................................................. 269
7.1.5 Authorization ................................................................................................... 281
7.1.6 Spring Data 集成 ............................................................................................. 294
7.2 Spring Security 實戰 .................................................................................................. 295
7.3 OAuth 2.0 ................................................................................................................... 301
7.3.1 OAuth 2.0 Authorization Server ...................................................................... 301
7.3.2 OAuth 2.0 Resource Server ............................................................................. 310
7.3.3 OAuth 2.0 Client ............................................................................................. 317
7.4 小結 ............................................................................................................................ 322
第 8 章 回響式編程 .................................................................................................... 323
8.1 Project Reactor............................................................................................................ 323
8.1.1 Reactive Streams 的基礎接口 ........................................................................ 323
8.1.2 Flux 和 Mono .................................................................................................. 325
8.2 Spring WebFlux .......................................................................................................... 327
8.2.1 Spring WebFlux 基礎 ..................................................................................... 327
8.2.2 Spring Boot 的自動配置 ................................................................................. 328
8.2.3 註解控制器 ..................................................................................................... 329
8.2.4 函式式端點 ..................................................................................................... 332
8.2.5 Spring WebFlux 的配置 ................................................................................. 334
8.3 Reactive NoSQL ......................................................................................................... 334
8.3.1 回響式 Elasticsearch ....................................................................................... 335
8.3.2 回響式 MongoDB ........................................................................................... 339
8.4 Reactive 關係型資料庫:R2DBC ............................................................................. 342
8.4.1 安裝 PostgreSQL ............................................................................................ 343
8.4.2 Spring Boot 的自動配置 ................................................................................. 343
8.4.3 示例 ................................................................................................................. 344
8.5 Reactive Spring Security ............................................................................................ 347
8.5.1 Reactive Spring Security 原理 ........................................................................ 347
8.5.2 Spring Boot 的自動配置 ................................................................................. 347
8.5.3 示例 ................................................................................................................. 348
8.6 小結 ............................................................................................................................ 354
第 9 章 事件驅動 ....................................................................................................... 355
9.1 JMS ............................................................................................................................. 355
9.1.1 安裝Apache ActiveMQ Artemis .................................................................... 355
9.1.2 新建套用 ......................................................................................................... 356
9.1.3 Spring Boot 的自動配置 ................................................................................. 356
9.1.4 示例 ................................................................................................................. 356
9.1.5 Topic 和 Queue ............................................................................................... 358
9.2 RabbitMQ ................................................................................................................... 360
9.3 Kafka .......................................................................................................................... 367
9.4 Websocket .................................................................................................................. 374
9.4.1 STOMP Websocket ......................................................................................... 374
9.4.2 Reactive Websocket ........................................................................................ 379
9.5 RSocket ....................................................................................................................... 382
9.5.1 新建套用 ......................................................................................................... 382
9.5.2 Spring Boot 的自動配置 ................................................................................. 383
9.5.3 示例 ................................................................................................................. 383
9.6 小結 ............................................................................................................................ 388
第 10 章 系統集成與批處理 ....................................................................................... 389
10.1 Spring Integration ..................................................................................................... 389
10.1.1 Spring Integration 基礎 ................................................................................. 389
10.1.2 Spring Integration Java DSL ......................................................................... 391
10.1.3 示例 ............................................................................................................... 392
10.2 Spring Batch ............................................................................................................. 395
10.2.1 Spring Batch 的流程 ..................................................................................... 396
10.2.2 Spring Boot 的自動配置 ............................................................................... 396
10.2.3 示例 ............................................................................................................... 397
10.3 小結 .......................................................................................................................... 402
第 11 章 Spring Cloud 與微服務 ................................................................................ 403 11.1 微服務基礎 .............................................................................................................. 403 11.1.1 微服務和雲原生套用 ................................................................................... 403
11.1.2 領域驅動設計 ............................................................................................... 404
11.2 Spring Cloud ............................................................................................................. 405
11.2.1 服務發現 ....................................................................................................... 405
11.2.2 配置管理 ....................................................................................................... 408
11.2.3 同步服務互動 ............................................................................................... 412
11.2.4 異步服務互動 ............................................................................................... 417
11.2.5 回響式異步互動 ........................................................................................... 427
11.2.6 套用網關:Spring Cloud Gateway .............................................................. 430
11.2.7 認證授權 ....................................................................................................... 433
11.3 小結 .......................................................................................................................... 442
第 12 章 Kubernetes 與微服務 .................................................................................. 443
12.1 Kubernetes ................................................................................................................ 443
12.1.1 安裝 ............................................................................................................... 443
12.1.2 Kubernetes 基礎知識 .................................................................................... 445
12.1.3 Helm .............................................................................................................. 460
12.1.4 DevOps .......................................................................................................... 463
12.1.5 安裝Jenkins .................................................................................................. 464
12.1.6 微服務示例 ................................................................................................... 465
12.1.7 鏡像倉庫和 Dockerfile ................................................................................. 467
12.1.8 使用Helm 打包套用 .................................................................................... 471
12.1.9 Jenkins 流程 .................................................................................................. 475
12.2 Service Mesh 和 Istio ............................................................................................... 481
12.2.1 安裝Istio ....................................................................................................... 482
12.2.2 微服務示例 ................................................................................................... 483
12.3 小結 .......................................................................................................................... 490
前言
緣起
距離我的第一本書《Java EE 開發的顛覆者:Spring Boot開發》的出版已經過去了四年,在這四年中,Spring Boot從剛開始被關注到現在被廣泛套用於企業級開發,我很榮幸參與了這一進程。
在這四年里,技術發生了許多的變化,微服務、雲原生已經成為技術流行詞,這也促使我們對技術的關注點從企業級開發逐漸向微服務、雲原生套用轉移。在規劃本書時,本想將其作為《Java EE開發的顛覆者:Spring Boot開發》的第二版,但最終還是以一本新書推出,原因有二:
第一,內容完全重新編寫,全部基於Spring 5.2和Spring Boot 2.2編寫,只是部分章節名稱和第一本書相同。
第二,添加了微服務、雲原生套用方面的知識,做到讓微服務、雲原生套用落地。
第一本書收到了許多讀者的好評,這給了我很大的信心再寫一本關於最新的Spring Boot實戰的書籍。為了能更多地照顧到套用開發的方方面面,最初我列出了26章,這讓本書幾乎無法完成。此時,我想起《人月神話》中的“第二系統效應”:在完成一個小型、優雅而成功的系統之後,人們傾向於對下一個計畫有過度的期待,可能因此建造出一個巨大的、有各種特色的“怪獸系統”。
這正是我最初在寫作本書時的心態,此時我將精力重新聚焦在Spring Boot在企業級套用和雲原生套用,經過近半年的努力,終於讓本書與大家見面。
特色
非常感謝各位讀者對我的第一本書的厚愛,期待大家同樣喜歡本書。本書特色如下:
◎由淺入深,適合初學者及各個級別的學習者學習。
◎無論是簡單的技術點,還是複雜的技術點,都配有實戰案例。
本書內容
第1章初識Spring Boot。以簡單的方式新建Spring Boot套用,讓初學者可以對Spring Boot有感性的認識。如果讀者已經學習或使用過Spring Boot,則可快速閱讀或略過本章。
第2章函式式編程。Spring 5.X 支持的 JDK 基線版本為 8,本書中的大量代碼都涉及函
數式編程的內容,回響式編程更是以函式式編程為基礎,所以學好函式式編程會給後面的學習 打下良好的基礎。
第3章Spring 5.X基礎。本章帶領讀者快速學習 Spring 5.2常用的主要內容,為學習和理解Spring Boot打下堅實的基礎。
第4章深入Spring Boot。本章首先講解Spring Boot的運行原理,然後講解Spring Boot 2.2的核心內容。
第5章Spring Web MVC。Spring Web MVC是工程師開發工作的核心,本章從簡單套用和深層配置等各個方面對Spring MVC進行深入的講解。
第6章數據訪問。本章講解Spring Data傘形項目,Spring Data是Spring生態中有魅力、能提高生產力的框架之一,它可以使用相同的編程模型對不同的資料庫技術進行開發,本章包含 Spring Data JPA、Spring Data Elasticsearch和數據快取。
第7章安全控制。Spring Security是Java EE領域成熟的安全解決方案,本章學習Spring Security和OAuth 2.0的套用。
第8章回響式編程。回響式編程是未來幾年的技術趨勢,本章從開發的各個環節完全打通回響式開發,包含Project Reactor、Spring WebFlux、Reactive NoSQL、R2DBC和Reactive Spring Security。
第9章事件驅動。本章講解在事件驅動開發中的常用技術,以達到套用之間的松耦合,本章包含 JMS、RabbitMQ、Kafka、Websocket和RSocket。
第10章系統集成與批處理。本章主要講解系統集成框架 Spring Integration和批處理框架Spring Batch。
第11章Spring Cloud與微服務。本章講解微服務和Spring Cloud的主要知識,還特別介紹了在Spring Cloud下OAuth 2.0的使用。
第12章Kubernetes 與微服務。在微服務開發完成後,它的部署主要基於Kubernetes平台。本章首先講解基於Kubernetes、Jenkins和Helm的部署,然後介紹服務格線Istio在微服務部署中的套用。

熱門詞條

聯絡我們