3 框架搭建
3.1 環(huán)境準(zhǔn)備
(1)VMware Workstation Pro安裝centos7 鏡像
(2)安裝docker
(3)拉取mySQL鏡像虱颗,并創(chuàng)建容器
(4)客戶端連接mysql容器萤捆,建庫(kù)建表(建庫(kù)建表語(yǔ)句在資源文件夾中提供)
虛擬機(jī)數(shù)據(jù):
虛擬機(jī)IP:192.168.211.132
虛擬機(jī)賬號(hào):root 密碼:123456
數(shù)據(jù)庫(kù)端口:3306
數(shù)據(jù)庫(kù)賬號(hào):root 密碼:123456
數(shù)據(jù)庫(kù)腳本:資料\數(shù)據(jù)庫(kù)腳本
3.2 項(xiàng)目結(jié)構(gòu)說(shuō)明
結(jié)構(gòu)說(shuō)明:
changgou-gateway
網(wǎng)關(guān)模塊,根據(jù)網(wǎng)站的規(guī)模和需要栏饮,可以將綜合邏輯相關(guān)的服務(wù)用網(wǎng)關(guān)路由組合到一起。在這里還可以做鑒權(quán)和限流相關(guān)操作曙咽。
changgou-service
微服務(wù)模塊竿拆,該模塊用于存放所有獨(dú)立的微服務(wù)工程。
changgou-service_api
對(duì)應(yīng)工程的JavaBean笨触、Feign懦傍、以及Hystrix配置,該工程主要對(duì)外提供依賴芦劣。
changgou-transaction-fescar
分布式事務(wù)模塊粗俱,將分布式事務(wù)抽取到該工程中,任何工程如需要使用分布式事務(wù)虚吟,只需依賴該工程即可寸认。
changgou-web
web服務(wù)工程签财,對(duì)應(yīng)功能模塊如需要調(diào)用多個(gè)微服務(wù),可以將他們寫(xiě)入到該模塊中偏塞,例如網(wǎng)站后臺(tái)唱蒸、網(wǎng)站前臺(tái)等
3.3 公共工程搭建
3.3.1 父工程搭建
創(chuàng)建父工程 changgou-parent ,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.changgou</groupId>
<artifactId>changgou-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<properties>
<skipTests>true</skipTests>
</properties>
<!--依賴包-->
<dependencies>
<!--測(cè)試包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
<!--swagger文檔-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<!--
http://localhost:9011/swagger-ui.html
-->
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
刪除src文件夾
3.3.2 其他公共模塊搭建
創(chuàng)建changgou-gateway、changgou-service烛愧、changgou-service-api油宜、changgou-web工程掂碱,工程全部為pom工程怜姿,并將所有工程的src文件刪除。
pom.xml中打pom包
<packaging>pom</packaging>
項(xiàng)目結(jié)構(gòu)如下:
3.4 Eureka微服務(wù)搭建
3.4.1 pom.xml依賴
創(chuàng)建模塊changgou-eureka 疼燥,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">
<parent>
<artifactId>changgou_parent</artifactId>
<groupId>com.changgou</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>changgou_eureka</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
3.4.2 appliation.yml配置
創(chuàng)建配置文件application.yml
server:
port: 7001
eureka:
instance:
hostname: 127.0.0.1
client:
register-with-eureka: false #是否將自己注冊(cè)到eureka中
fetch-registry: false #是否從eureka中獲取信息
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3.4.3 啟動(dòng)類(lèi)配置
創(chuàng)建包c(diǎn)om.changgou 包下創(chuàng)建啟動(dòng)類(lèi)EurekaApplication沧卢,代碼如下:
上圖代碼如下:
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class);
}
}
測(cè)試訪問(wèn)http://localhost:7001/
,效果如下:
3.5 公共模塊搭建
3.5.1 pom.xml依賴
創(chuàng)建公共子模塊changgou-common,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">
<parent>
<artifactId>changgou-parent</artifactId>
<groupId>com.changgou</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>changgou-common</artifactId>
<dependencies>
<!--web起步依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- redis 使用-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--微信支付-->
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>
<!--httpclient支持-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>
</project>
公共子模塊引入這些依賴后醉者,其他微服務(wù)引入changgou-common后也自動(dòng)引入了這些依賴
3.5.2 常用對(duì)象
創(chuàng)建entity包 但狭,在entity包下創(chuàng)建返回狀態(tài)碼實(shí)體類(lèi)==[StatusCode]==
/**
* 返回碼
*/
public class StatusCode {
public static final int OK = 20000;//成功
public static final int ERROR = 20001;//失敗
public static final int LOGINERROR = 20002;//用戶名或密碼錯(cuò)誤
public static final int ACCESSERROR = 20003;//權(quán)限不足
public static final int REMOTEERROR = 20004;//遠(yuǎn)程調(diào)用失敗
public static final int REPERROR = 20005;//重復(fù)操作
public static final int NOTFOUNDERROR = 20006;//沒(méi)有對(duì)應(yīng)的搶購(gòu)數(shù)據(jù)
}
包下建立類(lèi)==Result==用于微服務(wù)返回結(jié)果給前端
/**
* 返回結(jié)果實(shí)體類(lèi)
*/
public class Result<T> {
private boolean flag;//是否成功
private Integer code;//返回碼
private String message;//返回消息
private T data;//返回?cái)?shù)據(jù)
public Result(boolean flag, Integer code, String message, Object data) {
this.flag = flag;
this.code = code;
this.message = message;
this.data = (T) data;
}
public Result(boolean flag, Integer code, String message) {
this.flag = flag;
this.code = code;
this.message = message;
}
public Result() {
this.flag = true;
this.code = StatusCode.OK;
this.message = "操作成功!";
}
// getter and setter.....
}
在entity包下建立類(lèi)用于承載分頁(yè)的數(shù)據(jù)結(jié)果
/**
* 分頁(yè)結(jié)果類(lèi)
*/
public class PageResult<T> {
private Long total;//總記錄數(shù)
private List<T> rows;//記錄
public PageResult(Long total, List<T> rows) {
this.total = total;
this.rows = rows;
}
public PageResult() {
}
//getter and setter ......
}
當(dāng)然,我們還可以將其他工具類(lèi)都一起倒入到工程中撬即,以后會(huì)用到立磁,將資料\工具類(lèi)
中的所有類(lèi)直接導(dǎo)入到entity包下。
3.6 數(shù)據(jù)訪問(wèn)工程搭建
創(chuàng)建公共模塊changgou-common-db 剥槐,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>changgou-parent</artifactId>
<groupId>com.changgou</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>changgou-common-db</artifactId>
<!--依賴-->
<dependencies>
<!--對(duì)changgou-common的依賴-->
<dependency>
<groupId>com.changgou</groupId>
<artifactId>changgou-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--通用mapper起步依賴-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.4</version>
</dependency>
<!--MySQL數(shù)據(jù)庫(kù)驅(qū)動(dòng)-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis分頁(yè)插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</project>
這個(gè)公共模塊是連接mysql數(shù)據(jù)庫(kù)的公共微服務(wù)模塊唱歧,所以需要連接mysql的微服務(wù)都繼承自此工程。
3.7 商品微服務(wù)搭建
商品微服務(wù)主要是實(shí)現(xiàn)對(duì)商品的增刪改查相關(guān)操作粒竖,以及商品相關(guān)信息的增刪改查颅崩。
3.7.1 公共組件工程搭建
創(chuàng)建changgou-service-api子模塊changgou-service-goods-api,并將資料\javabean\changgou-service-goods-api
中的Pojo導(dǎo)入到工程中蕊苗。
修改父工程changgou-service-api的pom.xml沿后,添加persistence-api
和changgou-common
的依賴,代碼如下:
<dependencies>
<!--通用的common-->
<dependency>
<groupId>com.changgou</groupId>
<artifactId>changgou-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--每個(gè)工程都有Pojo朽砰,都需要用到該包對(duì)應(yīng)的注解-->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
3.7.2 微服務(wù)工程搭建
修改changgou-service的pom.xml引入changgou-common-db
的依賴尖滚,代碼如下:
<!--依賴-->
<dependencies>
<dependency>
<groupId>com.changgou</groupId>
<artifactId>changgou-common-db</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
在changgou-service中創(chuàng)建changgou-service-goods ,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">
<parent>
<artifactId>changgou-service</artifactId>
<groupId>com.changgou</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>changgou-service-goods</artifactId>
<!--依賴-->
<dependencies>
<dependency>
<groupId>com.changgou</groupId>
<artifactId>changgou-service-goods-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
在resources下創(chuàng)建配置文件application.yml
server:
port: 18081
spring:
application:
name: goods
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.211.132:3306/changgou_goods?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 123456
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka
instance:
prefer-ip-address: true
feign:
hystrix:
enabled: true
mybatis:
configuration:
map-underscore-to-camel-case: true
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.changgou.goods.pojo
在包c(diǎn)om.changgou.goods 包下創(chuàng)建啟動(dòng)類(lèi)GoodsApplication瞧柔,代碼如下:
上圖代碼如下:
@SpringBootApplication
@EnableEurekaClient
@MapperScan(basePackages = {"com.changgou.goods.dao"})
public class GoodsApplication {
public static void main(String[] args) {
SpringApplication.run(GoodsApplication.class);
}
}
注意 :@MapperScan是tk.mybatis.spring.annotation
包下的熔掺,用于掃描==Mapper==接口
啟動(dòng)changgou-service-goods
再訪問(wèn)<http://localhost:7001/>
效果如下: