简介
最近上手项目, 开始接触了 SpringBoot, 相比较 SpringMVC 而言, 省去了繁琐的配置, 秉承默认大于配置的原则, 使用起来更加简单方便。但越是
黑盒(简单), 使用起来越不安, 这里将学习过程中遇到的问题记录下来, 慢慢地打开这个黑盒
初始化工程问题
使用 Maven 初始化, 有两种方式, 以此解决 Maven 不支持多 parent 的问题
继承默认的 parent
1
2
3
4
5
6<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
</parent>
- 将 SpringBoot 的依赖添加到 dependencyManagement 中,并且设置 scope=import
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<dependencyManagement>
<dependencies>
<!-- Override Spring Data release train provided by Spring Boot -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Fowler-SR2</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<!-- Spring Boot basic dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.7.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2. Maven用户可以继承 spring-boot-starter-parent 项目来获取合适的默认设置。该parent项目提供以下特性
1
2
3
4
5
6
7
8
- 默认编译级别为Java 1.6
- 源码编码为 UTF-8
- 一个 dependency management 节点,允许你省略常见依赖的 <version> 标签,继承自 spring-boot-dependencies POM。
- 恰到好处的资源过滤
- 恰到好处的插件配置(exec插件,surefire,Git commit ID,shade)
- 恰到好处的对 application.properties 和 application.yml 进行筛选,
- 包括特定 profile(profile-specific)的文件,比如 applicationfoo.
- properties 和 application-foo.yml
3. Spring Boot Starters 列表
官方:https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#using-boot-starter
中文:http://blog.csdn.net/chszs/article/details/50610474
4. 配置工程自检功能:`spring-boot-starter-actuator`
1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
配置文件中增加:management.security.enabled=false
访问以下网站:
1
2
3
4
5
6
http://localhost:8080/beans
http://localhost:8080/env
http://localhost:8080/health
http://localhost:8080/metrics
http://localhost:8080/trace
http://localhost:8080/mappings
默认 的奥秘,来源于:
spring-boot-autoconfigure 的 JAR 文件
@SpringBootApplication 注解等价于以默认属性使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan,即开启组件扫描和自动配置
application.properties(yml) 默认加载路径:当前目录下 /config 子目录,当前目录,classpath 下的 /config,classpath 根路径。如果不喜欢将 application.properties 作为配置文件名,你可以通过指定 spring.config.name 环境属性来切换其他的名称,也可以使用spring.config.location 环境属性引用一个明确的路径(目录位置或文件路径列表以逗号分割)
Spring 4.0 的条件化配置是自动配置的基础,当满足一定条件时才会执行某段程序,比如在 classpath 中发现了 JdbcTemplate 时才自动注入,这使得众多 autoconfig 选项得以简化
配置的加载顺序
Spring Boot设计了一个非常特别的 PropertySource 顺序,以允许对属性值进行合理的覆盖,属性会以如下的顺序进行设值:
home目录下的devtools全局设置属性( ~/.spring-bootdevtools.properties ,如果devtools激活)
测试用例上的@TestPropertySource注解
测试用例上的@SpringBootTest#properties注解
命令行参数
来自 SPRING_APPLICATION_JSON 的属性(环境变量或系统属性中内嵌的内联JSON)
ServletConfig 初始化参数
ServletContext 初始化参数
来自于 java:comp/env 的JNDI属性
Java系统属性(System.getProperties())
操作系统环境变量
RandomValuePropertySource,只包含 random.* 中的属性
没有打进jar包的Profile-specific应用属性( application-{profile}.properties 和YAML变量)
打进jar包中的Profile-specific应用属性( application-{profile}.properties 和YAML变量)
没有打进jar包的应用配置( application.properties 和YAML变量)
打进jar包中的应用配置( application.properties 和YAML变量)
@Configuration 类上的 @PropertySource 注解
默认属性(使用 SpringApplication.setDefaultProperties 指定)
开发调试问题
使用 spring-boot-devtools 来实现热部署(TODO)
Maven 构建插件的主要功能是把项目打包成一个可执行的超级 JAR(uber-JAR),包括把应用程序的所有依赖打入 JAR 文件内,并为 JAR 添加一个描述文件,其中的内容能够通过
java -jar
来运行应用程序
Spring 启动问题
精辟:读取配置说明(xml,java配置,Groovy配置,其他类型配置),再应用程序上下文里初始化 Bean,将 Bean 注入依赖他们的其他 Bean 中,Spring 帮你通过 组件扫描,自动织入和生命切面等额外辅助功能,帮你简单的做了初始化的事情
导入的 starter 是如何 在 application.properties 中给出配置提示的
starter 的包 /META-INF/spring.factories 中会有 org.springframework.boot.autoconfigure.EnableAutoConfiguration 自动配置的实现类,打开这个类可以看到实现了 ImportBeanDefinitionRegistrar 接口的 registerBeanDefinitions 方法,如此一来,配置文件中就会给出提示啦
Spring Cloud 大家族
1、Spring Cloud Config 配置中心,利用git集中管理程序的配置。
2、Spring Cloud Netflix 集成众多Netflix的开源软件
3、Spring Cloud Bus 消息总线,利用分布式消息将服务和服务实例连接在一起,用于在一个集群中传播状态的变化
4、Spring Cloud for Cloud Foundry 利用Pivotal Cloudfoundry集成你的应用程序
5、Spring Cloud Cloud Foundry Service Broker 为建立管理云托管服务的服务代理提供了一个起点。
6、Spring Cloud Cluster 基于Zookeeper, Redis, Hazelcast, Consul实现的领导选举和平民状态模式的抽象和实现。
7、Spring Cloud Consul 基于Hashicorp Consul实现的服务发现和配置管理。
8、Spring Cloud Security 在Zuul代理中为OAuth2 rest客户端和认证头转发提供负载均衡
9、Spring Cloud Sleuth SpringCloud应用的分布式追踪系统,和Zipkin,HTrace,ELK兼容。
10、Spring Cloud Data Flow 一个云本地程序和操作模型,组成数据微服务在一个结构化的平台上。
11、Spring Cloud Stream 基于Redis,Rabbit,Kafka实现的消息微服务,简单声明模型用以在Spring Cloud应用中收发消息。
12、Spring Cloud Stream App Starters 基于Spring Boot为外部系统提供spring的集成
13、Spring Cloud Task 短生命周期的微服务,为SpringBooot应用简单声明添加功能和非功能特性。
14、Spring Cloud Task App Starters
15、Spring Cloud Zookeeper 服务发现和配置管理基于Apache Zookeeper。
16、Spring Cloud for Amazon Web Services 快速和亚马逊网络服务集成。
17、Spring Cloud Connectors 便于PaaS应用在各种平台上连接到后端像数据库和消息经纪服务。
18、Spring Cloud Starters (项目已经终止并且在Angel.SR2后的版本和其他项目合并)
19、Spring Cloud CLI 插件用Groovy快速的创建Spring Cloud组件应用。