spring boot 微服务入门

微服务:微服务是一种微服务架构思想的一种,在传统应用软件上的架构基础上,将系统业务按照功能拆分为更细的粒度的服务,每一个服务都是一个独立的应用,这些应用对外提供公共的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 AutoConfigurationImportSelector
		implements DeferredImportSelector, BeanClassLoaderAware, ResourceLoaderAware,
		BeanFactoryAware, EnvironmentAware, Ordered {


	@Override
	public 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);
	    }
	}


}

 

 

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

 

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
手把手视频详细讲解项目开发全过程,需要的小伙伴自行百度网盘下载,链接见附件,永久有效。 视频简介: 目前业界最流行的微服务架构正在或者已被各种规模的互联网公司广泛接受和认可,业已成为互联网开发人员必备技术。无论是互联网、云计算还是大数据Java平台已成为全栈的生态体系,其重要性几乎不可替代。Spring Boot作为微服务的基础设施之一,背靠强大的Spring 生态社区,支撑Spring Cloud技术体系。本课程采用由浅入深,层层递进的讲解方式, 让你轻松掌握SpringBoot的快速构建Spring项目的方式,并且还深入剖析SpringBoot内部核心原理,如:自动配置原理,start原理,自定义start等, 让你知其然,知其所以然 讲解方式: 本课程采用由浅入深,层层递进的讲解方式, 让你轻松掌握SpringBoot的快速构建Spring项目的方式,并且还深入剖析SpringBoot内部核心原理,如:自动配置原理,start原理,自定义start等, 让你知其然,知其所以然 课程亮点: 1、课程由浅到深,由原理到实践,适合零基础入门学习。 2、课程中包含大量SpringBoot 原理讲解、源码分析。 3、课程中涉及很多SpringBoot 实用插件技术、监控技术; 适用人群: 1、有一定的Java基础以及SSM框架知识。 2、对目前职业有进一步提升要求,希望从事数据行业高薪工作的在职人员。 基础课程主讲内容包括: 阶段一:SpringBoot 快速入门 1. SpringBoot介绍 2. SpringBoot的核心功能 3. SpringBoot的优势 4. SpringBoot入门程序 5. SpringBoot配置文件类型 6. 配置文件与配置类的属性映射方式 7. SpringBoot整合Mybatis 8. SpringBoot整合Junit 9. SpringBoot整合Redis 阶段二: SpringBoot核心原理 1. 起步依赖原理分析 2. 自动配置原理解析 3. 自定义起步依赖并实现自动配置 4. 事件监听 5. 初始化流程 6. SpringBoot服务监控

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值