当前位置: 首页> 汽车> 维修 > Spring Cloud Gateway 集成 Nacos、Knife4j

Spring Cloud Gateway 集成 Nacos、Knife4j

时间:2025/7/9 4:37:40来源:https://blog.csdn.net/weixin_44183847/article/details/139199852 浏览次数: 0次

目录

  • 1、gateway网关配置
    • 1.1 pom 配置
    • 2.2 配置文件
    • 1.3 yaml 配置
  • 2、其他服务配置
    • 2.1 pom 配置
    • 2.2 配置文件
    • 2.3 yaml 配置
  • 3、界面访问
  • 4、其他

官方文档地址:Spring Cloud Gateway集成Knife4j
官方完整源码:https://gitee.com/xiaoym/swagger-bootstrap-ui-demo
自己搭建的源码地址:https://gitee.com/sheng-wanping/spring-boot-gateway

其实Knife4j官方文档写的很全,自己也是按照文档搭建的,这里记录一下自己的搭建过程:
由于公司使用的springboot版本较低,是2.1.3.RELEASE,因此其他组件使用了对应较低版本。

  • springboot版本2.1.3.RELEASE
  • nacos 版本2.1.4.RELEASE
  • gateway版本2.1.0.RELEASE
  • knife4j版本2.0.2

springboot 其他版本可以参考:spring-cloud-alibaba 版本对应说明

Spring Cloud Alibaba VersionSpring Cloud VersionSpring Boot Version
2022.0.0.0-RC*Spring Cloud 2022.0.03.0.0
2021.0.4.0*Spring Cloud 2021.0.42.6.11
2021.0.1.0Spring Cloud 2021.0.12.6.3
2021.1Spring Cloud 2020.0.12.4.2
2.2.10-RC1*Spring Cloud Hoxton.SR122.3.12.RELEASE
2.2.9.RELEASESpring Cloud Hoxton.SR122.3.12.RELEASE
2.2.8.RELEASESpring Cloud Hoxton.SR122.3.12.RELEASE
2.2.7.RELEASESpring Cloud Hoxton.SR122.3.12.RELEASE
2.2.6.RELEASESpring Cloud Hoxton.SR92.3.2.RELEASE
2.2.1.RELEASESpring Cloud Hoxton.SR32.2.5.RELEASE
2.2.0.RELEASESpring Cloud Hoxton.RELEASE2.2.X.RELEASE
2.1.4.RELEASESpring Cloud Greenwich.SR62.1.13.RELEASE
2.1.2.RELEASESpring Cloud Greenwich2.1.X.RELEASE
2.0.4.RELEASE(停止维护,建议升级)Spring Cloud Finchley2.0.X.RELEASE
1.5.1.RELEASE(停止维护,建议升级)Spring Cloud Edgware1.5.X.RELEASE

1、gateway网关配置

1.1 pom 配置

<!-- nacos 配置和注册中心 -->
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency><!-- Gateway 网关相关 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId>
</dependency><!-- knife4j -->
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId>
</dependency>

2.2 配置文件

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;import java.util.ArrayList;
import java.util.List;@Slf4j
@Component
@Primary
@AllArgsConstructor
public class SwaggerResourceConfig implements SwaggerResourcesProvider {private final RouteLocator routeLocator;private final GatewayProperties gatewayProperties;@Overridepublic List<SwaggerResource> get() {List<SwaggerResource> resources = new ArrayList<>();List<String> routes = new ArrayList<>();routeLocator.getRoutes().subscribe(route -> routes.add(route.getId()));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("**", "v2/api-docs"))));});return resources;}private SwaggerResource swaggerResource(String name, String location) {log.info("name:{},location:{}",name,location);SwaggerResource swaggerResource = new SwaggerResource();swaggerResource.setName(name);swaggerResource.setLocation(location);swaggerResource.setSwaggerVersion("2.0");return swaggerResource;}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.*;
import java.util.Optional;/*** @author xiaoymin* 2020年10月29日 18:38:01*/
@RestController
public class SwaggerHandler {@Autowired(required = false)private SecurityConfiguration securityConfiguration;@Autowired(required = false)private UiConfiguration uiConfiguration;private final SwaggerResourcesProvider swaggerResources;@Autowiredpublic SwaggerHandler(SwaggerResourcesProvider swaggerResources) {this.swaggerResources = swaggerResources;}@GetMapping("/swagger-resources/configuration/security")public Mono<ResponseEntity<SecurityConfiguration>> securityConfiguration() {return Mono.just(new ResponseEntity<>(Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()), HttpStatus.OK));}@GetMapping("/swagger-resources/configuration/ui")public Mono<ResponseEntity<UiConfiguration>> uiConfiguration() {return Mono.just(new ResponseEntity<>(Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK));}@GetMapping("/swagger-resources")public Mono<ResponseEntity> swaggerResources() {return Mono.just((new ResponseEntity<>(swaggerResources.get(), HttpStatus.OK)));}
}
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;/*** @author fsl* @description: SwaggerHeaderFilter* @date 2019-06-0310:47*/
@Component
public class SwaggerHeaderFilter extends AbstractGatewayFilterFactory {private static final String HEADER_NAME = "X-Forwarded-Prefix";private static final String URI = "/v2/api-docs";@Overridepublic GatewayFilter apply(Object config) {return (exchange, chain) -> {ServerHttpRequest request = exchange.getRequest();String path = request.getURI().getPath();if (!StringUtils.endsWithIgnoreCase(path,URI )) {return chain.filter(exchange);}String basePath = path.substring(0, path.lastIndexOf(URI));ServerHttpRequest newRequest = request.mutate().header(HEADER_NAME, basePath).build();ServerWebExchange newExchange = exchange.mutate().request(newRequest).build();return chain.filter(newExchange);};}
}

1.3 yaml 配置

application.yaml 主要是网关路由和跨域配置

spring:cloud:# Spring Cloud Gateway 配置项,对应 GatewayProperties 类gateway:# 路由配置项,对应 RouteDefinition 数组routes:- id: byzq-wq-api # 路由的编号uri: lb://byzq-wq # 根据服务名找predicates:- Path=/byzq-wq/** # 映射到对应路径filters:- StripPrefix=1 # 删除第一个路径段 /byzq-wq/api -> /api- id: byzq-yc-api # 路由的编号uri: lb://byzq-ycpredicates:- Path=/byzq-yc/**filters:- StripPrefix=1# 全局跨域配置globalcors:cors-configurations: # 跨域配置列表'[/**]': # 匹配所有路径allowed-headers: "*" # 允许的请求头,*表示允许所有allowed-methods: # 允许的请求方法列表- GET- POST- PUT- DELETE- OPTIONSallowed-origins: "*" # 允许的请求来源,*表示允许所有exposed-headers: # 暴露的响应头列表- Authorization- Content-Typeallow-credentials: true # 是否允许凭证传递,true表示允许

bootstrap.yaml 主要是nacos配置

注: nacos服务必须配置到bootstrap.yaml 里面才能被正确加载,因为执行顺序:bootstrap.yaml > ApplicationContext > application.yaml

server:port: 9999# 需要在ApplicationContext创建之前加载配置文件
spring:application:name: byzq-gatewaycloud:nacos:# nacos 服务注册discovery:server-addr: 127.0.0.1:8848namespace: byzq-local # 命名空间# nacos 配置中心config:server-addr: 127.0.0.1:8848namespace: byzq-local # 命名空间group: DEFAULT_GROUP # 使用的 nacos 配置分组,默认为 DEFAULT_GROUPfile-extension: yaml # 使用的 nacos 配置集的 dataId 的文件拓展名,默认为 properties

2、其他服务配置

2.1 pom 配置

<!-- nacos 配置和注册中心 -->
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency><!-- springboot -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency><!-- Swagger 2 -->
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-micro-spring-boot-starter</artifactId>
</dependency>

2.2 配置文件

import org.springframework.beans.factory.annotation.Value;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration
@EnableSwagger2
public class SwaggerConfig {@Value("${swagger.enable}")private Boolean enable;@Beanpublic Docket api() {return new Docket(DocumentationType.SWAGGER_2).enable(enable).apiInfo(apiInfo()).select()//.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).apis(RequestHandlerSelectors.basePackage("org.example.controller")) // 替换为你的controller包路径.paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("巴渝制气-扬尘") // API文档标题.description("巴渝制气-扬尘接口文档") // API文档描述.version("1.0") // API版本//.contact(new Contact("Your Name", "http://www.yourcompany.com/contact", "your.email@example.com")) // 维护者信息//.termsOfServiceUrl("http://www.yourcompany.com/terms") // 服务条款URL.license("Apache 2.0") // 许可证.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html") // 许可证URL.build();}}

2.3 yaml 配置

bootstrap.yaml 配置如下

spring:application:name: byzq-yc# 需要在ApplicationContext创建之前加载配置文件cloud:nacos:# nacos 服务注册discovery:server-addr: 127.0.0.1:8848namespace: byzq-local # 命名空间# nacos 配置中心config:server-addr: 127.0.0.1:8848namespace: byzq-local # 命名空间group: DEFAULT_GROUP # 使用的 nacos 配置分组,默认为 DEFAULT_GROUPfile-extension: yaml # 使用的 nacos 配置集的 dataId 的文件拓展名,默认为 propertiesserver:port: 8082# 关闭swagger设置为false
swagger:enable: true

3、界面访问

启动服务访问 http://localhost:9999/doc.html 应该是能正常访问的。
在这里插入图片描述
如果你需要一套关于gateway+nacos+knife4j的项目可以看看的源码 https://gitee.com/sheng-wanping/spring-boot-gateway 里面还包括了 API网关权限校验、用户接口权限、单点登录,(因为公司要求轻量级因此没有引入spring security 和 jwt)如果觉得有帮到你的话可以帮忙点个 star,感谢!有什么疑问可以评论区留言或者私信。

4、其他

SpringBoot 集成 Nacos

关键字:Spring Cloud Gateway 集成 Nacos、Knife4j

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: