基于Spring Boot和Spring Cloud实现微服务架构学习(三)-Spring Boot应用

来源:互联网 发布:牛仔男士衬衫 淘宝 编辑:程序博客网 时间:2024/05/29 04:33

目录(?)[+]

前言

我们知道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依赖库:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  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,引入依赖。

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  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.    
  11.     <groupId>spring.boot</groupId>  
  12.     <artifactId>cloud-simple-helloword</artifactId>  
  13.     <version>0.0.1</version>  
  14.     <packaging>jar</packaging>  
  15.     <name>cloud-simple-helloword</name>  
  16.     <dependencies>  
  17.         <!--web应用基本环境配置 -->  
  18.         <dependency>  
  19.             <groupId>org.springframework.boot</groupId>  
  20.             <artifactId>spring-boot-starter-web</artifactId>  
  21.         </dependency>  
  22.             </dependencies>  
  23.     <build>  
  24.         <plugins>  
  25.             <plugin>  
  26.                 <groupId>org.springframework.boot</groupId>  
  27.                 <artifactId>spring-boot-maven-plugin</artifactId>  
  28.             </plugin>  
  29.         </plugins>  
  30.     </build>  
  31. </project>  

2、编写控制器和Make the application executable

  1. 控制器controller主要用于响应用户的情况,由注解@RestController指定;同时类或类下子方法可以通过注解@RequestMapping("/xxx")指定请求路径。

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. src/main/java/hello/GreetingController.java  
  2.   
  3. package hello;  
  4.   
  5. import java.util.concurrent.atomic.AtomicLong;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RequestParam;  
  8. import org.springframework.web.bind.annotation.RestController;  
  9.   
  10. @RestController  
  11. public class GreetingController {  
  12.   
  13.     private static final String template = "Hello, %s!";  
  14.     private final AtomicLong counter = new AtomicLong();  
  15.   
  16.     @RequestMapping("/greeting")  
  17.     public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {  
  18.         return new Greeting(counter.incrementAndGet(),  
  19.                             String.format(template, name));  
  20.     }  
  21. }  

    2、应用执行器由注解@SpringBootApplication实现,这个注解默认是集成了@Configuration、@EnableAutoConfiguration、@EnableWebMvc、@ComponentScan四个注解的功能,每个的功能从英文翻译上就能理解。

    [java] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. src/main/java/hello/Application.java  
    2.   
    3. package hello;  
    4.   
    5. import org.springframework.boot.SpringApplication;  
    6. import org.springframework.boot.autoconfigure.SpringBootApplication;  
    7.   
    8. @SpringBootApplication  
    9. public class Application {  
    10.   
    11.     public static void main(String[] args) {  
    12.         SpringApplication.run(Application.class, args);  
    13.     }  
    14. }  

    3、部署spring boot应用

    要部署运行spring boot应用,首选要打包spring boot应用,你在pom文件中看到的spring-boot-maven-plugin插件就是打包spring boot应用的。除了IDE的main Run方法,启动运行应用还有两种方式,一种是在工程目录执行mvn spring-boot:run启动,另一种则是先打成jar包,然后运行以Java方式运行jar,操作如下:

    [html] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. 1)mvn clean package  
    2. 2)java -jar target/gs-rest-service-0.1.0.jar  

    对应的Gradle依赖执行:./gradlew bootRun 另一种打jar操作:

    [html] view plain copy
     在CODE上查看代码片派生到我的代码片
    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文件中引入:

    [html] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. <dependency>  
    2.             <groupId>org.springframework.boot</groupId>  
    3.             <artifactId>spring-boot-starter-actuator</artifactId>  
    4. </dependency>  
    引入之后,spring boot是默认开启监控的,运行应用你可以在浏览器中输入:http://localhost:8080/health,就可以看到默认的监控信息了:
    [javascript] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. {"status":"UP","diskSpace":{"status":"UP","total":161067397120,"free":91618398208,"threshold":10485760}}  

    信息包括程序执行状态以及基本的磁盘信息。

    5、应用Docker部署

    对于多数Spring Boot微服务应用最终是要部署在Docker上,所以下一步我们应该编写Docker file文件,创建其镜像,然后Docker run运行镜像。这里所有Docker的操作,我会在Docker专题讲述。

    0 0