SpringBoot基础
微服务
- 解决大一统的服务化架构的问题
- 代码冲突问题
- 交付复杂,影响面大
- 测试困难
- 微服务的好处
- 可扩展性
- 隔离性
- 灵活性,多语言多技术生态
- 微服务的挑战
- 保持微服务的互通性
- 通过标准化降低开发成本
JavaConfig常用注解
- @Bean : 用于方法时,其返回值将作为bean注册到Ioc容器,方法名默认为id,也可以指定id
- @PropertySource : 加载*.properties文件,与@PropertySourcePlaceholderConfigurer配合使用来替换占位符
- @ImportResource : 加载*.xml文件
- @Import : 在当前JavaConfig配置类中整合另一个JavaConfig配置类
- @Conditional: 用于实现基于条件的“智能”自动化配置
- @AutoConfigureBefore/@AutoConfigurateAfter:用于调整自动配置的顺序
@SpringBootApplication
- 是一个三体复合Annotation
- @Configuration : 用于类表示这是一个JavaConfig配置类
- @EnableAutoConfiguration : 与其它的Enable开头的Annotation一样,通过@Import和SpringFactoriesLoader将所有@Configuration汇总加载到当前SpringBoot创建并使用的Ioc容器
- @ComponentScan : 扫描包,加载那些需要ComponentScan来加载的Bean
- 默认扫描SpringBootApplication启动类所在包与子包
- 如果需要扫描依赖jar包里的annonation,需要手动设置包路径@ComponentScan("com.citi.cv.rates")
- 示例
- @SpringBootApplication(scanBasePackages = { "com.common, com.db" )
- @PropertySource(value = { "x.${SERVER_ENV}.properties" })
- SpringApplication.run(BondLevelsLiveApp.class, args);
约定优于配置
- 配置方式的优先级
- 命令行参数
- 系统环境变量
- 文件系统中的配置文件
- classpath中的配置文件
- 代码中的配置项
- Spring默认配置文件application.properties
- 可以放在项目根目录
- 或者放在根目录下的config子目录下
其他
- spring-boot-maven-plugin可以将项目打包成可执行jar包
- http://start.spring.io/ : 官方构建项目脚手架
spring-boot-starter
开箱即用的依赖模块
- 以spring-boot-starter开头
- parent都是spring-boot-starters
- spring-boot-starters的parent是spring-boot-parent
- spring-boot-parent的parent是spring-boot-dependencies
spring-boot-starter-web
- Maven项目的webapp下面的所有东西放至src/main/resource下面
- src/main/resource/static下面放html,js,css等静态资源,可以跟在contextPath后面访问
spring-boot-starter-actuator
spring boot 2的变化
- endpoints.xxx 变为management.endpoint.xxx :注意endpoints的s没有了
- mapping path加上actuator
- management.endpoints.web.base-path=/ 修改默认path
- 默认只开启health/info
- management.endpoints.jmx.exposure.exclude
- management.endpoints.jmx.exposure.include=* 开启所有功能,默认*
- management.endpoints.web.exposure.exclude=env
- management.endpoints.web.exposure.include* 开启所有功能,默认 info, health
应用配置类
- mappings : Spring MVC controller映射
- beans : 查看spring容器的bean
- env : 查看系统属性,环境变量,自定义属性的配置
- configprops : 查看application属性的配置,属性的配置路径
- autoconfig :查找生效与未生效的自动化配置
- info : 查看application自定义信息,通过info.app.name=CitiVelocity, info.app.version=1.0.1配置
指标监控类
- metrics: 监控内存,线程数,gc, http请求,启动时间等信息
- health : 检查服务器状态,各种数据库,服务器的链接状态,磁盘空间使用情况
- dump : 所有线程详细信息
- trace : 最近100条http请求信息
操作控制类
- shutdown :
- 正确关闭spring boot 应用的方法
- endpoints.shutdown.enabled=true
- management.security.enabled=false 或者 endpoints.shutdown.sensitive=false关闭用户验证
- POST请求,可以用curl -X POST http://localhost:7070/MarkdownNote/shutdown
- 搭配spring boot security进行权限校验
其他
- 有些功能需要配置spring security或者设置management.security.enabled=false
spring boot 配置
- application.properties放在src/main/resource下面
- server.port=8081
- server.contextPath=/MarkdownNote
- server.tomcat.basedir=C://Base
- 除了INT,还支持YAML格式,但是YAML目前不支持@PropertySource来加载配置文件
- 配置文件里面也支持${xxx}进行参数引用
- 随机参数
- ${random.value}
- ${random.int}
- ${random.long}
- ${random.int[0,100]}
- 不同环境
- application-{profile}.properties : profile一般为dev,uat,prod
- spring.profiles.active=uat指定加载uat的配置
- 修改可执行jar包的配置文件
- 如果jar包同级目录下有application.properties,会直接加载这个而不是jar包里的
配置详解
- 修改默认path : management.endpoints.web.base-path
- springboot 2.0.x 默认/actuator
开发注意事项
修改spring boot内置的tomcat的 docBase目录
- 如果有src/main/webapp目录,docBase默认为此目录
- 如果没有则使用temp目录C:\Users\xs47604\AppData\Local\Temp\tomcat-docbase.58621522083049082.8081
- 如果打成可执行jar包也使用temp目录
自定义目录方法
@Bean public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer() { EmbeddedServletContainerCustomizer s = (ConfigurableEmbeddedServletContainer container) -> { container.addInitializers(); File file = new File("C://docbase"); if(!file.exists()) file.mkdirs(); container.setDocumentRoot(file); }; return s; }
创建resourcees目录下的File
- Resource resource = new ClassPathResource("DataSource.xml");
- File file = resource.getFile();
- 打包成jar之后不能通过操作系统的文件系统找到文件,可以使用InputStream
- InputStream inputStream = resource.getInputStream();
- File file = new File(inputStream)