欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

如何搭建后端框架

程序员文章站 2022-06-27 23:53:13
第一步建立父工程:引入依赖 org.springframework.boot spring-boot-starter-parent 2.2.5.RELEASE

第一步建立父工程:引入依赖

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.2.5.RELEASE</version>
  <!--设定一个空值将始终从仓库中获取,不从本地路径获取-->
  <relativePath/>
</parent>

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
  <spring-cloud.version>Hoxton.SR4</spring-cloud.version>
  <mapper.starter.version>2.0.3</mapper.starter.version>
  <mysql.version>8.0.19</mysql.version>
  <pageHelper.starter.version>1.2.5</pageHelper.starter.version>
  <intell.lates.version>1.0.0-SNAPSHOT</intell.lates.version>
  <fastDFS.client.version>1.26.1-RELEASE</fastDFS.client.version>
</properties>


<dependencyManagement>
  <dependencies>
    <!--spring-Cloud-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <!--版本管理-->
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>${spring-cloud.version}</version>
      <type>pom</type>
      <!--scope定义了类包在项目的使用阶段。项目阶段包括: 编译,运行,测试和发布-->
      <scope>import</scope>
    </dependency>
    <!--通用Mapper启动器-->
    <dependency>
      <groupId>tk.mybatis</groupId>
      <artifactId>mapper-spring-boot-starter</artifactId>
      <version>${mapper.starter.version}</version>
    </dependency>
    <!--分页助手启动器-->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper-spring-boot-starter</artifactId>
      <version>${pageHelper.starter.version}</version>
    </dependency>
    <!--分页助手启动器-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql.version}</version>
    </dependency>
    <!--FastDFS客户端-->
    <dependency>
      <groupId>com.github.tobato</groupId>
      <artifactId>fastdfs-client</artifactId>
      <version>${fastDFS.client.version}</version>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
  </dependency>
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
  </dependency>
</dependencies>
<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>

第二步,建立eureka注册中心:
引入依赖:

<groupId>com.intell.common</groupId>
<artifactId>intell-registry</artifactId>
<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  </dependency>
</dependencies>

配置属性:

server:
  port: 10086
spring:
  application:
    name: intell-registry
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka

启动类:

package com.intell;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
 * @description:
 * @program: intell
 * @author: Charles
 * @created: 2020-08-11 17:10
 **/
@SpringBootApplication
@EnableEurekaServer
public class RegistryApplication {
  public static void main(String[] args) {
    SpringApplication.run(RegistryApplication.class, args);
  }
}

第三步:搭建zuul路由
引入依赖:

<groupId>com.intell.common</groupId>
<artifactId>intell-gateway</artifactId>
<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>
</dependencies>

配置属性:

server:
  port: 10010
spring:
  application:
    name: api-gateway
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka
zuul:
  prefix: /api #添加路由
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 5000 #熔断超时时长: 5000ms
ribbon:
  ConnectTimeout: 1000    # ribbon链接超时时长,1000ms没有建立连接,抛出异常
  ReadTimeout: 3500   #超过两秒没有读取数据,抛出异常
  MaxAutoRetries: 0  #当前服务重试次数
  MaxAutoRetriesNextServer: 0  #切换服务器重试次数,不重试

启动类:

/**
 * @description:
 * @program: intell
 * @author: Charles
 * @created: 2020-08-11 17:30
 **/
@EnableZuulProxy
@SpringCloudApplication
public class GatewayApplication {
  public static void main(String[] args) {
    SpringApplication.run(GatewayApplication.class, args);
  }
}

本文地址:https://blog.csdn.net/lrqforest/article/details/109622608

相关标签: JAVA 电商项目