springboot學習筆記(一):基礎程式和配置
1 , springboot 介紹(來自百度百科)
簡介
微服務是一個新興的軟體架構,就是把一個大型的單個應用程式和服務拆分為數十個的支援微服務。一個微服務的策略可以讓工作變得更為簡便,它可擴充套件單個元件而不是整個的應用程式堆疊,從而滿足服務等級協議。
對於大型應用程式來說,增加更多的使用者則意味著提供更大型的彈性計算雲(EC2)例項規模,即便只是其中的一些功能擴大了規模亦是如此。其最終結果就是企業使用者只需為支援超過微服務的那部分需求的EC2例項支付費用。
微服務的優點
微服務應用的一個最大的優點是,它們往往比傳統的應用程式更有效地利用計算資源。這是因為它們通過擴充套件元件來處理功能瓶頸問題。這樣一來,開發人員只需要為額外的元件部署計算資源,而不需要部署一個完整的應用程式的全新迭代。最終的結果是有更多的資源可以提供給其它任務。
微服務應用程式的另一個好處是,它們更快且更容易更新。當開發者對一個傳統的單體應用程式進行變更時,他們必須做詳細的QA測試,以確保變更不會影響其他特性或功能。但有了微服務,開發者可以更新應用程式的單個元件,而不會影響其他的部分。測試微服務應用程式仍然是必需的,但它更容易識別和隔離問題,從而加快開發速度並支援DevOps和持續應用程式開發。
第三個好處是,微服務架構有助於新興的雲服務,如事件驅動計算。類似AWS Lambda(AWS Lambda是一個用於部署程式碼、管理服務以及監控輕量級服務執行狀態的細粒度方法)這樣的功能讓開發人員能夠編寫程式碼處於休眠狀態,直到應用程式事件觸發。事件處理時才需要使用計算資源,而企業只需要為每次事件,而不是固定數目的計算例項支付.
2 , 最基本的springboot實踐
建立標準的MavenWeb工程
pom.xml依賴配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding >UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<!-- web專案自動配置模組 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 自動配置模板(包含spring-boot-starter-web依賴) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 日誌配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
</dependencies>
<build>
<finalName>SpringBoot</finalName>
</build>
- Class
@SpringBootApplication
@RestController
public class SimpleExample {
@RequestMapping("/")
public String hello(){
return "SpringBoot SimpleExample !";
}
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
SpringBoot SimpleExample !
3, 預設配置和約定目錄結構
- 預設web容器和埠
預設嵌入Tomcat , 埠號為8080
- spring-boot專案原始碼目錄結構約定
Maven的資原始檔目錄:/src/java/resources
spring-boot專案靜態檔案目錄:/src/java/resources/static
spring-boot專案模板檔案目錄:/src/java/resources/templates
spring-boot靜態首頁的支援,即index.html放在以下目錄結構會直接對映到應用的根目錄下
classpath:/META-INF/resources/index.html
classpath:/resources/index.html
classpath:/static/index.html
calsspath:/public/index.html
/src/java/resources/templates這個目錄並不是首頁檔案的預設目錄,所以我們需要手動將應用根路徑對映到/src/java/resources/templates/index.html下.可以使用如下方式
@RequestMapping("/")
public String index(){
return "index";
}
- springboot預設配置檔案(用於配置各類基本屬性)
\src\main\resources\application.properties
下面是常用的一些配置項
# tomcat最大執行緒數,預設為200
server.tomcat.max-threads=800
# tomcat的URI編碼
server.tomcat.uri-encoding=UTF-8
# 存放Tomcat的日誌、Dump等檔案的臨時資料夾,預設為系統的tmp資料夾(如:C:\Users\Shanhy\AppData\Local\Temp)
server.tomcat.basedir=H:/springboot-tomcat-tmp
# 開啟Tomcat的Access日誌,並可以設定日誌格式的方法:
#server.tomcat.access-log-enabled=true
#server.tomcat.access-log-pattern=
# accesslog目錄,預設在basedir/logs
#server.tomcat.accesslog.directory=
# 日誌檔案目錄
logging.path=H:/springboot-tomcat-tmp
# 日誌檔名稱,預設為spring.log
logging.file=myapp.log
4 , 修改部分常用預設配置的方法以及頁面展示資料基本流程
- 修改預設埠號,主要有兩種方式
- 通過配置檔案修改
在application.properties檔案中新增以下配置資訊
server.port=8080
- 實現EmbeddedServletContainerCustomizer介面
@SpringBootApplication
@RestController
public class Application implements EmbeddedServletContainerCustomizer{
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
@Override
public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
configurableEmbeddedServletContainer.setPort(8081);
}
}
- 關閉thymeleaf快取
在application.properties檔案中新增以下配置資訊
spring.thymeleaf.cache: false
server.tomcat.access_log_enabled: true
server.tomcat.basedir: target/tomcat
- 頁面展示資料
例如:
@RequestMapping("/hello/{name}")
public String hello(@PathVariable("name") String name, Model model) {
model.addAttribute("name", name);
//預設會去\src\main\resources\templates目錄下查詢hello.html檔案
return "hello";
}
- 頁面上展示
<!DOCTYPE HTML>
<!-- 需要引入thymeleaf名稱空間 -->
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<!-- 獲取model中name的值 -->
<p th:text="'Hello , ' + ${name} + '!'" />
</body>
</html>
5 , 部署和執行
- 繼承SpringBootServletInitializer重寫configure方法
@SpringBootApplication
@RestController
public class SimpleExample extends SpringBootServletInitializer{
@RequestMapping("/")
public String hello(){
return "SpringBoot SimpleExample !";
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return super.configure(builder);
}
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
- 修改pom檔案中jar 為 war
<!-- <packaging>jar</packaging> -->
<packaging>war</packaging>
- 修改pom,排除tomcat外掛
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>