Author: Liao Ming (welcome to [email protected])

With the growing strength of the Spring Boot ecological community, the Beitchat server team has gradually adopted the Spring Boot framework when developing part of the new system, and has accumulated some experience. Here I would like to share with you.

Advantages of Spring Boot

What are the advantages of using Spring Boot for your project versus using Spring MVC directly? Here we have analyzed the following points:

  • Java Config is used to replace XML file configuration to reuse configuration classes. Avoid having to copy the XML configuration file into the old project every time you build a new Spring MVC build project.
  • The rich Starter POM dependencies provided by the community greatly facilitated the integration of third-party frameworks.
  • Provide embedded Servlet container, support JAR and WAR deployment, bring great convenience to operation and development.
  • Automatically manage dependencies to ease version conflicts.
  • Provides basic application monitoring functions.

In summary, Spring Boot makes it much easier to develop, configure, monitor, and deploy projects than it is to build projects using Spring MVC directly.

The application of Spring Boot in Baychat

Before talking about Spring Boot, it is necessary to talk about the technology stack that is now available on the shell server:

  • The basic framework Spring MVC.
  • For service governance, Dubbo is used.
  • At the database level, Mybatis, Druid, and multiple data sources are configured.
  • Permissions are controlled using Shiro.
  • Distributed configuration, using Disconf.
  • For distributed scheduled tasks, use Elastic-Job.
  • Cat is used for full-link monitoring.
  • For deployment, we use our own distribution system.

When we introduced Spring Boot, we had to support our existing technology stack and be familiar with it quickly for new colleagues. Based on the concept of Spring Boot convention over configuration, and various official and community Starter POM configurations, with the support of colleagues, the integration of all aspects was relatively smooth. In the following paragraphs, I will explain some of the situations in which Bay is using Spring Boot.

Use Dubbo in Spring Boot

Dubbo server uses the Dubbo framework to implement service governance. Dubbo is currently incubated in the Apache incubator and is officially provided with incubator-Dubo-spring-boot-Project, but the official version 0.1.0 was only released in April. Prior to that, We wrote our own spring-boot-starter-Dubbo and met existing requirements.

After the new project uses spring-boot-starter-dubbo, we expose and introduce dubbo service in the form of annotations, abandoning the original XML configuration mode. During development, we do not need to pay attention to whether there is service introduced in dubo.xml. It’s relatively easy to code.

Integration and Elastic – Job – Lite

Early on, Becca used Elastice-Job for distributed Job management and stuck with a lower version without upgrading. Existing projects, elastice-job configurations and jobs are stored in a spring-job. XML file. When integrating Elastice-Job with Spring Boot, the community has an open source Spring-boot-Starr-elastice-Job that uses the latest version of Elastice-Job-Lite. In this context, I prepared two scenarios:

  • Use @importResource ({“classpath:spring-job.xml”}) to work with an older version of the elastice-job task.
  • Upgrade the elastice-Job version to elastice-job-Lite, install the latest elastice-Console, refer to the community Starter, write and configure scheduled tasks using annotations, and remove XML.

After discussion with my colleagues, I adopted the latter solution, upgrading the elastice-job version to elastice-job-Lite, using spring-boot-starter-elastice-job configuration for the new project, and using annotations to declare jobs. At this point, another XML file is removed.

And Disconf integration

Discof is a distributed configuration management tool introduced by Beitchat in the early days. It is also very simple to use. Although the original author has stopped maintenance, there are still enthusiastic netizens who provide disconf-spring-boot-starter. It also greatly simplifies the configuration cost of our new project. New colleagues do not need to copy the XML configuration of the old project Diconf when configuring Discof.

Integration with Mybatis, Druid and multiple data sources

This part of integration is relatively easy. Mybatis and Druid both have Starter POM that provide automatic configuration right out of the box.

The particular thing that needs to be handled is the multi-data source configuration, where previously the system used the company’s uniform configuration class, which was just XML configuration. In order to reuse the code, we have converted it to Java Config mode and plan to use an automated configuration Starter pack in the future as well. Similarly, Shiro was integrated in the form of Java Config and JWT as an interface access token.

Integration with Cat monitoring

In terms of APM monitoring, our company uses Cat. Colleagues in the platform group set up Cat and provided corresponding integration methods. When configuring Bean injection, XML was still used, which was later changed to Java Config.

  • 1. Introduce CAT-related JAR packages;
  • 2. If it is a Web project, it is necessary to inject CatServletFilter to implement Cat configuration of MVC layer;
  • 3. If Dubbo is used in the project, add CatDubboFilter;
  • Add app.properties to meta-INF and declare the project name.
  • Add CatLogbackAppender configuration to logback-spring. XML.

In the above five steps, the first three steps can be combined into one, 2 and 3. You can change the XML configuration into Java Config and generate a spring-boot-starter-cat-monitor package to realize the reuse of configuration classes and simplify configuration steps. The following describes the principle of automatic configuration in Steps 2 and 3:

  • Injection CatServletFilter, mainly using the @ ConditionalOnWebApplication annotations, if the current environment is the web environment, the configuration class will take effect, the core code is as follows:
/** * function: In the Web environment, Integrated cat monitoring function * / @ Configuration @ ConditionalOnWebApplication public class CatWebFilterConfigure {@ Bean public FilterRegistrationBeancatFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new CatServletFilter());
        registration.addUrlPatterns("/ *");
        registration.setDispatcherTypes(DispatcherType.REQUEST,DispatcherType.FORWARD);
        registration.setName("cat-filter");
        returnregistration; }}Copy the code
  • Inject CatDubboFilter, mainly using Dubbo’s @activate to Activate the configuration unconditionally. Because all of our projects integrate Dubbo, there is no need to inject configuration classes depending on the environment.
/** * function: Dubbo CAT monitoring, */ @activate (group = {Constants.PROVIDER, Constants.CONSUMER}) public class CatBootDubboConfigure extends Filter {Copy the code

Use spring-boot-start-web-log to uniformly intercept request and response logs

As mentioned above, many Starter are using the existing wheels in the community. Of course, there are also some simple Starter packages in our group to realize code reuse and simplify configuration.

We often need to look at the interface request and response logs during the debugging phase of our daily interface development. There are several ways to use Spring to intercept interface request and response logs. You can define your own Filter, Intercepter, or Aspectj. It’s not a good idea to write an implementation for every Web project, or to copy it from another project. In accordance with the principle of Spring Boot convention over configuration, we internally encapsulated spring-boot-start-web-log and provided some common configuration items. When a new Web project needs it, just import the dependency and add the following configuration to the YML configuration file:

web.log:
    ## Intercept path
    mapping-path: "/ *"
    ## Exclude paths
    exclude-mapping-path: "/files/*; /favicon.ico; /login; /captcha/* ## print the header as '; 'separate print-header:"Authorization"## Whether to allow log printing, the default is true, it is recommended to generate disable enable: trueCopy the code

The log effect is as follows:

Deploy the Spring Boot application on an existing distribution system

When we first started deploying the Spring Boot application on our existing distribution system, we encountered some problems.

In terms of deploying the application, we tried both WAR package deployment and JAR deployment. When deploying war package, it was found that there was a conflict with the existing Resin, and part of the JAR of Resion needed to be removed, which was quite troublesome, so we adopted jar package deployment.

We also encountered some problems with JAR deployment. Our existing Dubbo application deployment uses JAR package deployment. Removing some JVM parameters is configurable and ends up running the application using commands to the java-cp JAR package path class name. Jar: java-jar spring-boot-app.jar: java-jar Spring-boot-app. jar: java-jar Spring-boot-app. jar: java-jar Spring-boot-app. jar: java-jar Spring-boot-app. jar: java-jar Spring-boot-app. jar: java-jar Spring-boot-app. jar: java-jar Spring-boot-app. jar: java-jar Spring-boot-app. jar

Dubbo into jars can run independently, using the maven – shade – plugin plug-in, specify the name of the class is com. Alibaba. Dubbo. Container. The Main. We use the spring-boot-Maven-plugin to package the Spring Boot application. When selecting the parameter class name required by the Java -cp command, we used the default Spring Boot Application to start the class at first. After all, when we developed, we also directly ran the main method of the class to start the Application. However, when the application is actually run, it is found that the application does not get up at all, indicating that the main class cannot be found or loaded. After some information search and study, the problem was solved.

The key is to understand how the JAR works, the manifest.mf file. Unzip the jar file from Spring Boot. The manifest.mf file is as follows:

Manifest-version: 1.0 implementation-title: Demo implementation-version: 0.0.1-SNAPSHOT archiver-version: Plexus Archiver Built-By: Ming Implementation-Vendor-Id: com.beiliao.app Spring-Boot-Version: RELEASE implementation-Vendor: Pivotal Software, Inc. Main-class: org.springframework.boot.loader.JarLauncher Start-Class: com.beiliao.app.DemoApplication Spring-Boot-Classes: Maven 3.3.1 build-JDK: 1.8.0_73 implementation-url: Maven 3.3.1 build-JDK: 1.8.0_73 implementation-url: Maven 3.3.1 build-JDK: 1.8.0_73 implementation-url: http://projects.spring.io/spring-boot/demo/Copy the code

Notice the main-class: . Org. Springframework. The boot loader. JarLauncher is the main method, using Java jar – spring – the boot – app. Jar can run spring boot application, Because the spring-boot-Maven-plugin plugin generated this file when packaging, the java-jar command will read this file. Therefore, when using the Java -cp command to start the Spring Boot application, the complete command should be:

windows: java -cp .; C: \ \ Project \ \ boot - demo \ \ target \ \ demo - 0.0.1 - the SNAPSHOT. Jar org. Springframework.. The boot loader. JarLauncher Linux: java -cp .:/data/project/demo/* org.springframework.boot.loader.JarLauncherCopy the code

conclusion

These are some applications of Spring Boot in Baychat. At present, Spring Boot has been used in several new projects of Baychat, which has received a good response in the group. Many of the developers who have used it say that it is much more convenient than using Spring MVC directly before. The company also has six Starter dependencies for its Gitlab repository, with more to come.