04-17.eri-test URL shortener pt.II-dockerization and other functions

Introduction

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

In this article, I will discuss Swagger documentation, docked applications, application caching and MySql scheduled events.

Swagger UI

Every time you develop an API, it is best to document it in some way. The documentation makes the API easier to understand and use. The API in this project was recorded using Swagger UI.

Swagger UI allows anyone to visualize and interact with the API's resources without having any of the implementation logic in place. It is automatically generated with visual documentation to easily implement back-end implementation and client use.

To include Swagger UI in the project, we need to perform several steps.
First, we need to add the Maven dependency to the pom.XML file:

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

For reference, you can view the complete pom.xml file here . After
adding the Maven dependency, it is time to add the Swagger configuration.
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”)
     。建立();
     }
 }

At the top of the class, we need to add a few notes.

@Configuration indicates that a class declares one or more @Beans methods, and can be processed by the Spring container to generate bean definitions and service requests for these beans at runtime.

@ EnableSwagger2 indicates that Swagger support should be enabled.

Next, we should add Docket bean, which provides reasonable default values ​​and convenient configuration methods for the main API configuration.

The apiInfo () method uses an ApiInfo object where we can configure all necessary API information-otherwise, it will use some default values. To make the code more tidy, we should make a private method that will configure and return the ApiInfo object and pass the method as a parameter to the apiInfo () method. In this case, the metadata () method.

The bee () method allows us to filter the software packages that are being recorded.

Now that the Swagger UI is configured, we can start recording our 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.

The most common type of cache is the in- memory cache, which stores cached data in RAM. When data is requested and found in the cache, the data will be provided from RAM, not from the database. In this way, we avoid calling expensive backends when users request data.

A URL shortener is an application that has more read requests than write requests, which means it is an ideal application that uses caching.

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. This annotation automatically stores the result of the method call into the cache. In @Cachable annotation, we set value parameter which is the name of the cache and key parameter which is the cache key. In this case, for the cache key, we will use "shortUrl" because we are sure it is unique. Sync parameter is set to true to ensure only a single thread is building the cache value.

That's it-our cache is set up, when we first use some short links to load the URL, the result will be saved to the cache, and any other calls to endpoints with the same short link will be from the cache instead Retrieve the results in the cache. database.

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. We will use OpenJDK v13, which is a free and open source version of 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. Therefore, we will copy the JAR file from the target folder to the / usr / src / app folder in the container. I will explain the creation of the JAR file later.
EXPOSE -Instruction that informs Docker that the container listens on the specified network ports at runtime. The default protocol is TCP, and you can specify whether to use UDP.
ENTRYPOINT -This instruction allows you to configure a container that will run as an executable. Here we need to specify how Docker will run the application. The command from which to run the application. The jar file is

java -jar <app_name>.jar

So we put these 3 words in an array and that's it.

Now, when we have a Dockerfile, we should build an image from it. But as I mentioned before, we first need to create the jar file in our project, so the COPY command in the Dockerfile works normally. Create an executable .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>

After that, we should run the command

mvn清洁包装

After completion, we can build the docker image. We need to make sure that we are in the same folder as the Dockerfile so that we can run this command

docker build -t url-shortener:latest。

-t is used to tag images, so in our example, this means that the name of the repository will be url-shortener, and the tag will be up to date. Tags are used for image version control. After completing the command, we can ensure that an image was created using the command

码头工人图像

That would give us something like this

Okay, what? Let's quickly repeat all the steps before performing the last step:

  1. Our project has been completed and we can be sure it is being built and running normally
  2. Create a new Dockerfile using the command that will create a new container and use up the .jar file
  3. Make sure we have Maven installed
  4. Use Maven to package the project into a .jar file
  5. Build Docker image from Dockerfile

For the last step, we should construct the image. I said the image is because we will also run the MySql server in the docker container. The database container will be isolated from the application container. To run the MySql server in a Docker container, just run

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

You can see documentation on Docker hub.

When we run the database in the container, we need to configure your application to connect to the MySql server. Inside application.properties set spring. Data source. URL to connect to the "shortener" container.

Since we made some changes in the project, we need to repeat steps 4 and 5 from above.

Now, when we have the docker image, we should run the container. We do this with commands

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

-d indicates that the Docker container is running in the background of the terminal.
--name allows you to set the name of the container
-p: -This just maps the port on the local computer to the port in the container. In this case, we exposed port 8080 in a container and decided to map it to our local port 8080
--link. In this way, we link the application container with the database container to allow the container Information that discovers and transfers securely from one container to another. It is important to know that the current heritage of this symbol will be deleted in the near future. In addition to links, we also need to create a network to facilitate communication between the two containers.
url-shortener -is the name of the Docker image we want to run.

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

Now you can publish the image to a Docker hub, for example, and easily run the application on any computer and server.

In order to improve our Docker experience, I also want to talk about two things. One is multi-stage construction, and the other is 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. You can selectively copy artifacts from one stage to another Stage, thereby leaving everything that is not needed in the final image.

Multi-stage builds are good for avoiding manually creating .jar files each time you change code. With a multi-stage build, we can define a stage of the build that will execute the maven package command, while another stage will copy the results from the first build to the file system of the Docker container.

You can see complete Dockerfile here.

Docker composition

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. Then, with a single command, you can create and start all services from the configuration.

With docker-compose, we package the application and database into a single configuration file and then run everything immediately. In this way, we avoid running the MySql container and then link it to the application container every time.

Docker-compose.yml is pretty much self-explanatory-first we configure MySql container by setting image mysql v8.0 and credentials of MySql server. After that, we configure the application container by setting the build parameters because we need to build the image instead Pull the image like using MySql. In addition, we need to set the application container depends on the MySql container.

Now we can run the entire project with just one command:
Docker composition

MySql scheduled event

This part is optional, but I think someone will find it useful. Therefore, in the last article, I talked about the expiration date of short links, which can be user-defined or some default value. For this problem, we can set up a planned event in the database. This event runs every x minutes and will delete any rows with an expiration date lower than the current time from the database. It's that simple. This works well for small amounts of data in the database.

Now, I need to warn you about a few problems with this solution.

First -This event will delete records from the database, but will not delete data from the cache. As we said before, if the cache can find matching data in the database, it will not look in the database. Therefore, even if the data no longer exists in the database due to the deletion of data, we can still get the data from the cache.

Second -In my example script I set that event runs every 2 minutes. If our database is large, it may happen that the event is not completed within its scheduling interval, and the result may be that multiple instances of the event are executed at the same time.

Conclusion

In this article, I added some details to my URL shortening tool project.
Swagger UI, docker containers and planned events are all useful and popular tools for developing modern APIs.

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

Published 0 original articles · liked 0 · visits 124

Guess you like

Origin blog.csdn.net/cunbang3337/article/details/105583634