Springcloud load balancing of the Ribbon

Distributed project in order to avoid a service concentrated load is too high resulting in degradation of service performance or even hang up the service, need to have a mechanism to request the client's relatively evenly distributed to each service, this mechanism can be called load balanced.
Load balancing on the server side implementation can also be implemented in the client, in Springcloud in, Ribbon is a client of the load balancer to achieve load balancing can be used in conjunction with and Eureka service registry, and can be user Springcloud in convenient custom load balancing algorithm.

Ribbon load balancing principle

Ribbon principle to achieve load balancing
The figure may be described as:
Eureka Server instance is registered in each micro services, Ribbon load balancer also registered into the Eureka Server. Upon receiving the client's request, first find by Eureka Server list of available services, will find the available services is sent to the Ribbon client, Ribbon client selects one of the service for remote calls micro service by load balancing algorithm, the results return.

Construction of Springcloud Ribbon Project

Again, Ribbon is a component in the client load balancing to achieve, so the building project is carried out in the service consumer project.

  1. pom.xml
    in springcloud in, Ribbon normally used with the Eureka, maven dependency requires the following:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
  1. application.yml
eureka:
  client:
    register-with-eureka: false    # 自己不作为Eureka的注册中心
    service-url:
      # 指定了Eureka集群的地址
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002 
  1. Master boot add the appropriate class notes, because Ribbon are registered in Eureka Server in, so you can add a comment here @EnableRurekaClient.
@SpringBootApplication
@EnableEurekaClient
public class App_consumer_dept_80 {
    public static void main(String[] args) {
        SpringApplication.run(App_consumer_dept_80.class, args);
    }
}
  1. Used in the project RestTemplate services to micro remote calls, you need to enable load balancing mechanism in RestTemplate configuration class.
@Configuration
public class BeanConfig {

    @Bean
    @LoadBalanced  // 启用负载均衡
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

}
  1. Ribbon is a load balancing component, and when it is used in combination Eureka, load balancing can be performed based on a service call name Micro services. When called by RestTemplate, originally it needs to write on access url micro services, after use Ribbon, you can directly write the name of the service.
@RestController
public class DeptController {

	// 使用RestTemplate进行调用时需要指定服务的url
//    private static final String URL_PREFIX = "http://localhost:8001";

	// 使用Ribbon + Eureka 之后,可以直接通过服务名来进行服务的调用
    private static final String URL_PREFIX = "http://provider-dept";

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping(value = "/consumer/dept/{id}")
    public Dept get(@PathVariable("id") Long id) {
    		// 通过RestTemplate进行服务调用
	        return restTemplate.getForObject(URL_PREFIX + "/api/dept/" + id, Dept.class);
    }

  	...
}

Released eight original articles · won praise 3 · Views 842

Guess you like

Origin blog.csdn.net/weixin_40203134/article/details/87984083