SpringBoot详解
1.什么是微服务?
- 实现和实施思路则更强调功能趋向单一,服务单元小型化和微型化
- 将功能进行拆分,将服务粒度做小,使之可以独立承担对外服务的职责
- 微服务带来的好处
- 可扩展性
- 隔离性
2.spring框架的本质?
- IOC
- DI(Dependency Injection):加载ApplicationContext通过getBean获取对象
- DL(Dependency Lookup)
- 收集和注册
- 分析和组装
- javaConfig
- 表达形式层面
- 基于XML的方式
- 基于@Configuration的方式
- 注册备案的定义层面
- 基于XML的方式
- 基于@Bean的方式
- 常用的Annotation
- @ComponentScan
- @PropertySource与@PropertySources
- @Import与@ImportResource
- 表达形式层面
3.springboot工作机制
- 约定优先于配置(Convention Over Configuration)
- @SpringBootApplication
- @Configuration springboot配置类
- @EnableAutoConfiguration 开启自动配置
- 通过@Import的帮助,将所有符合自动配置条件的bean定义加载到IOC容器
- SpringFactoriesLoader,从指定的配置文件META-INF/spring.factories加载配置
- 格式key=value.示例:example.Service=example.MyServiceImpl1
- @ComponentScan
- 自动扫描并加载符合条件的组件或bean定义,最终将这些bean定义加载到容器中
- SpringBoot程序启动
- SpringApplication.run(xxxxx.class) 启动主程序
- SpringApplication bootstrap = new SpringApplication(xxxx.class)
- 加载过程
- SpringApplicationRunListener
- ApplicationListener
- 通过SpringApplication.addListeners(…)或者SpringApplication.setListeners(…)添加
- 通过SpringFactoriesLoader机制,在META-INF/spring.factories文件中添加配置
- ApplicationContextinitializer
- CommandLineRunner
- 自动配置
- 基于条件的自动配置 @Conditional
- 调整自动配置的顺序 @AutoConfigureBefore 或者 @AutoConfigureAfter
4.spring-boot-starter
- 介绍
- 开箱即用
- 干预配置方式的种类
- 命令行参数(Command Line Args)
- 系统环境变量(Environment Variables)
- 位于文件系统中的配置文件
- 位于classpath中的配置文件
- 固化到代码中的配置项
- 默认配置文件:application.properties 存放在当前项目的根目录或者config的子目录下
- spring-boot-starter-logging
- logging.file或者logging.path来定义日志输出文件的名称和存放路径
- 扩展
- spring-boot-starter-log4j
- spring-boot-starter-log4j2
- spring-boot-starter-web
- 默认错误页面(whitelabel error page)
- mvn spring-boot:run 内嵌了tomcat
- 资源存放
- src/main/resources/static 用于存放各种静态资源,比如css,js等
- src/main/resources/templates用于存放模板文件,比如*.vm
- 嵌入式web容器层面的约定
- 服务器配置,server开始的配置,比如server.port=9000
- EmbeddedServletContainerCustomizer 类对容器进行定制
- 常用容器
- spring-boot-start-tomcat
- spring-boot-start-jetty
- spring-boot-start-undertow
- spring-boot-starter-jdbc
- 多数据声明的时候,避免注入异常,可以采用 @primary
- 扩展
- spring-boot-starter-data-jpa
- spring-boot-starter-mongodb
- spring-boot-starter-aop
- sprign.aop.auto=true 开启缓存
- spring.aop.proxy-target-class=false
- 扩展:与spring-boot-starter-metrics整合收集应用性能
- spring-boot-starter-security 应用安全
- spirng-boot-starter-actuator 应用监控
- Sensor用于"监"的场景,Actuator则服务于"控"的场景
- Sensor类的endpoints
- autoconfig
- beans
- configprops
- info
- health
- env
- metrics
- trace
- mapping
- Actuator类的endpoints
- shutdown
- dump
- Sensor类的endpoints
- 自定义应用的健康状态检查
- 应用监控的支持和实现方案
- CrshAutoConfiguration与spring-boot-starter-remote-shell
- SpringBoot的Metrics与Dropwizard的Metrics
- Auditing与Trace
- Sensor用于"监"的场景,Actuator则服务于"控"的场景
- 约定配置三板斧
- 建议优先使用相应spring-boot-starter默认的约定配置
- 建议约定配置无法满足需要的前提下,在基于spring-boot-starter原有约定配置的基础上进行适当的扩展配置
- 如果约定加适当扩展配置还是无法满足需求,则允许开发者推倒重来,基于spring框架的一些原有特性直接实现自已想要的功能
5.springboot微服务实践与探索
- 介绍
- 开发阶段,通过相应的微框架提供标准化的统一开发体验和支持
- 发布阶段,通过标准化的形式统一发布和管理
- 运维阶段,通过标准化的方式统一维护数量庞大的微服务
- 使用SpringBoot构建微服务
- 基于Dubbo框架
- 缺点
- 只限于Java应用之间的服务调用
- 服务访问需要依赖API及关联依赖,很多场景下会导致依赖管理混乱的问题
- 不易维护和升级
- 缺点
- 基于SpringBoot快速构建Web API
- 定义Web API规范
- 根据规范构建Web API
- 显式的强类型封装方式 — 范型
- 隐式的自动转换方式 ---- HttpMessageConverter
- Web API的短板和不足
- 根据代码元信息生成API文档(Swagger)
- API特性
- 提供API的功能支持,提供显式和隐式的转换方式
- 提供API文档相关功能的配置和设置
- 提供统一的Web API访问错误处理逻辑
- 使用SpringBoot构建其他形式的微服务 — akka框架的Actor模型
- 基于Dubbo框架
- 发布与部署
- 基于可执行的jar包 — mvn package
- 项目托管于Maven私服维护
- 基于RPM的发布与部署
- 基于Docker的发布与部署 — Dockerfile
- 注册与发现
- Dubbo基于Redis或者Zookeeper的注册与发现机制
- Spring Boot基于DNS的服务注册与发现
- 也可以基于Service Proxy的方式
- 监控与运维
- 监控
- 硬件层面
- 网络层面
- 系统层面
- Spring Boot微服务的应用层面
- 服务访问层面
- Zabbix 或者 Nagios
- 日志收集 - ELK
- 关于对日志的收集是推(PUSH)还是拉(PULL)好
- 局部性触发式报警到系统性智能化报警
- 监控
- 安全与防护
- 后端管理接口的安全防护一定不可以省略
6.SpringBoot与Scala
- Java平台三架强力马车,语言,类库和虚拟机
- 基于Maven构建和发布基于Spring Boot的Scala应用
- 需要引入Scala的编译器
- 引入Scala的类库
- 引入Scala的插件scala-maven-plugin
- 代码简化
- 基于Dash工具
- 基于spring-boot-starter-Scala简化
- Maven项目的依赖可以传递,但是插件不能传递
- 通过SPRING INITIALIZR构建初始化项目
- 使用Maven Archetype简化项目创建
- 基于Scala的Web API开发
- 使用SBT构建和发布基于SpringBoot的Scala应用
- 一个SpringBoot项目基本必备依赖
- spring-boot
- spring-boot-autoconfigure
- spring-boot-starter-logging
- spring-core
- snakeyaml
- 一个SpringBoot项目基本必备依赖