springcloud集成knife
本章目的在于将knife集成到springcloud微服务中,方便进行接口测试
本人spring版本:
springcloud:2021.0.8
springboot:2.7.14
1.项目结构
涉及到的核心模块:
mall-common:公共模块,不是一个服务,只是一个模块
mall-product:商品服务
renren-fast:人人开源后台项目
2.引入knife依赖
<knife4j.version>3.0.3</knife4j.version>
<!-- 接口测试 knife4j -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>
3.编写配置文件
package config;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
/**
* knife4j配置信息
* 访问:http://ip:port/doc.html#/home
*/
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class knife4jConfiguration {
@Bean(value = "dockerBean")
public Docket dockerBean() {
//指定使用Swagger2规范
Docket docket=new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
//描述字段支持Markdown语法
.title("~~商城微服务系统~~")
.description("# Knife4j RESTful APIs")
//.termsOfServiceUrl("https://doc.xiaominfo.com/")
.version("1.0")
.build())
//分组名称
.groupName("商城微服务系统")
.select()
//这里指定Controller扫描包路径
.apis(RequestHandlerSelectors.basePackage("com.mall"))
//.apis(RequestHandlerSelectors.basePackage("com.mall.*.controller"))
.paths(PathSelectors.any())
.build();
return docket;
}
}
4.配置文件自动注入
这里我要详细说一下,配置文件要卸载resources/META-INF/文件夹下的spring.factories文件中,路径千万不要错,没有META-INF就新建一个文件夹,spring.factories中的org.springframework.boot.autoconfigure.EnableAutoConfiguration是专门开启自动装配的,会将我们自己写的config/knife4jConfiguration进行自动注入。同理可实现在common模块中进行全局异常处理。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
config/knife4jConfiguration
5.解决Failed to start bean 'documentationPluginsBootstrapper’问题
在resource下新建bootstrap.yml,注意!是bootstrap.yml不是application.yml,如果在application.yml中配置以下代码,还是会出现此问题,而bootstrap.yml可以解决此问题是因为bootstrap.yml载入会在application.yml之前。
spring:
# 解决knife Failed to start bean 'documentationPluginsBootstrapper'问题
mvc:
pathmatch:
matching-strategy: ant_path_matcher
补充:bootsstrap.yml可能需要在common导入spring-cloud-starter-bootstrap依赖。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.1.0</version>
</dependency>
6.配置Api和ApiOperation
随便找个要启动的服务,配置Api和ApiOperation这两个注解,基本就够用了
7.启动
我在mall-product服务中配置了相关信息,就启动mall-product服务作为演示
服务启动后的端口为10001,在网址处输入http://localhost:10001/doc.html就能正常访问。本人用edge浏览器无法访问,换成google就正常访问,原因未知,有知道的兄弟麻烦留言告知一下。
8.renrne-fast无法启动
renrne-fast是一个不依赖common模块的服务,但又配置了swagger。具体不能启动原因不清楚,但只需要注释掉renren-fast中的swagger依赖就可以了。相当于放弃了对renrne-fast的swagger接口测试
9.目前存在的问题
问题:这是一个微服务项目,我们的跨域解决方案都是配置在网关中的,现在直接访问相当于是绕过网关,请求服务,也就是说没有进行跨域解决,能访问成功仅仅是因为没有触发跨域。如果以后项目部署到服务器后,你再访问knife,因为IP地址发生改变就会触发跨域问题,就访问不了了。甚至目前你可以开个热点,两台电脑组个局域网,knife在A电脑上启动,都会发现B电脑无法访问knife。
解决方案:微服务项目的入口统一都是网关,那么我们将原本直接请求服务改为请求网关,再进行网关转发就行了,这个我后续要用再跟新,因为目前就一台电脑,写了也不方便测试。