One 、 Understand microservice architecture
1、 Microservice technology stack
- The overall framework
Overall learning planning route 2、 The difference between microservice and single architecture
Monomer architecture : Develop all the functions of the business in one project , Deploy as a package
advantage
- Simple structure
- Low deployment cost
shortcoming
- High coupling degree , It is not conducive to construction and development
3、 Distributed architecture : Split the system according to the business function , Each business module is developed as an independent project , Become a service .
advantage :
- Reduce service coupling
- Service expansion is conducive to
shortcoming :
- The architecture is very complex
- Operation and maintenance 、 monitor , Deployment is more difficult
4、 Microservices : It is a distributed architecture scheme with good architecture design
Microservice architecture features :
- Single responsibility : Micro service dishes have smaller grains , Each service corresponds to a unique business capability , Single responsibility , Avoid duplicate business development
- Service oriented : Microservices expose business interfaces
- autonomous : Team independence , Technology independence , Data independence , Deploy independent
- Strong isolation : Isolate service calls 、 Fault tolerance 、 Downgrade 、 Avoid cascading problems
5、 Microservice technology comparison
6、 General enterprise demand comparison
Two 、SpringCloud
1、 Introduce :SpringCloud It is the most widely used microservice framework in China .
SpringCloud It integrates various micro service functional components , And based on SpringBoot The automatic assembly of these components is realized , This provides a good out of the box experience
SpringCloudyuSpringBoot The version compatibility relationship of is as follows :
2、 Service splitting and remote invocation
- Different microservices , The same business cannot be developed repeatedly
- Microservice data independence , Do not access the databases of other microservices
- Microservices can expose their business as interfaces , For other microservices to call
3、 Microservice remote call
- Remote call analysis : Issue in the module http request , Interfaces to access other modules , Realize the call of remote interface
- be based on RestTemplate Sponsored http Request to implement remote call
- http Requesting a remote call is a language independent call , Just know each other's ip, port , Interface path , Request parameters .
// In main class ( Configuration class ) Introduction in springboot With RestTemplate(), Inject spring Containers
// The main class springbootApplication Configuration class
@Bean public RestTemplate restTemplate(){ return new RestTemplate(); }
restTemplate.getForObject(url,User.class);
According to the requested address ( The request address is the same as the address in the browser ),User.class Can return json The type is converted to the corresponding class ( object );
Providers and consumers of remote calls :
- Service providers : In a business , Services invoked by other microservices .( Provide interfaces to other microservices )
- Serving consumers : In a business , Call the services of other microservices .( Call interfaces of other microservices )
- Intermediate role ( Call other microservices , It is called by other micro Services ): According to business logic , Providing interface and invoking interface are service provider and service consumer respectively
- The roles of providers and consumers are relatively ( Relative to business )
3、 ... and 、Eureka Registry Center
1、 The problem of traditional direct address call
Hard encoding : Write dead address ( How to solve multiple addresses of microservice )
Eureka The role of :
(1)Eureka The registry is divided into two parts :
Consumers choose the corresponding microservices according to load balancing , And make a remote call
The registry has a relationship with the service provider 30 Second heartbeat renewal , Check whether the microservice is still running normally , If it doesn't work properly ,
Just delete it from the registry , And consumers will no longer receive the corresponding information of this micro service , As shown in figure of 8083
The main problem : solve
- How can consumers obtain specific information about service providers ?
- When the service provider starts to Eureka Register your information
- Erueka Save this information
- The consumer sends to... According to the service name Eureka Pull provider information
- If there are multiple service providers , How consumers choose ?
- Consumers use load balancing algorithms , Pick one from the list of services
- How consumers perceive the health status of service providers ?
- The service provider will every 30 Second direction EurekaServer Send heartbeat request , Report health status
- Eureka The record service list information will be updated , Abnormal heartbeat will be eliminated
- Consumers can get the latest information
Eureka The main steps of building :
Eureka Server side :
- introduce eureka-server rely on
<!--eureka Server side --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
- Add... To the main class @EnableEurekaServer annotation
- stay application.yml Middle configuration Eureka Address
server:
port: 10086 # Service port
spring:
application:
name: eurekaserver # eureka Service name of
eureka:
client:
service-url: # eureka Address information for
defaultZone: http://127.0.0.1:10086/eureka
Eureka client :
- introduce Eureka-client rely on
<!--eureka Client dependency --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
- stay application.yml Middle configuration Eureka Address
spring:
application:
name: user-service # The service name
eureka:
client:
service-url: # eureka Address information for ( The client is the same as the server )
defaultZone: http://127.0.0.1:10086/eureka
General service construction process :@LoadBalanced Annotations are used for load balancing
Four 、Ribbon( Load balancing )SpringCloud The components of
- Load balancing principle
- Load balancing strategy
- Lazy loading
Implementation principle of the underlying source code :
technological process :
- Initiate request :RibbonLoadBanlancerClient according to URL Medium id Get the service list dynamically DynamicServerListLoadBalancer Pull the corresponding service list
- After getting the list of services, according to your own strategy IRule( Customizable ), Select a server to return to RibbonLoadBanlancerClient, Re splice the address , Request to the corresponding server
Polling strategy :
Modify load balancing strategy :(server: The server ;service: service )
1、 Code mode , In configuration class , Define a new IRule; After configuration in this way, no matter which service this class accesses, it will follow this rule , The servers assigned are random .
// Here is application( Need the main class of random server ) Configuration class configured in , The load balancing strategy is random
@Bean
public IRule randomRule() { return new RandomRule(); }
2、 In the configuration file application.yml, Adding a new configuration can also modify the configuration rules ; ( Here you can specify that certain service requests will use this rule , Here is userservice) Represents the class , This service class requests userservice Will follow this rule when , Whatever else .
userservice:
ribbon:
NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule # Load balancing rules
Load balancing : Initialize to lazy load , Cache after lazy loading , It will be faster to request access later
( Turn on hunger loading )
If there are multiple designated Services :