3、Spring Cloud Rest工程建立(通過IDEA建立)

HOsystem發表於2020-12-05

1.Rest微服務構建簡介

(1).介紹

  以Dept部門模組做一個微服務通用案例Consumer消費者(Client)通過REST呼叫Provider提供者(Server)提供的服務。

(2).Mybatis 

 https://www.cnblogs.com/HOsystem/category/1781871.html

(3).Maven

一個簡單的Maven模組結構是這樣的:

---- app-parent      一個父專案(app-parent)聚合很多子專案(app-util,app-dao,app-service,app-web)

      |---- pom.xml (pom)

      |

      |-------- app-util

      | |-------- pom.xml (jar)

      |

      |-------- app-dao

      | |-------- pom.xml (jar)

      |

      |-------- app-service

      | |-------- pom.xml (jar)

      |

      |-------- app-web

        |-------- pom.xml (war)

  一個Project帶著多個Module子模組。

  MicroServiceCloud父工程(Project)下初次帶著3個子模組(Module)。

[1].microservicecloud-api

  封裝的整體entity/介面/公共配置等


[2].microservicecloud-provider-dept-8001

  微服務落地的服務提供者

 

[3].microservicecloud-consumer-dept-80

  微服務呼叫的客戶端使用;80埠

80埠是為HTTP(HyperText Transport Protocol)即超文字傳輸協議開放的

此為上網衝浪使用次數最多的協議,主要用於WWW(World Wide Web)即全球資訊網傳輸資訊的協議。

可以通過HTTP地址(即常說的"網址")加":80"來訪問網站,

因為瀏覽網頁服務預設的埠號都是80,因此只需輸入網址即可,不用輸入":80"了。

 

2.SpringCloud版本

 

3.Rest工程構建

  約定>配置>編碼

(1).microservicecloud整體父工程Project

  父工程microservicecloud,切記是Packageing是pom模式

 

[1].建立microservicecloud工程

  IDEA中建立Maven父工程的時候不需要勾選'Create from archetype'

[2].配置pom檔案

<?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.hosystem</groupId>

    <artifactId>microservicecloud</artifactId>

    <version>1.0-SNAPSHOT</version>

    <packaging>pom</packaging>

 

    <properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <maven.compiler.source>1.8</maven.compiler.source>

        <maven.compiler.target>1.8</maven.compiler.target>

        <junit.version>4.12</junit.version>

        <log4j.version>1.2.17</log4j.version>

        <lombok.version>1.16.18</lombok.version>

    </properties>

 

 

    <dependencyManagement>

        <dependencies>

            <dependency>

                <groupId>org.springframework.cloud</groupId>

                <artifactId>spring-cloud-dependencies</artifactId>

                <version>Dalston.SR1</version>

                <type>pom</type>

                <scope>import</scope>

            </dependency>

            <dependency>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-dependencies</artifactId>

                <version>1.5.9.RELEASE</version>

                <type>pom</type>

                <scope>import</scope>

            </dependency>

            <dependency>

                <groupId>mysql</groupId>

                <artifactId>mysql-connector-java</artifactId>

                <version>5.0.4</version>

            </dependency>

            <dependency>

                <groupId>com.alibaba</groupId>

                <artifactId>druid</artifactId>

                <version>1.0.31</version>

            </dependency>

            <dependency>

                <groupId>org.mybatis.spring.boot</groupId>

                <artifactId>mybatis-spring-boot-starter</artifactId>

                <version>1.3.0</version>

            </dependency>

            <dependency>

                <groupId>ch.qos.logback</groupId>

                <artifactId>logback-core</artifactId>

                <version>1.2.3</version>

            </dependency>

            <dependency>

                <groupId>junit</groupId>

                <artifactId>junit</artifactId>

                <version>${junit.version}</version>

                <scope>test</scope>

            </dependency>

            <dependency>

                <groupId>log4j</groupId>

                <artifactId>log4j</artifactId>

                <version>${log4j.version}</version>

            </dependency>

        </dependencies>

    </dependencyManagement>

 

 

 

</project>

#建立子工程microservicecloud-api之後 pom會發生變化

 

<groupId>com.hosystem</groupId>

<artifactId>microservicecloud</artifactId>

<version>1.0-SNAPSHOT</version>

<packaging>pom</packaging>

<!--注:modules最開始建立的時候是不存在的,只有建立子工程的時候才會出現-->

<modules>

    <module>microservicecloud-api</module>

</modules>

(2).microservicecloud-api

  公共子模組Module;new->Module

 

[1].建立microservicecloud-api工程

  建立完成後請回到父工程檢視pom檔案變化

 

 

 

[2].匯入pom

<?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">

    <!-- 子類裡面顯示宣告才能有明確的繼承表現,無意外就是父類的預設版本否則自己定義 -->

    <parent>

        <artifactId>microservicecloud</artifactId>

        <groupId>com.hosystem</groupId>

        <version>1.0-SNAPSHOT</version>

    </parent>

    <modelVersion>4.0.0</modelVersion>

 

    <!--當前Module叫什麼名字 -->

    <artifactId>microservicecloud-api</artifactId>

 

    <!-- 當前Module需要用到的jar包,按自己需求新增,如果父類已經包含了,可以不用寫版本號 -->

    <dependencies>

        <dependency>

            <groupId>org.projectlombok</groupId>

            <artifactId>lombok</artifactId>

        </dependency>

    </dependencies>

 

</project>

[3].新建Entity

  新建部門Entity且配合lombok使用。

package com.hosystem.springcloud.entities;

 

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

import lombok.experimental.Accessors;

 

import java.io.Serializable;

 

@SuppressWarnings("serial")

@AllArgsConstructor

@NoArgsConstructor

@Data

@Accessors(chain = true)

//Entity orm mysql->Dept(table) 類表關係對映 https://baike.baidu.com/item/%E5%AF%B9%E8%B1%A1%E5%85%B3%E7%B3%BB%E6%98%A0%E5%B0%84/311152?fromtitle=ORM&fromid=3583252&fr=aladdin

public class Dept implements Serializable{  //必須序列化

    private Long  deptno;   //主鍵

    private String  dname;   //部門名稱

    private String  db_source;//來自那個資料庫,因為微服務架構可以一個服務對應一個資料庫,同一個資訊被儲存到不同資料庫

 

    public Dept(String dname) {

        this.dname = dname;

    }

 

}

[4].mvn clean和mvn install

  mvn clean install後給其它模組引用,達到通用目的。即需要用到部門實體的話,不用每個工程都定義一份,直接引用本模組即可。

 

 

(3).microservicecloud-provider-dept-8001

  部門微服務提供者Module

 

[1].建立工程

[2].配置pom檔案

<?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">

    <parent>

        <artifactId>microservicecloud</artifactId>

        <groupId>com.hosystem</groupId>

        <version>1.0-SNAPSHOT</version>

    </parent>

    <modelVersion>4.0.0</modelVersion>

 

    <artifactId>microservicecloud-provider-dept-8001</artifactId>

 

    <dependencies>

        <dependency><!-- 引入自己定義的api通用包,可以使用Dept部門Entity -->

            <groupId>com.hosystem</groupId>

            <artifactId>microservicecloud-api</artifactId>

            <version>${project.version}</version>

        </dependency>

        <dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

        </dependency>

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

        </dependency>

        <dependency>

            <groupId>com.alibaba</groupId>

            <artifactId>druid</artifactId>

        </dependency>

        <dependency>

            <groupId>ch.qos.logback</groupId>

            <artifactId>logback-core</artifactId>

        </dependency>

        <dependency>

            <groupId>org.mybatis.spring.boot</groupId>

            <artifactId>mybatis-spring-boot-starter</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-jetty</artifactId>

        </dependency>

        <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>

        </dependency>

        <!-- 修改後立即生效,熱部署 -->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>springloaded</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-devtools</artifactId>

        </dependency>

    </dependencies>

 

</project>

[3].配置applicaiton.yml檔案

server:

  port: 8001

 

mybatis:

  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置檔案所在路徑

  type-aliases-package: com.hosytem.springcloud.entities    # 所有Entity別名類所在包

  mapper-locations:

  - classpath:mybatis/mapper/**/*.xml                       # mapper對映檔案

 

#name spring.application.name=microservicecloud-dept  很重要很重要很重要

spring:

   application:

    name: microservicecloud-dept

   datasource:

    type: com.alibaba.druid.pool.DruidDataSource            # 當前資料來源操作型別

    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驅動包

    url: jdbc:mysql://192.168.188.188:3306/cloudDB01              # 資料庫名稱

    username: root

    password: 123456

    dbcp2:

      min-idle: 5                                           # 資料庫連線池的最小維持連線數

      initial-size: 5                                       # 初始化連線數

      max-total: 5                                          # 最大連線數

      max-wait-millis: 200                                  # 等待連線獲取的最大超時時間

[4].配置mybatis.cfg.xml檔案

  microservicecloud-provider-dept-8001\src\main\resources\mybatis\mybatis.cfg.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE configuration

        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

        "http://mybatis.org/dtd/mybatis-3-config.dtd">

 

<configuration>

 

    <settings>

        <setting name="cacheEnabled" value="true"<!-- 二級快取開啟 -->

相關文章