spring-cloud教程一:建立spring boot

背景

想一想,微服務這概念在當初剛從業時就聽過,那時也只是停留在概念上,缺乏技術支撐,或者說沒有公認完美的技術支撐。docker的出現,給微服務提供了平臺支持,spring cloud的出現給微服務提供全家桶式的解決方案,幾乎成了微服務的代名詞。
微服務須要解決的問題太多太多,既然spring cloud號稱是一站式的解決方案,對微服務中碰到的問題都提供相應的解決方案,所以spring cloud有品目繁多的項目,截止到目前,官網上列的就有24個項目,每一個項目都用於解決特定的問題。
其中spring boot應該是最核心的一個組件,用於具體業務邏輯的實現 。本文以一個簡單的接口根據用戶工號獲取用戶信息爲例,介紹spring boot的使用。java

建立spring boot工程

  1. 打開https://start.spring.io/生成一個標準spring boot工程. 由於須要restful支持,Dependencies須要輸入Web,提供對web的支持。

    圖片描述

  2. 建立IntelliJ IDEA項目,選擇支持Maven.
  3. 將壓縮包中src目錄覆蓋項目src目錄
  4. 將項目pom.xml替換爲壓縮包中的pom.xml文件。
  5. pom.xml文件上右鍵選擇Maven->Reimport導入相應的jar包。

目錄結構web

.
├── ./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.xmlspring

<?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>yaya.demo</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

建立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
 */
@RestController
public 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()
@WebAppConfiguration
public 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
相關文章
相關標籤/搜索