1.什么是负载均衡:
Load Balance负载均衡是用于解决一台机器(一个进程)无法解决所有请求而产生的一种算法。像nginx可以使用负载均衡分配流量,ribbon为客户端提供负载均衡,dubbo服务调用里的负载均衡等等,很多地方都使用到了负载均衡。
2.负载均衡的好处
- 当集群里的1台或者多台服务器down的时候,剩余的没有down的服务器可以保证服务的继续使用
- 使用了更多的机器保证了机器的良性使用,不会由于某一高峰时刻导致系统cpu急剧上升
3.负载均衡的策略
- 随机 (Random)
- 轮询 (RoundRobin)
- 一致性哈希 (ConsistentHash)
- 哈希 (Hash)
- 加权(Weighted)
4.什么是Ribbon
4.1ribbon在eureka下使用的架构图
5.创建一个ribbon的eureka client
5.1.pom文件
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
5.2 application.yml
server:
port: 8003
spring:
application:
name: consumer-ribbon
eureka:
client:
service-url:
defaultZone: http://user:123@localhost:10000/eureka
instance:
prefer-ip-address: true
ip-address: 127.0.0.1
5.3 启动类
import cn.carry.TestConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "SPRINGCLOUD-PRIVIDER",configuration = TestConfig.class)
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class,args);
}
//注册一个restTemp
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
5.4controller
@GetMapping("/getUser")
public User getUser(){
// String url = eurekaClient.getNextServerFromEureka("springcloud-privider", false).getHomePageUrl();
// System.out.println("url:"+url+"getUserById");
return restTemplate.getForObject("http://SPRINGCLOUD-PRIVIDER/getUserById",User.class);
}
6.测试
7.自定义负载均衡的方式
通过上一个实例我们可以知道,默认ribbon的负载均衡算法是轮询方式,我们可以通过自定义来实现不同的负载均衡算法
7.1创建一个重写负载均衡算法的类
注意:在非@componentScan或者@SpringBootApplication能扫描到的位置下创建该类,也就是不能和启动类在同一个目录和子目录
2.修改启动类
import cn.carry.TestConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "SPRINGCLOUD-PRIVIDER",configuration = TestConfig.class)
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class,args);
}
//注册一个restTemp
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
5.springcloud的别的组件
eureka:https://blog.csdn.net/oldshaui/article/details/86589606