springboot構建rest服務,打包docker鏡像

場景

項目提供rest服務,須要導出rest接口文檔,並把服務打包成docker鏡像。html

過程

1.使用SpringBoot實現rest服務

Maven的pom.xmljava

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> ​ <groupId>com.hll</groupId> <artifactId>springboot-docker</artifactId> <version>0.1</version> <packaging>jar</packaging> ​ <name>springboot-docker</name> <url>http://maven.apache.org</url> <parent>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-parent</artifactId>   <version>1.5.3.RELEASE</version>   <relativePath/> <!-- lookup parent from repository --> </parent> <properties>   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>   <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>   <java.version>1.8</java.version> </properties> <dependencies>   <dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-web</artifactId>   </dependency>   <dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-test</artifactId>     <scope>test</scope>   </dependency>   <!-- Swagger -->   <dependency>     <groupId>io.springfox</groupId>     <artifactId>springfox-swagger2</artifactId>     <version>2.6.1</version>   </dependency>   <dependency>     <groupId>io.springfox</groupId>     <artifactId>springfox-swagger-ui</artifactId>     <version>2.6.1</version>   </dependency>   <dependency>     <groupId>com.google.guava</groupId>     <artifactId>guava</artifactId>     <version>19.0</version>   </dependency> </dependencies> <build>   <plugins>     <plugin>       <groupId>org.springframework.boot</groupId>       <artifactId>spring-boot-maven-plugin</artifactId>     </plugin>   </plugins> </build> </project>

簡單的啓動類:web

@SpringBootApplication
public class Bootstrap { public static void main(String[] args) {   SpringApplication.run(Bootstrap.class); } }

兩個服務接口:spring

@RestController
@RequestMapping("/api") public class RestApi { ​ @RequestMapping(value = "/person", method = RequestMethod.GET) public Person get(String name, int num) {   return new Person(name, num); } ​ @RequestMapping(value = "/person", method = RequestMethod.POST) public Map<String, Object> post(Person person) {   System.out.println(person);   HashMap<String, Object> map = Maps.newHashMap();   map.put("status", "ok");   map.put("msg", "are you ok");   return map; } }

 

2.使用Swagger導出rest接口文檔

依賴的jar包在pom.xml中。docker

Swagger的簡單配置以下:apache

@Configuration
@EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() {   Predicate<RequestHandler> predicate = input -> true;   return new Docket(DocumentationType.SWAGGER_2)       .apiInfo(apiInfo())       .useDefaultResponseMessages(false)       .select()       .apis(predicate)       .build(); } ​ private ApiInfo apiInfo() {   return new ApiInfoBuilder()       .title("個人docker服務API文檔")//大標題       .version("1.0")//版本       .build(); } ​ }

在SpringMVC的方法上使用Swagger的註解api

@RestController
@RequestMapping("/api") @Api(value = "user", description = "用戶管理", produces = MediaType.APPLICATION_JSON_VALUE) public class RestApi { ​ @RequestMapping(value = "/person", method = RequestMethod.GET) @ApiOperation(value = "獲取用戶接口", notes = "獲取用戶接口詳細描述") public Person get(String name, int num) {   return new Person(name, num); } ​ @RequestMapping(value = "/person", method = RequestMethod.POST) @ApiOperation(value = "建立用戶接口", notes = "建立用戶接口詳細描述") public Map<String, Object> post(Person person) {   System.out.println(person);   HashMap<String, Object> map = Maps.newHashMap();   map.put("status", "ok");   map.put("msg", "are you ok");   return map; } } ​

啓動應用,訪問 http://localhost:8080/swagger-ui.html#/rest-api,出現Swagger生成的rest接口文檔。springboot

3.使用docker打包鏡像

先使用Maven打包,man clean package,生成jar包(springboot-docker-0.1.jar)app

編寫Dockerfile:maven

FROM java:8
​
MAINTAINER huangll99@126.com ​ ADD springboot-docker-0.1.jar /app.jar ​ EXPOSE 8080 ​ ENTRYPOINT ["/usr/bin/java","-jar","app.jar"]

建立一個空目錄,將Dockerfile和springboot-docker-0.1.jar放入其中,構建鏡像

docker build -t huangll/springboot-docker .

docker images 查看鏡像

REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
huangll/springboot-docker   latest              3407557eb443        About an hour ago   662 MB

662MB有點大呀。。。

運行容器:

docker run -d --name springboot -p 8080:8080 huangll/springboot-docker

docker ps 查看容器:

CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS              PORTS                    NAMES
762de16b3afc        huangll/springboot-docker   "/usr/bin/java -ja..."   About an hour ago   Up 30 minutes       0.0.0.0:8080->8080/tcp   springboot

如今在你的筆記本上訪問rest接口文檔吧!

相關文章
相關標籤/搜索