畅购01——工程搭建

项目介绍

  畅购是B2C架构,用户在线浏览商品,搜索商品,加入购物车从而下单购买,同时支持微信支付,用户还可以参与低价商品秒杀的购物网站。
  本网站采用了微服务架构,采用了SpringCloud技术栈,基于SpringBoot构建,采用SpringCloud GateWay将各个微服务的功能串联起来。

系统设计

技术选型

技术选型

系统架构图

在这里插入图片描述

项目构建

项目结构

项目结构

创建一级父工程

创建父工程 changgou_parent,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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.changgou</groupId>
    <artifactId>changgou_parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>changgou_eureka</module>
        <module>changgou_gateway</module>
        <module>changgou_service</module>
        <module>changgou_service_api</module>
        <module>changgou_web</module>
        <module>changgou_common</module>
        <module>changgou_common_db</module>
		<module>changgou_canal</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <skipTests>true</skipTests>
    </properties>

    <!--依赖包-->
    <dependencies>
        <!--测试包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

二级父工程模块搭建

创建changgou_gateway、changgou_service、changgou_service_api、changgou_web工程,工程全部为pom工程,并将所有工程的src文件删除。

Eureka微服务搭建

  1. pom文件里引入eureka-server依赖
  2. 创建配置文件application.yml
server:
  port: 6868
eureka:
  client:
    register-with-eureka: false #是否将自己注册到eureka中
    fetch-registry: false #是否从eureka中获取信息
    service-url:
      defaultZone: http://127.0.0.1:${server.port}/eureka/
  1. 创建启动类,加上@EnableEurekaServer注解

商品微服务API工程搭建

  1. changgou_service_api 引入依赖
    persistence作用: 实体类与数据库中的表建立映射关系
<dependencies>
   <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-openfeign</artifactId>
       <scope>provided</scope>
   </dependency>
   <dependency>
       <groupId>javax.persistence</groupId>
       <artifactId>persistence-api</artifactId>
       <version>1.0</version>
       <scope>compile</scope>
   </dependency>
</dependencies>
  1. changgou_service_api 下创建changgou_service_goods_api子模块并添加common依赖

微服务工程搭建

  1. changgou_service下创建changgou_service_goods子模块 ,pom.xml引入依赖
<dependencies>
   <dependency>
       <groupId>com.changgou</groupId>
       <artifactId>changgou_common_db</artifactId>
       <version>1.0-SNAPSHOT</version>
   </dependency>
   <dependency>
       <groupId>com.changgou</groupId>
       <artifactId>changgou_service_goods_api</artifactId>
       <version>1.0-SNAPSHOT</version>
   </dependency>
   <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
   </dependency>
</dependencies>
  1. 创建配置文件application.yml
server:
 port: 9011
spring:
 application:
   name: goods
 datasource:
   driver-class-name: com.mysql.jdbc.Driver
   url: jdbc:mysql://192.168.200.128:3306/changgou_goods?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
   username: root
   password: root
eureka:
 client:
   service-url:
     defaultZone: http://127.0.0.1:6868/eureka
 instance:
   prefer-ip-address: true
feign:
 hystrix:
   enabled: true
   
#hystrix 配置
hystrix:
 command:
   default:
     execution:
       timeout:
       #如果enabled设置为false,则请求超时交给ribbon控制
         enabled: true
       isolation:
         strategy: SEMAPHORE
  1. 创建包com.changgou.goods 包下创建启动类
    注意:@MapperScan是tk.mybatis…包下的
@SpringBootApplication
@EnableEurekaClient
@MapperScan(basePackages = {"com.changgou.goods.dao"})
public class GoodsApplication {
   public static void main(String[] args) {
       SpringApplication.run(GoodsApplication.class);
   }
}

异常统一处理

  1. 写一个业务异常类继承RuntimeException
public class BusinessException extends RuntimeException {
    private Result result;


    public BusinessException(Result result) {
        this.result = result;
    }


    public BusinessException(String message, Result result) {
        super(message);
        this.result = result;
    }

    public Result getResult() {
        return result;
    }

    public void setResult(Result result) {
        this.result = result;
    }
}

  1. 定义统一异常处理类
@ControllerAdvice
public class BaseExceptionHandler {

    @ExceptionHandler(value = BusinessException.class)
    @ResponseBody
    public Result ortherError(BusinessException e){
        e.printStackTrace();
        return e.getResult();
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();
        return new Result(false, StatusCode.ERROR, "执行出错");
    }
}

品牌信息的CRUD

  1. 对应的api包下的pojo已经有与数据库对应的注解。
    @Table和@Id都是JPA注解,@Table用于配置表与实体类的映射关系,@Id用于标识主键属性。
  2. 启动类有tk…下@MapperScan注解,指明接口所在的包
  3. dao层:定义接口继承Mapper接口,指定泛型为Brand。
  4. service层:定义接口,实现类中调用mapper层对应的方法。
    掌握…Selective有字段就操作,没有就不动,…ByPrimaryKey根据主键,selectByExample条件查询记录一下:
private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 品牌名称
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
           	}
            // 品牌图片地址
            if(searchMap.get("image")!=null && !"".equals(searchMap.get("image"))){
                criteria.andLike("image","%"+searchMap.get("image")+"%");
           	}
            // 品牌的首字母
            if(searchMap.get("letter")!=null && !"".equals(searchMap.get("letter"))){
                criteria.andLike("letter","%"+searchMap.get("letter")+"%");
           	}
            // 品牌id
            if(searchMap.get("id")!=null ){
                criteria.andEqualTo("id",searchMap.get("id"));
            }
            // 排序
            if(searchMap.get("seq")!=null ){
                criteria.andEqualTo("seq",searchMap.get("seq"));
            }
        }
        return example;
    }
  1. controller层:用的restFul风格,不做详细赘述。
©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页