緣起本項(xiàng)目是基于之前學(xué)習(xí)的一個(gè)Dubbo+SSM分布式項(xiàng)目進(jìn)行升級(jí),基于此項(xiàng)目對(duì)前后端分離項(xiàng)目、微服務(wù)項(xiàng)目進(jìn)一步深入學(xué)習(xí)。之前學(xué)習(xí)了vue、springBoot、springCloud后,沒(méi)有進(jìn)行更多實(shí)戰(zhàn)練習(xí),借助此機(jī)會(huì),整合之前所學(xué)知識(shí),搭建一套微服務(wù)電商系統(tǒng)。本項(xiàng)目純屬個(gè)人學(xué)習(xí)總結(jié),如有錯(cuò)誤之處,還希望大家能指出,共同討論學(xué)習(xí)。 正文1、項(xiàng)目依賴環(huán)境 工具:idea+vsCode 數(shù)據(jù)庫(kù):mysql 緩存:redis 消息中間件:ActiveMq 2、項(xiàng)目架構(gòu)圖(暫時(shí)留個(gè)位置) 3、整體框架結(jié)構(gòu) -parent 聚合工程 -api 各模塊提供服務(wù)的接口 -eurekaserver 服務(wù)注冊(cè)中心 -pojo 通用實(shí)體類層 -dao 通用數(shù)據(jù)訪問(wèn)層 -common 通用方法 -xxxxx-interface 某服務(wù)層接口 -xxxxx-service 某服務(wù)層實(shí)現(xiàn) -xxxxx-web 某web工程 4、數(shù)據(jù)庫(kù)表 (后期上傳) 一、搭建框架1、創(chuàng)建父工程 至于如何創(chuàng)建聚合工程,此處不在詳細(xì)說(shuō)明,網(wǎng)上有更多詳細(xì)的教程。 創(chuàng)建Maven工程pinyougou-parent (POM) ,groupId 為com.pinyougou ,artifactId 為pinyougou-parent 。 添加依賴(后期根據(jù)需求有所變更) <dependencies>
<!--安全框架-->
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>-->
<!-- springboot整合activemq -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!-- springboot整合amqp -->
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>-->
<!--數(shù)據(jù)庫(kù)jdbc連接池-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</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>2.0.0</version>
</dependency>
<!--集成springCloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!--集成熱部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<!--mysql連接驅(qū)動(dòng)-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 集成lombok 框架 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--springBoot測(cè)試-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 集成redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- jedis客戶端 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!-- spring2.X集成redis所需common-pool2,使用jedis必須依賴它-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.5.0</version>
</dependency>
<!-- 集成aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- 集成commons工具類 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<!-- 集成發(fā)送郵件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- 阿里巴巴數(shù)據(jù)源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.14</version>
</dependency>
<!-- httpclient -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.30</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
<!--<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>-->
</dependencies>2、服務(wù)注冊(cè)中心模塊 創(chuàng)建服務(wù)注冊(cè)中心模塊-pinyougou-eurekaserver 配置注冊(cè)中心,端口號(hào)設(shè)置為8761 server: port: 8761eureka: instance: hostname: localhost # eureka實(shí)例的主機(jī)名 client: register-with-eureka: false #不把自己注冊(cè)到eureka上 fetch-registry: false #不從eureka上來(lái)獲取服務(wù)的注冊(cè)信息 service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #服務(wù)的注冊(cè)地址 添加啟動(dòng)類,開(kāi)啟Eureka Server服務(wù) @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableEurekaServer //開(kāi)啟Eureka Serverpublic class EurekaServerApplication {public static void main(String[] args){
SpringApplication.run(EurekaServerApplication.class,args);
}
}測(cè)試:啟動(dòng)Eureka Server服務(wù),瀏覽器輸入http://localhost:8761/, 能訪問(wèn)springEureka說(shuō)明服務(wù)注冊(cè)中心配置成功. 3、通用實(shí)體類模塊 創(chuàng)建通用實(shí)體類模塊-pinyougou-pojo Pojo:數(shù)據(jù)庫(kù)實(shí)體類 Entity:統(tǒng)一返回?cái)?shù)據(jù)實(shí)體類 Pojogroup:封裝的實(shí)體類數(shù)據(jù) 4、通用數(shù)據(jù)訪問(wèn)模塊 創(chuàng)建通用數(shù)據(jù)訪問(wèn)模塊pinyougou-dao .添加依賴pinyougou-pojo 新建包com.pinyougou.mapper,寫(xiě)需要的mapper接口,注意:需要在加注釋@Mapper 在resource下,寫(xiě)mapper映射文件 5、通用工具類模塊 創(chuàng)建通用工具類模塊pinyougou-common 6、商家商品服務(wù)接口模塊 創(chuàng)建模塊pinyougou-sellergoods-interface,添加依賴pinyougou-pojo 新建包com.pinyougou.sellergoods.service,寫(xiě)service層接口 7、服務(wù)接口提供模塊 創(chuàng)建pinyougou-api(pom) 說(shuō)明:該模塊提供各模塊所需提供的接口,為api父工程,各模塊需要提供接口服務(wù)時(shí),在該父工程下新建各自的子模塊. 本項(xiàng)目實(shí)現(xiàn)流程:該模塊提供的接口,在對(duì)應(yīng)service模塊編寫(xiě)邏輯,調(diào)用方調(diào)用服務(wù)的時(shí)候,繼承api提供的接口,使用@FeignClient(‘service模塊的名稱’)注解調(diào)用服務(wù). 8、商品服務(wù)api 創(chuàng)建pinyougou-sellergoods-api模塊,依賴pinyougou-pojo 創(chuàng)建包com.pinyougou.pojo.TbBrand,編寫(xiě)一個(gè)測(cè)試類: @RequestMapping("/brand")public interface BrandApiService {
@RequestMapping("/findAll")public List<TbBrand> findAll();
}9、商家商品服務(wù)模塊 創(chuàng)建pinyougou-sellergoods-service, 添加依賴pinyougou-sellergoods-interface,pinyougou-dao,pinyougou-sellergoods-api 配置文件: server: port: 9001eureka: client: service-url: defaultZone: http://localhost:8761/eureka/spring: application: name: sellergoods datasource: name: pinyougoudb #?useUnicode=true&characterEncoding=utf8 url: jdbc:mysql://localhost:3306/pinyougoudb?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC username: root password: root # 使用druid數(shù)據(jù)源 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver # 數(shù)據(jù)源其他配置 initialSize: 5minIdle: 5maxActive: 20maxWait: 60000timeBetweenEvictionRunsMillis: 60000minEvictableIdleTimeMillis: 300000validationQuery: SELECT 1 FROM DUAL testWhileIdle: truetestOnBorrow: falsetestOnReturn: falsepoolPreparedStatements: true# 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無(wú)法統(tǒng)計(jì),'wall'用于防火墻 filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20useGlobalDataSourceStat: trueconnectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500mybatis: # 指定全局配置文件位置 #config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath*:/mapper/*Mapper.xml #實(shí)體掃描,多個(gè)package用逗號(hào)或者分號(hào)分隔 type-aliases-package: com.pinyougou.pojo configuration: #配置返回?cái)?shù)據(jù)庫(kù)(column下劃線命名&&返回java實(shí)體是駝峰命名),自動(dòng)匹配無(wú)需as(沒(méi)開(kāi)啟這個(gè),SQL需要寫(xiě)as: select user_id as userId) map-underscore-to-camel-case: true #配置JdbcTypeForNull, oracle數(shù)據(jù)庫(kù)必須配置 jdbc-type-for-null: 'null' 編寫(xiě)啟動(dòng)類,添加掃描mapper類的注解,將服務(wù)注冊(cè)到注冊(cè)中心 @MapperScan(value = "com.pinyougou.mapper") @SpringBootApplication @EnableEurekaClientpublic class SellerGoodsServiceApplication {public static void main(String[] args) { SpringApplication.run(SellerGoodsServiceApplication.class, args); } } 創(chuàng)建包com.pinyougou.sellergoods.service.impl,作用:實(shí)現(xiàn)interface模塊,編寫(xiě)數(shù)據(jù)訪問(wèn)層業(yè)務(wù)邏輯.注意:該類上要加@Service注解. 創(chuàng)建包com.pinyougou.sellergoods.api.service.impl,作用:實(shí)現(xiàn)服務(wù)提供者api接口,編寫(xiě)暴露接口的業(yè)務(wù)邏輯,注意:該類上要添加@RestController注解. 10、運(yùn)營(yíng)商管理后臺(tái) 創(chuàng)建包pinyougou-manager-web,依賴pinyougou-sellergoods-api 在resources下創(chuàng)建配置文件: server: port: 9101eureka: client: service-url: defaultZone: http://localhost:8761/eureka/spring: freemarker: suffix: .html templateLoaderPath: classpath:/templates/cache: false #禁用模板緩存,正式環(huán)境取消 application: name: managerweb main: allow-bean-definition-overriding: true 創(chuàng)建包com.pinyougou.manager.feign,用于服務(wù)的調(diào)用 @FeignClient("sellergoods") //寫(xiě)service層的名稱public interface BrandFeign extends BrandApiService {
}此時(shí),就可以使用BrandFeign調(diào)用BrandApiService的接口 創(chuàng)建包com.pinyougou.manager.controller,進(jìn)行測(cè)試 @RestController @RequestMapping("/brands")public class BrandController { @Autowiredprivate BrandFeign brandFeign; @RequestMapping("/findAll")public List<TbBrand> findAll(){ List<TbBrand> lists=null; lists=brandFeign.findAll();return lists; } } 啟動(dòng)服務(wù): 啟動(dòng)pinyougou-eurekaserver 啟動(dòng)pinyougou-sellergoods-service 啟動(dòng)pinyougou-manager-web 在瀏覽器輸入:http://localhost:9101/brands/findAll,如果能獲取到數(shù)據(jù)庫(kù)數(shù)據(jù),說(shuō)明服務(wù)調(diào)用成功. 此時(shí),瀏覽器中輸入http://localhost:8761/,能看到剛啟動(dòng)的兩個(gè)服務(wù)已經(jīng)注冊(cè)到Eureka注冊(cè)中心了.
目前的項(xiàng)目框架如下:
|
|
|