Content
微服务和单体应用的宏观理解
开始一个HelloWorld应用
HelloWorld分析
使用Spring Initializr快速构建SpringBoot应用
微服务和单体应用的宏观理解
微服务:一组小型应用通过HTTP的方式进行沟通的开发思想
单体应用:ALL IN ONE
单体应用的不足:
随着业务逻辑的不断更新和迭代开发,起初的小型应用会不断膨胀,当应用达到一定规模量时,敏捷开发和部署维护的效率就会举步维艰。也就是说,单体应用的可持续性能力较弱。
微服务的解决方案:
通过分解巨大的单体式应用使得可持续性开发和优化等扩展能力增强。
自由的开发技术选择。(不同组件的功能可以使用更加合适的技术实现)
微服务架构的不足:
微服务本身的分布式系统导致固有复杂性:组件之间消息传递的通讯成本。
迭代开发所遗留的耦合问题依然存在:在修改某一个服务时,其他与之相关的服务不可避免的也要相应修改,但较单体应用来说,业务逻辑更清晰,耦合度更低。
总结:相对来说,单体式开发更加适合简单轻量的应用,微服务虽然仍旧存在不足,但更适合构建复杂应用开发。
开始一个HelloWorld应用
1.新建Maven工程
2.导入pom依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3.创建主程序
package zkrun.top;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloWorld {
public static void main(String[] args) {
SpringApplication.run(HelloWorld.class,args);
}
}
4.创建测试Controller
package zkrun.top.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello()
{
return "hello world";
}
}
5.测试运行
6.部署
命令行直接部署
java -jar springboot-helloworld-1.0-SNAPSHOT.jar
HelloWorld分析
1. parent版本仲裁:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
spring-boot-starter-parent依赖的父项目spring-boot-dependencies定义了几乎所有经常使用依赖的版本,所以在以后导入依赖通常是不需要写版本号的(除了一些特殊的没有被声明的包)
2.spring-boot-starter场景启动器
web模块:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
spring-boot-starter-web包含:
场景启动器原理:
springboot是对spring框架的迭代封装(如果把spring理解为Java开发的框架,那springboot就是spring开发的框架)。
举个栗子:在spring开发中常用的jar包,例如spring在web开发中常用的spring-web和spring-webmvc这些都被集成到了spring-boot-starter-web场景启动器中。那么在springboot中开发web应用就不用导入web所需的包,而是直接使用已经集成了web常见包的场景启动器。
通俗来说,场景启动器就是某一类模块的jar包集合。
3.主程序类
package zkrun.top;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloWorld {
public static void main(String[] args) {
SpringApplication.run(HelloWorld.class,args);
}
}
@SpringBootApplication
@SpringBootConfiguration
@Configuration
@EnableAutoConfiguration 开启自动配置
将主配置类(@SpringBootApplication标注的类)的所在包及子包里面所有的组件扫描进IOC容器。
使用Spring Initializr快速构建SpringBoot应用
step1:创建Spring Initializr工程
step2:指定Maven相关配置
step3:选择项目所需模块
step4:创建Controller测试
package zkrun.top.springboot_quick.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//@Controller
//@ResponseBody
@RestController
public class HelloControlller {
@RequestMapping("/hello")
public String hello()
{
return "hello world quick";
}
}
其中RestController
使用Spring Initializr快速构建的特点
- 主程序默认已生成
- resource文件夹目录结构
- static:保存静态资源,js,css,img
- templates:保存所有的模板页面;(SpringBoot默认jar包使用嵌入式的tomcat,默认不支持jsp页面);可以使用模板引擎(freemarker,thymeleaf)
- application.properties:SpringBoot应用的配置文件