• 微服务下整合knife4j接口文档


    前言:本文旨在解决微服务下通过网关访问所用服务的knife4j文档,无需再通过其他服务单独访问

    功能模块配置:

    1.配置类:

    在这个文件中注意下basePackage的扫描路径,修改为对应controller下的路径。

    1. @Configuration
    2. @EnableSwagger2WebMvc
    3. public class SwaggerConfiguration {
    4. @Bean
    5. public Docket getDocket() {
    6. return new Docket(DocumentationType.SWAGGER_2)
    7. .apiInfo(getApiInfoBuilder())
    8. .select()
    9. .apis(RequestHandlerSelectors.basePackage("com.example.micorder.controller"))
    10. .paths(PathSelectors.any())
    11. .build();
    12. }
    13. private ApiInfo getApiInfoBuilder() {
    14. return new ApiInfoBuilder()
    15. .title("Knife4j-mic-order文档")
    16. .description("
      swagger-bootstrap-ui-demo RESTful APIs
      "
      )
    17. .termsOfServiceUrl("服务url")
    18. .contact(new Contact("xxx", "url", "xxx@qq.com"))
    19. .version("1.0")
    20. .build();
    21. }
    22. }
    2.pom.xml
    1. "1.0" encoding="UTF-8"?>
    2. "http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. 4.0.0
    5. com.example
    6. microservices
    7. 0.0.1-SNAPSHOT
    8. mic-elasticsearch
    9. 0.0.1-SNAPSHOT
    10. mic-elasticsearch
    11. Demo project for Spring Boot
    12. org.springframework.boot
    13. spring-boot-starter-web
    14. org.springframework.boot
    15. spring-boot-starter-test
    16. test
    17. com.github.xiaoymin
    18. knife4j-micro-spring-boot-starter
    19. ${knife4j.version}
    20. com.alibaba.cloud
    21. spring-cloud-starter-alibaba-nacos-discovery
    22. org.springframework.boot
    23. spring-boot-maven-plugin

    3.配置

    1. # https://doc.xiaominfo.com/knife4j
    2. knife4j:
    3. # 开启增强配置
    4. enable: true
    5. # 是否开启生产环境屏蔽 true:关闭swagger,false:开启swagger
    6. production: false
    7. basic:
    8. # 是否开启认证
    9. enable: false
    10. # Basic认证用户名
    11. username: admin
    12. # Basic认证密码
    13. password: 123456
    14. spring:
    15. application:
    16. name: test-knife4j

     

    网关模块

    1.配置类

    SwaggerResourceConfig

    1. @Slf4j
    2. @Component
    3. @Primary
    4. @AllArgsConstructor
    5. public class SwaggerResourceConfig implements SwaggerResourcesProvider {
    6. private final RouteLocator routeLocator;
    7. private final GatewayProperties gatewayProperties;
    8. @Override
    9. public List get() {
    10. List resources = new ArrayList<>();
    11. List routes = new ArrayList<>();
    12. routeLocator.getRoutes().subscribe(route -> routes.add(route.getId()));
    13. gatewayProperties.getRoutes().stream().filter(routeDefinition -> routes.contains(routeDefinition.getId())).forEach(route -> {
    14. route.getPredicates().stream()
    15. .filter(predicateDefinition -> ("Path").equalsIgnoreCase(predicateDefinition.getName()))
    16. .forEach(predicateDefinition -> resources.add(swaggerResource(route.getId(),
    17. predicateDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0")
    18. .replace("**", "v2/api-docs"))));
    19. });
    20. return resources;
    21. }
    22. private SwaggerResource swaggerResource(String name, String location) {
    23. log.info("name:{},location:{}", name, location);
    24. SwaggerResource swaggerResource = new SwaggerResource();
    25. swaggerResource.setName(name);
    26. swaggerResource.setLocation(location);
    27. swaggerResource.setSwaggerVersion("2.0");
    28. return swaggerResource;
    29. }
    30. }

    SwaggerHandler

    1. @RestController
    2. public class SwaggerHandler {
    3. @Autowired(required = false)
    4. private SecurityConfiguration securityConfiguration;
    5. @Autowired(required = false)
    6. private UiConfiguration uiConfiguration;
    7. private final SwaggerResourcesProvider swaggerResources;
    8. @Autowired
    9. public SwaggerHandler(SwaggerResourcesProvider swaggerResources) {
    10. this.swaggerResources = swaggerResources;
    11. }
    12. @GetMapping("/swagger-resources/configuration/security")
    13. public Mono> securityConfiguration() {
    14. return Mono.just(new ResponseEntity<>(
    15. Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()), HttpStatus.OK));
    16. }
    17. @GetMapping("/swagger-resources/configuration/ui")
    18. public Mono> uiConfiguration() {
    19. return Mono.just(new ResponseEntity<>(
    20. Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK));
    21. }
    22. @GetMapping("/swagger-resources")
    23. public Mono swaggerResources() {
    24. return Mono.just((new ResponseEntity<>(swaggerResources.get(), HttpStatus.OK)));
    25. }
    26. }

    网关模块配置文件如下

    1. server:
    2. port: 5000
    3. spring:
    4. application:
    5. name: gateway
    6. cloud:
    7. nacos:
    8. discovery:
    9. server-addr: 127.0.0.1:8848
    10. group: microservices
    11. gateway:
    12. routes:
    13. - id: elasticsearch
    14. uri: lb://elasticsearch
    15. predicates:
    16. - Path=/elasticsearch/**
    17. filters:
    18. - StripPrefix=1

    pom

    1. "1.0" encoding="UTF-8"?>
    2. "http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. 4.0.0
    5. com.example
    6. microservices
    7. 0.0.1-SNAPSHOT
    8. mic-gateway
    9. 0.0.1-SNAPSHOT
    10. mic-gateway
    11. 网关模块
    12. com.alibaba.cloud
    13. spring-cloud-starter-alibaba-nacos-discovery
    14. org.springframework.cloud
    15. spring-cloud-starter-gateway
    16. org.springframework.cloud
    17. spring-cloud-starter-loadbalancer
    18. org.springframework.boot
    19. spring-boot-starter-test
    20. test
    21. org.projectlombok
    22. lombok
    23. 1.18.22
    24. provided
    25. com.github.xiaoymin
    26. knife4j-spring-boot-starter
    27. 3.0.3
    28. org.springframework.boot
    29. spring-boot-maven-plugin

    nacos中模块是否在同一分组下以及是否注册。

    访问下边的路径即可 

     http://localhost:5000/doc.html

  • 相关阅读:
    HIVE自定义UDTF函数
    hadoop单机版安装
    【Axure高保真原型】树切换动态面板案例
    连接无限:5G技术构建智慧互联新生态
    Qt/自定义控件的封装
    spring声明式事务(@Transactional)开发常犯的几个错误及解决办法
    重建与发展:数字资产借贷行业朝着可持续发展迈进!
    window.eventBus 在Vue中的使用方法(一)
    泛型——常考面试题
    不适合的学习方法
  • 原文地址:https://blog.csdn.net/weixin_59244784/article/details/134484694