架构之旅-多层架构-服务

简述

本文中,将使用spring boot来构建服务,使用swagger2来提供api ui。最终部署到linux下tomcat中。

源代码

iarc-service

环境

IDE: IntelliJ IDEA
JDK: 1.8.0_u162
Framework: Spring Boot, Swagger2
WebServer: Tomcat(本地端口8088)

开发

工程结构

1
2
3
4
5
--root(maven pom父工程)
----iarc-entity (实体类 module)
----iarc-service-api (服务接口 module)
----iarc-service (服务站点-spring boot)
----iarc-data (数据服务 module)

root

pom.xml中值得注意的只有packaging节点,类型为pom

1
<packaging>pom</packaging>

iarc-entity

设置root作为parent,使用lombok来简化Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
<parent>
<groupId>cn.icmyfuture.iarc</groupId>
<artifactId>root</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<dependencies>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

iarc-service

  • pom.xmlpackaging节点类型为war,dependencies/build使用spring boot标准配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    <packaging>war</packaging>
    ...
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    ...
    <!-- springfox -->
    <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>${swagger2.version}</version>
    </dependency>
    <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>${swagger2.version}</version>
    </dependency>
    ...
    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>
  • 端口和JMX唯一性配置
    ./iarc-service/src/main/resources/application.properties中设置参数

    1
    2
    3
    server.port=8001
    endpoints.jmx.unique-names=true
    spring.jmx.default-domain=iarc.data
  • swagger2 configuration class

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    package cn.icmyfuture.iarc.service.config;

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;

    @Configuration
    @EnableSwagger2
    public class Swagger2Config {
    @Bean
    public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
    .apiInfo(apiInfo())
    .select()
    .apis(RequestHandlerSelectors.basePackage("cn.icmyfuture.iarc.service.controller"))
    .paths(PathSelectors.any())
    .build();
    }

    private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
    .title("Spring Boot using Swagger2 for RESTful APIs")
    .description("ivan's swagger2")
    .termsOfServiceUrl("http://localhost:8001")
    .contact("ivan")
    .version("1.0")
    .build();
    }

    }

调试

IntelliJ IDEA中Run ServiceApplication,使用spring boot内置的tomcat容器作为宿主,

  • 输入http://localhost:8001/user/getUser/123来进行验证
  • 输入http://localhost:8001/swagger-ui.html来查看api ui

部署

打包

打开IntelliJ IDEA中Maven Projects窗口。由于使用了maven父工程,打包时需要先执行root相关操作

  • root

    1
    Lifecycle - 依次clean compile install
  • iarc-service

    1
    Lifecyle - 依次clean compile package

部署到linux下tomcat中

  • linux下jdk以及tomcat的安装/自启动可参考这里
  • 将打包好的iarc-service.war复制到 $CATALINA_HOME/webapps目录下,tomcat会自动解压war包,访问http://localhost:8080/iarc-service/user/getUser/123进行验证

参考