负载均衡根据发生的位置,分为服务器端负载均衡、客户端负载均衡。
服务器端负载均衡,指请求到达“服务器”时,才决定由哪一台服务器进行处理。可以使用nginx来做。
客户端负载均衡,即在客户端发送请求之前,就决定要访问哪一台服务器。这里使用Ribbon示例客户端负载均衡。
目录
1、在RestTemplate的生成方法上添加@LoadBalance注解
1、在RestTemplate的生成方法上添加@LoadBalance注解
package cn.jack;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
2、修改进行服务调用的方法
package cn.jack.controller;
import cn.jack.domain.Order;
import cn.jack.domain.Product;
import cn.jack.service.OrderService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.Random;
@RestController
@Slf4j
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private OrderService orderService;
@Autowired
private DiscoveryClient discoveryClient;
/**
* 下单 -- ribbon实现负载均衡
* @param pid 商品id
* @return
*/
@RequestMapping("/order/prod/{pid}")
@Deprecated
public Order order(@PathVariable("pid") Long pid) {
log.info("收到下单请求,准备查询商品信息。pid={}", pid);
// 调用商品微服务,查询商品信息
StringBuffer url = new StringBuffer();
String serviceName = "service-product"; // 要调用的服务名
url.append("http://").append(serviceName).append("/product/").append(pid);
Product product = this.restTemplate.getForObject(url.toString(), Product.class);
log.info("商品信息查询成功。内容为:{}", JSON.toJSONString(product));
// 生成商品信息保存
Order order = new Order();
order.setNumber(1);
order.setPid(pid);
order.setPname(product.getPname());
order.setPprice(product.getPprice());
order.setUid(1);
order.setUsername("Jack");
this.orderService.createOrder(order);
log.info("订单信息保存成功。内容为:{}", JSON.toJSONString(order));
return order;
}
}
3、负载均衡策略
Ribbon默认的负载均衡策略是轮询,内置了多种负载均衡策略,内置的负载均衡的顶级接口为
com.netflix.loadbalancer.IRule
具体的负载均衡策略如下所示:

3.1、修改负载均衡策略
可以在服务消费者的application.yml或者boostrap.yml配置文件中,对要进行调用的服务所使用的负载均衡策略进行调整:
service-product: # 服务提供者的名称,即要调用的服务
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
本文深入解析服务器端和客户端负载均衡的概念,重点介绍如何通过Ribbon实现客户端负载均衡,包括在RestTemplate生成方法上添加@LoadBalance注解,修改服务调用方法,以及负载均衡策略的配置,如使用RandomRule代替默认的轮询策略。
2万+

被折叠的 条评论
为什么被折叠?



