在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進行管理和部署。