本項目的筆記和資料的Download,請點擊這一句話自行獲取。
day01-springboot(理論篇) ;day01-springboot(實踐篇)
day02-springcloud(理論篇一) ;day02-springcloud(理論篇二) ;day02-springcloud(理論篇三) ;day02-springcloud(理論篇四) ;
day03-springcloud(Hystix,Feign) ;day03-springcloud(Zuul網關)
day04-項目搭建(一) ; day04-項目搭建(二); day04-ES6語法入門
day05-Vue入門學習
14 微服務電商【黑馬樂優商城】:day04-項目搭建(二)
0.學習目標
- 了解樂優商城項目結構
- 能獨立搭建項目基本框架
- 能參考使用ES6的新語法
3.4.創建父工程
創建統一的父工程:leyou,用來管理依賴及其版本,注意是創建project,而不是module
填寫項目信息:
然后將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.leyou.parent</groupId> <artifactId>leyou</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.8.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> <spring-cloud.version>Finchley.SR4</spring-cloud.version> <mybatis.starter.version>1.3.5</mybatis.starter.version> <mapper.starter.version>2.1.15</mapper.starter.version> <pageHelper.starter.version>1.2.12</pageHelper.starter.version> <druid.starter.version>1.1.10</druid.starter.version> <mysql.version>5.1.45</mysql.version> <leyou.latest.version>1.0.0-SNAPSHOT</leyou.latest.version> <fastDFS.client.version>1.26.1-RELEASE</fastDFS.client.version> </properties> <dependencyManagement> <dependencies> <!-- springCloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </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> <!--FastDFS客戶端--> <dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>${fastDFS.client.version}</version> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
在父工程中引入了SpringCloud等,很多以后需要用到的依賴,以后創建的子工程就不需要自己引入了。
可以刪除src目錄,工程結構如下:
3.5.創建EurekaServer
3.5.1.創建工程
我們的注冊中心,起名為:leyou-registry
選擇新建module:
然后填寫項目坐標,我們的項目名稱為 leyou-registry
選擇安裝目錄,因為是聚合項目,目錄應該是在父工程leyou的下面。
3.5.2.添加依賴
添加EurekaServer的依賴:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>
3.5.3.編寫啟動類
com.leyou.LeyouRegistryApplication
@SpringBootApplication @EnableEurekaServer public class LeyouRegistryApplication { public static void main(String[] args) { SpringApplication.run(LeyouRegistryApplication.class, args); } }
3.5.4.配置文件
#tomcat服務器端口 server: port: 10086 #服務名稱 spring: application: name: leyou-registry #注冊中心 eureka: client: service-url: defaultZone: http://127.0.0.1:${server.port}/eureka register-with-eureka: false # 把自己注冊到eureka服務列表 fetch-registry: false # 拉取eureka服務信息 server: enable-self-preservation: false # 關閉自我保護 eviction-interval-timer-in-ms: 5000 # 每隔5秒鍾,進行一次服務列表的清理
3.6.創建Zuul網關
3.6.1.創建工程
與上面類似,選擇maven方式創建Module,然后填寫項目名稱,我們命名為:leyou-gateway
3.6.2.添加依賴
這里我們需要添加Zuul和EurekaClient的依賴:
<dependencies> <!--Zuul網關的依賴啟動器--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <!--eureka-client的依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- springboot提供微服務檢測接口,默認對外提供幾個接口 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
3.6.3.編寫啟動類
com.leyou.LeyouGatewayApplication
@SpringBootApplication @EnableDiscoveryClient @EnableZuulProxy public class LeyouGatewayApplication { public static void main(String[] args) { SpringApplication.run(LeyouGatewayApplication.class, args); } }
3.6.4.配置文件
#tomcat服務端口 server: port: 10010 #服務名稱 spring: application: name: leyou-gateway #注冊中心的客戶端配置 eureka: client: service-url: defaultZone: http://127.0.0.1:10086/eureka registry-fetch-interval-seconds: 5 #拉取eureka服務信息的時間間隔 #zuul網關的配置項 zuul: prefix: /api # 添加路由前綴 routes: item-service: /item/** # 商品微服務的映射路徑 #熔斷器的配置 hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 5000 # 熔斷超時時長:5000ms #負載均衡 ribbon: ConnectTimeout: 1000 # ribbon鏈接超時時長 ReadTimeout: 3500 # ribbon讀取超時時長 MaxAutoRetries: 0 # 當前服務重試次數 MaxAutoRetriesNextServer: 0 # 切換服務重試次數
3.6.5.項目結構
目前,leyou下有兩個子模塊:
- leyou-registry:服務的注冊中心(EurekaServer)
- leyou-gateway:服務網關(Zuul)
截止到這里,我們已經把基礎服務搭建完畢,為了便於開發,統一配置中心(ConfigServer)我們留待以后添加。
3.7.創建商品微服務
既然是一個全品類的電商購物平台,那么核心自然就是商品。因此我們要搭建的第一個服務,就是商品微服務。其中會包含對於商品相關的一系列內容的管理,包括:
- 商品分類管理
- 品牌管理
- 商品規格參數管理
- 商品管理
- 庫存管理
3.7.1.微服務的結構
因為與商品的品類相關,我們的工程命名為leyou-item
.
需要注意的是,我們的leyou-item是一個微服務,那么將來肯定會有其它系統需要來調用服務中提供的接口,獲取的接口數據,也需要對應的實體類來封裝,因此肯定也會使用到接口中關聯的實體類。
因此這里我們需要使用聚合工程,將要提供的接口及相關實體類放到獨立子工程中,以后別人引用的時候,只需要知道坐標即可。
我們會在leyou-item中創建兩個子工程:
- leyou-item-interface:主要是對外暴露的接口及相關實體類
- leyou-item-service:所有業務邏輯及內部使用接口
調用關系如圖所示:
3.7.2.leyou-item
依然是使用maven構建:
因為是聚合工程,所以把項目打包方式設置為pom
<!-- 因為是聚合工程,所以打包方式為pom --> <packaging>pom</packaging>
3.7.3.leyou-item-interface
在leyou-item工程上點擊右鍵,選擇new --> module:
依然是使用maven構建,注意父工程是leyou-item:
注意:目錄結構,保存到leyou-item
下的leyou-item-interface
目錄中。
3.7.4.leyou-item-service
與leyou-item-interface
類似,我們選擇在leyou-item
上右鍵,新建module,然后填寫項目信息:
3.7.5.整個微服務結構
如圖所示:
我們打開leyou-item的pom查看,會發現leyou-item-interface和leyou-item-service都已經成為module了:
可以刪除leyou-item工程的src目錄
3.7.6.添加依賴
接下來我們給leyou-item-service
中添加依賴:
思考一下我們需要什么?
- Eureka客戶端
- web啟動器
- mybatis啟動器
- 通用mapper啟動器
- 分頁助手啟動器
- 連接池,我們用默認的Hykira
- mysql驅動
- 千萬不能忘了,我們自己也需要
ly-item-interface
中的實體類
這些依賴,我們在頂級父工程:leyou中已經添加好了。所以直接引入即可:
<?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>leyou-item</artifactId> <groupId>com.leyou.item</groupId> <version>1.0.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.leyou.item</groupId> <artifactId>leyou-item-service</artifactId> <dependencies> <!-- web啟動器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- eureka客戶端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--mybatis的啟動器--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!-- 通用mapper啟動器 --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> </dependency> <!--分頁助手啟動器--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> </dependency> <!-- mysql驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--平級的模塊之間調用要手動導依賴坐標--> <dependency> <groupId>com.leyou.item</groupId> <artifactId>leyou-item-interface</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <!-- springboot檢測服務啟動器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> </project>
leyou-item-interface中需要什么我們暫時不清楚,所以先不管。以后需要什么依賴,再引入。
通用mapper的啟動器內部自帶引用 mybatis和jdbc的啟動器。
3.7.7.編寫啟動和配置
在整個leyou-item工程
中,只有leyou-item-service
是需要啟動的。因此在其中編寫啟動類即可:
@SpringBootApplication @EnableDiscoveryClient public class LeyouItemServiceApplication { public static void main(String[] args) { SpringApplication.run(LeyouItemServiceApplication.class, args); } }
然后是全局屬性application.yml文件:
#tomcat服務端口 server: port: 8081 #服務名稱 spring: application: name: item-service datasource: url: jdbc:mysql://localhost:3306/leyou username: root password: root hikari: max-lifetime: 28830000 # 一個連接的生命時長(毫秒),超時而且沒被使用則被釋放(retired),缺省:30分鍾,建議設置比數據庫超時時長少30秒,參考MySQL wait_timeout參數(show variables like '%timeout%';) maximum-pool-size: 8 # 連接池中允許的最大連接數。缺省值:10;推薦的公式:((core_count * 2) + effective_spindle_count) driver-class-name: com.mysql.jdbc.Driver #注冊中心 eureka: client: service-url: defaultZone: http://127.0.0.1:10086/eureka #register-with-eureka: false # 把自己注冊到eureka服務列表 #fetch-registry: false # 拉取eureka服務信息 instance: prefer-ip-addresss: true ip-address: 127.0.0.1 lease-renewal-interval-in-seconds: 5 # 5秒鍾發送一次心跳 lease-expiration-duration-in-seconds: 10 # 10秒不發送就過期
3.8.添加商品微服務的路由規則
既然商品微服務已經創建,接下來肯定要添加路由規則到Zuul中,我們不使用默認的路由規則。
使用如下代碼到leyou-gateway工程的application.yml配置文件:
zuul: prefix: /api # 路由路徑前綴 routes: item-service: /item/** # 商品微服務的映射路徑
3.9.啟動測試
我們按順序分別啟動:leyou-registry,leyou-gateway,leyou-item-service
查看Eureka面板:
3.10.測試路由規則
為了測試路由規則是否暢通,我們是不是需要在item-service中編寫一個controller接口呢?
其實不需要,SpringBoot提供了一個依賴:actuator
只要我們添加了actuator的依賴,它就會為我們生成一系列的訪問接口:
- /info
- /health
- /refresh
添加依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
重啟后訪問Eureka控制台:
鼠標懸停在item-service上,會顯示一個地址:
這就是actuator提供的接口,我們點擊訪問:
因為我們沒有添加信息,所以是一個空的json,但是可以肯定的是:我們能夠訪問到item-service了。
接下來我們通過路由訪問試試,根據路由規則,我們需要訪問的地址是:
http://127.0.0.1:10010/api/item/actuator/info
3.11.通用工具模塊
有些工具或通用的約定內容,我們希望各個服務共享,因此需要創建一個工具模塊:leyou-common
右鍵leyou工程,使用maven來構建module:
工程結構:
目前還不需要自己寫工具類,我們從課前資料里直接導入即可。
選中java目錄,新建一個package:com.leyou.common.utils
把資料里的4個工具類copy到剛才新建的utils目錄里。
在leyou-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>leyou</artifactId> <groupId>com.leyou.parent</groupId> <version>1.0.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.leyou.common</groupId> <artifactId>leyou-common</artifactId> <dependencies> <dependency> <!-- log日志啟動器 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-core</artifactId> <scope>provided</scope> </dependency> <dependency><!--springMVC的JSON反序列化把{}字符串轉成對象--> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.9.3</version> </dependency> <dependency> <groupId>org.jetbrains</groupId> <artifactId>annotations</artifactId> <version>13.0</version> <scope>compile</scope> </dependency> </dependencies> </project>
jackson的工具類JsonUtils的使用
5.通用異常處理
在上圖interface的java目錄下新建一個實體類:
com.leyou.item.pojo.Item
package com.leyou.item.pojo; import lombok.Data; @Data //字節碼階段自動生成get/set/toString方法 public class Item { private Integer id; private String name; private Long price; }
在商品服務模塊下新建一個類:
com.leyou.item.service.ItemService
@Service public class ItemService { public Item saveItem(Item item){ //模擬商品新增 int id = new Random().nextInt(100); //產生隨機數0~99 item.setId(id); return item; } }
REST規范的URI定義
HTTP協議響應狀態碼
Insomnia.Setup.7.0.1.exe 下載該軟件作為調試REST工具
https://www.insomnia.rest/
Debug APIs like a human, not a robot
Finally, a REST client you'll love
新建一個web層的Controller類
com.leyou.item.web.ItemController
@RestController @RequestMapping("item") public class ItemController { @Autowired private ItemService itemService; @PostMapping public ResponseEntity<Item> saveItem(Item item){ //校驗價格。如果價格為空,則拋出異常,返回400狀態碼 if(item.getPrice() == null){ return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); } Item result = itemService.saveItem(item); return ResponseEntity.status(HttpStatus.CREATED).body(result); } }
模擬客戶端瀏覽器提交POST請求,需要使用一個REST Client作為測試工具。
https://github.com/wisdom-projects/rest-client
特點是使用方便,因為他只是一個jar包,雙擊執行(當然前提是你電腦上安裝並配置環境變量jdk1.8版本)
5.2.3 使用springMVC提供的統一異常攔截器
我們先修改controller的代碼,把異常拋出:
@RestController @RequestMapping("item") public class ItemController { @Autowired private ItemService itemService; @PostMapping public ResponseEntity<Item> saveItem(Item item){ //校驗價格。如果價格為空,則拋出異常,返回400狀態碼 if(item.getPrice() == null){ //return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); throw new RuntimeException("價格不能為空"); } Item result = itemService.saveItem(item); return ResponseEntity.status(HttpStatus.CREATED).body(result); } }
然后在leyou-common工程下的pom.xml添加SpringMVC的依賴坐標
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency>
com.leyou.common.advice.CommonExceptionHandler
也可以叫作BasicExceptionHandler
@ControllerAdvice //默認攔截所有加了@Controller注解的類 public class CommonExceptionHandler { @ExceptionHandler(RuntimeException.class) public ResponseEntity<String> handleException(RuntimeException e){ return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); } }
leyou-Item-service 的pom.xml添加引入leyou-common模塊的依賴坐標:
<dependency> <groupId>com.leyou.common</groupId> <artifactId>leyou-common</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency>
5.2.4 自定義異常
返回至ly-common子模塊,新建一個package
com.leyou.common.enums
新建一個枚舉類(有固定實例化個數的Class)
package com.leyou.common.enums; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; @Getter @NoArgsConstructor @AllArgsConstructor public enum ExceptionEnum { PRICE_CANNOT_BE_NULL(400,"價格不能為空!") ; private int code; private String msg; }
創建自定義異常類繼承於 RuntimeException
package com.leyou.common.exception; import com.leyou.common.enums.ExceptionEnum; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; @NoArgsConstructor @AllArgsConstructor @Getter public class LyException extends RuntimeException { private ExceptionEnum exceptionEnum; //1.自定義異常先繼承 RuntimeException //2.把枚舉類作為私有的成員變量 //3.提供Getter方法 //4.提供空參構造器和全參構造器 }
創建一個異常結果對象類,用來封裝異常屬性信息。
com.leyou.common.vo.ExceptionResult
package com.leyou.common.vo; import com.leyou.common.enums.ExceptionEnum; import lombok.Data; @Data public class ExceptionResult { private int status; private String message; private Long timestamp; public ExceptionResult(ExceptionEnum em){ this.status = em.getCode(); this.message = em.getMsg(); this.timestamp = System.currentTimeMillis(); } }
最后返回到com.leyou.common.advice.CommonExceptionHandler 修改后的代碼如下:
package com.leyou.common.advice; import com.leyou.common.enums.ExceptionEnum; import com.leyou.common.exception.LyException; import com.leyou.common.vo.ExceptionResult; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; @ControllerAdvice //默認攔截所有加了@Controller注解的類 public class CommonExceptionHandler { @ExceptionHandler(LyException.class) public ResponseEntity<ExceptionResult> handleException(LyException e){ //從被攔截的異常中取出枚舉的成員變量 ExceptionEnum em = e.getExceptionEnum(); return ResponseEntity.status(em.getCode()) .body(new ExceptionResult(e.getExceptionEnum())); } }
com.leyou.item.web.ItemController
@RestController @RequestMapping("item") public class ItemController { @Autowired private ItemService itemService; @PostMapping public ResponseEntity<Item> saveItem(Item item){ //校驗價格。如果價格為空,則拋出異常,返回400狀態碼 if(item.getPrice() == null){ //return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); //throw new RuntimeException("價格不能為空"); throw new LyException(ExceptionEnum.PRICE_CANNOT_BE_NULL); } Item result = itemService.saveItem(item); return ResponseEntity.status(HttpStatus.CREATED).body(result); } }
最后不要忘記在leyou-common工具類子模塊的pom.xml添加以下打包插件的配置:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions><!--是為了解決Unable to find main class的問題--> <execution> <phase>none</phase> </execution> </executions> <configuration><!--是為了解決install找不到依賴包的問題--> <classifier>execute</classifier> </configuration> </plugin> </plugins> </build>
自定義異常處理返回de封裝對象 HTTP RESTClient
=============================================
參考資料:
spring-boot-maven-plugin 插件 install時報錯 程序包不存在以及找不到類的情況
end