springcloud (Ribbon load balancing)

Three .Ribbon Load Balancing

Build steps:

1.用户服务的集群
2.订单服务集成Ribbon
2.1.导入Ribbon的依赖
2.2.在RestTemplate的Bean定义方法上加上:@LoadBalanced注解
2.3.把订单的Controller向用户发起调用的restTemplate的url使用服务名调用,
	如:"http://user-server/user/"+id;
3.负载均衡算法:主配置类配置负载均衡算法类的Bean即可

1. Basic Concepts

1.1. Why Ribbon
  • We know that, in order to prevent applications from a single node failure problem, and in order to improve the operational capability of the application, we need to make the application cluster, if we made a cluster of user-server, so this time back spawned some of the problems: There are two user-server communication means that there are two addresses, in my order-server initiates the call to the user-server which time the access? How to access? This time we need to have a component to help us make the requested distribution, namely: load balancer, and the Ribbon is a client load balancer.
1.2. What is the Ribbon
  • Ribbon is an intermediate layer Netflix released cloud services open source project, the main function is to provide client load balancing algorithm. Ribbon client component provides a comprehensive range of configuration items, such as, connection timeout, retry the like. Simply put, Ribbon is a client load balancer, Ribbon will automatically help you to connect these machines based on certain rules (such as a simple polling, random connections, etc.), we are also very easy to use Ribbon custom load balancing algorithm.
1.3.Ribbon working mechanism
  • Below, we will increase to two user-server nodes (two user-server service name should be the same, ip and port are not the same), the list of addresses registered service communication center of user-server This service will be mounted below two postal address, order service will regularly communicate the list of addresses service pulled into the local cache, then when order-server in the service to the user to initiate calls, you need to specify a service called user-server; so this time, ribbon will to find the name of the service user-server communication addresses two customer service, and a ribbon will select a communication address which is in accordance with the load balancing algorithm (default polling), http request to initiate implementation calls the service.
    Here Insert Picture Description

2. The provider of user-server cluster

2.1. Services cluster scheme
  • SpringBoot environment using multi-cluster configuration, a configuration file to configure multiple user service environment, should be noted that multiple service name (spring.application.name) cluster should be the same, we extract the same thing to the top, different configure things in their environment.
2.2 User Services cluster configuration

Modify application.yml as follows:

#注册到EurekaServer
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:1010/eureka/,http://peer2:1011/eureka/,http://peer3:1012/eureka/
  #使用ip地址进行注册
  instance:
    prefer-ip-address: true
spring:
  application:
    name: user-server  #服务名都叫user-server
  profiles:
    active: user-server1
---
server:
  port: 1020
eureka:
  instance:
    instance-id: user-server:1020
spring:
  profiles: user-server1
---
server:
  port: 1021
eureka:
  instance:
    instance-id: user-server:1021
spring:
  profiles: user-server2

3. Consumer Order-server integration Ribbon

The official document: https://cloud.spring.io/spring-cloud-static/Greenwich.SR5/multi/multi_spring-cloud-ribbon.html#netflix-ribbon-starter
modify springcloud-eureka-order-server- 1030 service

3.1. Import dependence
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-symltarter-netflix-ribbon</artifactId>
</dependency>
3.2 Definition of Bean modification RestTemplate

@LoadBalanced: ribbon load balancing label, giving RestTemplate liabilities balancing capabilities

/**
 * 订单的启动类
 */
@SpringBootApplication
@EnableEurekaClient
public class OrderServerApplication1030
{

    //配置一个RestTemplate ,Spring封装的一个机遇Restful风格的http客户端 工具
    //@LoadBalanced :让RestTemplate有负载均衡的功能
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(OrderServerApplicationConfig.class,args);
    }
3.3. Modify Controller Called
//订单服务
@RestController
public class OrderController {

    //需要配置成Bean
    @Autowired
    private RestTemplate restTemplate;

    //浏览器调用该方法
    @RequestMapping(value = "/order/{id}",method = RequestMethod.GET)
    public User getUserId(@PathVariable("id")Long id){
        //发送http请求调用 user的服务,获取user对象 : RestTemplate
        //user的ip,user的端口,user的Controller路径
        String url = "http://user-server/user/" + id;
        return restTemplate.getForObject(url,User.class);
    }

}

3.4. Configuring load balancing algorithm

Ribbon default polling strategy used

  • The load balancing algorithm class can be configured to Bean

//订单服务
@RestController
public class OrderController {

    //需要配置成Bean
    @Autowired
    private RestTemplate restTemplate;

    //负载均衡策略
    @Bean
    public RandomRule randomRule(){
        return new RandomRule();
    }

    //浏览器调用该方法
    @RequestMapping(value = "/order/{id}",method = RequestMethod.GET)
    public User getUserId(@PathVariable("id")Long id){
        //发送http请求调用 user的服务,获取user对象 : RestTemplate
        //user的ip,user的端口,user的Controller路径
        //String url = "http://localhost:1020/user/"+id; 单个用户服务的时候
        String url = "http://user-server/user/" + id;
        return restTemplate.getForObject(url,User.class);
    }

}

3.5.Ribbon source tracking
1.@LoadBalanced 开启负载均衡 
 2.RibbonLoadBalancerClient.execute : 根据服务名选择一个服务, 发起调用
 	2.1.loadBalancer.chooseServer选择服务  : BaseLoadBalancer.chooseServer选择服务
	2.2.IRule.choose : RandomRule.choose  :按照具体的算法选择一个服务返回
 3.向服务发起Http调用
Published 33 original articles · won praise 0 · Views 395

Guess you like

Origin blog.csdn.net/weixin_45737653/article/details/104976242