微服务:微服务是一种微服务架构思想的一种,在传统应用软件上的架构基础上,将系统业务按照功能拆分为更细的粒度的服务,每一个服务都是一个独立的应用,这些应用对外提供公共的API,可以独立承担对外的职责。通过这种思想所开发的软件服务就是微服务。

微服务作为一种新型的技术架构,主要是它的优点是可以拆分很多很小的服务,单独团队开发,这样的话,可以省去不少人力,又可以快速部署,上手容易, 还可以和其它组件整合,是一个不错的中间件框架, 在Spring的基础上开发的,代码很简单,启动方式却很独特,主要是在主类中加注解启动。

Springboot的用法,在Spring框架基础上不变的是pom.xml配置,就是减少了代码量。启动方式如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*** 启动类* @author mac1094**/
@SpringBootApplication
public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);}}

就是从这里启动的,简单的几行代码。

它是怎么启动的?我们就要看源码探索了。

类中通过注解完成启动的,这个注解代表的什么意思?

这个注解代表的一切从这里开始启动,就是自动装配原理,没有这个注解,代码启动会报错。所以我们从这个注解开始探索。

从右边底部注解开始AutoConfigurationimportSelector开始向上看,到enableAutoConfiguration。

public class AutoConfigurationImportSelectorimplements DeferredImportSelector, BeanClassLoaderAware, ResourceLoaderAware,BeanFactoryAware, EnvironmentAware, Ordered {@Overridepublic String[] selectImports(AnnotationMetadata annotationMetadata) {if (!isEnabled(annotationMetadata)) {return NO_IMPORTS;}AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);AnnotationAttributes attributes = getAttributes(annotationMetadata);//去mata-info/spring.factories文件中 查询 EnableAutoConfiguration对于值List<String> configurations = getCandidateConfigurations(annotationMetadata,attributes);//去除重复的配置类,若我们自己写的starter 可能存主重复的configurations = removeDuplicates(configurations);Set<String> exclusions = getExclusions(annotationMetadata, attributes);checkExcludedClasses(configurations, exclusions);configurations.removeAll(exclusions);//根据maven 导入的启动器过滤出 需要导入的配置类configurations = filter(configurations, autoConfigurationMetadata);fireAutoConfigurationImportEvents(configurations, exclusions);return StringUtils.toStringArray(configurations);}
}   //去spring.factories 中去查询EnableAutoConfirution类private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {MultiValueMap<String, String> result = cache.get(classLoader);if (result != null) {return result;}try {Enumeration<URL> urls = (classLoader != null ?classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));result = new LinkedMultiValueMap<>();while (urls.hasMoreElements()) {URL url = urls.nextElement();UrlResource resource = new UrlResource(url);Properties properties = PropertiesLoaderUtils.loadProperties(resource);for (Map.Entry<?, ?> entry : properties.entrySet()) {List<String> factoryClassNames = Arrays.asList(StringUtils.commaDelimitedListToStringArray((String) entry.getValue()));result.addAll((String) entry.getKey(), factoryClassNames);}}cache.put(classLoader, result);return result;}catch (IOException ex) {throw new IllegalArgumentException("Unable to load factories from location [" +FACTORIES_RESOURCE_LOCATION + "]", ex);}}

看懂这个注解就知道自动装配原理了。

启动流程:

1.运行main()方法:

2.运行run()方法:

public class SpringBootSource {public ConfigurableApplicationContext run(String... args) {// 1.创建并启动计时监控类StopWatch stopWatch = new StopWatch();stopWatch.start();// 2.声明应用上下文对象和异常报告集合ConfigurableApplicationContext context = null;Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();// 3.设置系统属性 headless 的值this.configureHeadlessProperty();// 4.创建所有 Spring 运行监听器并发布应用启动事件SpringApplicationRunListeners listeners = this.getRunListeners(args);listeners.starting();Collection exceptionReporters;try {// 5.处理 args 参数ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);// 6.准备环境ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);this.configureIgnoreBeanInfo(environment);// 7.创建 Banner 的打印类Banner printedBanner = this.printBanner(environment);// 8.创建应用上下文context = this.createApplicationContext();// 9.实例化异常报告器exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);// 10.准备应用上下文this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);// 11.刷新应用上下文this.refreshContext(context);// 12.应用上下文刷新之后的事件的处理this.afterRefresh(context, applicationArguments);// 13.停止计时监控类stopWatch.stop();// 14.输出日志记录执行主类名、时间信息if (this.logStartupInfo) {(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);}// 15.发布应用上下文启动完成事件listeners.started(context);// 16.执行所有 Runner 运行器this.callRunners(context, applicationArguments);} catch (Throwable var10) {this.handleRunFailure(context, var10, exceptionReporters, listeners);throw new IllegalStateException(var10);}try {// 17.发布应用上下文就绪事件listeners.running(context);// 18.返回应用上下文对象return context;} catch (Throwable var9) {this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);throw new IllegalStateException(var9);}}}

有时间的可以看下以下代码,从中可以看到很多细节,就是流程如何启动的?

以上就是我对源码的解读,希望能帮到大家,谢谢!如有错误之处,请提出宝贵的意见!

spring boot 微服务入门相关推荐

  1. spring boot 微服务集群 + 注册中心

    spring boot 微服务框架下载地址: https://start.spring.io/ 注册中心 Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进 ...

  2. Docker容器及Spring Boot微服务应用

    2019独角兽企业重金招聘Python工程师标准>>> Docker容器及Spring Boot微服务应用 1 什么是Docker 1.1 Docker的出现 问题一:项目实施环境复 ...

  3. Spring Boot微服务的黑匣子测试是如此简单

    当我需要进行原型设计,概念验证或在空闲时间使用一些新技术时,开始新项目对于Maven来说总是有点烦人. 不得不说,设置Maven项目并不难,您可以使用Maven原型. 但是原型通常是过时的. 谁想玩旧 ...

  4. Docker基础篇 - (六)Docker 网络Spring Boot微服务打包Docker镜像

    ⑦ Docker 网络 7.1 理解Docker0 清空下前面的docker 镜像.容器 # 删除全部容器 [root@cVzhanshi tomcat-diy]# docker rm -f $(do ...

  5. 视频教程-Spring Cloud微服务--入门到精通-Java

    Spring Cloud微服务--入门到精通 本系列课程由多位老师共同录制而成,旨在为想要学习Java的用户提供一套系统的成长方案. Java从入门到进阶 ¥59.00 立即订阅 扫码下载「CSDN程 ...

  6. Spring Boot 微服务编码风格指南和最佳实践

    文奇摄于世界尽头州立公园 通过多年来使用 Spring Boot 微服务,我编制了一份编码风格指南和最佳实践列表.这份清单并不全面,但我希望您能找到一两点可以借鉴的地方,无论您是新手还是经验丰富的 S ...

  7. Spring Boot微服务间文件返回实现

    Feign接口获取文件流问题_Java_wyazyf的博客-CSDN博客 https://blog.csdn.net/wyazyf/article/details/93200033 Spring Bo ...

  8. 高级版的 jvisualvm :Spring Boot Admin 监控 Spring Boot 微服务项目

    前奏:先说一下 Java VisualVM Java VisualVM 是一个能够监控 JVM 的 jdk 自带的图形化工具: 在 $JAVA_HOME/bin 目录下,可直接运行它. 要想监控远程服 ...

  9. Spring Boot微服务,Docker和Kubernetes研讨会–第3部分

    在之前的文章中,我们为使用Docker和Spring Boot的订单管理系统构建了一些微服务(订单服务,产品服务,客户服务). 我们使用Netflix库来管理,发现和平衡微服务. 管理这些微服务及其多 ...

最新文章

  1. 一文搞懂K近邻算法(KNN),附带多个实现案例
  2. 点云距离度量:完全解析EMD距离(Earth Mover's Distance)
  3. Nodejs教程30(完结):PM2入门
  4. java 二维数组内存溢出_模拟Java内存溢出
  5. linux 同步与异步--阻塞与非阻塞型I/O
  6. 闭包函数 装饰器 迭代器
  7. 【学习笔记】斜率优化
  8. redis php 守护进程,PHP守护进程利用Redis队列实现业务
  9. 小米 android 8,小米华为们谁最良心?10大手机厂商安卓8.0升级情况盘点
  10. 一个PHP压缩类,在线压缩文件
  11. 详述支付路由的设计方案
  12. 用数据告诉你出租车资源配置是否合理
  13. C语言课设图书管理系统(大作业)
  14. 北邮通信博士万字长文,带你深入了解 4G/5G 区别!
  15. Colorbox 参数设置-中文版
  16. java俄罗斯方块七中图形类_shell中的俄罗斯方块小游戏
  17. 你能说更多关于崩坏3琪亚娜的细节吗
  18. 技术干货| MindSpore新一代自主研发分子模拟库:Mind-Sponge
  19. linux高可用小知识点汇总-行云管家
  20. 【Web技术】剖析前端异常及降级处理

热门文章

  1. 安装Linux win双系统 无法正常启动 读不出U盘
  2. 【LOJ 6485】LJJ 学二项式定理(单位根反演)(模板)
  3. 艾诺迪亚【八门神器+超级教程】
  4. Linux下同时打开编辑多个文件 【VSP、vim -o】
  5. PTA L1-054 福到了 (15 分)
  6. mysql字段类型详解_MySQL字段类型详解
  7. 基于javaweb的小区物业管理系统
  8. 专访美创科技王利强:站在数字化转型的时代浪尖上,争做行业推动者
  9. sso单点登陆实现过程汇总记录
  10. mysql delete in删除数据