SpringBoot学习笔记(一)

SpringBoot学习笔记

核心思想

约定大于配置 (CoC: Convention over Configuration),开发人员仅需规定应用中不符合约定的那部分,定制化一下就可以。而绝大部分约定俗成的东西Spring Boot 已经帮你配置好了。不用再繁琐的去配置各种xml以及bean文件。

为什么使用SpringBoot

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.

能快速创建出生产级别的Spring应用

简单说就是将各种spring技术栈(spring,springsecurity,spring data)整合起来,方便开发

SpringBoot 优点

  • Create stand-alone Spring applications

  • 创建独立Spring应用

  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)

  • 内嵌web服务器

  • Provide opinionated ‘starter’ dependencies to simplify your build configuration

  • 自动starter依赖,简化构建配置

  • Automatically configure Spring and 3rd party libraries whenever possible

  • 自动配置Spring以及第三方功能

  • Provide production-ready features such as metrics, health checks, and externalized configuration

  • 提供生产级别的监控、健康检查及外部化配置

  • Absolutely no code generation and no requirement for XML configuration

  • 无代码生成、无需编写XML

SpringBoot 是整合spring 技术栈的一站式框架,SpringBoot 是简化Spring技术栈的快速开发脚手架。

SpringBoot 缺点

  • 人称版本帝,迭代快,需要时刻关注变化
  • 封装太深,内部原理复杂,不容易精通

微服务

James Lewis and Martin Fowler (2014) 提出微服务完整概念。https://martinfowler.com/microservices/

In short, the microservice architectural style is an approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API. These services are built around business capabilities and independently deployable by fully automated deployment machinery. There is a bare minimum of centralized management of these services, which may be written in different programming languages and use different data storage technologies.-- James Lewis and Martin Fowler (2014)

  • 微服务是一种架构风格

  • 一个应用拆分为一组小型服务

  • 每个服务运行在自己的进程内,也就是可独立部署和升级

  • 服务之间使用轻量级HTTP交互

  • 服务围绕业务功能拆分

  • 可以由全自动部署机制独立部署

  • 去中心化,服务自治。服务可以使用不同的语言、不同的存储技术

分布式

将各个小应用系统搭建起来 ,组成一个分布式系统。

分布式的困难

  • 远程调用

  • 服务发现

  • 负载均衡

  • 服务容错

  • 配置管理

  • 服务监控

  • 链路追踪

  • 日志管理

  • 任务调度

分布式的解决方案

image-20210728233252388

云原生

原生应用如何上云。Cloud Native

上云的困难

  • 服务自愈
  • 弹性伸缩
  • 服务隔离
  • 自动化部署
  • 灰度发布
  • 流量治理

image-20220115103321673

SpringBoot 官方文档

SpringBoot 官方文档

image-20220115103540305

SpringBoot2入门

1、系统要求

  • Java 8 & 兼容java14 .
  • Maven 3.3+
  • idea 2019.1.2

image-20220117110113717

1.1、maven设置

// 配置阿里云镜像
<mirrors>
      <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>central</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
      </mirror>
  </mirrors>
 
  <profiles>
         <profile>
              <id>jdk-1.8</id>
              <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
              </activation>
              <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
              </properties>
         </profile>
  </profiles>

2、HelloWorld

需求:浏览发送/hello请求,响应 Hello,Spring Boot 2

2.1、创建maven工程

先检查setting 中maven 的配置

image-20220209100936215

再创建Maven 项目

image-20220209101129851

2.2、引入依赖

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
 </parent>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

2.3、创建主程序

/**
 * 主程序类
 * @SpringBootApplication:这是一个SpringBoot应用
 */
@SpringBootApplication
public class MainApplication {

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class,args);
    }
}

2.4、编写业务

@RestController
public class HelloController {


    @RequestMapping("/hello")
    public String handle01(){
        return "Hello, Spring Boot 2!";
    }


}

2.5、测试

直接运行main方法

2.6、简化配置

application.properties

官方文档配置

server.port=8888

2.7、简化部署

 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

把项目打成jar包,直接在目标服务器执行即可。

选中“clean” and “package” 点击运行

image-20210803224308716

运行情况

image-20210803224508121

在cmd 中 执行 jar包

image-20210803225021639

注意点:(当 命令窗口出现快速闪退时)

  • 取消掉cmd的快速编辑模式

image-20210803225216600

参考文章

尚硅谷雷神SpringBoot2零基础入门springboot全套完整版(spring boot2
linux服务器上jar包启动及关闭方式

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涛涛之海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值