輕量級微服務架構(一)--springBoot入門

首先說啊,這個輕量級微服務架構的,我也是初探門徑,沒有很深的理解,寫的目的,一是寫下來印象深一些,另外就是跟大家一起學習,歡迎留言,歡迎交流。大致看了一下網上的SpringBoot項目和一些教程,基本上也都是1.5.*.RELESE 的,這個事情其實本身就是很麻煩,雖然大部分的代碼是沒有問題,但是由於版本導致的一些命令不兼容,沒有看到預期的代碼效果而踩的坑還是蠻多的。

文章涉及的代碼及工具如下(後面的設計的版本也會逐步列出): 

內容 版本 
IntelliJ IDEA 2018.2
JRE 1.8.0_152-release-1248-b8  X64
Windows 7
SpringBoot 2.0.5.RELEASE
webServer tomcat

1、爲什麼要使用SpringBoot?

這個現在是個老生常談的問題了,我之前還沒學習微服務的時候,找工作碰到這樣一個電話,因爲當時我已經在使用SpringBoot進行開發了,電話那邊是這麼問的:你好,看你的簡歷你是比較熟悉微服務是吧。當時還是很鬱悶的,我會SpringBoot就會微服務了?直到我開始正式學習微服務這個物我才發現,作爲一個鐵桿的JAVA開發,後臺不用SpringBoot還用什麼。

  • SpringBoot的出現還是有點顛覆傳統JavaWeb應用的感覺,當然這種顛覆只存在你不假思索之前。之前的JavaWeb項目在發佈的時候我們首選的是打一個war包,在將war包丟到服務器上,然後命令啓動(或者是腳本啓動)服務,現在的是直接將項目發不成一個jar包即可,而實際上也只是在SpringBoot項目的內部內置了一個Tomcat但是我們同樣可以在pom裏面加一個小小的配置,讓它在外部Tomcat裏面跑起來;
  • 通過一個main方法啓動Web項目,你沒有看錯,是真的,在main方法裏面加一個@SpringBootApplication額註解,這個註解就是SpringBoot啓動的入口;
  • 配置呢?XML文件呢?配置肯定還是要配置的但是一般會使用可讀性更高的yml文件,集成一些功能比較簡單,pom裏面引入一下插件,配置文件配置屬性,不用像之前那樣又是Spring配置,又是XML引入的;
  • 此外這個容易上手,容易到什麼程度?如果你使用過SpringMVC,那麼SpringBoot從知道這個名詞到使用只需要10分鐘,最多10分鐘你就能寫出來一個demo,它是一個對SpringMVC深度封裝的架構;
  • 開箱即用的插件集,SpringBoot中你可以無縫集成數據庫、MQ、模板引擎等你之前項目用到的技術

總的說,SpringBoot是一個很經典的核心+插件的系統架構,典型的核心不夠插件湊的很親民的輕量級的後臺架構。

2、構建第一個SpringBoot應用程序

使用idea構建SpringBoot應用,你需要只是點點點

這個就是生成的SpringBoot的主類

package com.example.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class, args);
    }
}

通過點點點生成的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.example</groupId>
    <artifactId>hello</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.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.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </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>

現在直接啓動主類是肯定報錯的,因爲現在導入了Mysql的驅動,想要啓動的話,就把

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

這兩個註釋掉就行了,這裏在啓動時候就已經產生了dataSource實例,連接不到數據庫肯定就會出問題的。

有這個註釋又怎麼跑起來?

  • 將application.properties 改爲application.yml (快捷鍵alt+shift+r)
  • 增加以下目錄
  1. TblUser.java
    package com.example.hello.entity;
    
    /**
     * Create By Miccke(煎餅)
     * Create Time 2018/9/26 16:02
     */
    public class TblUser {
    
        private Integer id;
    
        private String userName;
    
        private String passWord;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getPassWord() {
            return passWord;
        }
    
        public void setPassWord(String passWord) {
            this.passWord = passWord;
        }
    }
    
  2. TblService.java
    package com.example.hello.service;
    
    import com.example.hello.entity.TblUser;
    
    import java.util.List;
    
    /**
     * Create By Miccke(煎餅)
     * Create Time 2018/9/26 16:03
     */
    public interface TblUserService {
    
        List<TblUser> queryList();
    
    }
    
  3. TblUserServiceImpl.java
    package com.example.hello.service.impl;
    
    import com.example.hello.dao.TblUserDao;
    import com.example.hello.entity.TblUser;
    import com.example.hello.service.TblUserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    /**
     * Create By Miccke(煎餅)
     * Create Time 2018/9/26 16:03
     */
    @Service("TblUserService")
    public class TblUserServiceImpl implements TblUserService {
    
        @Autowired
        private TblUserDao tblUserDao;
    
        @Override
        public List<TblUser> queryList() {
            return this.tblUserDao.queryList();
        }
    }
    
  4. TblUserDao.java
    package com.example.hello.dao;
    
    import com.example.hello.entity.TblUser;
    import org.apache.ibatis.annotations.Mapper;
    
    import java.util.List;
    
    /**
     * Create By Miccke(煎餅)
     * Create Time 2018/9/26 16:05
     */
    @Mapper
    public interface TblUserDao {
    
        List<TblUser> queryList();
    
    }
    
  5. TblUserController.java
    package com.example.hello.controller;
    
    import com.example.hello.entity.TblUser;
    import com.example.hello.service.TblUserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    /**
     * Create By Miccke(煎餅)
     * Create Time 2018/9/26 16:06
     */
    @RestController
    @RequestMapping("/user")
    public class TblUserController {
    
        @Autowired
        private TblUserService tblUserService;
    
        @GetMapping("/queryList")
        public List<TblUser> queryList(){
            return this.tblUserService.queryList();
        }
    
    
    }
    
  6. TblUserMapper.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.example.hello.dao.TblUserDao">
    
        <select id="queryList" resultType="com.example.hello.entity.TblUser">
            select * from tbl_user
        </select>
    
    </mapper>
  7. application.yml
    server:
      port: 8080
    spring:
      datasource:
        url: jdbc:mysql://120.79.224.104:3306/weChattest?useUnicode=true&characterEncoding=UTF-8
        username: root
        password: *******
    
    mybatis:
      mapper-locations: classpath:mapper/*.xml  #注意:一定要對應mapper映射xml文件的所在路徑
      type-aliases-package: com.example.hello.entity  # 注意:對應實體類的路徑
      configuration:
        map-underscore-to-camel-case: true #駝峯映射

啓動main()後訪問接口http://localhost:8080/user/queryList,即可返回自己插入的數據,至此第一個帶有數據庫查詢功能的SpringBoot應用就完成了。

本文源代碼鏈接:https://github.com/Miccke/Hello

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章