Spring-Cloud-Netflix- System Architecture

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

  1. Code coupling, development and maintenance difficulties
  2. Not targeted optimized for different modules
  3. Not horizontal expansion
  4. 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

  1. Split system to achieve traffic sharing, to solve the problem of concurrency
  2. Can be optimized for different modules
  3. Convenient horizontal scaling, load balancing, fault tolerance rate increase
  4. 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:

  1. Single Responsibility : micro-services Each service corresponds to a unique business ability, so single responsibility
  2. Service resolving very small size : for example, a user management service can be used as a
  3. 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
  4. 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:

  1. The basic services were calling each other between the extraction system and improve the efficiency of development and code reuse
  2. 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:

  1. Distributed goal is: a machine can not afford, or cost, have to use multiple machines to complete the deployment of services
  2. 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:

  1. 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

  1. There is no listening service down : After you deploy a lot of services, listening to the service if there is no downtime
  2. Load balancing : too much for a service, you want to deploy multiple services, multiple services deployed balancing call
  3. Fuse : The service there is a problem, can not let the program stuck in there
  4. Limiting : current limit is exceeded for the expected traffic, limiting the request of some "blown" by limiting the selectivity of predefined rules
  5. 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
  6. 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:

  1. Remote Produce Call the remote procedure call, there are similar RMI. Custom data formats, native TCP-based communication, high speed, high efficiency
  2. Early webservice, now popular dubbo, are typical RPC
  3. Limits the platform, only the Java platform

Http:

  1. http is actually a network transmission protocol, based on TCP, it specifies the format of data transmission
  2. 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
  3. Without limiting the platform

Analog Micro services:

  1. Creating a common parent project, delete the src directory, file retention pom
    Here Insert Picture Description
    Here Insert Picture Description
  2. Add the parent project among springboot rely
    Here Insert Picture Description
    Code:
	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.3.RELEASE</version>
    </parent>
  1. Create a sub-module user
    Here Insert Picture Description
    Here Insert Picture Description
  2. Add springboot-web initiator relies
    Here Insert Picture Description
    Code:
	<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
  1. Create a user project to start classes UserApplication
    Here Insert Picture Description
    Code:
@SpringBootApplication
public class UserApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class,args);
    }
}

  1. Util package to create, copy ResponseResult tools to the package
    Here Insert Picture Description
    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);
    }

}
  1. Create a sub-package controller, the controller UserController created among
    Here Insert Picture Description
    the code:
@RestController
public class UserController {
    @RequestMapping("/getUser.do")
    public ResponseResult getUser() {
        return ResponseResult.success("返回成功","myUser");
    }
}
  1. Creating application.yml in the resources directory
    Here Insert Picture Description
  2. Start visit http: // localhost: 5000 / getUser.do
    10.
  3. Create a sub-module goods
    Here Insert Picture Description
  4. The same configuration and user, modify server-port port: 5001
    Here Insert Picture Description
    Here Insert Picture Description
  5. Start visit http: // localhost: 5001 / getGoods.do
    Here Insert Picture Description
  6. user calls the project engineering goods among interfaces :
    Add RestTemplate of Bean in UserApplication in
    Here Insert Picture Description
    the code:
 @Bean
    public RestTemplate restTemplate(){
        return  new RestTemplate();
    }

RestTemplate injected in the user works UserController them
Here Insert Picture Description
through an interface calls between restTemplate realization of the project
Here Insert Picture Description
is running two projects at the same time, visit http: // localhost: 5000 / getGoods.do
Here Insert Picture Description
above operation is realized between the two project calls

Use nginx achieve Cluster Setup

  1. Download nginx URL: nginx Download
    Here Insert Picture Description
    download, unzip: Do not pay attention to extract the directory with the Chinese
  2. 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.
    Here Insert Picture Description
  3. Nginx modified configuration file
    Here Insert Picture Description
    to the server before the code to delete, replace this
    Here Insert Picture Description
    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;
        }
  1. Start nginx.exe After configuration
    Here Insert Picture Description
    startup flashed a normal situation

  2. The user of the caller port number to 80
    Here Insert Picture Description

  3. Address Start user, goods, goods1 user's access: HTTP: // localhost: 5000 / getGoods.do
    Here Insert Picture Description
    Here Insert Picture Description
    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
Here Insert Picture Description
next update of SpringCloud-Netflix-Eureka Instructions

Guess you like

Origin www.cnblogs.com/joker-dj/p/12657537.html