微服务开发之Springboot入门
2017-11-22 23:11
190 查看
说明
之前一直在做传统的web项目使用的也一直是ssh那一套老旧的开发框架。随着Spring的发展,Spring推出了Springboot、Springcloud等一系列的开发框架。其中Springboot内置集成了tomcat,对于简单的应用项目具有高效的开发效率。结合maven等项目管理工具,极大的提高了服务的开发项目。
开发环境
idea2017+Springboot1.5.2+java1.8
配置文件采用yaml格式。
yaml格式相较于properties来说,具有清晰的层次关系。
搭建步骤
3.1 创建maven项目,这里我们创建一个Springboothelloworld,修改pom.xml文件如下:
我们在pom文件中引入了Springboot的父级依赖,采用1.5.2版本,同时在依赖中加入了Spring-boot-starter-web,对于简单的helloworld这就够了。
3.2 创建application
Springboot的启动,很简单了,只需要一行代码加上@SpringbootApplication即可启动一个微服务应用。当然tomcat默认端口为8080
Application.java如下:
至此,简单的helloworld就完成了,我们需要测试是否可以正常访问。那么我们需要一个controller来处理请求信息。在Springboot中默认的注解扫描路径是Application所在包及其子包。这里我们通过@ComponentScan指定包路径。修改Application加上该注解:
我们创建TestController用于处理/hello请求:
重启服务,后再浏览器输入http://localhost:8080/hello/lisi
至此基本的Springboot启动完成
3.3 修改配置文件
Springboot默认在classpath根目录下加载名为application.yml/application.properties配置文件,这里我们用yaml文件。在resources目录下创建application.yml,我们修改服务器端口为8888内容如下:
这时访问http://localhost:8888/hello/lisi
项目代码地址:
https://github.com/niemingming/springboothelloworld
4. 项目上传到GIThub
4.1 首先配置GIThub在file–settings弹出窗中配置github如下图:
选择password方式,输入你的GitHub账户和密码,并测试
4.2 设置可以版本控制
弹出窗中选择git
在项目目录先右键选择git–commit
这时会在本地创建一个git仓库,代码是提交到本地的,我们如果要提交到GitHub,需要在登录你的GitHub,新建一个项目
输入项目名称和项目说明,
点击create repository在新页面拷贝生成的目录
回到idea,右键git—respository–remotes
在弹出窗中加入刚刚赋值的http地址
然后右键git—respository–pull,等提交完成会发想代码已经提交到GitHub上了。完成。
之前一直在做传统的web项目使用的也一直是ssh那一套老旧的开发框架。随着Spring的发展,Spring推出了Springboot、Springcloud等一系列的开发框架。其中Springboot内置集成了tomcat,对于简单的应用项目具有高效的开发效率。结合maven等项目管理工具,极大的提高了服务的开发项目。
开发环境
idea2017+Springboot1.5.2+java1.8
配置文件采用yaml格式。
yaml格式相较于properties来说,具有清晰的层次关系。
搭建步骤
3.1 创建maven项目,这里我们创建一个Springboothelloworld,修改pom.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.nmm.study</groupId> <artifactId>springboothelloworld</artifactId> <version>1.0-SNAPSHOT</version> <!--引入Springboot的父级依赖。--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencies> <!--引入Springboot的web starter--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
我们在pom文件中引入了Springboot的父级依赖,采用1.5.2版本,同时在依赖中加入了Spring-boot-starter-web,对于简单的helloworld这就够了。
3.2 创建application
Springboot的启动,很简单了,只需要一行代码加上@SpringbootApplication即可启动一个微服务应用。当然tomcat默认端口为8080
Application.java如下:
package com.nmm.study; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @description Springboot的启动类 * @date 2017/11/22 * @author Niemingming */ @SpringBootApplication public class Application { public static void main(String[]args){ //一行代码即可启动服务。 SpringApplication.run(Application.class,args); } }
至此,简单的helloworld就完成了,我们需要测试是否可以正常访问。那么我们需要一个controller来处理请求信息。在Springboot中默认的注解扫描路径是Application所在包及其子包。这里我们通过@ComponentScan指定包路径。修改Application加上该注解:
@SpringBootApplication @ComponentScan("com.nmm.study") public class Application
我们创建TestController用于处理/hello请求:
package com.nmm.study.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; //@Controller //controller是常用的控制器注解,对于restful请求,我们也可以显示的用restController来声明。 @RestController public class TestController { @ResponseBody @RequestMapping("/hello") public String hello(@PathVariable String name) { return "hello " + name; } }
重启服务,后再浏览器输入http://localhost:8080/hello/lisi
页面将会显示 hello lisi
至此基本的Springboot启动完成
3.3 修改配置文件
Springboot默认在classpath根目录下加载名为application.yml/application.properties配置文件,这里我们用yaml文件。在resources目录下创建application.yml,我们修改服务器端口为8888内容如下:
spring: application: #应用名称 name: helloworld server: port: 8888
这时访问http://localhost:8888/hello/lisi
项目代码地址:
https://github.com/niemingming/springboothelloworld
4. 项目上传到GIThub
4.1 首先配置GIThub在file–settings弹出窗中配置github如下图:
选择password方式,输入你的GitHub账户和密码,并测试
4.2 设置可以版本控制
弹出窗中选择git
在项目目录先右键选择git–commit
这时会在本地创建一个git仓库,代码是提交到本地的,我们如果要提交到GitHub,需要在登录你的GitHub,新建一个项目
输入项目名称和项目说明,
点击create repository在新页面拷贝生成的目录
回到idea,右键git—respository–remotes
在弹出窗中加入刚刚赋值的http地址
然后右键git—respository–pull,等提交完成会发想代码已经提交到GitHub上了。完成。
相关文章推荐
- 微服务开发的入门级框架Spring Boot详解(二)
- 微服务开发的入门级框架Spring Boot详解:集成Mybatis
- 微服务开发的入门级框架Spring Boot详解(四)
- Springboot微服务开发教程系列:开发入门
- 微服务开发的入门级框架Spring Boot详解:注解
- 微服务开发的入门级框架Spring Boot详解(三)
- 微服务开发的入门级框架Spring Boot详解:属性配置
- 微服务开发的入门级框架Spring Boot详解(三)
- 微服务开发的入门级框架Spring Boot详解(四)
- 微服务开发的入门级框架Spring Boot详解(四)
- 微服务开发的入门级框架Spring Boot详解(三)
- 微服务开发的入门级框架Spring Boot详解(五)
- 微服务开发的入门级框架Spring Boot详解(五)
- 【项目搭建】使用spring-boot进行REST风格的微服务开发入门
- 微服务开发的入门级框架Spring Boot详解(二)
- 构建微服务:Spring boot 入门篇