04-17.eri-test 网址缩短器pt.II-dockerization和其他功能

Introduction

In my last post I explained what is URL shortening service and how to implement one.

在这篇文章中,我将讨论Swagger文档,码头化应用程序,应用程序缓存和MySql计划的事件。

Swagger UI

每次开发API时,最好以某种方式对其进行记录。 文档使API更易于理解和使用。 使用Swagger UI记录了该项目中的API。

Swagger UI allows anyone to visualize and interact with the API’s resources without having any of the implementation logic in place. 它是自动生成的,带有可视化文档,可轻松实现后端实施和客户端使用.

要在项目中包含Swagger UI,我们需要执行几个步骤。
首先,我们需要将Maven依赖项添加到pom.XML文件文件中:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

供参考,您可以查看完整的pom.xml file here.
添加Maven依赖项之后,该添加Swagger配置了.
Inside the config folder, we need to create a new class - SwaggerConfig.java

@组态
 @ EnableSwagger2
 公共类SwaggerConfig {

 @豆
 public 案卷 apiDocket(){
     返回新的Docket(DocumentationType.SWAGGER_2)
         .apiInfo(metadata())
         。选择()
         .apis(RequestHandlerSelectors.basePackage(“ com.amarin”))
         。建立();
 }

 私有ApiInfo元数据(){
     返回新的ApiInfoBuilder()
     .title(“网址缩短器API”)
     .description(“开发人员的API参考”)
     .version(“ 1.0”)
     。建立();
     }
 }

在类的顶部,我们需要添加几个注释。

@Configuration表示一个类声明了一个或多个@Beans方法,并且可以由Spring容器进行处理,以在运行时为这些bean生成bean定义和服务请求。

@ EnableSwagger2表示应启用Swagger支持。

接下来,我们应该添加Docketbean,它为主要的API配置提供合理的默认值和方便的配置方法。

apiInfo()方法采用ApiInfo对象,我们可以在其中配置所有必需的API信息-否则,它将使用一些默认值。 为了使代码更整洁,我们应该制作一个私有方法,该方法将配置并返回ApiInfo对象,并将该方法作为参数传递给apiInfo()方法。 在这种情况下元数据()方法。

蜜蜂()方法允许我们过滤正在记录的软件包。

现在已配置Swagger UI,我们可以开始记录我们的API. Inside UrlController, above every endpoint we can use @ApiOperation annotation to add description. Depending on your needs you can use some other annotations.

It is also possible to document DTOs and using @ApiModelProperty which allows you to add allowed values, descriptions, etc.

Caching

According to Wikipedia, a cache is a hardware or software component that stores data so that future requests for that data can be served faster; the data stored in a cache might be the result of an earlier computation or a copy of data stored elsewhere.

最常用的缓存类型是内存中缓存将缓存的数据存储在RAM中。 当请求数据并在高速缓存中找到数据时,将从RAM提供数据,而不是从数据库提供数据。 这样,我们避免了在用户请求数据时调用昂贵的后端。

网址缩短器是一种具有比写入请求更多的读取请求的应用程序,这意味着它是使用缓存的理想应用程序。

To enable caching in Spring Boot application we just need to add @EnableCaching annotation in UrlShortenerApiApplication class.

After that, in the controller we need to set @Cachable annotation above GET method. 该注释自动将方法调用的结果存储到缓存中. In @Cachable annotation, we set value parameter which is the name of the cache and key parameter which is the cache key. 在这种情况下,对于缓存键,我们将使用“ shortUrl”,因为我们确定它是唯一的. Sync parameter is set to true to ensure only a single thread is building the cache value.

就是这样-设置了我们的缓存,当我们第一次使用某些短链接加载URL时,结果将保存到缓存中,并且对具有相同短链接的端点的任何其他调用都将从缓存中而不是从缓存中检索结果。 数据库。

Dockerization

Dockerization is the process of packaging application and its dependencies in a Docker container. Once we configure Docker container we can easily run the application on any server or computer that supports Docker.

The first thing we need to do is to create a Dockerfile. A Dockerfile is a text file that contains all the commands a user could call on the command line to assemble an image.

来自openjdk:13-jdk-alpine
 复制./target/url-shortener-api-0.0.1-SNAPSHOT.jar /usr/src/app/url-shortener-api-0.0.1-SNAPSHOT.jar
 展览8080
 ENTRYPOINT [“ java”,“-jar”,“ / usr / src / app / url-shortener-api-0.0.1-SNAPSHOT.jar”]

FROM - This is where we set base image for build base. 我们将使用OpenJDK v13,它是Java的免费开源版本. You can find other images for your base image at Docker hub which is a place for sharing images.
COPY - This command copies files from the local filesystem (your computer) to the filesystem of the container at the path we specified. 因此,我们要将JAR文件从目标文件夹复制到容器中的/ usr / src / app文件夹中. 稍后我将解释创建JAR文件.
EXPOSE - Instruction that informs Docker that the container listens on the specified network ports at runtime. 默认协议为TCP,您可以指定是否要使用UDP.
ENTRYPOINT - This instruction allows you to configure a container that will run as an executable. 在这里,我们需要指定Docker将如何运行应用程序. 从中运行应用程序的命令 .jar文件是

java -jar <app_name>.jar

所以我们将这3个单词放在一个数组中就是这样。

现在,当我们有了Dockerfile时,我们应该从中构建映像. 但是就像我之前提到的,我们首先需要创建 .我们项目中的jar文件,因此Dockerfile中的COPY命令可以正常工作. 创建可执行文件 .jar we are going to use maven. We need to make sure we have maven inside our pom.xml. If maven is missing, we can add it

<build>    
    <plugins>    
        <plugin>    
            <groupId>org.springframework.boot</groupId>    
            <artifactId>spring-boot-maven-plugin</artifactId>    
        </plugin>    
    </plugins>    
</build>

之后,我们应该运行命令

mvn清洁包装

完成后,我们可以构建docker映像。 我们需要确保我们在Dockerfile所在的文件夹中,以便我们可以运行此命令

docker build -t url-shortener:latest。

-t用于标记图像,因此在我们的示例中,这意味着存储库的名称将是url-shortener,而标记将是最新的。 标记用于图像的版本控制。 完成该命令后,我们可以确保使用该命令创建了一个映像

码头工人图像

那会给我们这样的东西

好吧,什么? 让我们快速重复所有步骤,然后再执行最后一步:

  1. 我们的项目已经完成,我们可以确定它正在构建并正常运行
  2. 使用将创建新容器并用完.jar文件的命令创建新的Dockerfile
  3. 确保我们已经安装了Maven
  4. 使用Maven将项目打包到.jar文件中
  5. 从Dockerfile构建Docker映像

对于最后一步,我们应该构建图像。 我说图像是因为我们还将在docker容器中运行MySql服务器。 数据库容器将与应用程序容器隔离。 要在Docker容器中运行MySql服务器,只需运行

$ docker run  - 名称 shorter -e MYSQL_ROOT_PASSWORD = my-secret-pw -d -p 3306:3306 mysql:8

You can see documentation on Docker hub.

当我们在容器中运行数据库时,我们需要配置您的应用程序以连接到该MySql服务器. Inside application.properties set spring.数据源.网址以连接到“缩短器”容器.

由于我们在项目中进行了一些更改,因此需要从上面重复步骤4和5。

现在,当我们拥有docker映像时,我们应该运行容器。 我们用命令做到这一点

码头工人运行-d --name url-shortener-api -p 8080:8080  - 链接shorter url-shortener

-d表示Docker容器在终端的后台运行。
--name让您设置容器的名称
-p:-这只是将本地计算机上的端口映射到容器内的端口。 在这种情况下,我们将端口8080暴露在一个容器内,并决定将其映射到我们的本地端口8080
--link通过这种方式,我们将应用程序容器与数据库容器链接在一起,以允许容器彼此发现并将安全地从一个容器传输到另一容器的信息。 重要的是要知道此标志现在遗产并将在不久的将来将其删除。 除了链接之外,我们还需要创建一个网络来促进两个容器之间的通信。
url-shortener-是我们要运行的Docker映像的名称。

And with this, we are done - in browser visit http://localhost:8080/swagger-ui.html

现在,您可以将映像发布到例如Docker集线器,并轻松在任何计算机和服务器上运行应用程序。

为了改善我们的Docker体验,我还想谈两件事。 一种是多阶段构建,另一种是docker-compose。

Multi-stage build

With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. 您可以有选择地将工件从一个阶段复制到另一个阶段,从而在最终图像中留下不需要的一切.

多阶段构建对避免每次更改代码时手动创建.jar文件都是有好处的。 通过多阶段构建,我们可以定义构建的一个阶段,该阶段将执行maven package命令,而另一阶段会将结果从第一个构建复制到Docker容器的文件系统中。

You can see complete Dockerfile here.

Docker组成

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. 然后,使用一个命令,即可从配置中创建并启动所有服务.

使用docker-compose,我们会将应用程序和数据库打包到单个配置文件然后立即运行所有内容。 这样,我们避免运行MySql容器,然后每次都将其链接到应用程序容器。

Docker-compose.yml is pretty much self-explanatory - first we configure MySql container by setting image mysql v8.0和MySql服务器的凭据. 之后,我们通过设置构建参数来配置应用程序容器,因为我们需要构建映像而不是像使用MySql那样拉出映像. 另外,我们需要设置应用程序容器取决于MySql容器.

现在,我们只需一个命令即可运行整个项目:
码头工人组成

MySql scheduled event

这部分是可选的但我认为总有人会觉得有用。 因此,在上一篇文章中,我谈到了短链接的失效日期,该失效日期可以是用户定义的或一些默认值。 对于此问题,我们可以在数据库中设置一个计划的事件。 此事件每隔x分钟运行一次,并将从数据库中删除到期日期低于当前时间的任何行。 就那么简单。 这对数据库中的少量数据效果很好。

现在,我需要警告您有关此解决方案的几个问题。

第一-此事件将从数据库中删除记录,但不会从缓存中删除数据。 就像我们之前说过的那样,如果缓存可以在数据库中找到匹配的数据,它将不会在数据库中查找。 因此,即使由于删除数据而导致数据库中不再存在数据,我们仍然可以从缓存中获取数据。

Second - In my example script I set that event runs every 2 minutes. 如果我们的数据库很大,则可能发生事件未在其调度间隔内完成执行的情况,结果可能是该事件的多个实例同时执行.

Conclusion

在这篇文章中,我为我的网址缩短工具项目添加了一些细节。
Swagger UI,docker容器和计划的事件都是开发现代API的有用且流行的工具。

from: https://dev.to//antemarin/url-shortener-pt-ii-dockerization-and-other-stuff-51pp

发布了0 篇原创文章 · 获赞 0 · 访问量 124

猜你喜欢

转载自blog.csdn.net/cunbang3337/article/details/105583634
今日推荐