JEE application construction based on SpringBoot

        The evolution of the Spring ecosystem never stops. Since SpringBoot appeared in 2013, the way Spring-based applications are built has gradually changed.

        Spring is undoubtedly one of the most successful development platforms in the development of JEE, but this does not stop everyone from complaining. In earlier versions, developers needed to configure a lot of XML - this has improved with the introduction of annotations, plus dealing with complex (version) dependencies, a little bit of error can cause the system to not function properly. SpringBoot solves the above problems, and at the same time brings a new development experience, providing an excellent solution for the RAD field.

        This article will try to integrate some mainstream frameworks, build an application architecture suitable for certain scenarios based on SpringBoot, and provide specific technical selection references.

 

1. Entry-level applications

        It can be thought of as including the front end, the middle end and the back end. The front-end part is rendered using the officially recommended thymeleaf template, and the middle-end part is a SpringBoot-based microservice application. As for the backend, it can be other services or database systems. Here we take mysql as an example. The following will describe in appropriate detail what a light service typically consists of.

        Create a project with maven as the build tool. pom.xml is configured as follows:

        

<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>cn.j1ee</groupId>
	<artifactId>testing</artifactId>
	<version>1.0</version>
	<packaging>jar</packaging>
	<name>testing</name>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.3.RELEASE</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<dependency>
			<groupId>com.zaxxer</groupId>
			<artifactId>HikariCP</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

 

 

The persistence layer component uses jpa instead of Mybatis, and the database connection pool component uses HikariCP. The application needs some necessary configuration, some information as shown in /src/main/resources/application.properties:

 

        

#server
server.port=8081
server.session-timeout=20
server.context-path=/testing
server.tomcat.uri-encoding=UTF-8


#datasource
spring.datasource.url=jdbc:mysql://192.168.124.7:3306/test?useUnicode=true&characterEncoding=utf8
spring.datasource.username=mysqlx
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=100
spring.datasource.max-wait=10000
spring.datasource.min-idle=10
spring.datasource.initial-size=10
spring.datasource.type=com.zaxxer.hikari.HikariDataSource

#jpa
spring.jpa.database = MYSQL
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto=none  
spring.jpa.generate-ddl=true  
spring.jpa.properties.hibernate.show_sql=true

 

 

 

Create a startup class. The directory of this class is required and needs to be placed under the root package. The main method where this class is located is the entry point of the application, handing over the system boot work to SpringBoot.

        

@SpringBootApplication
public class App {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

 

 

Taking a request as an example, first there is an entity bean:

@Entity
@Table(name = "users")
public class User {
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private long id;
	@NotNull
	private String name;
	@NotNull
	private int age;

//…
}

 

 

There is a Data Access Object:

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
	public User findByName(String name);
}

 

 

There is a template /src/main/resources/templates/user.html required for front-end display:

<h3>user info:</h3>
<div>
	name:	<span th:text="${user.name}">not find the user</span><br/>
	age:	<span th:text="${user.age}">no age</span><br/>
</div>

 

 

There is another UserService and UserServiceImpl, as the logic layer, which is skipped here;

Finally, a Controller is needed:

@Controller
public class UserController {

	@Autowired
	UserService userService;

	@RequestMapping("/user/{name}")
	public String getByName(@PathVariable String name, Model model) {
		User user = userService.findByName(name);
		model.addAttribute("user", user);
		return "user";
	}
}

 

 

There is a prototype of such an MVC module. Start the application through mvn spring-boot:run, enter http://localhost:8081/testing/user/du in the browser to access, the following results appear:

 

user info:
name: you
age: 18

 

 

        Boot provides some features, such as an embedded application service container (default tomcat). Maven can package the application into a fat jar, and run it directly with java on the server to start the program.

        Based on the principle of convention over configuration, Boot has done a lot of automatic configuration work. As mentioned earlier, these (default) configurations can be modified as needed or passed in via startup command parameters, the latter method being more useful when deploying in the cloud.

 

        The technical architecture of an entry-level SpringBoot application is as follows:

 

 

 

 

2. HA/Load Balancing

        Some important systems need to support ha, and load balancing needs to be done in the case of a certain concurrency scale, which does not seem to have much to do with SpringBoot. This is not actually the case. Boot supports out-of-the-box features that make deployment easier than ever. Due to Boot's prioritization capability for configuration support, it is allowed to run by passing in parameters outside the application, further improving its usability.

        For large-scale systems, how to improve throughput and solve IO bottlenecks has always been the focus of attention. The traditional method is sub-library (vertical) sub-table (horizontal), which in many cases requires a lot of extra work and brings various problems. In order to solve this problem and realize the database cluster load and data security, Oracle provides a RAC solution, and then MyCat, a tool that supports multiple databases, appeared.

        MongoDB is a better choice in many cases, it supports three clustering methods, which can be used according to the actual situation. Boot can easily integrate most database systems including MongoDB, and only need to introduce the necessary starter components (you also need to configure some data connection-related parameters, omitted). :

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

 

 

        This section does not cover DNS and CDN, which are also common techniques for load balancing.

        Taking the LVS softswitch as an example, the SpringBoot cluster deployment diagram is as follows:

 

 

 

 

3. Content application

        Others require some changes. For systems with strong content, such as Weibo and forums, a large amount of content needs to be cached to reduce unnecessary IO operations and improve response speed. These content are usually not very real-time, and only need to maintain eventual consistency. The current popular solution is redis, in fact, mongodb can also meet the requirements.

        Import dependencies:

<dependency>  
<groupId>org.springframework.boot</groupId>  
<artifactId>spring-boot-starter-redis</artifactId>  
</dependency>  

 

 

        Possible ways:

 

 

 

        For massive data, if data analysis is required, consider introducing hadoop/spark to replace the current persistent storage.

 

4. High Concurrency System

        Some extreme cases require special handling. I have tried some quantitative trading research, and I will briefly introduce the stock market as an example. If you need to make an Http service to provide users with stock market information, and this thing has a very large number of visits. How to do it?

        In addition to load, the key point is to reduce loss: reduce IO, reduce calculation, and reduce conversion. It is best to return the required market data directly when a request comes up to the server. So, the crux of the matter is this data. The possible process is as follows: the original data is cleaned and processed, and then pushed to a data source, and the special tool obtains the required data from the data source and pushes it to the server (if possible, it is obviously better to push directly to the server without going through the data source. fast), the server caches this data and responds to client requests. Obviously, the data processing and pushing process will cause a little delay in the market, and this delay needs to be kept as small as possible. In this way, one data processing can handle N multiple requests, and this N basically depends on server performance and network conditions.

 

 

 

        As shown in the figure, EhCache is used as the server-side cache component of the microservice Service to build a cluster environment. Among them, the dotted line part identifies an EhCache cluster, which connects two services: Service and Helper. The Helper service is responsible for obtaining data (passive or active) and writing it to the cache. EhCache will broadcast the data to other nodes to complete synchronization. Service applications only need to perform read operations, which minimizes IO frequency and repeated calculations.

 

 

        It is not difficult to find that SpringBoot is easily integrated with some current mainstream frameworks and is suitable for the development of many application scenarios. After several years of development of the Spring framework, it seems that the culmination of the Spring framework is Boot. Of course, it is not just a certain technology or a certain tool, we should pay more attention to the ideas behind it, and master the method to solve the problem.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326477804&siteId=291194637