system structure
Outline
With the development of the Internet, expanding the scale web applications. The surge in demand caused by a technical pressure. System architecture and therefore also continue to evolve, upgrade, iteration.
From a single application, to split vertically, the distributed services to the SOA, and now the hot microService Architecture
Centralized architecture
Outline
When the site traffic is small, only one application all the functions are deployed together to reduce costs and deploy nodes on a project from start to finish, deployment time, just labeled as a war package
Feature
- Code coupling, development and maintenance difficulties
- Not targeted optimized for different modules
- Not horizontal expansion
- Single point fault-tolerance is low, poor concurrency
Vertical Split
Outline
When the traffic gradually increased, a single application can not meet the demand, this time to cope with higher concurrency and business needs, we split the system according to business functions
Feature
- Split system to achieve traffic sharing, to solve the problem of concurrency
- Can be optimized for different modules
- Convenient horizontal scaling, load balancing, fault tolerance rate increase
- Between systems independent of each other, there will be a lot of duplication of development work, affecting the development efficiency
Classification System Architecture
Micro Services
Micro service is to all modules of a project originally bloated split open and do not associate with each other, you can not even use the same database
Micro-service features:
- Single Responsibility : micro-services Each service corresponds to a unique business ability, so single responsibility
- Service resolving very small size : for example, a user management service can be used as a
- Service-oriented : Service-oriented is to say each service to be exposed to the external service interface API, technology is not concerned about the implementation of the service, so that regardless of platform and language, is not limited by what technology, as long as the interface to provide Rest
- Autonomy : Autonomy is a saying among service independent of each other without disturbing each other
Distributed Service:
Overview:
As more and more vertical applications, the inevitable interaction between applications, will be drawn out of the core business, as an independent service
distributed, is the huge system is divided into multiple modules (This and services like micro) deployed to different because a machine could not afford so much pressure on the machine
each module interface for data exchange by, in fact, it is a kind of micro-distributed service.
Features:
- The basic services were calling each other between the extraction system and improve the efficiency of development and code reuse
- High degree of coupling between systems, calling relations are complicated, difficult to maintain
Micro Services and distributed difference:
Micro-services are distributed to split open the module becomes an independent unit that provides an interface to call
The difference lies in the nature of their different goals:
- Distributed goal is: a machine can not afford, or cost, have to use multiple machines to complete the deployment of services
- Target micro services: just let the various modules split open, will not be affected by each other, for example, or upgrade modules also appear BUG and so on ...
Micro-service problems to be faced:
- Service Governance (SOA)
Overview:
As more and more services, capacity assessment, waste of resources and other small service issues is gradually emerging, this time a dispatch center based on the need to increase access to real-time pressure management cluster capacity, improve cluster utilization.
(SOA) to improve machine utilization of resource scheduling and control center
problem:
More and more services, the need to manage each service call to address complex relationships, difficult to sort out excessive dependency services, the service status is difficult to manage, not based on dynamic management of service
What services do governance:
Service registry, to achieve automatic service registration and discovery, without human service record service address automatically subscribe, the list of services automatic push service calls transparency, without caring about the dependencies
of dynamic monitoring service status monitoring report, human control service status
- There is no listening service down : After you deploy a lot of services, listening to the service if there is no downtime
- Load balancing : too much for a service, you want to deploy multiple services, multiple services deployed balancing call
- Fuse : The service there is a problem, can not let the program stuck in there
- Limiting : current limit is exceeded for the expected traffic, limiting the request of some "blown" by limiting the selectivity of predefined rules
- Downgrade : When the server pressure surge, according to the actual business situation and flow of some services and pages do not deal with or have easy ways to change the policy process, freeing server resources to ensure the normal operation of the core of the transaction or the efficient operation
- Gateway : the unified management of a network gateway API, channel, is the only entrance to all requests of the whole micro-service platform
What is springClould
Micro-service architecture is just a way of the project, or that is a concept
Spring-Cloud is the realization of this technology
for a unified micro-encapsulation process on issues facing the service
Remote invocation:
RPC:
- Remote Produce Call the remote procedure call, there are similar RMI. Custom data formats, native TCP-based communication, high speed, high efficiency
- Early webservice, now popular dubbo, are typical RPC
- Limits the platform, only the Java platform
Http:
- http is actually a network transmission protocol, based on TCP, it specifies the format of data transmission
- Now the client browser and server communications are basically using Http protocol. It can also be used for remote service call. The disadvantage is that encapsulation message bloated
- Without limiting the platform
Analog Micro services:
- Creating a common parent project, delete the src directory, file retention pom
- Add the parent project among springboot rely
Code:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.3.RELEASE</version>
</parent>
- Create a sub-module user
- Add springboot-web initiator relies
Code:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
- Create a user project to start classes UserApplication
Code:
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class,args);
}
}
- Util package to create, copy ResponseResult tools to the package
of code:
public class ResponseResult extends HashMap {
public static String SUCCESS_CODE = "200";
public static String ERROR_CODE = "500";
public static String DATA_KEY = "data";
public static String MSG_KEY = "msg";
private ResponseResult() {
}
public ResponseResult set(String key, Object object) {
super.put(key, object);
return this;
}
private static ResponseResult newResponseResult() {
return new ResponseResult();
}
public static ResponseResult success() {
return ResponseResult.newResponseResult()
.set("code", ResponseResult.SUCCESS_CODE)
.set(ResponseResult.MSG_KEY, "操作成功");
}
public static ResponseResult success(String msg) {
return ResponseResult.newResponseResult()
.set("code", ResponseResult.SUCCESS_CODE)
.set(ResponseResult.MSG_KEY, msg);
}
public static ResponseResult success(String msg, Object object) {
return ResponseResult.newResponseResult()
.set("code", ResponseResult.SUCCESS_CODE)
.set(ResponseResult.MSG_KEY, msg)
.set(ResponseResult.DATA_KEY, object);
}
public ResponseResult data(Object obj) {
return this.set("data", obj);
}
public static ResponseResult error() {
return ResponseResult.newResponseResult()
.set(ResponseResult.MSG_KEY, "操作失败")
.set("code", ResponseResult.ERROR_CODE);
}
public static ResponseResult error(String msg) {
return ResponseResult.newResponseResult()
.set(ResponseResult.MSG_KEY, msg)
.set("code", ResponseResult.ERROR_CODE);
}
public static ResponseResult error(String msg, Object object) {
return ResponseResult.newResponseResult()
.set(ResponseResult.MSG_KEY, msg)
.set(ResponseResult.DATA_KEY, object)
.set("code", ResponseResult.ERROR_CODE);
}
}
- Create a sub-package controller, the controller UserController created among
the code:
@RestController
public class UserController {
@RequestMapping("/getUser.do")
public ResponseResult getUser() {
return ResponseResult.success("返回成功","myUser");
}
}
- Creating application.yml in the resources directory
- Start visit http: // localhost: 5000 / getUser.do
- Create a sub-module goods
- The same configuration and user, modify server-port port: 5001
- Start visit http: // localhost: 5001 / getGoods.do
- user calls the project engineering goods among interfaces :
Add RestTemplate of Bean in UserApplication in
the code:
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
RestTemplate injected in the user works UserController them
through an interface calls between restTemplate realization of the project
is running two projects at the same time, visit http: // localhost: 5000 / getGoods.do
above operation is realized between the two project calls
Use nginx achieve Cluster Setup
- Download nginx URL: nginx Download
download, unzip: Do not pay attention to extract the directory with the Chinese - Create a new goods1 works exactly the same goods and only need to change the port number can be used to set up a cluster.
- Nginx modified configuration file
to the server before the code to delete, replace this
code is:
upstream mServer {
server localhost:5001;
server localhost:5002;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://mServer;
}
-
Start nginx.exe After configuration
startup flashed a normal situation -
The user of the caller port number to 80
-
Address Start user, goods, goods1 user's access: HTTP: // localhost: 5000 / getGoods.do
will find each refresh access, will return two different works controller, to build a complete cluster.
There is a problem, if one service is stopped, and when accessed, there will sometimes be slow case
solution: use the Eureka registry SpringCloud to manage our service
next update of SpringCloud-Netflix-Eureka Instructions