既然是一個全品類的電商購物平台,那麼核心自然就是商品。是以我們要搭建的第一個服務,就是商品微服務。其中會包含對于商品相關的一系列内容的管理,包括:
- 商品分類管理
- 品牌管理
- 商品規格參數管理
- 商品管理
- 庫存管理
1.微服務的結構
因為與商品的品類相關,我們的工程命名為
yigou-item
.
需要注意的是,我們的
yigou-item
是一個微服務,那麼将來肯定會有其它系統需要來調用服務中提供的接口,是以肯定也會使用到接口中關聯的實體類。
是以這裡我們需要使用聚合工程,将要提供的接口及相關實體類放到獨立子工程中,以後别人引用的時候,隻需要知道坐标即可。
我們會在
yigou-item
中建立兩個子工程:
yigou-item-interface:主要是對外暴露的接口及相關實體類
yigou-item-service:所有業務邏輯及内部使用接口
調用關系如圖所示:
2.建立父工程yigou-item
依然是使用maven建構:
儲存位置:
不需要任何依賴,我們可以把項目打包方式設定為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>yigou</artifactId>
<groupId>com.yigou.parent</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>yigou-item</artifactId>
<!-- 打包方式為pom -->
<packaging>pom</packaging>
</project>
3.建立yigou-item-interface
在yigou-item工程上點選右鍵,選擇new > module:
注意:接下來填寫的目錄結構需要自己手動完成,儲存到
yigou-item
下的
yigou-item-interface
目錄中:
點選Finish完成。
此時的項目結構:
4.建立yigou-item-service
與
yigou-item-interface
類似,我們選擇在
yigou-item
上右鍵,建立module,然後填寫項目資訊:
填寫存儲位置,是在
/yigou-item/yigou-item-service
目錄
點選Finish完成。
4.1.整個微服務結構
如圖所示:
我們打開
yigou-item
的pom檢視,會發現
yigou-item-interface
和
yigou-item-service
都已經稱為module了:
<?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>yigou</artifactId>
<groupId>com.yigou.parent</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>yigou-item</artifactId>
<!-- 打包方式為pom -->
<packaging>pom</packaging>
<modules>
<module>yigou-item-interface</module>
<module>yigou-item-service</module>
</modules>
</project>
5.添加依賴
接下來我們給
yigou-item-service
中添加依賴:
思考一下我們需要什麼?
- Eureka用戶端
- web啟動器
- mybatis啟動器
- 通用mapper啟動器
- 分頁助手啟動器
- 連接配接池,我們用預設的Hykira
- mysql驅動
- 千萬不能忘了,我們自己也需要yigou-item-interface中的實體類
這些依賴,我們在頂級父工程:yigou中已經添加好了。是以直接引入即可:
<?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>yigou-item</artifactId>
<groupId>com.yigou.parent</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>yigou-item-service</artifactId>
<dependencies>
<!--Eureka用戶端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--web啟動器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis啟動器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.starter.version}</version>
</dependency>
<!-- 通用Mapper啟動器 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>${mapper.starter.version}</version>
</dependency>
<!-- 分頁助手啟動器 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pageHelper.starter.version}</version>
</dependency>
<!-- mysql驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.yigou.parent</groupId>
<artifactId>yigou-item-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
yigou-item-interface
中需要什麼我們暫時不清楚,是以先不管。
6.編寫啟動和配置
在整個
yigou-item工程
中,隻有
yigou-item-service
是需要啟動的。是以在其中編寫啟動類即可:
/**
* @author bruceliu
* @create 2019-09-01 11:35
* @description
*/
@SpringBootApplication
@EnableDiscoveryClient
public class YigouItemService {
public static void main(String[] args) {
SpringApplication.run(YigouItemService.class, args);
}
}
然後是全局屬性檔案:
server:
port: 8081
spring:
application:
name: item-service
datasource:
url: jdbc:mysql://localhost:3306/yigou
username: root
password: 123456
hikari:
maximum-pool-size: 30
minimum-idle: 10
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
instance:
lease-renewal-interval-in-seconds: 5 # 每隔5秒發送一次心跳
lease-expiration-duration-in-seconds: 10 # 10秒不發送就過期
prefer-ip-address: true
ip-address: 127.0.0.1
instance-id: ${spring.application.name}:${server.port}
7.添加商品微服務的路由規則
既然商品微服務已經建立,接下來肯定要添加路由規則到Zuul中,我們不使用預設的路由規則。
zuul:
prefix: /api # 添加路由字首
retryable: true
routes:
item-service: /item/** # 将商品微服務映射到/item/**