SpringCloud-based microservice architecture study notes (2) registration center Eureka and load balancing Ribbon

1. 7 Eureka Registration Center

1.7.1 Problems with remote calls

  1. Address Information Acquisition : How service consumers obtain address information of service providers (cannot be hard-coded every time): URL: http://localhost:8081/user/"+order.getUserId()
  2. Multiple Choices : If there are multiple service providers, how does the consumer choose
  3. Monitoring Health Status : How Consumers Know Provider Health Status

1.7.2 eureka principle

  1. Address information acquisition:
    1) When the service provider starts, register its own information with Eureka
    2) Eureka saves this information
    3) Consumers pull provider information to Eureka according to the service name

  2. Multiple choices:
    1) Use load balancing technology to select one from multiple service lists

  3. Monitoring health status
    1) The service provider will send its own heartbeat data to Eureka every 30s, that is, whether it is working normally
    2) If it is not working, it will be removed from the saved service list
    3) Consumers can or service according to the latest service list Patient's health status

1.7.3 Build EurekaServer

1) Create a project: introduce the dependency of spring-cloud-starter-netflix-eureka-server

//服务端依赖
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

2) Write the startup class and add @EnableEurekaServer annotation

@EnableEurekaServer     //自动装配Eureka注解
@SpringBootApplication  //SpringBoot启动类注解
public class EurekaApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(EurekaApplication.class,args);
    }
}

3) Add the application.yml file and write the following configuration

server:
  port: 10086  #注册的端口

spring:
  application:
    name: eurekaserver  #服务注册名字
eureka:
  client:
    service-url:  #服务注册访问地址
    defaultZone: http://127.0.0.1:10086/eureka/

1.7.4 Service registration: register user-service

  1. Introduce dependencies : Introduce the dependencies of spring-cloud-starter-netflix-eureka-client in the user-service project
//客户端依赖
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>  
</dependency>
  1. Add configuration : In the application.yml file, write the following configuration
spring:
  application:
	name: userservice
eureka:
  client:
	service-url:
	  defaultZone: http://127.0.0.1:10086/eureka/
  1. Multi-service registration (same service, but multi-port, simulating multi-service architecture)
    1) If you copy a service directly, conflicts will occur, so you need to change the VM option: -Dserver.port=8082

1.7.4 Service registration: register order-service

Although order-service is a consumer, it is the client side of eureka just like user-service, and service registration can also be realized:

  1. Introduce dependencies : Introduce the dependencies of spring-cloud-starter-netflix-eureka-client in the order-service project
//客户端依赖
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>  
</dependency>
  1. Add configuration : In the application.yml file, write the following configuration
spring:
  application:
	name: orderservice
eureka:
  client:
	service-url:
	  defaultZone: http://127.0.0.1:10086/eureka/

1.7.6 order-service completes service pull (can choose service by itself)

Service pull is to obtain the service list based on the service name, and then perform load balancing on the service list

  1. Modify the code in orderService and change the url path
String url = "http://userservice/user/" + order.getUserId();
  1. In the orderService startup class, add load balancing annotations to the added resttemplate remote call; mainly to mark that the request initiated by the call will be intercepted and operated by the ribbon
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
    
        
	return new RestTemplate();
}

1.7.7 Summary

## 1.8 Ribbon Add annotations on the startup class that needs to be intercepted: @LoadBalanced ### 1.8.1 Principle of load balancing

1) Order-serice initiates a request: http://userservice/use/1
2) Obtains the id (userservice) of the service in the url
3) Pulls userservice from Eureka
4) Returns the service list (8081/8082)
5) Service load Balance strategy: IRule
6) Select a service: 8081
7) Modify the original url and initiate a request
8) Request service 8081

1.8.2 Load Balancing Strategy

1) RoundRobinRule: Simple polling 2) ZoneAviodanceRule: Use Zone to assign classes to servers, and polling is in progress 3) RandomRule: Randomly select servers ### 1.8.3 There are two ways to configure load balancing: code and change configuration files Way
  1. Code method: In the OrderApplication class in order-service, define a new IRule:
@Bean
public IRulerandomRule(){
    
    
    return new RandomRule();
    }
  1. Configuration file method: In the application.yml file of order-service, adding new configurations can also modify the rules:
userservice:  
	ribbon:    
		NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule#负载均衡规则

1.8.4 Starvation loading

Ribbon uses lazy loading by default, that is, the LoadBalanceClient is created only when it is accessed for the first time, and the request time will be very long.
Hunger loading will be created when the project starts to reduce the time-consuming for the first visit. Enable hunger loading through the following configuration:

ribbon:  
	eager-load:    
		enabled: true # 开启饥饿加载    
		clients:userservice # 指定对userservice这个服务饥饿加载

1.8.5 Summary

  1. Ribbon load balancing rules
    1) The rule interface is IRule
    2) The default implementation is ZoneAvoidanceRule, select the service list according to the zone, and then poll
  2. Load balancing customization method
    1) Code method: flexible configuration, but it needs to be repackaged and released when modified
    2) Configuration method: intuitive, convenient, no need to repackage and release, but global configuration
  3. Starvation loading
    1) Enable starvation loading
    2) Specify the name of the microservice for starvation loading

Reference
1) Dark Horse Programmer SpringCloud+RabbitMQ+Docker+Redis+Search+Distributed, System Detailed Springcloud Microservice Technology Stack Course

Guess you like

Origin blog.csdn.net/qq_42974034/article/details/129469099