您好,登錄后才能下訂單哦!
這篇文章主要介紹“knife4j3.0.3怎么整合gateway和注冊中心”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“knife4j3.0.3怎么整合gateway和注冊中心”文章能幫助大家解決問題。
<properties> <java.version>1.8</java.version> <spring-boot.version>2.3.12.RELEASE</spring-boot.version> <spring-cloud.version>Hoxton.SR12</spring-cloud.version> <spring-cloud-alibaba.version>2.2.10-RC1</spring-cloud-alibaba.version> <knife4j.version>3.0.3</knife4j.version> </properties> <dependencyManagement> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-alibaba-dependencies --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.github.xiaoymin/knife4j-dependencies --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-dependencies</artifactId> <version>${knife4j.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
<!--網關--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!--nacos注冊中心--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--swagger美化——knife4j--> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> </dependency>
server: port: 8888 spring: main: web-application-type: reactive application: name: gateway
spring: cloud: nacos: server-addr: localhost:8848 username: nacos password: nacos discovery: enabled: true namespace: 776074db-06ad-4991-bb68-99b41ae971c9 gateway: routes: - id: news uri: lb://news predicates: - Path=/news/** filters: - StripPrefix=1
import lombok.RequiredArgsConstructor; 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.SwaggerResource; import springfox.documentation.swagger.web.SwaggerResourcesProvider; import java.util.List; /** * @author √Angelの愛灬 * @date 2023/3/23 */ @RestController @RequiredArgsConstructor public class SwaggerController { private final SwaggerResourcesProvider swaggerResources; @GetMapping("/swagger-resources") public Mono<ResponseEntity<List<SwaggerResource>>> swaggerResources() { return Mono.just((new ResponseEntity<>(swaggerResources.get(), HttpStatus.OK))); } }
import com.msf.gateway.enums.ServiceEnum; import lombok.RequiredArgsConstructor; import org.springframework.cloud.gateway.config.GatewayProperties; import org.springframework.cloud.gateway.route.RouteDefinition; 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; /** * 獲取網關路由判斷哪些路由需要使用文檔 * * @author √Angelの愛灬 * @date 2023/3/23 */ @Primary @Component @RequiredArgsConstructor public class SwaggerResourceConfig implements SwaggerResourcesProvider { private final String API_URI = "v2/api-docs"; private final GatewayProperties gatewayProperties; @Override public List<SwaggerResource> get() { // 接口資源列表 List<SwaggerResource> resources = new ArrayList<>(); // resources為所有路由都加載到文檔,如果需要部分顯示,在下方使用filter進行過濾即可 for (RouteDefinition route : gatewayProperties.getRoutes()) { route.getPredicates().stream() .filter(predicateDefinition -> ("Path").equalsIgnoreCase(predicateDefinition.getName())) .forEach(predicateDefinition -> resources.add(swaggerResource(ServiceEnum.getServiceName(route.getId()), predicateDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0").replace("**", API_URI)))); } return resources; } private SwaggerResource swaggerResource(String name, String url) { SwaggerResource swaggerResource = new SwaggerResource(); swaggerResource.setName(name); swaggerResource.setLocation(url); swaggerResource.setUrl(url); swaggerResource.setSwaggerVersion("2.0"); return swaggerResource; } }
import lombok.AllArgsConstructor; /** * 服務列表 * * @author √Angelの愛灬 * @date 2023/3/23 */ @AllArgsConstructor public enum ServiceEnum { //新聞模塊 news("新聞模塊"); private final String serviceName; public static String getServiceName(String name) { for (ServiceEnum service : ServiceEnum.values()) { if (name.equals(service.name())) { return service.serviceName; } } return "服務路由字典錯誤!"; } }
此后如果新增了其他子服務,在服務枚舉類中添加對應的模塊即可:枚舉name=spring.cloud.gateway.routes.id,serviceName=服務名稱
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--nacos服務注冊發現--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--swagger美化——knife4j--> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-micro-spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> </exclusion> </exclusions> </dependency>
server: port: 8080 spring: application: name: news
spring: cloud: nacos: server-addr: localhost:8848 username: nacos password: nacos discovery: enabled: true namespace: 776074db-06ad-4991-bb68-99b41ae971c9 knife4j: enable: true
import com.msf.common.constant.Constant; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import springfox.documentation.builders.*; import springfox.documentation.service.*; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; import java.util.List; /** * Swagger配置 * * @author √Angelの愛灬 * @date 2023/3/21 */ @Slf4j @Configuration @EnableSwagger2 public class SwaggerConfig { @Value("${spring.profiles.active:}") private String active; @Bean public Docket docDocket() { boolean enableSwagger = !Constant.SPRING_PROFILES_ACTIVE_PRO.equalsIgnoreCase(active); return new Docket(DocumentationType.SWAGGER_2) // 非生產環境啟用 .enable(enableSwagger) .apiInfo(apiInfo()) .globalResponses(HttpMethod.GET, globalResponse()) .globalResponses(HttpMethod.POST, globalResponse()) .globalResponses(HttpMethod.PUT, globalResponse()) .globalResponses(HttpMethod.DELETE, globalResponse()) .select() .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build() .globalRequestParameters(globalRequestParameters()); } private ApiInfo apiInfo() { String title = "新聞模塊"; String description = "<div style='font-size:14px;color:red;'>新聞模塊knife4j接口文檔</div>"; String version = "1.0.0"; String termsOfServiceUrl = ""; Contact contact = new Contact("√Angelの愛灬", "", ""); return new ApiInfoBuilder() .title(title) .description(description) .version(version) .termsOfServiceUrl(termsOfServiceUrl) .contact(contact) .build(); } private List<Response> globalResponse(){ List<Response> responseList = new ArrayList<>(); responseList.add(new ResponseBuilder().code("401").description("未認證").build()); responseList.add(new ResponseBuilder().code("403").description("請求被禁止").build()); responseList.add(new ResponseBuilder().code("404").description("找不到資源").build()); return responseList; } private List<RequestParameter> globalRequestParameters() { List<RequestParameter> parameters = new ArrayList<>(); RequestParameterBuilder requestParameterBuilder = new RequestParameterBuilder(); RequestParameter username = requestParameterBuilder .name("username").description("用戶賬號").in(ParameterType.HEADER) .build(); RequestParameter token = requestParameterBuilder .name("token").description("token").in(ParameterType.HEADER) .build(); parameters.add(username); parameters.add(token); return parameters; } }
關于“knife4j3.0.3怎么整合gateway和注冊中心”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。