Cgb2005 - Jingtao 17 (SOA, RPC, micro service idea, nginx's disadvantages of load balancing, zookeeper registry, Dubbo framework, and Dubbo load balancing method)

Cool breeze AAA 2022-02-13 07:43:06 阅读数:726

cgb2005 cgb jingtao soa rpc

Be careful

1.SOA thought ( Standards for microservice code editing )
2.RPC Concept ( Specification of remote call )
3. Micro service idea
nginx Mainly do reverse proxy instead of load balancing
The microservice invokes the original
Zookeeper Registration Center Introduction download install Cluster description Data storage structure
4 Dubbo The framework is introduced
Introductory cases Interview questions Load balancing

1. SOA thought ( Standards for microservice code editing )

Service Oriented Architecture (SOA) It's a component model , It will be different functional units of the application ( It's called service ) To break up , And through the well-defined Interface Linked to the agreement . Interfaces are defined in a neutral way , It should be independent of the hardware platform that implements the service 、 Operating system and programming language . In this way, various components and services can interact in a unified way .
Generalization :SOA The idea is to split the service according to the business , Then interact according to the unified and neutral interface .
Knowledge review : Object-oriented thinking / Interface oriented development / Aspect oriented development / Service oriented development

Be careful : there Interface It's a neutral rule , Not just public interface.
The person who invokes the service : consumer . People who implement services : producer / Provider .
 Insert picture description here

2. RPC Concept ( Specification of remote call )

RPC Is a remote procedure call (Remote Procedure Call) Abbreviated form .SAP System RPC The principle of call is very simple , There are some similar to three-tier architecture C/S System , The third-party client program calls through the interface SAP Internal standard or custom functions , Get the data returned by the function and display or print it after processing .
 Insert picture description here

Examples of life :
A sunny morning , My wife is looking through the technical magazines I subscribed to .

“ Husband , What is? RPC ah , Why do you programmers talk so much !”, My wife is still curious as always .
“RPC, Namely Remote Procedure Call Short for , In Chinese, it means remote procedure call ”, I was reading , One side answers casually .
“ what ? What are you talking about ? Who doesn't know what it means to translate it into Chinese ? You waste wood , Get out of here and wash the dishes !”
“ I went to ...”, I wake up like a dream , I'm not sitting across from a programmer , In order not to wash dishes , I instantly mobilize all the brain cells , The sea of stars converges in my mind , Inspiration came out ……

“ It's like this , Remote procedure call , Naturally, it is relative to the local procedure call .”
“ Uh huh , Let's talk to my mother first , What is a local procedure call ?”
“ Local procedure call , It's like you're at home , You want to wash the dishes , Then you just put the dishes in the dishwasher , Turn on the dishwasher switch and you can wash it . This is called a local procedure call .”

“ Ouch! , I won't do it , What is remote procedure call ?”
“ Long distance , That's when you're not at home , I went with my sisters , All of a sudden, I found that the bowl hasn't been washed , Made a phone call , Tell me to wash the dishes , This is the remote procedure call ”, What an easy explanation , I'm a genius !

“ Oh ! Oh, I see ”, Said , My wife started packing .
“ What are you doing ”
“ I ? I'm going out on the waves , Remember to receive my remote call later , Oh no , Let's be professional , It should be said , I'll remember to receive my RPC Oh !”
……
summary :

  1. When you can't finish a business, you can't do it directly , Business that needs help from a third party .
  2. Use RPC when " Feeling " You are calling your own methods to complete the business .

3. Micro service idea

standard :
1. According to the idea of business splitting the Distributed The design of the ( Distributed thinking : Demolition ).
2. When an exception occurs in the service, you can Automatic implementation of fault migration There is no need for human intervention ( automation ).

3.1 Traditional service invocation methods

Scene analysis : If you go to a large event, double 11, It must be configured with multiple tomac Server to access ( increase web manage sso And so on ), And then pass nginx Load balancing ( Manually modify nginx.conf The configuration file ) Distribute requests to different servers . because nginx Need to do reverse proxy ( Servers need to use domain names to access , It's all going through nginx analysis ), Now all servers need nginx Transfer for load balancing , So it will cause nginx Access pressure is too high, and downtime is easy to occur .
summary
1. If the nginx The way Load balancing , When the number of services changes , All have to be modified manually nginx.conf The configuration file , Not smart enough .
2. All requests will pass nginx Server as transit , If nginx Once the server goes down , It directly affects the whole system , therefore nginx It's best to do only reverse proxy and simple load balancing .
 Insert picture description here

3.2 Introduction to the calling principle of microservices

 Insert picture description here
Call step :
1. Write service information to registry (1. The service name 2. service IP Address 3. port )
2. The registry received server information , Maintain service list data dynamically .
3. consumer The registry will be linked at startup . Purpose to obtain service list data .
4. The registry will synchronize the service list data to consumer , And save it locally to consumers . It is convenient to call later .
5. When the consumer starts a business call , Based on known service information Load balancing operation , Visit the service provider .
6. When a service provider goes down , Because the registry has Heartbeat detection mechanism . So the list of services will be maintained dynamically .
7. When the list of services in the registry changes , It will broadcast all over the network Inform all consumers Update local service list .

Be careful :Dubble and SpringCloud The principle of the registry of the framework is the same .

3.3 Zookeeper Registration Center Introduction

ZooKeeper It's a distributed one , Open source distributed application coordination service , yes Google Of Chubby An open source implementation , yes Hadoop and Hbase Important components . It is a software that provides consistency services for distributed applications , The features provided include : Configuration maintenance 、 Domain name service 、 Distributed synchronization 、 Group service, etc .
ZooKeeper The goal is to encapsulate complex, error-prone, critical services , Will be simple to use interface and efficient performance 、 A stable system is provided to the user .
ZooKeeper Contains a simple set of primitives , Provide Java and C The interface of .
ZooKeeper In the code version , Provides distributed exclusive locks 、 The election 、 The interface of the queue , Code in $zookeeper_home\src\recipes. The distribution locks and queues are Java and C Two versions , The election is just Java edition .
Generalization : ZK The main task is Service scheduling , Provide The function of consistency .
Official website
 Insert picture description here

3.4 Zookeeper download

Dubbo The registry used for the framework is Zookeeper. If I use theta spring cloud The registry is Eureka( There may be a charge )
Download steps :
 Insert picture description here
 Insert picture description here
Installation packages are .tar.gz That is the LInux edition , So it's installed in Linux Under the system .
 Insert picture description here
 Insert picture description here

3.5 Zookeeper install

explain : because Zookeeper yes java Language development, so you need to install jdk.
1). Upload the compressed package to linux Working directory and check jdk Whether the environment is installed .
 Insert picture description here
2). See the pre class materials and documents for specific steps
Because a single machine Zookeeper Poor pressure resistance and easy downtime , So install Zookeeper colony .

3.6 About Zookeeper Cluster description

3.6.1 The smallest cluster units are

The formula : The number of surviving nodes > N/2 Clusters can create
1 platform : 1-1 > 1/2 fake
2 platform : 2-1> 2/2 fake
3 platform : 3-1> 3/2 correct
4 platform : 4-1> 4/2 correct

Conclusion : Build the smallest unit of a cluster 3 platform .

3.6.2 Why clusters are usually odd

3 platform How many clusters are down at most Cluster works Most downtime 1 platform
4 platform The cluster is down at most. Several clusters can work normally Most downtime 1 platform
explain : If you implement the same function Odd numbers are better .

3.7 About zk Cluster election rules

principle : myid Maximum priority myid The higher the value, the easier it is to be the host . More than half of them agree to be elected host .
subject : 1,2,3,4,5,6,7 Start in turn Who is the host ??? Who can never be elected host ???
answer :4 When the host 1 2 3 Never be a host
reason :
(1).ZK Cluster has 7 So at least start 4 platform To build a successful (7-3=4>3.5 Can be built ,7-4=3<3.5 Can't build ),4 A cluster myid The biggest is the second 4 platform , therefore 4 Bit host .
(2).7 platform ZK Host downtime begins , be myid The largest once elected host , Fall off 7 be 6 Selected host ,6 Downtime 5 Selected host ,5 Downtime 4 Selected host ,4 Downtime should have been 3 Selected host, but only 3 platform ZK The number of primary nodes cannot be maintained , The cluster crashed, so 1 2 3 Never be Host selection .

3.8 About ZK Data storage structure (day18pm 1.41)

explain : stay ZK The data in is stored in a tree structure
command :sh zkCli.sh
Query command :ls/…
 Insert picture description here

4 Dubbo The framework is introduced

explain :
1).Dubbo The registry used for the framework is Zookeeper,SpringCloud The registry for eureka.
2). Suitable for small and medium-sized projects .

4.1 website

Developed by Alibaba

Apache Dubbo |ˈdʌbəʊ| It's a high performance 、 Lightweight open source Java RPC frame , It provides Three core competencies Interface oriented remote method call , Intelligent fault tolerance and load balancing , And automatic service registration and discovery .
 Insert picture description here

4.2 Dubbo characteristic

 Insert picture description here

4.3 Dubbo working principle

similar zookeeper Registry Center
 Insert picture description here

4.4 Dubbo Introductory cases

4.4.1 Import the project

explain : This item is related to jt Project level , Put the pre class materials dubbo-jt Project copy to idea working space , After the pom Right click file add maven Take effect .
 Insert picture description here
modify springboot Version number :
 Insert picture description here

4.4.2 Define common third-party interface projects

Be careful : Interface is a common communication specification , Is not necessarily interface. stay dubble The interface in happens to be interface , And in the springcloud The interface is not .

explain : Interface projects generally define common parts , And being relied on by third parties .
explain :
1). The interface belongs to a neutral third party . In this case, there is a pojo class :User, One interface Interface :UserService
2). because User Class is used by more than one project, so it is placed in Common interface project , If others want to use it, they just need to add dependencies .UserService This neutral interface Only do the definition of methods, not the implementation of methods , The interface is implemented by the provider .

 Insert picture description here
Specific code :
user.

package com.jt.dubbo.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
@Data
@Accessors(chain=true)
@TableName
public class User implements Serializable{

//dubbo Objects transferred in the protocol must be serialized 
private static final long serialVersionUID = 1L;
@TableId(type=IdType.AUTO)
private Integer id;
private String name;
private Integer age;
private String sex;
}

UserService.

package com.jt.dubbo.service;
import com.jt.dubbo.pojo.User;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
//service Interfaces only define methods , Do not implement the method , The interface is implemented by the provider .
public interface UserService {

// Query all user information 
List<User> findAll();
// Add user receipt operation .
@Transactional
void saveUser(User user);
}

consumer All providers should use this interface , Need to be in their respective pom Add dependencies to the file .
 Insert picture description here
 Insert picture description here
 Insert picture description here

4.4.3 Service providers 1 explain ( Generator )

4.4.3.1 Provider code structure

explain : Producers only need service Implementation class mapper
 Insert picture description here

4.4.3.2 Edit the main boot class

 Insert picture description here

4.4.3.3 edit Provider implementation class mapper Interface

Specific code :
UserServiceImpl : Be careful service The annotation guide package is Alibaba's .

package com.jt.dubbo.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.jt.dubbo.mapper.UserMapper;
import com.jt.dubbo.pojo.User;
@Service(timeout=3000)// Timeout for the consumer to invoke the provider If 3 If there is no connection within seconds, it means timeout Internally implemented rpc Note that this annotation is Alibaba's package 
//@org.springframework.stereotype.Service// Give the object to spring Container management 
public class UserServiceImpl implements UserService {

@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {

System.out.println(" I was the first service provider ");
return userMapper.selectList(null);
}
@Override
public void saveUser(User user) {

userMapper.insert(user);
}
}

UserMapper .

package com.jt.dubbo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jt.dubbo.pojo.User;
public interface UserMapper extends BaseMapper<User>{

}

4.4.3.4 Of the provider YML Profile description

 Insert picture description here

server:
port: 9000 # Define port
spring:
datasource:
# introduce druid data source This is Ali's data source
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: root
# About Dubbo To configure
dubbo:
scan:
basePackages: com.jt # Appoint dubbo The package path of In order to scan dubbo annotation (@service), You can specify a larger range com,jt
application: # apply name
name: provider-user # An interface corresponds to a service name ( An interface can have multiple implementations , However, if the same interface is implemented, the services provided should also be the same . eg: Lao Wang Lao Li sells vegetables and implements the same interface , Lao sun sells meat under different excuses from Lao Wang and Lao Li )
registry: # Registry Center 2181 The slave is connected backup( spare ) The user gets the data from the machine The host is only responsible for monitoring the entire cluster Data synchronization , So this place should be connected to the slave instead of the host
address: zookeeper://192.168.126.129:2181?backup=192.168.126.129:2182,192.168.126.129:2183
protocol: # Designated agreement name:dubbo Fixed writing
name: dubbo # Use dubbo agreement (tcp-ip) web-controller Call directly sso-Service
port: 20880 # Each service has its own specific port Can't repeat .
mybatis-plus:
type-aliases-package: com.jt.dubbo.pojo # Configure alias package path
mapper-locations: classpath:/mybatis/mappers/*.xml # add to mapper The mapping file configuration: map-underscore-to-camel-case: true # Turn on hump mapping rules 

protocol-port=20880 Port number Description :
As shown in the figure below , Consumers' tomact The port number is 9001, Producer's tomact The port number is 9000,20880 Start for producer tomact Server load Dubbo A framework dynamically generated Port number of the external service .
Dubbo The agreement is only responsible for the communication between consumers and producers , It has nothing to do with the browser .

 Insert picture description here

4.4.3.5 Start producer

 Insert picture description here

4.4.4 Service consumer description

4.4.4.1 Consumer project structure

 Insert picture description here

4.4.4.2 Edit the main boot class

Consumers don't need to connect to the database .
 Insert picture description here

4.4.4.3 edit Controller layer

Be careful :
1. The object data passed on a remote call must be serialized
2. The object injection method of consumers should use dubbo Comments provided by the framework Reference

package com.jt.dubbo.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.jt.dubbo.pojo.User;
import com.jt.dubbo.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {

//@Reference(loadbalance="leastactive")
@Reference // utilize dubbo To create a proxy object for the interface utilize rpc call namely dubbo The framework passes @Reference Annotation injection object 
private UserService userService;
/** * Dubbo Framework call features : long-range RPC Calling is as simple as calling your own local service * @return */
@RequestMapping("/findAll")
public List<User> findAll(){

// The object data passed on a remote call must be serialized .
return userService.findAll();
}
@RequestMapping("/saveUser/{name}/{age}/{sex}")
public String saveUser(User user) {

userService.saveUser(user);
return " The user has entered the database successfully !!!";
}
}

 Insert picture description here

4.4.4.4 Edit the consumer's YML The configuration file

server:
port: 9001
dubbo:
scan:
basePackages: com.jt # Define scan annotations
application:
name: consumer-user # Define the consumer name
registry: # Address of Registration Center
address: zookeeper://192.168.126.129:2181?backup=192.168.126.129:2182,192.168.126.129:2183

 Insert picture description here

4.4.4.5 Start the consumer for access testing

explain : Start the generator first 1, Starting consumers .
 Insert picture description here

4.4.5 Service providers 2 explain

explain :yml Some configuration files are different ,service Implementation classes and mapper The code in the interface is the same .

server:
port: 9003
spring:
datasource:
# introduce druid data source
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: root
dubbo:
scan:
basePackages: com.jt
application:
name: provider-user # The same interface implemented does not need to be renamed
registry:
address: zookeeper://192.168.126.129:2181?backup=192.168.126.129:2182,192.168.126.129:2183
protocol:
name: dubbo
port: 20882 # Each service should have its own independent port
mybatis-plus:
type-aliases-package: com.jt.dubbo.pojo # Configure alias package path
mapper-locations: classpath:/mybatis/mappers/*.xml # add to mapper The mapping file configuration: map-underscore-to-camel-case: true # Turn on hump mapping rules 

 Insert picture description here

4.4.6 Start producer 1 Generator 2 Consumer access test

explain : Start... First 2 A producer , Starting consumers . Because the generator code is the same, the path is the same , So you can visit , From the following figure 2 The background of a producer can see 2 All producers can access , This is a dubbo Load balancing provided by the framework .
 Insert picture description here
 Insert picture description here
 Insert picture description here

4.5 About Dubbo Interview questions

problem 1: If one of the servers goes down ,zk The cluster is normal Whether user access is limited ?
answer : No limits , because zk With the help of the , Make programs always have access to the right server . And when the service restarts ,duboo There's automatic discovery of services , Consumers don't need to restart to access new services .

problem 2: If ZK The cluster is down for a short time , The server is OK Whether user access is limited ?
answer : User access Unaffected , Because consumers store service list information locally ( Because the information of the registry is kept locally ), When accessing a faulty machine , Automatically change the logo information to down attribute .

problem 3: If ZK The cluster is down for a short time ,2 If a server blocks a user, whether access is affected ?
Unaffected If the access to the one that is down, it will be updated to down No more access to this server ,2 If the platform is blocked, it will certainly affect .

4.6 About Dubbo Description of load balancing

4.6.1 Types of load balancing ( In terms of usage )

Be careful : Load balancing is Dubbo The framework provides, not Zookeeper,zk Only responsible for consistency Services .
1). Client load balancing :Dubbo/SpringCloud Wait for microservice framework
explain : In the process of microservice invocation, the consumer of each service can realize load balancing operation on the client , Before each request, obtain the service information to be accessed through the service list . Achieved pressure privatization .
 Insert picture description here
2). Server side ( Centralized ) Load balancing of :Nginx
explain : because nginx At the center of load balancing , So any kind of service will go through nginx Then switch to different servers , Can cause nginx The load pressure is very high .
.

 Insert picture description here

4.6.2 Dubbo Load balancing (4 Kind of )

1). Default mode : Random algorithm

2). How to find the type of load balancing in the code :
Step one : Click the magnifying glass icon and enter keywords to search
 Insert picture description here
Step two : Double click to select ctrl+t Find the implementation class of the interface .
 Insert picture description here
Step three : Enter the implementation class
 Insert picture description here

3). How to configure... In the project :
explain : The client load balancing method is configured among consumers .
 Insert picture description here


@Reference(loadbalance = "random") // The default policy Load balancing random strategy 
@Reference(loadbalance = "roundrobin") // Polling mode 
@Reference(loadbalance = "consistenthash") // Uniformity hash Consumer binding server provider 
@Reference(loadbalance = "leastactive") // Select the server with small load to access 
}
copyright:author[Cool breeze AAA],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130743010332.html