首页 > 代码库 > springboot构建rest服务,打包docker镜像

springboot构建rest服务,打包docker镜像

场景

项目提供rest服务,需要导出rest接口文档,并把服务打包成docker镜像。

过程

1.使用SpringBoot实现rest服务

Maven的pom.xml

<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>

简单的启动类:

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

两个服务接口:

@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中。

Swagger的简单配置如下:

@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的注解

@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接口文档。

技术分享

3.使用docker打包镜像

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

编写Dockerfile:

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接口文档吧!

springboot构建rest服务,打包docker镜像