基於【 springBoot +springCloud+vue 項目】一 || 後端搭建

 緣起

本項目是基於以前學習的一個Dubbo+SSM分佈式項目進行升級,基於此項目對先後端分離項目、微服務項目進一步深刻學習。以前學習了vue、springBoot、springCloud後,沒有進行更多實戰練習,藉助此機會,整合以前所學知識,搭建一套微服務電商系統。本項目純屬我的學習總結,若有錯誤之處,還但願你們能指出,共同討論學習。html

 

正文

一、項目依賴環境vue

 

工具:idea+vsCodejava

數據庫:mysqlmysql

緩存:redisweb

消息中間件:ActiveMqredis

二、項目架構圖(暫時留個位置)spring

 

三、總體框架結構sql

-parent        聚合工程數據庫

-api          各模塊提供服務的接口apache

-eurekaserver    服務註冊中心

-pojo         通用實體類層

-dao          通用數據訪問層

-common       通用方法

-xxxxx-interface  某服務層接口

-xxxxx-service   某服務層實現

-xxxxx-web     某web工程  

四、數據庫表

(後期上傳)

1、搭建框架

一、建立父工程

至於如何建立聚合工程,此處不在詳細說明,網上有更多詳細的教程。

建立Maven工程pinyougou-parent POM) ,groupId com.pinyougou ,artifactId pinyougou-parent

添加依賴(後期根據需求有所變動)

<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>-->

    <!--數據庫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鏈接驅動-->
    <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>
    <!-- 集成發送郵件-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <!-- 阿里巴巴數據源 -->
    <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>

二、服務註冊中心模塊

建立服務註冊中心模塊-pinyougou-eurekaserver

配置註冊中心,端口號設置爲8761

server:
  port: 8761
eureka:
  instance:
    hostname: localhost # eureka實例的主機名
  client:
    register-with-eureka: false #不把本身註冊到eureka上
    fetch-registry: false #不從eureka上來獲取服務的註冊信息
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #服務的註冊地址

 

添加啓動類,開啓Eureka Server服務

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableEurekaServer //開啓Eureka Server
public class EurekaServerApplication {
    public static void main(String[] args){
        SpringApplication.run(EurekaServerApplication.class,args);
    }
}

測試:啓動Eureka Server服務,瀏覽器輸入http://localhost:8761/,

能訪問springEureka說明服務註冊中心配置成功.

 

三、通用實體類模塊

建立通用實體類模塊-pinyougou-pojo

Pojo:數據庫實體類

Entity:統一返回數據實體類

Pojogroup:封裝的實體類數據

四、通用數據訪問模塊

建立通用數據訪問模塊pinyougou-dao .添加依賴pinyougou-pojo

新建包com.pinyougou.mapper,寫須要的mapper接口,注意:須要在加註釋@Mapper

resource,mapper映射文件

五、通用工具類模塊

建立通用工具類模塊pinyougou-common 

六、商家商品服務接口模塊

建立模塊pinyougou-sellergoods-interface,添加依賴pinyougou-pojo

新建包com.pinyougou.sellergoods.service,service層接口

七、服務接口提供模塊

建立pinyougou-api(pom)

說明:該模塊提供各模塊所需提供的接口,api父工程,各模塊須要提供接口服務時,在該父工程下新建各自的子模塊.

本項目實現流程:該模塊提供的接口,在對應service模塊編寫邏輯,調用方調用服務的時候,繼承api提供的接口,使用@FeignClient(‘service模塊的名稱’)註解調用服務.

八、商品服務api

建立pinyougou-sellergoods-api模塊,依賴pinyougou-pojo

建立包com.pinyougou.pojo.TbBrand,編寫一個測試類:

 @RequestMapping("/brand")
public interface BrandApiService {
    @RequestMapping("/findAll")
public List<TbBrand> findAll();
}

 

九、商家商品服務模塊

建立pinyougou-sellergoods-service,

添加依賴pinyougou-sellergoods-interface,pinyougou-dao,pinyougou-sellergoods-api

配置文件:

server:
  port: 9001
eureka:
  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數據源
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    #   數據源其餘配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    #   配置監控統計攔截的filters,去掉後監控界面sql沒法統計,'wall'用於防火牆
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
mybatis:
  # 指定全局配置文件位置
  #config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath*:/mapper/*Mapper.xml
  #實體掃描,多個package用逗號或者分號分隔
  type-aliases-package: com.pinyougou.pojo
  configuration:
    #配置返回數據庫(column下劃線命名&&返回java實體是駝峯命名),自動匹配無需as(沒開啓這個,SQL須要寫as: select user_id as userId)
    map-underscore-to-camel-case: true
    #配置JdbcTypeForNull, oracle數據庫必須配置
    jdbc-type-for-null: 'null'

編寫啓動類,添加掃描mapper類的註解,將服務註冊到註冊中心

@MapperScan(value = "com.pinyougou.mapper")
@SpringBootApplication
@EnableEurekaClient
public class SellerGoodsServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(SellerGoodsServiceApplication.class, args);
    }
}

 建立包com.pinyougou.sellergoods.service.impl,做用:實現interface模塊,編寫數據訪問層業務邏輯.注意:該類上要加@Service註解.

建立包com.pinyougou.sellergoods.api.service.impl,做用:實現服務提供者api接口,編寫暴露接口的業務邏輯,注意:該類上要添加@RestController註解.

 

十、運營商管理後臺

建立包pinyougou-manager-web,依賴pinyougou-sellergoods-api

resources下建立配置文件

server:
  port: 9101
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
spring:
  freemarker:
    suffix: .html
    templateLoaderPath: classpath:/templates/
    cache: false #禁用模板緩存,正式環境取消
  application:
    name: managerweb
  main:
    allow-bean-definition-overriding: true

 

建立包com.pinyougou.manager.feign,用於服務的調用

 

@FeignClient("sellergoods") //寫service層的名稱
public interface BrandFeign extends BrandApiService {
}

 

此時,就可使用BrandFeign調用BrandApiService的接口

建立包com.pinyougou.manager.controller,進行測試

 

@RestController
@RequestMapping("/brands")
public class BrandController {
    @Autowired
    private BrandFeign brandFeign;
    @RequestMapping("/findAll")
    public List<TbBrand> findAll(){
        List<TbBrand> lists=null;
        lists=brandFeign.findAll();
        return lists;
    }
}

 

啓動服務:

啓動pinyougou-eurekaserver

啓動pinyougou-sellergoods-service

啓動pinyougou-manager-web

在瀏覽器輸入:http://localhost:9101/brands/findAll,若是能獲取到數據庫數據,說明服務調用成功.

此時,瀏覽器中輸入http://localhost:8761/,能看到剛啓動的兩個服務已經註冊到Eureka註冊中心了.

 

目前的項目框架以下:

 

 本人聯繫方式QQ:1136069753,後期項目會發布共享,項目持續更新

相關文章
相關標籤/搜索