Article Directory
microservice architecture
introduce
A microservices architecture is a software design approach that breaks down an application into small, autonomous services. Each service is relatively independent, has its own database, is deployed in its own container, and can use different programming languages and technology stacks. By using microservices, greater scalability, reliability, and flexibility can be achieved while enabling developers to develop and deliver new functionality faster.
architecture diagram
As shown in the figure is the spring cloud microservice architecture diagram of spring official website:
the following is a more detailed architecture diagram of a specific project
core components
In the follow-up, you will learn each component in detail, output code, and develop your own microservice project scaffolding.
Spring Cloud version correspondence
When using spring cloud, you must pay attention to the corresponding relationship between spring cloud and spring boot versions. If the versions are inconsistent, there will be some bugs.
You can obtain the current specific version correspondence through the official website: https://start.spring.io/actuator/info
Subsequent version matching:
spring cloud H version
spring boot 2.2.5RELEASE
JDK 8
Basic framework construction
1. Build the parent project
pom introduces basic dependencies:
<?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.lufei</groupId>
<artifactId>lf-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.18</lombok.version>
<mysql.version>5.1.47</mysql.version>
<druid.version>1.2.16</druid.version>
<mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
</properties>
<!-- 子模块继承之后,提供作用:锁定版本+子modlue不用写groupId和version -->
<dependencyManagement>
<dependencies>
<!--spring boot 2.2.5-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud Hoxton.SR1-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>lf-cloud</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.5.RELEASE</version>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
2. Build sub-projects
Create a new auth user authorization service and a user information service.
Remote call of simple mock service
userEngineering construction
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">
<parent>
<artifactId>lf-cloud</artifactId>
<groupId>com.lufei</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lf-user</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!--mysql-connector-java-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 9001
spring:
application:
name: lf-user
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包 com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.lufei.springcloud.domain
Start class UserApplication
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class,args);
}
}
UserController class, do not check the database for the time being, return directly
@RestController
@RequestMapping("/user")
public class UserController {
/**
* 获取当前用户信息
*/
@GetMapping("/info/{username}")
public String info(@PathVariable("username") String username)
{
return username + " login success";
}
}
The startup test can be accessed (some errors may be reported if the database is not configured, so let’s ignore it)
auth engineering construction
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.yml
server:
port: 9002
spring:
application:
name: lf-auth
Start class AuthApplication
@SpringBootApplication
public class AuthApplication {
public static void main(String[] args) {
SpringApplication.run(AuthApplication.class,args);
}
}
Run the test to ensure that the project does not report errors
RestTemplate implements remote calls of microservices
The above two simple sub-projects have been built.
Next, we simply simulate that when the auth authentication service logs in, the user service is remotely called to obtain information and log in successfully.
We first use RestTemplate to achieve this operation
Introduction to RestTemplate
RestTemplate is an HTTP client tool provided by the Spring framework for interacting with RESTful web services. It simplifies the process for developers to send HTTP requests and process HTTP responses using Java code.
Using RestTemplate, you can easily send HTTP requests such as GET, POST, PUT, DELETE, and process the response. It also supports common HTTP features such as various HTTP authentication, request headers, and cookies.
Another advantage of RestTemplate is that it can be combined with Spring's various data binding and transformation technologies (such as Jackson JSON processor), so that you can easily convert HTTP responses into Java objects.
Placement RestTemplate
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Auth service controller interface
@RestController
@RequestMapping("/auth")
public class AuthController {
@Autowired
RestTemplate restTemplate;
public static final String USER_URL = "http://localhost:9001";
@PostMapping("login")
public String login(@RequestBody String name)
{
return restTemplate.getForObject(USER_URL+"/user/info/"+name,String.class);
}
}
Test remote access
As shown in the figure, the authorization service of 9002 finally successfully accesses the user service of 9001
Summarize
So far we have been able to call services remotely through RestTemplate, but there are some obvious problems:
- These codes need to be written every time the service is called, and there is a lot of code redundancy
- If the service address is modified, the maintenance cost will increase
- Not flexible enough to use
These problems require dynamic service registration and service discovery through the registration center.
The next issue will continue the registration and discovery of microservices.