微服务开发之Springboot入门
来源:互联网 发布:手机移动网络改dns 编辑:程序博客网 时间:2024/05/24 04:42
- 说明
之前一直在做传统的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 */@SpringBootApplicationpublic 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来声明。@RestControllerpublic 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: helloworldserver: 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上了。完成。
- 微服务开发之Springboot入门
- Springboot微服务开发教程系列:开发入门
- springboot微服务框架10分钟入门
- SpringBoot微服务之分布式跟踪系统(springboot+zipkin)
- SpringBoot微服务之分布式跟踪系统(springboot+zipkin)
- springboot微服务架构——搭建入门
- 微服务,微架构[零]springboot入门介绍
- SpringCloud微服务系列笔记(1)-SpringBoot简单入门
- springboot微服务
- springboot构建微服务
- 微服务之分布式跟踪系统(springboot+zipkin)
- 微服务之分布式跟踪系统(springboot+zipkin)
- 微服务之分布式跟踪系统(springboot+zipkin+mysql)
- 微服务,微架构[一]之springboot[helloWorld]
- 微服务,微架构[二]之springboot集成jdbc
- 微服务,微架构[四]之springboot集成Redis缓存
- 微服务,微架构[五]之springboot读取properties文件
- 微服务,微架构[六]之springboot集成mybatis
- 【Java】Enum实践
- PID算法的简单C语言实现
- 立即执行函数
- Office CVE-2017-11882本地复现
- [基础题]4、设计一个家政服务规范: 洗衣服, 扫地, 买菜, 做饭
- 微服务开发之Springboot入门
- 用redis-cli连接redis服务超时问题
- 深度 | 理解深度学习中的卷积
- Python selenium
- 记录一下自己学习C语言的点滴
- C中运算符简知
- c语言:冒泡排序,输入一定量的数,进行排序
- Servlet的HTTP请求(成长记录)
- 【无线安全实践入门】网络扫描和ARP欺骗