从企业级开发到云原生微服务:Spring Boot实战基本
时间:2023-02-15 03:57:02 | 来源:营销百科
时间:2023-02-15 03:57:02 来源:营销百科
从企业级开发到云原生微服务:Spring Boot实战基本内容:
[1]内容提要
[1]《从企业级开发到云原生微服务:Spring Boot实战》以Spring 5.2和Spring Boot 2.2为基础,系统地讲解了在日常企业级开发和微服务开发中面临的大部分的问题,如函数式编程、Spring MVC、Spring Data、Spring Security、响应式编程、事件驱动、Spring Integration、Spring Batch、Spring Cloud、Kubernetes与微服务等。《从企业级开发到云原生微服务:Spring Boot实战》内容由浅入深,适合有Java基础的初级程序员学习。同时,本书还介绍了较深的理论及原理知识,可供中、高级工程师提升使用。本书讲解的所有主题都附有实战案例,读者可快速将相关技术应用于工作实践中。
[1]目录
[1]第 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
[1]前言
[1]缘起
距离我的第一本书《Java EE 开发的颠覆者:Spring Boot开发》的出版已经过去了四年,在这四年中,Spring Boot从刚开始被关注到现在被广泛应用于企业级开发,我很荣幸参与了这一进程。
在这四年里,技术发生了许多的变化,微服务、云原生已经成为技术流行词,这也促使我们对技术的关注点从企业级开发逐渐向微服务、云原生应用转移。在规划本书时,本想将其作为《Java EE开发的颠覆者:Spring Boot开发》的第二版,但最终还是以一本新书推出,原因有二:
第一,内容完全重新编写,全部基于Spring 5.2和Spring Boot 2.2编写,只是部分章节名称和第一本书相同。
第二,添加了微服务、云原生应用方面的知识,做到让微服务、云原生应用落地。
第一本书收到了许多读者的好评,这给了我很大的信心再写一本关于最新的Spring Boot实战的书籍。为了能更多地照顾到应用开发的方方面面,最初我列出了26章,这让本书几乎无法完成。此时,我想起《人月神话》中的'第二系统效应':在完成一个小型、优雅而成功的系统之后,人们倾向于对下一个计划有过度的期待,可能因此建造出一个巨大的、有各种特色的'怪兽系统'。
这正是我最初在写作本书时的心态,此时我将精力重新聚焦在Spring Boot在企业级应用和云原生应用,经过近半年的努力,终于让本书与大家见面。
[1]特色
[1]非常感谢各位读者对我的第一本书的厚爱,期待大家同样喜欢本书。本书特色如下:
◎由浅入深,适合初学者及各个级别的学习者学习。
◎无论是简单的技术点,还是复杂的技术点,都配有实战案例。
本书内容
第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在微服务部署中的应用。
[1]