本項目是基于之前學(xué)習(xí)的一個Dubbo+SSM分布式項目進(jìn)行升級,基于此項目對前后端分離項目、微服務(wù)項目進(jìn)一步深入學(xué)習(xí)。之前學(xué)習(xí)了vue、springBoot、springCloud后,沒有進(jìn)行更多實戰(zhàn)練習(xí),借助此機(jī)會,整合之前所學(xué)知識,搭建一套微服務(wù)電商系統(tǒng)。本項目純屬個人學(xué)習(xí)總結(jié),如有錯誤之處,還希望大家能指出,共同討論學(xué)習(xí)。
1、項目依賴環(huán)境
工具:idea+vsCode
數(shù)據(jù)庫:mysql
緩存:redis
消息中間件:ActiveMq
2、項目架構(gòu)圖(暫時留個位置)
3、整體框架結(jié)構(gòu)
-parent 聚合工程
-api 各模塊提供服務(wù)的接口
-eurekaserver 服務(wù)注冊中心
-pojo 通用實體類層
-dao 通用數(shù)據(jù)訪問層
-common 通用方法
-xxxxx-interface 某服務(wù)層接口
-xxxxx-service 某服務(wù)層實現(xiàn)
-xxxxx-web 某web工程
4、數(shù)據(jù)庫表
(后期上傳)
1、創(chuàng)建父工程
至于如何創(chuàng)建聚合工程,此處不在詳細(xì)說明,網(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ù)庫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ū)動--> <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測試--> <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ù)注冊中心模塊
創(chuàng)建服務(wù)注冊中心模塊-pinyougou-eurekaserver
配置注冊中心,端口號設(shè)置為8761
server: port: 8761eureka: instance: hostname: localhost # eureka實例的主機(jī)名 client: register-with-eureka: false #不把自己注冊到eureka上 fetch-registry: false #不從eureka上來獲取服務(wù)的注冊信息 service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #服務(wù)的注冊地址
添加啟動類,開啟Eureka Server服務(wù)
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})@EnableEurekaServer //開啟Eureka Serverpublic class EurekaServerApplication {public static void main(String[] args){ SpringApplication.run(EurekaServerApplication.class,args); }}
測試:啟動Eureka Server服務(wù),瀏覽器輸入http://localhost:8761/,
能訪問springEureka說明服務(wù)注冊中心配置成功.
3、通用實體類模塊
創(chuàng)建通用實體類模塊-pinyougou-pojo
Pojo:數(shù)據(jù)庫實體類
Entity:統(tǒng)一返回數(shù)據(jù)實體類
Pojogroup:封裝的實體類數(shù)據(jù)
4、通用數(shù)據(jù)訪問模塊
創(chuàng)建通用數(shù)據(jù)訪問模塊pinyougou-dao .添加依賴pinyougou-pojo
新建包com.pinyougou.mapper,寫需要的mapper接口,注意:需要在加注釋@Mapper
在resource下,寫mapper映射文件
5、通用工具類模塊
創(chuàng)建通用工具類模塊pinyougou-common
6、商家商品服務(wù)接口模塊
創(chuàng)建模塊pinyougou-sellergoods-interface,添加依賴pinyougou-pojo
新建包com.pinyougou.sellergoods.service,寫service層接口
7、服務(wù)接口提供模塊
創(chuàng)建pinyougou-api(pom)
說明:該模塊提供各模塊所需提供的接口,為api父工程,各模塊需要提供接口服務(wù)時,在該父工程下新建各自的子模塊.
本項目實現(xiàn)流程:該模塊提供的接口,在對應(yīng)service模塊編寫邏輯,調(diào)用方調(diào)用服務(wù)的時候,繼承api提供的接口,使用@FeignClient(‘service模塊的名稱’)注解調(diào)用服務(wù).
8、商品服務(wù)api
創(chuàng)建pinyougou-sellergoods-api模塊,依賴pinyougou-pojo
創(chuàng)建包com.pinyougou.pojo.TbBrand,編寫一個測試類:
@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)計攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計,'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 #實體掃描,多個package用逗號或者分號分隔 type-aliases-package: com.pinyougou.pojo configuration: #配置返回數(shù)據(jù)庫(column下劃線命名&&返回java實體是駝峰命名),自動匹配無需as(沒開啟這個,SQL需要寫as: select user_id as userId) map-underscore-to-camel-case: true #配置JdbcTypeForNull, oracle數(shù)據(jù)庫必須配置 jdbc-type-for-null: 'null'
編寫啟動類,添加掃描mapper類的注解,將服務(wù)注冊到注冊中心
@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,作用:實現(xiàn)interface模塊,編寫數(shù)據(jù)訪問層業(yè)務(wù)邏輯.注意:該類上要加@Service注解.
創(chuàng)建包com.pinyougou.sellergoods.api.service.impl,作用:實現(xiàn)服務(wù)提供者api接口,編寫暴露接口的業(yè)務(wù)邏輯,注意:該類上要添加@RestController注解.
10、運(yùn)營商管理后臺
創(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") //寫service層的名稱public interface BrandFeign extends BrandApiService {}
此時,就可以使用BrandFeign調(diào)用BrandApiService的接口
創(chuàng)建包com.pinyougou.manager.controller,進(jìn)行測試
@RestController@RequestMapping("/brands")public class BrandController { @Autowiredprivate BrandFeign brandFeign; @RequestMapping("/findAll")public List<TbBrand> findAll(){ List<TbBrand> lists=null; lists=brandFeign.findAll();return lists; }}
啟動服務(wù):
啟動pinyougou-eurekaserver
啟動pinyougou-sellergoods-service
啟動pinyougou-manager-web
在瀏覽器輸入:http://localhost:9101/brands/findAll,如果能獲取到數(shù)據(jù)庫數(shù)據(jù),說明服務(wù)調(diào)用成功.
此時,瀏覽器中輸入http://localhost:8761/,能看到剛啟動的兩個服務(wù)已經(jīng)注冊到Eureka注冊中心了.
目前的項目框架如下:
聯(lián)系客服