preface

In July 2020, SpringFox 3.0.0 was released, adding support for WebFlux and OpenAPI 3, adapting to Gateway. In order to facilitate the management of microservices, the interface document of the microservices, we are going to explore the integrated management of Spring Cloud Gateway. The whole process has no code, easy to use

1. Environment and tool introduction

Gradle, a breakthrough JVM-based build tool, simplifies the tedious configuration of XML in Maven. Nacos, a dynamic service discovery, configuration, and service management platform developed by Alibaba, is not used by Appllo. The Java MVC Framework integrates swagger-bootstrap-ui (swagger2) with an enhanced solution for generating API documents with Swagger. Spring Cloud version uses HOXTON. Release, SpringBoot version 2.2.1. Release Gradle, Nacos environment to build, relatively simple, do not do it again

2. Microserver

2.1. Write two simple microservices (Example, Cart-Service).

The ports are set to 8081 and 8082, respectively

server: 
  port: 8081

build.gradle:

Plugins {id 'org.springframework.boot' version '2.2.1. Release 'id' IO. Spring. Dependency - Management 'version '1.0.9.RELEASE' id 'Java'} group 'com.asan.cart' version '1.0.0-snapshot' sourceCompatibility = 1.8ext { Set ('springBootVersion', "2.2.1.RELEASE") set('springCloudVersion', "hoxton. RELEASE") set(' alibabaCloudversion ', 'alibabaCloudversion'), "2.2.1. RELEASE")} repositories {mavenLocal () maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'}} configurations { compile.exclude module: 'tomcat-embed-el' compile.exclude module: 'spring-boot-starter-tomcat' } dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' compile (/** microservice API documentation, Don't need to introduce the front-end UI package * / 'com. Making. Xiaoymin: knife4j - micro - spring - the boot - starter: 3.0.2', /** Nacos config */ 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config', /** Nacos Registration and Discovery Center */ 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery', 'org.springframework.boot:spring-boot-starter-web', 'org.springframework.boot:spring-boot-starter-undertow' ) } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springBootVersion}" mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" mavenBom "com.alibaba.cloud:spring-cloud-alibaba-dependencies:${alibabaCloudVersion}" } }

Undertow is the container’s default, Tomcat is the web’s default. If you need to use undertow, add the following to eliminate Tomcat dependencies:

configurations {
    compile.exclude module: 'tomcat-embed-el'
    compile.exclude module: 'spring-boot-starter-tomcat'
}

Add a configuration class for both microservice clients:

package com.asan.example.config; import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration @EnableSwagger2WebMvc public class Swagger2Config { @Bean public Docket createRestApi(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.asan.example.controller")) .paths(PathSelectors.any()) .build(); } private apiInfo apiInfo() {Contact Contact = new Contact(" http://www.asan.com", "http://www.asan.com"); Return new APIInfoBuilder ().title(" API documentation ").description(" API documentation ").contact(contact).version("1.0") .build(); }}

Add startup classesExampleApplication

package com.asan.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ExampleApplication {
    public static void main(String[] args) {
        SpringApplication.run(ExampleApplication.class, args);
    }

}

Then there is a Controller for documentation:

So I’m going to write the Controller whatever you want, if you can see it, and I’m going to paste it here because it’s not so empty

package com.asan.example.controller; import com.alibaba.fastjson.JSON; import com.asan.example.entity.Cat; import com.asan.example.pojo.dto.CatDto; import com.asan.example.pojo.vo.PageResult; import com.asan.example.pojo.vo.ResultVO; import com.asan.example.service.impl.CatServiceImpl; import com.asan.example.util.BeanCopierUtil; import com.asan.example.util.RedisClient; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import tk.mybatis.mapper.entity.Example; @RestController @RequestMapping("/v1/example/cat") @Slf4j public class CatController { @Autowired private CatServiceImpl  catService; @Autowired private RedisClient redisClient; @ApiOperation(" fetch information ") @GetMapping public resultVo <String> getCat(@ApiParam(" primary key ") @RequestHeader(" CID ") Integer CID) { Cat cat = catService.selectById(cid); if (cat ! = null) {/ / test cache redisClient setForTimeMIN (" SOA: TEXMPLE: the CAT: "+ cid. The toString (), JSON. ToJSONString (CAT), 30); } return new ResultVO(cat); } @apiOperation (" new info ") @postMapping public ResultVO<String> addCat(@requestBody CatDto CatDto) {Cat Cat = new Cat(); BeanCopierUtil.copy(catDto, cat); catService.insert(cat); return new ResultVO(Constants.RESULT_SUCCESS); } @APIOperation (" change information ") @PutMapping public resultVo <String> updateCat(@requestBody catDTO catDTO) {Cat Cat = new Cat();  BeanCopierUtil.copy(catDto, cat); catService.updateById(cat); return new ResultVO(Constants.RESULT_SUCCESS); } @apiOperation (" delete information ") @deleteMapping public ResultVO<String> deleteCat(@apiparam (" primary key ") @requestHeader ("cid") Integer cid) { catService.deleteById(cid); return new ResultVO(Constants.RESULT_SUCCESS); }}

I’m gonna copy the project and I’m gonna call it cart-service, settings-gradle and I’m gonna change it to controller and I’m just gonna change the path and the class name CartController

3. Gateway integrated microserver

Start the class like a copy over, rename the GatewayApplication build.gradle file to add dependencies

Compile (/** API documentation, Contains the front-end UI package * / 'com. Making. Xiaoymin: knife4j - spring - the boot - starter: 3.0.2', /** Nacos config */ 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config', /** Nacos Registration and Discovery Center */ 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery', 'javax.mail. Servlet: javax.mail. Servlet - API: 3.1.0', 'org. Springframework. Cloud: spring - the cloud - starter - gateway')

Now add a configuration class

package com.asan.gateway.config; import org.springframework.cloud.gateway.config.GatewayProperties; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.support.NameUtils; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import springfox.documentation.swagger.web.SwaggerResource; import springfox.documentation.swagger.web.SwaggerResourcesProvider; import springfox.documentation.swagger2.annotations.EnableSwagger2WebFlux; import java.util.ArrayList; import java.util.List; /** * @author Jaakko */ @Configuration @Primary @EnableSwagger2WebFlux public class SwaggerResourceConfig implements SwaggerResourcesProvider { public static final String API_URI = "v2/api-docs"; private final RouteLocator routeLocator; private final GatewayProperties gatewayProperties; public SwaggerResourceConfig(RouteLocator routeLocator, GatewayProperties gatewayProperties) { this.routeLocator = routeLocator; this.gatewayProperties = gatewayProperties; } @Override public List<SwaggerResource> get() { List<SwaggerResource> resources = new ArrayList<>(); List<String> routes = new ArrayList<>(); Subscribe (route-> Routes. Add (route.getId())); // Filter out the Path Route predicate-> to create the swaggerResource by splice the Path into api-docs path-> gatewayProperties.getRoutes().stream().filter(routeDefinition -> routes.contains(routeDefinition.getId())).forEach(route  -> route.getPredicates().stream() .filter(predicateDefinition -> ("Path").equalsIgnoreCase(predicateDefinition.getName())) .forEach(predicateDefinition -> resources.add(swaggerResource(route.getId(), predicateDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0") .replace("**", API_URI))))); return resources; } private SwaggerResource swaggerResource(String name, String location) { SwaggerResource swaggerResource = new SwaggerResource(); swaggerResource.setName(name); swaggerResource.setLocation(location); SwaggerResource. SetSwaggerVersion (" 2.0 "); return swaggerResource; }}

Gateway nacos configuration

Server: port: 9200 spring: cloud: gateway: # Routes: -id: URI: lb:// Predicates: - Path=/example/** filters: - StripPrefix=1 - id: cart-service uri: lb://cart-service predicates: -path =/cart-service/** filters: -stripPrefix =1 discovery: locator: # enabled: / /cart-service/** filters: -stripPrefix =1 True # uses a lowercase service name. Default is upper case lower-case-service-id: true

Start the two microservers and then start the Gateway service

Browser access: localhost:9200/doc.html

Here some rely on manually added, not actually verified!! (you may feel pit, but eight nine does not leave ten), the possibility exists compatible problem, should be ok roughly, have a problem to be able to leave a message to discuss!!!

Renderings here do not cut, their own play