Before learning SpringCloud, let’s sort out the basics. The goal of this chapter is to understand the following basics:
- Microservices
- SpringCloud
- Springboot
- Finally, this chapter ends by building a simple Springboot helloworld.
1. What are microservices
In the traditional enterprise architecture, it is generally divided into database, server-side processing, and front-end display. The server side is generally a single module or a single process. As the business on the web side becomes more and more complex, the requirements for the server side become higher and higher, and there are more and more service interfaces. In the beginning, people used service bus to solve the problems of service discovery and service governance. With the rise of the Internet, the performance and stability of interface calls became more and more prominent. Therefore, decentralization has become a highly valued issue after service governance. In this context, more and more companies use microservices for server-side design. From the previous Taobao dubbo to the later SpringCloud, microservices have now become the most popular technology selection in back-end service design. .
2. Introduction to SpringCloud
After the rise of the concept of microservices, many related frameworks have been circulated among technicians. If the dubbo mentioned above, there are other technical frameworks of Elastic-job for batch tasks, but they are all based on one of the microservices. The emergence of SpringCloud provides technical personnel with a comprehensive solution framework to solve the implementation of microservice architecture.
SpringCloud contains multiple sub-projects:
Spring Cloud Config: Configuration Management Tool
Spring Cloud Netflix: core components, including Eureka: service governance components (service registry, service registration and discovery mechanism)
Hystrix: fault-tolerant management component that implements the circuit breaker pattern
Ribbon: service call component for client load balancing
Spring Cloud Bus: event, message bus
Spring Cloud Cluster: Cluster
Wait, there are many more to list.
3. Introduction to Spring Boot
The project construction of SpringCloud is based on the implementation of Spring Boot, so it is necessary to understand Spring Boot first, and learn about Spring Boot by creating a simple web project: IDEA is recommended as a development tool
First create a SpringBoot project by creating a Model:
Click Next:
After filling in the metadata related information of the Model, continue to next
Here select Web, after next, fill in the name of the module:
After clicking finish, the following project structure appears:
The above picture shows the project structure, in which pom.xml is the configuration file of the maven project, and there will be project dependencies in it. We use the spring initialzr wizard, so the initial dependencies will be automatically filled in pom.xml; secondly, the default There is a java and a resources in the src main directory, where code is placed in java, and project resources such as configuration files are placed in resources. The application.yml in the figure is also a configuration file, which was added later. This is the characteristic configuration file of springboot , the HelloControler in the code area is also the later code. It will be explained later that the system will have an application entry class by default, SpringbootApplication. The final test area is the area where unit test code is placed.
After establishing the project, we are ready to develop a small demo, helloword.
First look at pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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>com.study</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
If you have other dependencies in your project in the future, you can add them yourself. The above was created when the project was created. Remember the web option we chose earlier, there is sping-boot-starter-web here.
Second, we write a controller, and the default calls are handled by this controller
package com.study.springboot.controler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/sayhello") public String Hello() { return "Hello World!"; } }
In springboot, the annotation method is used to develop and improve the development efficiency. The above paragraph is to return hello world when http://localhost:port/sayhello is called. Next, create a configuration file for the application. In the resource directory, we create a file application.yml
server: #Configure the server startup port port: 8028 #Configure the context path for access, which is the context path required after localhost:8028 context-path: /sayhello
Springboot uses a progressive-style configuration file format yml, which is somewhat similar to python, of course, you can also use the default application.properties
The above is equal to server.port=8028
Finally, let's take a look at the entry of the demo:
package com.study.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringbootApplication { public static void main(String[] args) { SpringApplication.run(SpringbootApplication.class, args); } }
This class does not do any processing, it is just like this when it is created, and we run it directly:
After running, we enter the url http://localhost:8028/sayhello in the browser, and the browser will return the following information:
A simple Springboot demo is done.