Debian下Swagger與Kubernetes如何集成

Debian下Swagger與Kubernetes如何集成

debian操作系統中將Swagger與kubernetes進行整合,可按照如下流程操作:

Debian系統安裝Swagger步驟

  1. 更新軟件包列表并升級已安裝的包
sudo apt update && sudo apt upgrade -y
  1. 安裝Node.JSnpm(若尚未安裝):
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

隨后,你可以在瀏覽器中打開 https://www.php.cn/link/8e5687e2d6ab87e5da2f833f3e8986a4 來使用Swagger UI。

spring Boot項目中啟用Swagger

  1. 生成spring boot工程: 通過Spring Initializr創建一個新工程,并確保引入spring-boot-starter-web和spring-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; <p>@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. 編寫測試用控制器: 創建一個簡單的REST控制器以供測試:
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;</p><p>@RestController @RequestMapping("/api") public class HelloController { @GetMapping("/hello") public String sayHello() { return "Hello, Swagger!"; } }
  1. 運行Spring Boot應用
./mvnw spring-boot:run
  1. 訪問Swagger用戶界面: 打開瀏覽器,輸入以下地址:
<a href="https://www.php.cn/link/3f2624ba9ffc5ebd40c98284e1379e99">https://www.php.cn/link/3f2624ba9ffc5ebd40c98284e1379e99</a>

此時應能看到Swagger UI界面,并瀏覽到所定義的API文檔。

Kubernetes部署Swagger組件

  1. 創建Swagger UI的Deployment
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:</p><ul><li>name: swagger-ui image: swaggerapi/swagger-ui:v4.6.0 ports:<ul><li>containerPort: 8080
  1. 創建Swagger Editor的Deployment
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:</li></ul></li><li>name: swagger-editor image: swaggerapi/swagger-editor:v4.6.0 ports:<ul><li>containerPort: 8080
  1. 訪問Swagger UI頁面: 啟動你的Spring Boot或spring mvc應用后,在瀏覽器中輸入如下URL來查看和調用API接口
<a href="https://www.php.cn/link/49f925ccf67d449d8ca3ad8b5b8fea76">https://www.php.cn/link/49f925ccf67d449d8ca3ad8b5b8fea76</a>

通過上述步驟,即可實現在Debian平臺上將Swagger整合進Spring Boot應用,并借助Kubernetes完成容器化部署。

? 版權聲明
THE END
喜歡就支持一下吧
點贊11 分享