SpringBoot——HelloWorld

微服務和單體應用的宏觀理解

微服務:一組小型應用經過HTTP的方式進行溝通的開發思想java

單體應用:ALL IN ONEgit

單體應用的不足:github

隨着業務邏輯的不斷更新和迭代開發,起初的小型應用會不斷膨脹,當應用達到必定規模量時,敏捷開發和部署維護的效率就會舉步維艱。也就是說,單體應用的可持續性能力較弱。web

微服務的解決方案:spring

經過分解巨大的單體式應用使得可持續性開發和優化等擴展能力加強。數據庫

自由的開發技術選擇。(不一樣組件的功能可使用更加合適的技術實現)apache

微服務架構的不足:springboot

微服務自己的分佈式系統致使固有複雜性:組件之間消息傳遞的通信成本。架構

迭代開發所遺留的耦合問題依然存在:在修改某一個服務時,其餘與之相關的服務不可避免的也要相應修改,但較單體應用來講,業務邏輯更清晰,耦合度更低。併發

總結:相對來講,單體式開發更加適合簡單輕量的應用,微服務雖然仍舊存在不足,但更適合構建複雜應用開發。

新的體會:

根據演化原則,微服務是單體應用在業務發展到必定規模的演化策略。微服務的出現並非替換單體,而是在單體應用沒法知足業務需求的狀況下破局的一種解決方案。

【舉個栗子】

  • 2003年4月,淘寶祕密起始於湖畔花園,購買了一個LAMP(Linux+Apache+MySQL+PHP)架構的網站。

    img

  • (2008年後)服務化/中心化的業務系統架構

img

LAMP架構是最簡單的架構,可是同時也是最合適當初淘寶的架構。在淘寶無人問津的時代,幾個億的併發量又有什麼意義呢?最初的LAMP到使用Oracle數據庫,經歷重量級EJB框架的淘寶,最終實現服務化的架構模式。

技術的做用是實現業務,業務的發展纔是技術革新的標準。

更多淘寶技術發展可參考:https://blog.csdn.net/wp1603710463/article/details/50166797


微服務架構是這個時代最火的技術,但卻不是業務的起點。

反手一個HelloWorld

環境:JDK8

開發工具:Intellij IDEA

SpringBoot版本:2.1.8

Maven和Tomcat都是使用IDEA內置的。

使用Spring Initializr構建應用

  • 建立工程

1568260736945

1568260807248

選擇web模塊測試

1568260841696

  • 建立Controller測試

    1568261112940

package zkrun.top.helloworld.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {


    @RequestMapping("/hello")
    public String hello()
    {
        return "hello SpringBoot!";
    }
}
  • 啓動

1568261163680

  • 訪問:http://localhost:8080/hello

1568261201001

關於RestController

1568261267260

關於POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>zkrun.top</groupId>
    <artifactId>helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>helloworld</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

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

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

</project>

parent版本仲裁

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

spring-boot-starter-parent依賴的父項目spring-boot-dependencies定義了常常使用依賴的版本,因此在之後導入依賴一般是不須要寫版本號的(除了一些特殊的沒有被聲明的包)

1568261496300

spring-boot-starter場景啓動器

好比說web場景啓動器包含了諸如Tomcat,webmvc等場景啓動器

1568261659130

webmvc場景啓動器又包含了beans,core,web等jar包

1568261831930

場景啓動器是將開發時經常使用的jar包根據使用的場景進行了一次整合,使得開發更加的方便,快速。

【同時必定程度上解決了本身引用jar包致使的衝突,但也會致使jar包不可見性,使得開發者在熟悉底層原理的到路上越走越遠,有利有弊吧】

@SpringBootApplication

1568262109871

@SpringBootApplication包含@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan等註解

1568262155660

@SpringBootConfiguration包含@Configuration

1568262210210

@Configuration包含@Component

1568262306101

@EnableAutoConfiguration開啓自動配置包含

1568262434887

@AutoConfigurationPackage包含

1568262457020

將主配置類(@SpringBootApplication標註的類)的所在包及子包裏面全部的組件掃描進IOC容器。

部署

1568262739454

1568262753007

找到jar所在目錄

java -jar helloworld-0.0.1-SNAPSHOT.jar

訪問:http://localhost:8080/hello

1568262920952


代碼參考:

https://github.com/HCJ-shadow/SpringBootPlus

相關文章
相關標籤/搜索