spring-boot入门篇

分类:spring-boot
阅读:1475
作者:majingjing
发布:2016-12-19 13:25

现在微服务已经是趋于稳定,各种功能能够使用微服务的就会架起微服务的口号.
显得很高大上,微服务的框架也有很多,我所接触到的就有spring-boot,dropwizard等.
在项目中都有使用过,由于在dropwizard遇到了太多的坑(如: 长时间不访问假死,字符编码长时间运行后就会几率性出现乱码.等总之就是长时间运行后就会出现各种各样让人抓狂的问题),虽然有dropwizard-metrics插件提供各种检测,但还是感觉太复杂了.就不再写文章描述这个框架的使用.
由于搞java开发的,都接触过spring框架,所以上手spring-boot会更加的快速.

下面介绍下spring-boot的入门环境搭建.

  1. 本文及以后文章都是采用 1.4.2 版本 因为截止到现在这个是稳定版,后续版本都还只是快照版本.
    QQ截图20161219131236.png
  2. 新建maven项目
  3. 在pom.xml中增加如下配置
    1. <parent>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-parent</artifactId>
    4. <version>1.4.2.RELEASE</version>
    5. </parent>
    6. <dependencies>
    7. <dependency>
    8. <groupId>org.springframework.boot</groupId>
    9. <artifactId>spring-boot-starter-web</artifactId>
    10. </dependency>
    11. </dependencies>
  4. 新建 hello/SampleController.java
  1. package hello;
  2. import org.springframework.boot.*;
  3. import org.springframework.boot.autoconfigure.*;
  4. import org.springframework.stereotype.*;
  5. import org.springframework.web.bind.annotation.*;
  6. @Controller
  7. @EnableAutoConfiguration
  8. public class SampleController {
  9. @RequestMapping("/")
  10. @ResponseBody
  11. String home() {
  12. return "Hello World!";
  13. }
  14. public static void main(String[] args) throws Exception {
  15. SpringApplication.run(SampleController.class, args);
  16. }
  17. }

到此为止,一个简单的微服务已经搭建完成.可以run下试试效果.打开浏览器输入 http://localhost:8080

QQ截图20161219132342.png