轻量级微服务架构(一)--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

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