Spring Cloud 从入门到精通

Spring Cloud 从入门到精通

转载来自:https://blog.csdn.net/valada/article/details/80892573

课程介绍

Spring Cloud 是一套完整的微服务解决方案,基于 Spring Boot 框架,准确的说,它不是一个框架,而是一个大的容器,它将市面上较好的微服务框架集成进来,从而简化了开发者的代码量。

本课程由浅入深带领大家一步步攻克 Spring Cloud 各大模块,接着通过一个实例带领大家了解大型分布式微服务架构的搭建过程,最后深入源码加深对它的了解。

本课程共分为四个部分:

第一部分(第1-3课),初识 Spring Boot,掌握 Spring Boot 基础知识,为后续入门 Spring Cloud 打好基础 。

第二部分(第4-13课),Spring Cloud 入门篇,主要介绍 Spring Cloud 常用模块,包括服务发现、服务注册、配置中心、链路追踪、异常处理等。

第三部分(第14-18课),Spring Cloud 进阶篇,介绍大型分布式系统中事务处理、线程安全等问题,并以一个实例项目手把手教大家搭建完整的微服务系统。

第四部分(第19-20课),Spring Cloud 高级篇,解析 Spring Cloud 源码,并讲解如何部署基于 Spring Cloud 的大型分布式系统。

作者介绍

李熠,从事 Java 后端开发6年,现任职某大型互联网公司,担任 Java 高级开发工程师,CSDN 博客专家,全栈工程师。

课程内容

导读:什么是 Spring Cloud 及应用现状

Spring Cloud 是什么?

在学习本课程之前,读者有必要先了解一下 Spring Cloud。

Spring Cloud 是一系列框架的有序集合,它利用 Spring Boot 的开发便利性简化了分布式系统的开发,比如服务发现、服务网关、服务路由、链路追踪等。Spring Cloud 并不重复造轮子,而是将市面上开发得比较好的模块集成进去,进行封装,从而减少了各模块的开发成本。换句话说:Spring Cloud 提供了构建分布式系统所需的“全家桶”。

Spring Cloud 现状

目前,国内使用 Spring Cloud 技术的公司并不多见,不是因为 Spring Cloud 不好,主要原因有以下几点:

  1. Spring Cloud 中文文档较少,出现问题网上没有太多的解决方案。
  2. 国内创业型公司技术老大大多是阿里系员工,而阿里系多采用 Dubbo 来构建微服务架构。
  3. 大型公司基本都有自己的分布式解决方案,而中小型公司的架构很多用不上微服务,所以没有采用 Spring Cloud 的必要性。

但是,微服务架构是一个趋势,而 Spring Cloud 是微服务解决方案的佼佼者,这也是作者写本系列课程的意义所在。

Spring Cloud 优缺点

其主要优点有:

  1. 集大成者,Spring Cloud 包含了微服务架构的方方面面。
  2. 约定优于配置,基于注解,没有配置文件。
  3. 轻量级组件,Spring Cloud 整合的组件大多比较轻量级,且都是各自领域的佼佼者。
  4. 开发简便,Spring Cloud 对各个组件进行了大量的封装,从而简化了开发。
  5. 开发灵活,Spring Cloud 的组件都是解耦的,开发人员可以灵活按需选择组件。

接下来,我们看下它的缺点:

  1. 项目结构复杂,每一个组件或者每一个服务都需要创建一个项目。
  2. 部署门槛高,项目部署需要配合 Docker 等容器技术进行集群部署,而要想深入了解 Docker,学习成本高。

Spring Cloud 的优势是显而易见的。因此对于想研究微服务架构的同学来说,学习 Spring Cloud 是一个不错的选择。

Spring Cloud 和 Dubbo 对比

Dubbo 只是实现了服务治理,而 Spring Cloud 实现了微服务架构的方方面面,服务治理只是其中的一个方面。下面通过一张图对其进行比较:

这里写图片描述

可以看出,Spring Cloud 比较全面,而 Dubbo 由于只实现了服务治理,需要集成其他模块,需要单独引入,增加了学习成本和集成成本。

Spring Cloud 学习

Spring Cloud 基于 Spring Boot,因此在研究 Spring Cloud 之前,本课程会首先介绍 Spring Boot 的用法,方便后续 Spring Cloud 的学习。

本课程不会讲解 SpringMVC 的用法,因此学习本课程需要读者对 Spring 及 SpringMVC 有过研究。

本课程共分为四个部分:

  • 第一部分初识 Spring Boot,掌握 Spring Boot 基础知识,为后续入门 Spring Cloud 打好基础 。

  • 第二部分 Spring Cloud 入门篇,主要介绍 Spring Cloud 常用模块,包括服务发现、服务注册、配置中心、链路追踪、异常处理等。

  • 第三部分 Spring Cloud 进阶篇,介绍大型分布式系统中事务处理、线程安全等问题,并以一个实例项目手把手教大家搭建完整的微服务系统。

  • 第四部分 Spring Cloud 高级篇,解析 Spring Cloud 源码,并讲解如何部署基于 Spring Cloud 的大型分布式系统。

本课程的所有示例代码均可在:https://github.com/lynnlovemin/SpringCloudLesson 下载。

第01课:Spring Boot 入门

什么是 Spring Boot

Spring Boot 是由 Pivotal 团队提供的基于 Spring 的全新框架,其设计目的是为了简化 Spring 应用的搭建和开发过程。该框架遵循“约定大于配置”原则,采用特定的方式进行配置,从而使开发者无需定义大量的 XML 配置。通过这种方式,Spring Boot 致力于在蓬勃发展的快速应用开发领域成为领导者。

Spring Boot 并不重复造轮子,而且在原有 Spring 的框架基础上封装了一层,并且它集成了一些类库,用于简化开发。换句话说,Spring Boot 就是一个大容器。

下面几张图展示了官网上提供的 Spring Boot 所集成的所有类库:

这里写图片描述

这里写图片描述

这里写图片描述

Spring Boot 官方推荐使用 Maven 或 Gradle 来构建项目,本教程采用 Maven。

第一个 Spring Boot 项目

大多数教程都是以 Hello World 入门,本教程也不例外,接下来,我们就来搭建一个最简单的 Spring Boot 项目。

首先创建一个 Maven 工程,请看下图:

这里写图片描述

然后在 pom.xml 加入 Spring Boot 依赖:

 
  1. <parent>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-parent</artifactId>

  4. <version>2.0.1.RELEASE</version>

  5. </parent>

  6. <dependencies>

  7. <dependency>

  8. <groupId>org.springframework.boot</groupId>

  9. <artifactId>spring-boot-starter-web</artifactId>

  10. </dependency>

  11. </dependencies>

创建一个 Controller 类 HelloController:

 
  1. import org.springframework.boot.SpringApplication;

  2. import org.springframework.boot.autoconfigure.SpringBootApplication;

  3. import org.springframework.web.bind.annotation.RequestMapping;

  4. import org.springframework.web.bind.annotation.RestController;

  5.  
  6. @RestController

  7. @SpringBootApplication

  8. public class HelloController {

  9.  
  10. @RequestMapping("hello")

  11. String hello() {

  12. return "Hello World!";

  13. }

  14.  
  15. public static void main(String[] args) {

  16. SpringApplication.run(HelloController.class, args);

  17. }

  18. }

运行 main 方法,Spring Boot 默认会启动自带的 Tomcat 容器,启动成功后,浏览器访问:http://localhost:8080/hello,则会看到下图:

这里写图片描述

我们可以注意到,没有写任何的配置文件,更没有显示的使用任何容器,它是如何启动程序的呢,具体原理我将在第3课中具体分析。

这里我们可以初步分析出,Spring Boot 提供了默认的配置,在启动类里加入 @SpringBootApplication 注解,则这个类就是整个应用程序的启动类。

properties 和 yaml

Spring Boot 整个应用程序只有一个配置文件,那就是 .properties 或 .yml 文件。但是,在前面的示例代码中,我们并没有看到该配置文件,那是因为 Spring Boot 对每个配置项都有默认值。当然,我们也可以添加配置文件,用以覆盖其默认值,这里以 .properties 文件为例,首先在 resources 下新建一个名为 application.properties(注意:文件名必须是 application)的文件,键入内容为:

 
  1. server.port=8081

  2. server.servlet.context-path=/api

并且启动 main 方法,这时程序请求地址则变成了:http://localhost:8081/api/hello。

Spring Boot 支持 properties 和 yaml 两种格式的文件,文件名分别对应 application.properties 和 application.yml,下面贴出 yaml 文件格式供大家参考:

 
  1. server:

  2. port: 8080

  3. servlet:

  4. context-path: /api

可以看出 properties 是以逗号隔开,而 yaml 则换行+ tab 隔开,这里需要注意的是冒号后面必须空格,否则会报错。yaml 文件格式更清晰,更易读,这里作者建议大家都采用 yaml 文件来配置。

本教程的所有配置均采用 yaml 文件。

打包、运行

Spring Boot 打包分为 war 和 jar两个格式,下面将分别演示如何构建这两种格式的启动包。

在 pom.xml 加入如下配置:

 
  1. <packaging>war</packaging>

  2. <build>

  3. <finalName>index</finalName>

  4. <resources>

  5. <resource>

  6. <directory>src/main/resources</directory>

  7. <filtering>true</filtering>

  8. </resource>

  9. </resources>

  10. <plugins>

  11. <plugin>

  12. <groupId>org.springframework.boot</groupId>

  13. <artifactId>spring-boot-maven-plugin</artifactId>

  14. </plugin>

  15. <plugin>

  16. <artifactId>maven-resources-plugin</artifactId>

  17. <version>2.5</version>

  18. <configuration>

  19. <encoding>UTF-8</encoding>

  20. </configuration>

  21. </plugin>

  22. <plugin>

  23. <groupId>org.apache.maven.plugins</groupId>

  24. <artifactId>maven-surefire-plugin</artifactId>

  25. <version>2.18.1</version>

  26. <configuration>

  27. <skipTests>true</skipTests>

  28. </configuration>

  29. </plugin>

  30. </plugins>

  31. </build>

这个时候运行 mvn package 就会生成 war 包,然后放到 Tomcat 当中就能启动,但是我们单纯这样配置在 Tomcat 是不能成功运行的,会报错,需要通过编码指定 Tomcat 容器启动,修改 HelloController 类:

 
  1. @RestController

  2. @SpringBootApplication

  3. public class HelloController extends SpringBootServletInitializer{

  4.  
  5. @RequestMapping("hello")

  6. String hello() {

  7. return "Hello World!";

  8. }

  9.  
  10. public static void main(String[] args) {

  11. SpringApplication.run(HelloController.class, args);

  12. }

  13. @Override

  14. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

  15. return application.sources(Application.class);

  16. }

  17.  
  18. }

这时再打包放到 Tomcat,启动就不会报错了。

接下来我们继续看如果达成 jar 包,在 pom.xml 加入如下配置:

 
  1. <packaging>jar</packaging>

  2. <build>

  3. <finalName>api</finalName>

  4. <resources>

  5. <resource>

  6. <directory>src/main/resources</directory>

  7. <filtering>true</filtering>

  8. </resource>

  9. </resources>

  10. <plugins>

  11. <plugin>

  12. <groupId>org.springframework.boot</groupId>

  13. <artifactId>spring-boot-maven-plugin</artifactId>

  14. <configuration>

  15. <fork>true</fork>

  16. <mainClass>com.lynn.yiyi.Application</mainClass>

  17. </configuration>

  18. <executions>

  19. <execution>

  20. <goals>

  21. <goal>repackage</goal>

  22. </goals>

  23. </execution>

  24. </executions>

  25. </plugin>

  26. <plugin>

  27. <artifactId>maven-resources-plugin</artifactId>

  28. <version>2.5</version>

  29. <configuration>

  30. <encoding>UTF-8</encoding>

  31. <useDefaultDelimiters>true</useDefaultDelimiters>

  32. </configuration>

  33. </plugin>

  34. <plugin>

  35. <groupId>org.apache.maven.plugins</groupId>

  36. <artifactId>maven-surefire-plugin</artifactId>

  37. <version>2.18.1</version>

  38. <configuration>

  39. <skipTests>true</skipTests>

  40. </configuration>

  41. </plugin>

  42. <plugin>

  43. <groupId>org.apache.maven.plugins</groupId>

  44. <artifactId>maven-compiler-plugin</artifactId>

  45. <version>2.3.2</version>

  46. <configuration>

  47. <source>1.8</source>

  48. <target>1.8</target>

  49. </configuration>

  50. </plugin>

  51. </plugins>

  52. </build>

然后通过 mvn package 打包,最后通过 java 命令启动:

java -jar api.jar

这样,最简单的 Spring Boot 就完成了,但是对于一个大型项目,这是远远不够的,Spring Boot 的详细操作可以参照官网

下面展示一个最基础的企业级 Spring Boot 项目的结构:

这里写图片描述

其中,Application.java 是程序的启动类,Startup.java 是程序启动完成前执行的类,WebConfig.java 是配置类,所有 bean 注入、配置、拦截器注入等都放在这个类里面。

以上实例只是最简单的 Spring Boot 项目入门实例,后面会深入研究 Spring Boot。

第02课:Spring Boot 进阶

上一篇带领大家初步了解了如何使用 Spring Boot 搭建框架,通过 Spring Boot 和传统的 SpringMVC 架构的对比,我们清晰地发现 Spring Boot 的好处,它使我们的代码更加简单,结构更加清晰。

从这一篇开始,我将带领大家更加深入的认识 Spring Boot,将 Spring Boot 涉及到东西进行拆解,从而了解 Spring Boot 的方方面面。学完本文后,读者可以基于 Spring Boot 搭建更加复杂的系统框架。

我们知道,Spring Boot 是一个大容器,它将很多第三方框架都进行了集成,我们在实际项目中用到哪个模块,再引入哪个模块。比如我们项目中的持久化框架用 MyBatis,则在 pom.xml 添加如下依赖:

 
  1. <dependency>

  2. <groupId>org.mybatis.spring.boot</groupId>

  3. <artifactId>mybatis-spring-boot-starter</artifactId>

  4. <version>1.1.1</version>

  5. </dependency>

  6. <dependency>

  7. <groupId>mysql</groupId>

  8. <artifactId>mysql-connector-java</artifactId>

  9. <version>5.1.40</version>

  10. </dependency>

yaml/properties 文件

我们知道整个 Spring Boot 项目只有一个配置文件,那就是 application.yml,Spring Boot 在启动时,就会从 application.yml 中读取配置信息,并加载到内存中。上一篇我们只是粗略的列举了几个配置项,其实 Spring Boot 的配置项是很多的,本文我们将学习在实际项目中常用的配置项(注:为了方便说明,配置项均以 properties 文件的格式写出,后续的实际配置都会写成 yaml 格式)。

配置项说明举例
server.port应用程序启动端口server.port=8080,定义应用程序启动端口为8080
server.context-path应用程序上下文server.port=/api,则访问地址为:http://ip:port/api
spring.http.multipart.maxFileSize最大文件上传大小,-1为不限制spring.http.multipart.maxFileSize=-1
spring.jpa.database数据库类型spring.jpa.database=MYSQL,指定数据库为mysql
spring.jpa.properties.hibernate.dialecthql方言spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.datasource.url数据库连接字符串spring.datasource.url=jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8&useSSL=true
spring.datasource.username数据库用户名spring.datasource.username=root
spring.datasource.password数据库密码spring.datasource.password=root
spring.datasource.driverClassName数据库驱动spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.showSql控制台是否打印sql语句spring.jpa.showSql=true

下面是我参与的某个项目的 application.yml 配置文件内容:

 
  1. server:

  2. port: 8080

  3. context-path: /api

  4. tomcat:

  5. max-threads: 1000

  6. min-spare-threads: 50

  7. connection-timeout: 5000

  8. spring:

  9. profiles:

  10. active: dev

  11. http:

  12. multipart:

  13. maxFileSize: -1

  14. datasource:

  15. url: jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8&useSSL=true

  16. username: root

  17. password: root

  18. driverClassName: com.mysql.jdbc.Driver

  19. jpa:

  20. database: MYSQL

  21. showSql: true

  22. hibernate:

  23. namingStrategy: org.hibernate.cfg.ImprovedNamingStrategy

  24. properties:

  25. hibernate:

  26. dialect: org.hibernate.dialect.MySQL5Dialect

  27. mybatis:

  28. configuration:

  29. #配置项:开启下划线到驼峰的自动转换. 作用:将数据库字段根据驼峰规则自动注入到对象属性。

  30. map-underscore-to-camel-case: true

以上列举了常用的配置项,所有配置项信息都可以在官网中找到,本课程就不一一列举了。

多环境配置

在一个企业级系统中,我们可能会遇到这样一个问题:开发时使用开发环境,测试时使用测试环境,上线时使用生产环境。每个环境的配置都可能不一样,比如开发环境的数据库是本地地址,而测试环境的数据库是测试地址。那我们在打包的时候如何生成不同环境的包呢?

这里的解决方案有很多:

  1. 每次编译之前手动把所有配置信息修改成当前运行的环境信息。这种方式导致每次都需要修改,相当麻烦,也容易出错。
  2. 利用 Maven,在 pom.xml 里配置多个环境,每次编译之前将 settings.xml 里面修改成当前要编译的环境 ID。这种方式会事先设置好所有环境,缺点就是每次也需要手动指定环境,如果环境指定错误,发布时是不知道的。
  3. 第三种方案就是本文重点介绍的,也是作者强烈推荐的方式。

首先,创建 application.yml 文件,在里面添加如下内容:

 
  1. spring:

  2. profiles:

  3. active: dev

含义是指定当前项目的默认环境为 dev,即项目启动时如果不指定任何环境,Spring Boot 会自动从 dev 环境文件中读取配置信息。我们可以将不同环境都共同的配置信息写到这个文件中。

然后创建多环境配置文件,文件名的格式为:application-{profile}.yml,其中,{profile} 替换为环境名字,如 application-dev.yml,我们可以在其中添加当前环境的配置信息,如添加数据源:

 
  1. spring:

  2. datasource:

  3. url: jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8&useSSL=true

  4. username: root

  5. password: root

  6. driverClassName: com.mysql.jdbc.Driver

这样,我们就实现了多环境的配置,每次编译打包我们无需修改任何东西,编译为 jar 文件后,运行命令:

java -jar api.jar --spring.profiles.active=dev

其中 --spring.profiles.active 就是我们要指定的环境。

常用注解

我们知道,Spring Boot 主要采用注解的方式,在上一篇的入门实例中,我们也用到了一些注解。

本文,我将详细介绍在实际项目中常用的注解。

@SpringBootApplication

我们可以注意到 Spring Boot 支持 main 方法启动,在我们需要启动的主类中加入此注解,告诉 Spring Boot,这个类是程序的入口。如:

 
  1. @SpringBootApplication

  2. public class Application {

  3.  
  4. public static void main(String[] args) {

  5. SpringApplication.run(Application.class, args);

  6. }

  7. }

如果不加这个注解,程序是无法启动的。

我们查看下 SpringBootApplication 的源码,源码如下:

 
  1. @Target(ElementType.TYPE)

  2. @Retention(RetentionPolicy.RUNTIME)

  3. @Documented

  4. @Inherited

  5. @SpringBootConfiguration

  6. @EnableAutoConfiguration

  7. @ComponentScan(excludeFilters = {

  8. @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),

  9. @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

  10. public @interface SpringBootApplication {

  11.  
  12. /**

  13. * Exclude specific auto-configuration classes such that they will never be applied.

  14. * @return the classes to exclude

  15. */

  16. @AliasFor(annotation = EnableAutoConfiguration.class, attribute = "exclude")

  17. Class<?>[] exclude() default {};

  18.  
  19. /**

  20. * Exclude specific auto-configuration class names such that they will never be

  21. * applied.

  22. * @return the class names to exclude

  23. * @since 1.3.0

  24. */

  25. @AliasFor(annotation = EnableAutoConfiguration.class, attribute = "excludeName")

  26. String[] excludeName() default {};

  27.  
  28. /**

  29. * Base packages to scan for annotated components. Use {@link #scanBasePackageClasses}

  30. * for a type-safe alternative to String-based package names.

  31. * @return base packages to scan

  32. * @since 1.3.0

  33. */

  34. @AliasFor(annotation = ComponentScan.class, attribute = "basePackages")

  35. String[] scanBasePackages() default {};

  36.  
  37. /**

  38. * Type-safe alternative to {@link #scanBasePackages} for specifying the packages to

  39. * scan for annotated components. The package of each class specified will be scanned.

  40. * <p>

  41. * Consider creating a special no-op marker class or interface in each package that

  42. * serves no purpose other than being referenced by this attribute.

  43. * @return base packages to scan

  44. * @since 1.3.0

  45. */

  46. @AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")

  47. Class<?>[] scanBasePackageClasses() default {};

  48.  
  49. }

在这个注解类上有3个注解,如下:

 
  1. @SpringBootConfiguration

  2. @EnableAutoConfiguration

  3. @ComponentScan(excludeFilters = {

  4. @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),

  5. @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

因此,我们可以用这三个注解代替 SpringBootApplication,如:

 
  1. @SpringBootConfiguration

  2. @EnableAutoConfiguration

  3. @ComponentScan

  4. public class Application {

  5.  
  6. public static void main(String[] args) {

  7. SpringApplication.run(Application.class, args);

  8. }

  9. }

其中,SpringBootConfiguration 表示 Spring Boot 的配置注解,EnableAutoConfiguration 表示自动配置,ComponentScan 表示 Spring Boot 扫描 Bean 的规则,比如扫描哪些包。

@Configuration

加入了这个注解的类被认为是 Spring Boot 的配置类,我们知道可以在 application.yml 设置一些配置,也可以通过代码设置配置。

如果我们要通过代码设置配置,就必须在这个类上标注 Configuration 注解。如下代码:

 
  1. @Configuration

  2. public class WebConfig extends WebMvcConfigurationSupport{

  3.  
  4. @Override

  5. protected void addInterceptors(InterceptorRegistry registry) {

  6. super.addInterceptors(registry);

  7. registry.addInterceptor(new ApiInterceptor());

  8. }

  9. }

不过 Spring Boot 官方推荐 Spring Boot 项目用 SpringBootConfiguration 来代替 Configuration。

@Bean

这个注解是方法级别上的注解,主要添加在 @Configuration 或 @SpringBootConfiguration 注解的类,有时也可以添加在 @Component 注解的类。它的作用是定义一个Bean。

请看下面代码:

 
  1. @Bean

  2. public ApiInterceptor interceptor(){

  3. return new ApiInterceptor();

  4. }

那么,我们可以在 ApiInterceptor 里面注入其他 Bean,也可以在其他 Bean 注入这个类。

@Value

通常情况下,我们需要定义一些全局变量,都会想到的方法是定义一个 public static 变量,在需要时调用,是否有其他更好的方案呢?答案是肯定的。下面请看代码:

 
  1. @Value("${server.port}")

  2. String port;

  3. @RequestMapping("/hello")

  4. public String home(String name) {

  5. return "hi "+name+",i am from port:" +port;

  6. }

其中,server.port 就是我们在 application.yml 里面定义的属性,我们可以自定义任意属性名,通过 @Value 注解就可以将其取出来。

它的好处不言而喻:

  1. 定义在配置文件里,变量发生变化,无需修改代码。
  2. 变量交给Spring来管理,性能更好。

注: 本课程默认针对于对 SpringMVC 有所了解的读者,Spring Boot 本身基于 Spring 开发的,因此,本文不再讲解其他 Spring 的注解。

注入任何类

本节通过一个实际的例子来讲解如何注入一个普通类,并且说明这样做的好处。

假设一个需求是这样的:项目要求使用阿里云的 OSS 进行文件上传。

我们知道,一个项目一般会分为开发环境、测试环境和生产环境。OSS 文件上传一般有如下几个参数:appKey、appSecret、bucket、endpoint 等。不同环境的参数都可能不一样,这样便于区分。按照传统的做法,我们在代码里设置这些参数,这样做的话,每次发布不同的环境包都需要手动修改代码。

这个时候,我们就可以考虑将这些参数定义到配置文件里面,通过前面提到的 @Value 注解取出来,再通过 @Bean 将其定义为一个 Bean,这时我们只需要在需要使用的地方注入该 Bean 即可。

首先在 application.yml 加入如下内容:

 
  1. appKey: 1

  2. appSecret: 1

  3. bucket: lynn

  4. endPoint: https://www.aliyun.com

其次创建一个普通类:

 
  1. public class Aliyun {

  2.  
  3. private String appKey;

  4.  
  5. private String appSecret;

  6.  
  7. private String bucket;

  8.  
  9. private String endPoint;

  10.  
  11. public static class Builder{

  12.  
  13. private String appKey;

  14.  
  15. private String appSecret;

  16.  
  17. private String bucket;

  18.  
  19. private String endPoint;

  20.  
  21. public Builder setAppKey(String appKey){

  22. this.appKey = appKey;

  23. return this;

  24. }

  25.  
  26. public Builder setAppSecret(String appSecret){

  27. this.appSecret = appSecret;

  28. return this;

  29. }

  30.  
  31. public Builder setBucket(String bucket){

  32. this.bucket = bucket;

  33. return this;

  34. }

  35.  
  36. public Builder setEndPoint(String endPoint){

  37. this.endPoint = endPoint;

  38. return this;

  39. }

  40.  
  41. public Aliyun build(){

  42. return new Aliyun(this);

  43. }

  44. }

  45.  
  46. public static Builder options(){

  47. return new Aliyun.Builder();

  48. }

  49.  
  50. private Aliyun(Builder builder){

  51. this.appKey = builder.appKey;

  52. this.appSecret = builder.appSecret;

  53. this.bucket = builder.bucket;

  54. this.endPoint = builder.endPoint;

  55. }

  56.  
  57. public String getAppKey() {

  58. return appKey;

  59. }

  60.  
  61. public String getAppSecret() {

  62. return appSecret;

  63. }

  64.  
  65. public String getBucket() {

  66. return bucket;

  67. }

  68.  
  69. public String getEndPoint() {

  70. return endPoint;

  71. }

  72. }

然后在 @SpringBootConfiguration 注解的类添加如下代码:

 
  1. @Value("${appKey}")

  2. private String appKey;

  3. @Value("${appSecret}")

  4. private String appSecret;

  5. @Value("${bucket}")

  6. private String bucket;

  7. @Value("${endPoint}")

  8. private String endPoint;

  9.  
  10. @Bean

  11. public Aliyun aliyun(){

  12. return Aliyun.options()

  13. .setAppKey(appKey)

  14. .setAppSecret(appSecret)

  15. .setBucket(bucket)

  16. .setEndPoint(endPoint)

  17. .build();

  18. }

最后在需要的地方注入这个 Bean 即可:

 
  1. @Autowired

  2. private Aliyun aliyun;

拦截器

我们在提供 API 的时候,经常需要对 API 进行统一的拦截,比如进行接口的安全性校验。

本节,我会讲解 Spring Boot 是如何进行拦截器设置的,请看接下来的代码。

创建一个拦截器类:ApiInterceptor,并实现 HandlerInterceptor 接口:

 
  1. public class ApiInterceptor implements HandlerInterceptor {

  2. //请求之前

  3. @Override

  4. public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

  5. System.out.println("进入拦截器");

  6. return true;

  7. }

  8. //请求时

  9. @Override

  10. public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

  11.  
  12. }

  13. //请求完成

  14. @Override

  15. public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

  16.  
  17. }

  18. }

@SpringBootConfiguration 注解的类继承 WebMvcConfigurationSupport 类,并重写 addInterceptors 方法,将 ApiInterceptor 拦截器类添加进去,代码如下:

 
  1. @SpringBootConfiguration

  2. public class WebConfig extends WebMvcConfigurationSupport{

  3.  
  4. @Override

  5. protected void addInterceptors(InterceptorRegistry registry) {

  6. super.addInterceptors(registry);

  7. registry.addInterceptor(new ApiInterceptor());

  8. }

  9. }

异常处理

我们在 Controller 里提供接口,通常需要捕捉异常,并进行友好提示,否则一旦出错,界面上就会显示报错信息,给用户一种不好的体验。最简单的做法就是每个方法都使用 try catch 进行捕捉,报错后,则在 catch 里面设置友好的报错提示。如果方法很多,每个都需要 try catch,代码会显得臃肿,写起来也比较麻烦。

我们可不可以提供一个公共的入口进行统一的异常处理呢?当然可以。方法很多,这里我们通过 Spring 的 AOP 特性就可以很方便的实现异常的统一处理。

 
  1. @Aspect

  2. @Component

  3. public class WebExceptionAspect {

  4.  
  5. private static final Logger logger = LoggerFactory.getLogger(WebExceptionAspect.class);

  6.  
  7. //凡是注解了RequestMapping的方法都被拦截 @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")

  8. private void webPointcut() {

  9. }

  10.  
  11. /**

  12. * 拦截web层异常,记录异常日志,并返回友好信息到前端 目前只拦截Exception,是否要拦截Error需再做考虑

  13. *

  14. * @param e

  15. * 异常对象

  16. */

  17. @AfterThrowing(pointcut = "webPointcut()", throwing = "e")

  18. public void handleThrowing(Exception e) {

  19. e.printStackTrace();

  20. logger.error("发现异常!" + e.getMessage());

  21. logger.error(JSON.toJSONString(e.getStackTrace()));

  22. //这里输入友好性信息

  23. writeContent("出现异常");

  24. }

  25.  
  26. /**

  27. * 将内容输出到浏览器

  28. *

  29. * @param content

  30. * 输出内容

  31. */

  32. private void writeContent(String content) {

  33. HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())

  34. .getResponse();

  35. response.reset();

  36. response.setCharacterEncoding("UTF-8");

  37. response.setHeader("Content-Type", "text/plain;charset=UTF-8");

  38. response.setHeader("icop-content-type", "exception");

  39. PrintWriter writer = null;

  40. try {

  41. writer = response.getWriter();

  42. } catch (IOException e) {

  43. e.printStackTrace();

  44. }

  45. writer.print(content);

  46. writer.flush();

  47. writer.close();

  48. }

  49. }

这样,我们无需每个方法都添加 try catch,一旦报错,则会执行 handleThrowing 方法。

优雅的输入合法性校验

为了接口的健壮性,我们通常除了客户端进行输入合法性校验外,在 Controller 的方法里,我们也需要对参数进行合法性校验,传统的做法是每个方法的参数都做一遍判断,这种方式和上一节讲的异常处理一个道理,不太优雅,也不易维护。

其实,SpringMVC 提供了验证接口,下面请看代码:

 
  1. @GetMapping("authorize")

  2. public void authorize(@Valid AuthorizeIn authorize, BindingResult ret){

  3. if(result.hasFieldErrors()){

  4. List<FieldError> errorList = result.getFieldErrors();

  5. //通过断言抛出参数不合法的异常

  6. errorList.stream().forEach(item -> Assert.isTrue(false,item.getDefaultMessage()));

  7. }

  8. }

  9. public class AuthorizeIn extends BaseModel{

  10.  
  11. @NotBlank(message = "缺少response_type参数")

  12. private String responseType;

  13. @NotBlank(message = "缺少client_id参数")

  14. private String ClientId;

  15.  
  16. private String state;

  17.  
  18. @NotBlank(message = "缺少redirect_uri参数")

  19. private String redirectUri;

  20.  
  21. public String getResponseType() {

  22. return responseType;

  23. }

  24.  
  25. public void setResponseType(String responseType) {

  26. this.responseType = responseType;

  27. }

  28.  
  29. public String getClientId() {

  30. return ClientId;

  31. }

  32.  
  33. public void setClientId(String clientId) {

  34. ClientId = clientId;

  35. }

  36.  
  37. public String getState() {

  38. return state;

  39. }

  40.  
  41. public void setState(String state) {

  42. this.state = state;

  43. }

  44.  
  45. public String getRedirectUri() {

  46. return redirectUri;

  47. }

  48.  
  49. public void setRedirectUri(String redirectUri) {

  50. this.redirectUri = redirectUri;

  51. }

  52. }

在 controller 的方法需要校验的参数后面必须跟 BindingResult,否则无法进行校验。但是这样会抛出异常,对用户而言不太友好!

那怎么办呢?

很简单,我们可以利用上一节讲的异常处理,对报错进行拦截:

 
  1. @Component

  2. @Aspect

  3. public class WebExceptionAspect implements ThrowsAdvice{

  4.  
  5. public static final Logger logger = LoggerFactory.getLogger(WebExceptionAspect.class);

  6.  
  7. //拦截被GetMapping注解的方法 @Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping)")

  8. private void webPointcut() {

  9. }

  10.  
  11. @AfterThrowing(pointcut = "webPointcut()",throwing = "e")

  12. public void afterThrowing(Exception e) throws Throwable {

  13. logger.debug("exception 来了!");

  14. if(StringUtils.isNotBlank(e.getMessage())){

  15. writeContent(e.getMessage());

  16. }else{

  17. writeContent("参数错误!");

  18. }

  19.  
  20. }

  21.  
  22. /**

  23. * 将内容输出到浏览器

  24. *

  25. * @param content

  26. * 输出内容

  27. */

  28. private void writeContent(String content) {

  29. HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())

  30. .getResponse();

  31. response.reset();

  32. response.setCharacterEncoding("UTF-8");

  33. response.setHeader("Content-Type", "text/plain;charset=UTF-8");

  34. response.setHeader("icop-content-type", "exception");

  35. PrintWriter writer = null;

  36. try {

  37. writer = response.getWriter();

  38.  
  39. writer.print((content == null) ? "" : content);

  40. writer.flush();

  41. writer.close();

  42. } catch (IOException e) {

  43. e.printStackTrace();

  44. }

  45. }

  46. }

这样当我们传入不合法的参数时就会进入 WebExceptionAspect 类,从而输出友好参数。

我们再把验证的代码单独封装成方法:

 
  1. protected void validate(BindingResult result){

  2. if(result.hasFieldErrors()){

  3. List<FieldError> errorList = result.getFieldErrors();

  4. errorList.stream().forEach(item -> Assert.isTrue(false,item.getDefaultMessage()));

  5. }

  6. }

这样每次参数校验只需要调用 validate 方法就行了,我们可以看到代码的可读性也大大的提高了。

接口版本控制

一个系统上线后会不断迭代更新,需求也会不断变化,有可能接口的参数也会发生变化,如果在原有的参数上直接修改,可能会影响线上系统的正常运行,这时我们就需要设置不同的版本,这样即使参数发生变化,由于老版本没有变化,因此不会影响上线系统的运行。

一般我们可以在地址上带上版本号,也可以在参数上带上版本号,还可以再 header 里带上版本号,这里我们在地址上带上版本号,大致的地址如:http://api.example.com/v1/test,其中,v1 即代表的是版本号。具体做法请看代码:

 
  1. @Target({ElementType.METHOD,ElementType.TYPE})

  2. @Retention(RetentionPolicy.RUNTIME)

  3. @Documented

  4. @Mapping

  5. public @interface ApiVersion {

  6.  
  7. /**

  8. * 标识版本号

  9. * @return

  10. */

  11. int value();

  12. }

  13. public class ApiVersionCondition implements RequestCondition<ApiVersionCondition> {

  14.  
  15. // 路径中版本的前缀, 这里用 /v[1-9]/的形式

  16. private final static Pattern VERSION_PREFIX_PATTERN = Pattern.compile("v(\\d+)/");

  17.  
  18. private int apiVersion;

  19.  
  20. public ApiVersionCondition(int apiVersion){

  21. this.apiVersion = apiVersion;

  22. }

  23.  
  24. @Override

  25. public ApiVersionCondition combine(ApiVersionCondition other) {

  26. // 采用最后定义优先原则,则方法上的定义覆盖类上面的定义

  27. return new ApiVersionCondition(other.getApiVersion());

  28. }

  29.  
  30. @Override

  31. public ApiVersionCondition getMatchingCondition(HttpServletRequest request) {

  32. Matcher m = VERSION_PREFIX_PATTERN.matcher(request.getRequestURI());

  33. if(m.find()){

  34. Integer version = Integer.valueOf(m.group(1));

  35. if(version >= this.apiVersion)

  36. {

  37. return this;

  38. }

  39. }

  40. return null;

  41. }

  42.  
  43. @Override

  44. public int compareTo(ApiVersionCondition other, HttpServletRequest request) {

  45. // 优先匹配最新的版本号

  46. return other.getApiVersion() - this.apiVersion;

  47. }

  48.  
  49. public int getApiVersion() {

  50. return apiVersion;

  51. }

  52. }

  53. public class CustomRequestMappingHandlerMapping extends

  54. RequestMappingHandlerMapping {

  55.  
  56. @Override

  57. protected RequestCondition<ApiVersionCondition> getCustomTypeCondition(Class<?> handlerType) {

  58. ApiVersion apiVersion = AnnotationUtils.findAnnotation(handlerType, ApiVersion.class);

  59. return createCondition(apiVersion);

  60. }

  61.  
  62. @Override

  63. protected RequestCondition<ApiVersionCondition> getCustomMethodCondition(Method method) {

  64. ApiVersion apiVersion = AnnotationUtils.findAnnotation(method, ApiVersion.class);

  65. return createCondition(apiVersion);

  66. }

  67.  
  68. private RequestCondition<ApiVersionCondition> createCondition(ApiVersion apiVersion) {

  69. return apiVersion == null ? null : new ApiVersionCondition(apiVersion.value());

  70. }

  71. }

  72. @SpringBootConfiguration

  73. public class WebConfig extends WebMvcConfigurationSupport {

  74.  
  75. @Bean

  76. public AuthInterceptor interceptor(){

  77. return new AuthInterceptor();

  78. }

  79.  
  80. @Override

  81. public void addInterceptors(InterceptorRegistry registry) {

  82. registry.addInterceptor(new AuthInterceptor());

  83. }

  84.  
  85. @Override

  86. @Bean

  87. public RequestMappingHandlerMapping requestMappingHandlerMapping() {

  88. RequestMappingHandlerMapping handlerMapping = new CustomRequestMappingHandlerMapping();

  89. handlerMapping.setOrder(0);

  90. handlerMapping.setInterceptors(getInterceptors());

  91. return handlerMapping;

  92. }

  93. }

Controller 类的接口定义如下:

 
  1. @ApiVersion(1)

  2. @RequestMapping("{version}/dd")

  3. public class HelloController{}

这样我们就实现了版本控制,如果增加了一个版本,则创建一个新的 Controller,方法名一致,ApiVersion 设置为2,则地址中 v1 会找到 ApiVersion 为1的方法,v2 会找到 ApiVersion 为2的方法。

自定义 JSON 解析

Spring Boot 中 RestController 返回的字符串默认使用 Jackson 引擎,它也提供了工厂类,我们可以自定义 JSON 引擎,本节实例我们将 JSON 引擎替换为 fastJSON,首先需要引入 fastJSON:

 
  1. <dependency>

  2. <groupId>com.alibaba</groupId>

  3. <artifactId>fastjson</artifactId>

  4. <version>${fastjson.version}</version>

  5. </dependency>

其次,在 WebConfig 类重写 configureMessageConverters 方法:

 
  1. @Override

  2. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

  3. super.configureMessageConverters(converters);

  4. /*

  5. 1.需要先定义一个convert转换消息的对象;

  6. 2.添加fastjson的配置信息,比如是否要格式化返回的json数据

  7. 3.在convert中添加配置信息

  8. 4.将convert添加到converters中

  9. */

  10. //1.定义一个convert转换消息对象

  11. FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();

  12. //2.添加fastjson的配置信息,比如:是否要格式化返回json数据

  13. FastJsonConfig fastJsonConfig=new FastJsonConfig();

  14. fastJsonConfig.setSerializerFeatures(

  15. SerializerFeature.PrettyFormat

  16. );

  17. fastConverter.setFastJsonConfig(fastJsonConfig);

  18. converters.add(fastConverter);

  19. }

单元测试

Spring Boot 的单元测试很简单,直接看代码:

 
  1. @SpringBootTest(classes = Application.class)

  2. @RunWith(SpringJUnit4ClassRunner.class)

  3. public class TestDB {

  4.  
  5. @Test

  6. public void test(){

  7. }

  8. }

模板引擎

在传统的 SpringMVC 架构中,我们一般将 JSP、HTML 页面放到 webapps 目录下面,但是 Spring Boot 没有 webapps,更没有 web.xml,如果我们要写界面的话,该如何做呢?

Spring Boot 官方提供了几种模板引擎:FreeMarker、Velocity、Thymeleaf、Groovy、mustache、JSP。

这里以 FreeMarker 为例讲解 Spring Boot 的使用。

首先引入 FreeMarker 依赖:

 
  1. <dependency>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-freemarker</artifactId>

  4. </dependency>

在 resources 下面建立两个目录:static 和 templates,如图所示:

这里写图片描述

其中 static 目录用于存放静态资源,譬如:CSS、JS、HTML 等,templates 目录存放模板引擎文件,我们可以在 templates 下面创建一个文件:index.ftl(freemarker 默认后缀为 .ftl),并添加内容:

 
  1. <!DOCTYPE html>

  2. <html>

  3. <head>

  4.  
  5. </head>

  6. <body>

  7. <h1>Hello World!</h1>

  8. </body>

  9. </html>

然后创建 PageController 并添加内容:

 
  1. @Controller

  2. public class PageController {

  3.  
  4. @RequestMapping("index.html")

  5. public String index(){

  6. return "index";

  7. }

  8. }

启动 Application.java,访问:http://localhost:8080/index.html,就可以看到如图所示:

这里写图片描述

第03课:Spring Boot 启动原理

第04课:初识 Spring Cloud

第05课:服务注册与发现

第06课:服务网关

第07课:服务消费者

第08课:服务异常处理

第09课:配置中心

第10课:消息总线

第11课:服务链路追踪

第12课:分布式锁

第13课:分布式事务

第14课:Spring Cloud 实例详解——基础框架搭建(一)

第15课:Spring Cloud 实例详解——基础框架搭建(二)

第16课:Spring Cloud 实例详解——基础框架搭建(三)

第17课:Spring Cloud 实例详解——业务代码实现

第18课:Spring Cloud 实例详解——系统发布

第19课:Spring Cloud 源码解析

第20课:K8S+Docker 部署 Spring Cloud 集群

阅读全文: http://gitbook.cn/gitchat/column/5af108d20a989b69c385f47a

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值