debian

Debian下Swagger与Kubernetes如何集成

小樊
32
2025-06-13 13:14:26
栏目: 智能运维

在Debian系统下集成Swagger与Kubernetes可以通过以下步骤实现:

在Debian系统上安装Swagger

  1. 更新系统
sudo apt update && sudo apt upgrade -y
  1. 安装Node.js和npm(如果尚未安装):
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs
  1. 安装Swagger UI
sudo npm install -g swagger-ui
  1. 启动Swagger UI
swagger-ui --host localhost:3000

然后,你可以在浏览器中访问 http://localhost:3000 来查看和与Swagger UI交互。

在Spring Boot应用中集成Swagger

  1. 创建Spring Boot项目: 使用Spring Initializr创建一个新的Spring Boot项目,确保包含spring-boot-starter-webspring-boot-starter-security依赖。

  2. 添加Swagger依赖: 在pom.xml文件中添加springfox-boot-starter依赖。

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>
  1. 配置Swagger: 创建一个配置类来配置Swagger。
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();
    }
}
  1. 创建控制器: 创建一个简单的控制器来测试Swagger。
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!";
    }
}
  1. 运行项目: 在终端中运行Spring Boot应用程序。
./mvnw spring-boot:run
  1. 访问Swagger UI: 打开浏览器并访问以下URL:
http://localhost:8080/swagger-ui.html

你应该能够看到Swagger UI界面,并且可以看到你创建的API文档。

在Kubernetes中部署Swagger

  1. 部署Swagger UI
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
  1. 部署Swagger Editor
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
  1. 访问Swagger UI: 启动你的Spring Boot或Spring MVC应用程序后,你可以通过浏览器访问Swagger UI来查看和测试API文档。
http://your-k8s-node-ip:8080/swagger-ui/index.html

通过以上步骤,你可以在Debian系统下成功集成Swagger与Spring Boot,并通过Kubernetes进行管理和部署。

0
看了该问题的人还看了