天天看點

基于Spring Boot和Spring Cloud實作微服務架構學習(三)-Spring Boot應用

前言

我們知道spring Boot 是 Spring 産品中一個新的子項目,緻力于簡便快捷地搭建基于 Spring 的獨立可運作的應用。大多數的 Spring Boot 應用隻需要非常少的 Spring 配置,并且這些都統一配置在application.yml中,極好的解決了各種配置檔案的困擾。

說到這裡,如果你覺得看英文文檔很吃力,推薦一本老師推薦的書《JavaEE開發的颠覆者:Spring Boot實戰》,這本書先是介紹Spring主流的一些架構,然後分别demo講解Spring boot的各個依賴庫,每個都不算太深,可以算是針對官網英文導讀的一次翻譯實作,對于入門我感覺是夠了,畢竟我就是這樣過來的。至于學習的深入,我個人是主張實戰出真知,是以入門了就拿到項目中多用,這是個要長期總結的過程。

Spring Boot Starter 依賴庫

上篇文章提過Spring Boot對Maven及Gradle等建構工具支援力度非常大。其内置一個’Starter POM’,對項目建構進行了高度封裝,最大化簡化項目建構的配置。作為一個微服務架構,Boot的很大一部分價值在于它能夠無縫地為基于Maven和Gradle的項目提供各種建構工具。通過使用Spring Boot插件,就能夠利用該架構的能力,将項目打包為一個輕量級的、可運作的部署包,而除此之外幾乎不需要進行任何額外的配置。

作為一個架構,SpringBoot中内建了一些聚合子產品,通常稱為“啟動者”。這些啟動子產品中是一些類庫的已知的、良好的、具備互操作性的版本的組合,這些類庫能夠為應用程式提供某些方面的功能。Boot能夠通過應用程式的配置對這些類庫的進行設定,這也為整個開發周期中帶來了配置勝于約定的便利性,達到開箱即用的微服務效果。下面就列出幾個經典的Spring Boot Starter pom依賴庫:

  1. spring-boot-starter:Spring Boot核心Starter,包含自動配置、日志、yaml配置檔案的支援  
  2. spring-boot-starter-amqb:使用spring-rabbit來支援AMQP  
  3. spring-boot-starter-web:支援全棧web開發,裡面包括了Tomcat和Spring-webmvc。  
  4. spring-boot-starter-data-jpa:對JPA的支援,包含spring-data-jpa、spring-orm和Hibernate  
  5. spring-boot-starter-thymeleaf:對Thymeleaf模版引擎的支援,包含于Spring整合的配置  
  6. spring-boot-starter-ws: 提供對Spring Web Services的支援  
  7. spring-boot-starter-cloud-connectors:對雲平台(Cloud Foundry、Heroku)提供的服務簡化連接配接方式  
  8. spring-boot-starter-test:提供對常用測試架構的支援,包括JUnit,Hamcrest以及Mockito等。  
  9. spring-boot-starter-actuator:支援産品環境下的一些功能,比如管理應用、名額度量及監控等。  
  10. spring-boot-starter-jetty:支援jetty容器。  
  11. spring-boot-starter-log4j:引入預設的log架構(logback)  

SpringBoot項目搭建過程

在官網導讀,每個demo除了講解怎麼搭建外,還提供了GitHub的下載下傳連接配接,支援直接clone運作體驗。因為Spring Boot是新寵,它的JDK官方要求1.8以上版本,同時,Maven要3.0+或者Gradle2.3+,其他IDE沒有限制,下面說下應用大緻實作步驟,隻是步驟,以Maven為例:

1、建立項目,添加依賴包。

一般情況下可以通過IDE建立Maven項目,自動生成各種路徑的檔案夾,當然也可以指令行(mkdir -p src/main/Java/hello)手動一個個建立,然後修改pom.xml,引入依賴。

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <!-- spring boot基本環境 -->  
  5.     <parent>  
  6.         <groupId>org.springframework.boot</groupId>  
  7.         <artifactId>spring-boot-starter-parent</artifactId>  
  8.         <version>1.3.1.RELEASE</version>  
  9.     </parent>  
  10.     <groupId>spring.boot</groupId>  
  11.     <artifactId>cloud-simple-helloword</artifactId>  
  12.     <version>0.0.1</version>  
  13.     <packaging>jar</packaging>  
  14.     <name>cloud-simple-helloword</name>  
  15.     <dependencies>  
  16.         <!--web應用基本環境配置 -->  
  17.         <dependency>  
  18.             <groupId>org.springframework.boot</groupId>  
  19.             <artifactId>spring-boot-starter-web</artifactId>  
  20.         </dependency>  
  21.             </dependencies>  
  22.     <build>  
  23.         <plugins>  
  24.             <plugin>  
  25.                 <groupId>org.springframework.boot</groupId>  
  26.                 <artifactId>spring-boot-maven-plugin</artifactId>  
  27.             </plugin>  
  28.         </plugins>  
  29.     </build>  
  30. </project>  

2、編寫控制器和Make the application executable

  1. 控制器controller主要用于響應使用者的情況,由注解@RestController指定;同時類或類下子方法可以通過注解@RequestMapping("/xxx")指定請求路徑。
  1. src/main/java/hello/GreetingController.java  
  2. package hello;  
  3. import java.util.concurrent.atomic.AtomicLong;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.bind.annotation.RequestParam;  
  6. import org.springframework.web.bind.annotation.RestController;  
  7. @RestController  
  8. public class GreetingController {  
  9.     private static final String template = "Hello, %s!";  
  10.     private final AtomicLong counter = new AtomicLong();  
  11.     @RequestMapping("/greeting")  
  12.     public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {  
  13.         return new Greeting(counter.incrementAndGet(),  
  14.                             String.format(template, name));  
  15.     }  
  16. }  
2、應用執行器由注解@SpringBootApplication實作,這個注解預設是內建了@Configuration、@EnableAutoConfiguration、@EnableWebMvc、@ComponentScan四個注解的功能,每個的功能從英文翻譯上就能了解。
  1. src/main/java/hello/Application.java  
  2. import org.springframework.boot.SpringApplication;  
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  4. @SpringBootApplication  
  5. public class Application {  
  6.     public static void main(String[] args) {  
  7.         SpringApplication.run(Application.class, args);  

3、部署spring boot應用

要部署運作spring boot應用,首選要打包spring boot應用,你在pom檔案中看到的spring-boot-maven-plugin插件就是打包spring boot應用的。除了IDE的main Run方法,啟動運作應用還有兩種方式,一種是在工程目錄執行mvn spring-boot:run啟動,另一種則是先打成jar包,然後運作以Java方式運作jar,操作如下:

  1. 1)mvn clean package  
  2. 2)java -jar target/gs-rest-service-0.1.0.jar  

對應的Gradle依賴執行:./gradlew bootRun 另一種打jar操作:

  1. 1)./gradlew build  
  2. 2)java -jar build/libs/gs-rest-service-0.1.0.jar   

注意Maven和Gradle生成的jar路徑不同。如此,你就看到一個基于jar包的web應用啟動了。

4、監控

Spring boot提供的一些開箱即用的應用非常容易使用,比如監控,你隻需要在pom檔案中引入:

  1. <dependency>  
  2.             <artifactId>spring-boot-starter-actuator</artifactId>  
  3. </dependency>  
  1. {"status":"UP","diskSpace":{"status":"UP","total":161067397120,"free":91618398208,"threshold":10485760}}  

5、應用Docker部署