博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring-cloud教程一:创建spring boot
阅读量:7191 次
发布时间:2019-06-29

本文共 5234 字,大约阅读时间需要 17 分钟。

背景

想想,微服务这概念在当初刚从业时就听过,那时也只是停留在概念上,缺少技术支撑,或者说没有公认完美的技术支撑。docker的出现,给微服务提供了平台支持,spring cloud的出现给微服务提供全家桶式的解决方案,几乎成了微服务的代名词。

微服务需要解决的问题太多太多,既然spring cloud号称是一站式的解决方案,对微服务中碰到的问题都提供相应的解决方案,因此spring cloud有品目繁多的项目,截止到目前,官网上列的就有24个项目,每个项目都用于解决特定的问题。
其中spring boot应该是最核心的一个组件,用于具体业务逻辑的实现 。本文以一个简单的接口根据用户工号获取用户信息为例,介绍spring boot的使用。

创建spring boot工程

  1. 打开生成一个标准spring boot工程. 因为需要restful支持,Dependencies需要输入Web,提供对web的支持。

    图片描述

  2. 创建IntelliJ IDEA项目,选择支持Maven.
  3. 将压缩包中src目录覆盖项目src目录
  4. 将项目pom.xml替换为压缩包中的pom.xml文件。
  5. pom.xml文件上右键选择Maven->Reimport导入相应的jar包。

目录结构

.├── ./pom.xml├── ./springboot.iml└── ./src    ├── ./src/main    │   ├── ./src/main/java    │   │   └── ./src/main/java/yaya    │   │       └── ./src/main/java/yaya/demo    │   │           └── ./src/main/java/yaya/demo/springboot    │   │               └── ./src/main/java/yaya/demo/springboot/SpringbootApplication.java    │   └── ./src/main/resources    │       ├── ./src/main/resources/application.properties    │       ├── ./src/main/resources/static    │       └── ./src/main/resources/templates    └── ./src/test        └── ./src/test/java            └── ./src/test/java/yaya                └── ./src/test/java/yaya/demo                    └── ./src/test/java/yaya/demo/springboot                        └── ./src/test/java/yaya/demo/springboot/SpringbootApplicationTests.java

pom.xml

4.0.0
yaya.demo
springboot
0.0.1-SNAPSHOT
jar
springboot
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.4.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin

创建controller

1. 创建用户类yaya.demo.springboot.pojos.User
package yaya.demo.springboot.pojos;/** * @Description: * @author: jianfeng.zheng * @since: 2018/8/14 下午7:48 * @history: 1.2018/8/14 created by jianfeng.zheng */public class User {    private String username;    private String uid;    private String email;    //...getter and setter}
2. 创建controlleryaya.demo.springboot.controller.UserController
package yaya.demo.springboot.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import yaya.demo.springboot.pojos.User;/** * @Description: * @author: jianfeng.zheng * @since: 2018/8/14 下午7:45 * @history: 1.2018/8/14 created by jianfeng.zheng */@RestControllerpublic class UserController {    @RequestMapping(value = "/user",method = RequestMethod.GET)    public User getUserInfo(@RequestParam(name = "uid")String uid){        User user=new User();        user.setEmail("jianfeng.zheng@definesys.com");        user.setUsername("jianfeng.zheng");        user.setUid(uid);        return user;    }}
3. 运行启动类SpringbootApplication
4. 测试
# curl http://localhost:8080/user?uid=004{"username":"jianfeng.zheng","uid":"004","email":"jianfeng.zheng@definesys.com"}

单元测试

修改单元测试类SpringbootApplicationTests
package yaya.demo.springboot;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import org.springframework.test.web.servlet.MockMvc;import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;import org.springframework.test.web.servlet.setup.MockMvcBuilders;import yaya.demo.springboot.controller.UserController;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest()@WebAppConfigurationpublic class SpringbootApplicationTests {    private MockMvc mvc;    @Before    public void init() {        mvc = MockMvcBuilders.standaloneSetup(new UserController()).build();    }    @Test    public void getUserInfo() throws Exception {        mvc.perform((MockMvcRequestBuilders.get("/user")                .param("uid", "004")))                .andDo(print())                .andExpect(status().isOk());    }}

打包运行

  1. 在工程目录下执行命令mvn -Dmaven.test.skip=true install
  2. 运行target文件夹下生成可运行jar包.
java -jar springboot-0.0.1-SNAPSHOT.jar

转载地址:http://rynkm.baihongyu.com/

你可能感兴趣的文章
正在连接...ORA-12541: TNS: 无监听程序
查看>>
最新淘宝漂亮星星评分-JQUERY版
查看>>
Trie 树 及Java实现
查看>>
EntLib 3.1学习笔记(3) : Cryptography Application Block
查看>>
一种神经元探索系统方法及装置
查看>>
算法精讲学习笔记 二分搜索
查看>>
魔哥--Css:背景色透明,内容不透明之终极方法!兼容(不支持ie6)。
查看>>
在Windows Mobile上实现自动拼写和匹配建议
查看>>
SQL 性能调优日常积累
查看>>
入坑系列之HAProxy负载均衡
查看>>
大整数加法
查看>>
ASP.NET MVC 5 学习教程:添加验证
查看>>
TestNG官方文档中文版(4)-运行TestNG
查看>>
使用IsLine FrameWork开发ASP.NET程序之四—使用AppLogProvider日志处理框架
查看>>
linux内存管理-内核用户空间 【转】
查看>>
和菜鸟一起学linux之V4L2摄像头应用流程【转】
查看>>
linux下usb转串口驱动分析【转】
查看>>
关于关闭Eclipse的控制台自动跳出
查看>>
EntityFramework Core Raw Query再叙注意事项后续
查看>>
使用VS2010编译OpenSSL的过程记录
查看>>