先贴出图给大家看一下微服的构成:
在搭建项目时会遇到spring boot,spring cloud版本不兼容的问题,大家可以参考:https://spring.io/projects/spring-cloud#learn
点击preference doc
关于此处就不多说,直接看代码:
mv-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>cesske.com</groupId>
<artifactId>mv-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>../mv-common</module>
<module>../mv-user</module>
<module>../mv-api-gateway</module>
<module>../mv-tool</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.11</version>
</parent>
<properties>
<java.version>1.8</java.version>
<spring.cloud.version>3.1.0</spring.cloud.version>
</properties>
<!--<dependencyManagement>-->
<!--<dependencies>-->
<!--<dependency>-->
<!--<groupId>org.springframework.cloud</groupId>-->
<!--<artifactId>spring-cloud-dependencies</artifactId>-->
<!--<version>2021.0.4</version>-->
<!--<type>pom</type>-->
<!--<scope>import</scope>-->
<!--</dependency>-->
<!--</dependencies>-->
<!--</dependencyManagement>-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!--redis的起步依赖:直接在项目中使用RedisTemplate(StringRedisTemplate)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- swagger 3-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>${spring.cloud.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>${spring.cloud.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
<version>${spring.cloud.version}</version>
</dependency>
</dependencies>
</project>
mv-user微服依赖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">
<parent>
<artifactId>mv-parent</artifactId>
<groupId>cesske.com</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../mv-parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mv-user</artifactId>
<dependencies>
<dependency>
<groupId>cesske.com</groupId>
<artifactId>mv-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
swagger配置 :Swagger3Config.java
package com.cesske.config;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Response;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
import java.util.List;
@Configuration
@EnableOpenApi //注解启动用Swagger的使用,同时在配置类中对Swagger的通用参数进行配置
public class Swagger3Config implements EnvironmentAware {
private String applicationName;
private String applicationDescription;
@Bean
public Docket createRestApi() {
//返回文档概要信息
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build()
// .globalRequestParameters(getGlobalRequestParameters())
.globalResponses(HttpMethod.GET, getGlobalResponseMessage())
.globalResponses(HttpMethod.POST, getGlobalResponseMessage());
}
/*
生成接口信息,包括标题,联系人等
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(applicationName + "接口文档")
.description(applicationDescription)
.contact(new Contact("Cesske", "http://www.cesske.com", "[email protected]"))
.version("1.0")
.build();
}
/*
封装通用相应信息
*/
private List<Response> getGlobalResponseMessage() {
List<Response> responseList = new ArrayList<>();
responseList.add(new ResponseBuilder().code("404").description("未找到资源").build());
return responseList;
}
@Override
public void setEnvironment(Environment environment) {
this.applicationDescription = environment.getProperty("spring.application.description");
this.applicationName = environment.getProperty("spring.application.name");
}
}
网关mv-api-gateway依赖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">
<parent>
<artifactId>mv-parent</artifactId>
<groupId>cesske.com</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../mv-parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mv-api-gateway</artifactId>
<dependencies>
<dependency>
<groupId>cesske.com</groupId>
<artifactId>mv-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- knife4j-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
<exclusions>
<exclusion>
<artifactId>swagger-annotations</artifactId>
<groupId>io.swagger</groupId>
</exclusion>
<exclusion>
<artifactId>swagger-models</artifactId>
<groupId>io.swagger</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
</dependencies>
</project>
网关配置 application.yml
server:
port: 9090
spring:
application:
name: mv-api-gateway
mvc:
pathmatch:
matching-strategy: ant_path_matcher
cloud:
inetutils:
timeout-seconds: 10
gateway:
enabled: true
discovery:
locator:
lower-case-service-id: true
routes:
- id: mv-user
uri: lb://mv-user
predicates:
- Path=/mv-user/**
filters:
- StripPrefix=1
- id: mv-tool
uri: lb://mv-tool
predicates:
- Path=/mv-tool/**
filters:
- StripPrefix=1
springfox:
documentation:
swagger-ui:
enabled: true # false关闭swagger-ui界面 但不关闭openapi
代码就不一一贴出来的,大家需要直接下载吧!去下载