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);
}
}
- 直接運行main方法
- 瀏覽器訪問http://localhost:8080/ 即可顯示下面的信息
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
參考文檔 : 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>