在Debian系统下集成Swagger与Kubernetes可以通过以下步骤实现:
sudo apt update && sudo apt upgrade -y
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo npm install -g swagger-ui
swagger-ui --host localhost:3000
然后,你可以在浏览器中访问 http://localhost:3000
来查看和与Swagger UI交互。
创建Spring Boot项目:
使用Spring Initializr创建一个新的Spring Boot项目,确保包含spring-boot-starter-web
和spring-boot-starter-security
依赖。
添加Swagger依赖:
在pom.xml
文件中添加springfox-boot-starter
依赖。
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
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.EnableSwagger2WebMvc;
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example"))
.paths(PathSelectors.any())
.build();
}
}
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Swagger!";
}
}
./mvnw spring-boot:run
http://localhost:8080/swagger-ui.html
你应该能够看到Swagger UI界面,并且可以看到你创建的API文档。
apiVersion: apps/v1
kind: Deployment
metadata:
name: swagger-ui
spec:
replicas: 1
selector:
matchLabels:
app: swagger-ui
template:
metadata:
labels:
app: swagger-ui
spec:
containers:
- name: swagger-ui
image: swaggerapi/swagger-ui:v4.6.0
ports:
- containerPort: 8080
apiVersion: apps/v1
kind: Deployment
metadata:
name: swagger-editor
spec:
replicas: 1
selector:
matchLabels:
app: swagger-editor
template:
metadata:
labels:
app: swagger-editor
spec:
containers:
- name: swagger-editor
image: swaggerapi/swagger-editor:v4.6.0
ports:
- containerPort: 8080
http://your-k8s-node-ip:8080/swagger-ui/index.html
通过以上步骤,你可以在Debian系统下成功集成Swagger与Spring Boot,并通过Kubernetes进行管理和部署。