springBoot
微服务
集群:一个业务,分布在不同的计算机上
Redis服务器
分布式:多个业务分布在不同的计算机上
MapReduce
微服务:可以理解为一种分布式
单体应用架构——SOA架构——微服务架构
优点:从紧耦合到松耦合到解耦合,便于修改
缺点:分布式系统的复杂性
单体应用架构是一个整体,将MVC三层同时打包
SOA架构,将三层分开设计
微服务架构,将三层又分别切分为一个个微服务
每一个微服务都是不同的功能
一个简单的hello程序(简化springmvc)
1:导入Maven依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2:编写主程序
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
//启动
SpringApplication.run(HelloWorldMainApplication.class, args);
}
}
3:编写业务模型(handler)
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello SpringBoot,this is my first Application";
}
}
执行上述程序,即可在页面上打印:hello SpringBoot,this is my first Application
启动器
基本依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/>
</parent>
<!--
org.springframework.boot他的父项目是spring-boot-dependencies
他来真正管理Spring Boot应用里面的所有依赖版本;
Spring Boot的版本仲裁中心;
以后我们导入依赖默认是不需要写版本;(没有在dependencies里面管理的依赖自然需要声明版本号)
-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
1:父依赖
若ABC分别倒入log4j,当修改版本,则都要修改。
优化为parent倒入log4j,则ABC只需要倒入parent即可
根据版本仲裁原则进行依赖
版本仲裁原则:对于依赖某个组件的多个版本,maven的仲裁过程,并不是简单的使用高版本,而是根据从根节点到各个组件节点之间的路径深度,路径短的组件优先,如果路径深度相同,则是先发现的那个。类似一棵树的广度遍历
2:spring-boot场景启动器dependency
父依赖实际上就是包含多个启动器dependency
spring-boot-starter:帮我们导入了web模块正常运行所依赖的组件;
Spring Boot将所有的功能场景都抽取出来,做成一个个的starters(启动器)
starters:包含了一系列可以集成到应用里面的依赖包
Spring Initializer?
简单的生成一个Spring Boot项目,包含快速启动所需的一切。和往常一样,我们需要一个好的项目框架;它有助于你正确创建项目结构/框架。
通过图形化选择取代maven加载包等
YAML
标记语言:用于为 Bean读入数据
以前的配置文件;大多都使用的是 xxxx.xml文件;
YAML:以数据为中心,比json、xml等更适合做配置文件;
对象、Map(属性和值):
k: v在下一行来写对象的属性和值的关系;注意缩进
person:
name: 张三
gender: 男
age: 22
数组(List、Set)
fruits:
- 苹果
- 桃子
- 香蕉
实例:Bean文件的注解装配
方式一:@ConfigurationProperties
方式二:@Component和@value
但是@ConfigurationProperties支持复杂类型封装,支持校验,不支持EL表达式。
@ConfigurationProperties(prefix = “person.yaml”)
public class Person{
-----
}
person.yml文件:
person:
name: 张三
gender: 男
age: 36
boss: true
birth: 1982/10/1
maps: {k1: v1,k2: v2}
lists:
- apple
- peach
- banana
pet:
name: 小狗
age: 12
测试文件:@SpringBootTest表明测试文件
@SpringBootTest
class HelloworldquickstartApplicationTest