京东秒杀之项目搭建

shop-parent [pom] (商品父模块)

1 创建maven项目

京东秒杀之项目搭建_第1张图片

2 配置pom.xml


<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.0modelVersion>

    <groupId>com.yy.shopgroupId>
    <artifactId>shop-parentartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>pompackaging>
    <modules>
        <module>eureka-servermodule>
        <module>config-servermodule>
        <module>zuul-servermodule>
    modules>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>
    <dependencyManagement>
        <dependencies>

            <dependency>
                <groupId>com.google.code.gsongroupId>
                <artifactId>gsonartifactId>
                <version>2.8.5version>
            dependency>

            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-parentartifactId>
                <version>2.1.4.RELEASEversion>
            dependency>


            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Greenwich.SR1version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

project>

3 删除src文件夹

京东秒杀之项目搭建_第2张图片

eureka-server [jar] (注册中心)

1 创建maven模块

京东秒杀之项目搭建_第3张图片

2 配置pom.xml


<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.0modelVersion>
    <parent>
        <groupId>com.yy.shopgroupId>
        <artifactId>shop-parentartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>eureka-serverartifactId>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
        dependency>
    dependencies>

project>

3 创建启动类

创建com.yy.shop文件夹
京东秒杀之项目搭建_第4张图片
在该文件夹下创建EurekaServerApp启动类
京东秒杀之项目搭建_第5张图片

@SpringBootApplication
@EnableEurekaClient
public class EurekaServerApp {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApp.class, args);
    }

}

在resources文件夹下创建application.yml文件
京东秒杀之项目搭建_第6张图片

server:

  port: 8761 #设置Eureka服务器的端口号为8761
eureka:
  instance:

    hostname: localhost #指定Eureka实例的主机名为localhost
  client:
    fetch-registry: false #禁用Eureka客户端从服务注册表中获取信息
    register-with-eureka: false # 禁止Eureka客户端向Eureka服务器注册自身
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka # 设置Eureka客户端连接到Eureka服务器的默认URL

启动启动类进行测试
京东秒杀之项目搭建_第7张图片
启动成功
京东秒杀之项目搭建_第8张图片

config-server [jar] (配置中心)

1 创建maven模块

京东秒杀之项目搭建_第9张图片

2 配置pom.xml


<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.0modelVersion>
    <parent>
        <groupId>com.yy.shopgroupId>
        <artifactId>shop-parentartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>config-serverartifactId>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-config-serverartifactId>
        dependency>
    dependencies>
project>

3 创建启动类

创建com.yy.shop文件夹
京东秒杀之项目搭建_第10张图片
在该文件夹下创建ConfigServerApp启动类
京东秒杀之项目搭建_第11张图片

@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer //启用配置中心
public class ConfigServerApp {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApp.class, args);
    }
}

在resources文件夹下创建application.yml文件
京东秒杀之项目搭建_第12张图片

server:
  port: 9100
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/shanyue_jianguang/microservices.git #此处放置自己的gitee仓库
          username: [email protected]
          password: xxxxxxx 
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka

在自己的gitee仓库下提交zuul-server.yml

京东秒杀之项目搭建_第13张图片
编辑zuul-server.yml

server:
  port: 9000

ribbon:
  ReadTimeout: 6000000
  ConnectTimeout: 6000000

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 6000000
zuul:
  host:
    connect-timeout-millis: 600000
    socket-timeout-millis: 6000000
  force-original-query-string-encoding: true
  ignored-patterns: /*-server/**
  routes:
    member-server-route:
      path: /member/**
      service-id: member-server
      sensitive-headers:
      custom-sensitive-headers: true
      ribbon:
        MaxAutoRetriesNextServer: 0
        OkToRetryOnAllOperations: false
        ConnectTimeout: 10000
        ReadTimeout: 10000
    good-server-route:
      path: /good/**
      service-id: good-server
      sensitive-headers:
      custom-sensitive-headers: true
      ribbon:
        MaxAutoRetriesNextServer: 0
        OkToRetryOnAllOperations: false
        ConnectTimeout: 10000
        ReadTimeout: 10000
    seckill-server-route:
      path: /seckill/**
      service-id: seckill-server
      sensitive-headers:
      custom-sensitive-headers: true
      ribbon:
        MaxAutoRetriesNextServer: 0
        OkToRetryOnAllOperations: false
        ConnectTimeout: 10000
        ReadTimeout: 10000

zuul-server [jar] (网关)

1 创建maven模块

京东秒杀之项目搭建_第14张图片

2 配置pom.xml


<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.0modelVersion>
    <parent>
        <groupId>com.yy.shopgroupId>
        <artifactId>shop-parentartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>zuul-serverartifactId>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>


    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-zuulartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-config-clientartifactId>
        dependency>
    dependencies>
project>

3 创建启动类

创建com.yy.shop文件夹
京东秒杀之项目搭建_第15张图片
在该文件夹下创建ZuulServerApp启动类

@SpringBootApplication
@EnableZuulProxy //启用网关
public class ZuulServerApp {

    public static void main(String[] args) {
        SpringApplication.run(ZuulServerApp.class,args);
    }
}

在resources文件夹下创建bootstrap.yml文件
京东秒杀之项目搭建_第16张图片

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka #配置 Eureka 注册中心
spring:
  application:
    name: zuul-server #设置应用程序的名称为 "zuul-server"。这个名称将在 Eureka 中注册和发现服务时使用
  cloud:
    config:
      name: zuul-server #指定要获取的配置文件的名称
      label: master #确定要获取的配置文件的版本,当前文件在master分支
      discovery:
        service-id: config-server #指定配置服务的服务ID
        enabled: true #启用配置中心的服务发现功能

4 测试网关

打开eureka-server启动类
打开config-server启动类
打开zuul-server启动类
京东秒杀之项目搭建_第17张图片
网关成功通过config来访问到gitee中zuul-server.yml的9000端口
若显示8080,则说明没有访问到

shop-common [jar]

1 创建maven模块

2 配置pom.xml


<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.0modelVersion>
    <parent>
        <groupId>com.yy.shopgroupId>
        <artifactId>shop-parentartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>shop-commonartifactId>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>
project>

shop-provider-api [pom]

1 创建maven模块

2 配置pom.xml


<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.0modelVersion>
    <parent>
        <groupId>com.yy.shopgroupId>
        <artifactId>shop-parentartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>shop-provider-apiartifactId>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>
    <dependencies>
        <dependency>
            <groupId>com.yy.shopgroupId>
            <artifactId>shop-commonartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>

    dependencies>
project>

3 删除src文件夹

member-api [jar]

1 创建maven模块

2 配置pom.xml


<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.0modelVersion>
    <parent>
        <groupId>com.yy.shopgroupId>
        <artifactId>shop-provider-apiartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>member-apiartifactId>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

project>

good-api [jar]

1 创建maven模块

2 配置pom.xml


<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.0modelVersion>
    <parent>
        <groupId>com.yy.shopgroupId>
        <artifactId>shop-provider-apiartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>good-apiartifactId>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

project>

seckill-api [jar]

1 创建maven模块

2 配置pom.xml


<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.0modelVersion>
    <parent>
        <groupId>com.yy.shopgroupId>
        <artifactId>shop-provider-apiartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>seckill-apiartifactId>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

project>

shop-provider [pom]

1 创建maven模块

2 配置pom.xml


<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.0modelVersion>
    <parent>
        <groupId>com.yy.shopgroupId>
        <artifactId>shop-parentartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>shop-providerartifactId>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <dependencies>
        <dependency>
            <groupId>com.yy.shopgroupId>
            <artifactId>member-apiartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-config-clientartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
        dependency>

    dependencies>
project>

3 删除src文件夹

member-server [jar]

1 创建maven模块

2 配置pom.xml


<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.0modelVersion>
    <parent>
        <groupId>com.yy.shopgroupId>
        <artifactId>shop-providerartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>member-serverartifactId>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <dependencies>

        

        <dependency>
            <groupId>com.yy.shopgroupId>
            <artifactId>member-apiartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.33version>
        dependency>

        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.2.0version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-validationartifactId>
            <version>2.0.1.RELEASEversion>
        dependency>
        <dependency>
            <groupId>commons-codecgroupId>
            <artifactId>commons-codecartifactId>
            <version>1.10version>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.2.8version>
        dependency>


        
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>2.9.2version>
        dependency>

        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>2.9.2version>
        dependency>


    dependencies>
project>

good-server [jar]

1 创建maven模块

2 配置pom.xml


<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.0modelVersion>
    <parent>
        <groupId>com.yy.shopgroupId>
        <artifactId>shop-providerartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>good-serverartifactId>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <dependencies>
        <dependency>
            <groupId>com.yy.shopgroupId>
            <artifactId>good-apiartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>


        <dependency>
            <groupId>com.yy.shopgroupId>
            <artifactId>myredis-spring-boot-starterartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>

        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.2.0version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-validationartifactId>
        dependency>
        <dependency>
            <groupId>commons-codecgroupId>
            <artifactId>commons-codecartifactId>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.0.9version>
        dependency>


        
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>2.9.2version>
        dependency>

        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>2.9.2version>
        dependency>

    dependencies>

project>

3 创建bootstrap.yml

京东秒杀之项目搭建_第18张图片

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka #配置 Eureka 注册中心
spring:
  application:
    name: good-server #设置应用程序的名称为 "good-server"。这个名称将在 Eureka 中注册和发现服务时使用
  cloud:
    config:
      name: good-server#指定要获取的配置文件的名称
      label: master #确定要获取的配置文件的版本,当前文件在master分支
      discovery:
        service-id: config-server #指定配置服务的服务ID
        enabled: true #启用配置中心的服务发现功能

seckill-server [jar]

1 创建maven模块

2 配置pom.xml


<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.0modelVersion>
    <parent>
        <groupId>com.yy.shopgroupId>
        <artifactId>shop-providerartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>seckill-serverartifactId>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-amqpartifactId>
        dependency>
        <dependency>
            <groupId>com.yy.shopgroupId>
            <artifactId>seckill-apiartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>

        <dependency>
            <groupId>com.yy.shopgroupId>
            <artifactId>good-apiartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>

        <dependency>
            <groupId>com.yy.shopgroupId>
            <artifactId>member-apiartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>

        

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>

        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.2.0version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-validationartifactId>
        dependency>
        <dependency>
            <groupId>commons-codecgroupId>
            <artifactId>commons-codecartifactId>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.0.9version>
        dependency>


        
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>2.9.2version>
        dependency>

        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>2.9.2version>
        dependency>

    dependencies>
project>

3 创建bootstrap.yml

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka #配置 Eureka 注册中心
spring:
  application:
    name: seckill-server #设置应用程序的名称为 "good-server"。这个名称将在 Eureka 中注册和发现服务时使用
  cloud:
    config:
      name: seckill-server#指定要获取的配置文件的名称
      label: master #确定要获取的配置文件的版本,当前文件在master分支
      discovery:
        service-id: config-server #指定配置服务的服务ID
        enabled: true #启用配置中心的服务发现功能

frontend-server [jar]

1 创建maven模块

京东秒杀之项目搭建_第19张图片
京东秒杀之项目搭建_第20张图片

2 添加前端页面

京东秒杀之项目搭建_第21张图片

mybatis-spring-boot-starter [jar] (自定义的redis启动器)

1 创建maven模块

京东秒杀之项目搭建_第22张图片
创建包
京东秒杀之项目搭建_第23张图片

2 配置pom.xml


<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.0modelVersion>
    <parent>
        <groupId>com.yy.shopgroupId>
        <artifactId>shop-parentartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>myredis-spring-boot-starterartifactId>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-configuration-processorartifactId>
            <optional>trueoptional>
        dependency>

        <dependency>
            <groupId>redis.clientsgroupId>
            <artifactId>jedisartifactId>
        dependency>

        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.47version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>
project>

3 创建 KeyPrefix接口 (操作redis到的接口【前缀和过期时间】)

京东秒杀之项目搭建_第24张图片

public interface KeyPrefix {

    /**
     * 前缀 xxxxxxx:用户信息过期时间
     * @return
     */
    public String getPrefix();

    /**
     * 有效期时间
     * @return
     */
    public int getExpireSeconds();
}

4 创建 MyRedisAutoConfiguration(自动装配类)

@Configuration
@EnableConfigurationProperties({RedisProperties.class})
@ConditionalOnProperty(prefix = "redis", value = "host")
public class MyRedisAutoConfiguration {

    @Bean
    public MyRedisTemplate myRedisTemplate(){
        return new MyRedisTemplate();
    }

    @Bean
    public JedisPool jedisPool(RedisProperties redisProperties){
        JedisPoolConfig config = new JedisPoolConfig();
        //设置参数
        //config.setMinIdle(null);

        return new JedisPool(config, redisProperties.getHost(), redisProperties.getPort(), redisProperties.getTimeout(), redisProperties.getPassword());

    }
}

5 创建 MyRedisTemplate(redis操作类)

public class MyRedisTemplate {

    //使用redis连接池
    private static JedisPool jedisPool;

    static {
        JedisPoolConfig config = new JedisPoolConfig();
        //设置信息
        config.setMaxIdle(500);
        config.setMaxTotal(500);
        config.setMinIdle(100);
        //指定本地
        jedisPool = new JedisPool(config, "127.0.0.1", 6379, 10000000);

    }

    /**
     * 将数据以 JSON 格式序列化后存储到 Redis 中
     * @param keyPrefix
     * @param key
     * @param data
     * @param 
     */
    public <T> void set(KeyPrefix keyPrefix, String key, T data){
        //set key value
        Jedis jedis = null;

        try {
            //获取redis
            jedis = jedisPool.getResource();
            //给key加上前缀
            String realkey = keyPrefix.getPrefix() + key;

            if (keyPrefix.getExpireSeconds() > 0 ){
                //没有过期,设置失效时间
                jedis.setex(realkey, keyPrefix.getExpireSeconds(), JSON.toJSONString(data));
            }else {
                jedis.set(realkey, JSON.toJSONString(data));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jedis.close();
        }

    }

    /**
     * 根据键名和前缀拼接出真正的键,然后从 Redis 中获取对应的值
     * @param keyPrefix
     * @param key
     * @param clazz
     * @return
     * @param 
     */
    public <T> T get(KeyPrefix keyPrefix,String key,Class<T> clazz){

        try(Jedis jedis = jedisPool.getResource()){
            //获取一下 key
            String realKey = keyPrefix.getPrefix()+key;
            String s = jedis.get(realKey);
            if(StringUtils.isEmpty(s)){
                return null;
            }
            return JSON.parseObject(s,clazz);

        }catch (Exception ex){
            ex.printStackTrace();
            throw new RuntimeException(ex.getMessage());
        }

    }

    /**
     * 元素是否存在
     * @param keyPrefix
     * @param key
     * @return
     */
    public boolean exists(KeyPrefix keyPrefix,String key){
        try(Jedis jedis = jedisPool.getResource()){
            //获取一下 key
            String realKey = keyPrefix.getPrefix()+key;
            return jedis.exists(realKey);
        }catch (Exception ex){
            ex.printStackTrace();
            throw new RuntimeException(ex.getMessage());
        }
    }

    /**
     * 将数据以 JSON 格式序列化后存储到 Redis 哈希数据类型中
     * @param keyPrefix
     * @param key
     * @param field
     * @param data
     * @param 
     */
    public <T> void hset(KeyPrefix keyPrefix,String key,String field,T data){
        try(Jedis jedis = jedisPool.getResource()){
            //获取一下 key
            String realKey = keyPrefix.getPrefix()+key;
            jedis.hset(realKey,field,JSON.toJSONString(data));
        }catch (Exception ex){
            ex.printStackTrace();
            throw new RuntimeException(ex.getMessage());
        }
    }

    /**
     * 根据键名、字段名和前缀拼接出真正的键,然后从 Redis 哈希数据类型中获取对应的值
     * @param keyPrefix
     * @param key
     * @param field
     * @param clazz
     * @return
     * @param 
     */
    public <T> T hget(KeyPrefix keyPrefix,String key,String field,Class<T> clazz){
        try(Jedis jedis = jedisPool.getResource()){
            //获取一下 key
            String realKey = keyPrefix.getPrefix()+key;

            String obj = jedis.hget(realKey, field);
            if(obj==null){
                return null;
            }

            return JSON.parseObject(obj,clazz);

        }catch (Exception ex){
            ex.printStackTrace();
            throw new RuntimeException(ex.getMessage());
        }
    }

    /**
     * 遍历哈希数据类型中的所有字段和值,并将它们反序列化为指定的数据类型后存储到一个 Map 对象中返回
     * @param keyPrefix
     * @param key
     * @param clazz
     * @return
     * @param 
     */
    public <T> Map<String,T> hgetAll(KeyPrefix keyPrefix, String key, Class<T> clazz){
        try(Jedis jedis = jedisPool.getResource()){
            //获取一下 key
            String realKey = keyPrefix.getPrefix()+key;

            Map<String, String> map = jedis.hgetAll(realKey);
            if(map==null){
                return null;
            }

            Map<String,T>   resultMap = new HashMap<>();

            for(Map.Entry<String,String> entry:map.entrySet()){
                resultMap.put(entry.getKey(),JSON.parseObject(entry.getValue(),clazz));
            }

            return resultMap;

        }catch (Exception ex){
            ex.printStackTrace();
            throw new RuntimeException(ex.getMessage());
        }
    }

    /**
     * 根据键名和前缀拼接出真正的键,对该键对应的值进行增加操作
     * @param keyPrefix
     * @param key
     * @return
     */
    public Long incr(KeyPrefix keyPrefix,String key){
        try(Jedis jedis = jedisPool.getResource()){
            //获取一下 key
            String realKey = keyPrefix.getPrefix()+key;

            return jedis.incr(realKey);

        }catch (Exception ex){
            ex.printStackTrace();
            throw new RuntimeException(ex.getMessage());
        }
    }

    /**
     * 根据键名和前缀拼接出真正的键,对该键对应的值进行减少操作
     * @param keyPrefix
     * @param key
     * @return
     */
    public Long decr(KeyPrefix keyPrefix,String key){
        try(Jedis jedis = jedisPool.getResource()){
            //获取一下 key
            String realKey = keyPrefix.getPrefix()+key;

            return jedis.decr(realKey);

        }catch (Exception ex){
            ex.printStackTrace();
            throw new RuntimeException(ex.getMessage());
        }
    }


    /**
     * 根据键名和前缀拼接出真正的键,设置该键的过期时间为指定的秒数
     * @param keyPrefix
     * @param key
     * @param expireSeconds
     * @return
     */
    public Long expire(KeyPrefix keyPrefix,String key,int expireSeconds) {
        try (Jedis jedis = jedisPool.getResource()) {
            //获取一下 key
            String realKey = keyPrefix.getPrefix() + key;

            return jedis.expire(realKey, expireSeconds);

        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex.getMessage());
        }
    }
}

6 创建 RedisProperties(redis的链接参数类)

@Setter
@Getter
@ConfigurationProperties(prefix = "redis")
public class RedisProperties {

    //主机
    private String host;
    //端口
    private int port;
    private int timeout;
    private String password;
}

7 创建 META-INF文件下的spring.factories文件(定义自动装配类以及实现类)京东秒杀之项目搭建_第25张图片

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.yy.shop.MyRedisAutoConfiguration

你可能感兴趣的:(微服务)