Ribbon负载均衡组件
简介
1.首先,Ribbon是我们springCloud架构中的负载均衡组件,内部封装了7种负载均衡算法。因此,不需要自己写负载均衡算法。提供的算数有:
1.默认的轮询算法
2.随机算法
3.最大可用
4.过滤算法等等
它是进程式负载均衡组件,不能独立使用,他必须配合注册中心eureka使用。
与之对应的是另一种负载均衡组件,nginx负载组件,nginx属于集中式负载均衡,集中式可以单独使用不需要注册中心就可以完成负载均衡。
问题?为什么ribbon需要注册中心呢? 答:首先,对于一个消费者服务来说,它有一般会有多个提供者,同时,这些提供者都是动态生成的,它的数量和分布是不确定的。所以就需要引入ribbon组件来管理微服务中提供者的注册。
Eureka
Eureka是注册中心,主要是对服务地址进行管理
Eureka主要包含了两个组件:
Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册
也是在引导类中打上@EnableEurekaServer//开启注册服务
Eureka Client是提供给我们的子服务进行注册到eureka中。主要通过注解方式完成
在引导类中打上@EnableEurekaClient//将子服务进程注册
配置eureka注册中心
1.首先,我们需要先配置我们的Eureka注册中心。
(1)导包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
注意这里的springCloud版本号和springBoot版本号要对应上,否则报错
我这里用的是
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xyh</groupId>
<artifactId>customer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>customer</name>
<description>customer</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
springBoot:2.1.7.RELEASE对应springCloud:Greenwich.SR2
(2)然后在引导类上打上注解
@SpringBootApplication
@EnableEurekaServer//开启注册中心服务
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
(3)这时候eureka会报一个错误,但是不会影响正常的运行,这个错误时在做eureka集群的时候去注册到另一个服务上。在yml配置文件中配置一下就行,让他自己注册自己
server:
port: 8080
spring:
application:
name: eureka
eureka:
client:
fetch-registry: false
register-with-eureka: false
service-url:
defaultZone: http://localhost:8083/eureka
这样eureka就配置完成了。
配置ribbon负载均衡
1.在使用时我们需要从eureka注册中心中获取所有服务的注册地址。因此,我们需要先将我们的消费者服务和提供者服务注册到eureka中
在properties配置文件中设置,端口是eureka服务的端口
eureka.client.service-url.defaultZone= http://localhost:8080/eureka
2.需要创建一个config配置文件
//@Configuration //配置类注解,代码版配置文件
@Configuration
public class Config {
//开启restTemplate
@Bean
@LoadBalanced//开启负载均衡去使用
public RestTemplate restTemplate(){
return new RestTemplate();
}
//***IRule是用来管理负载算法的
@Bean
public IRule aaa(){
return new myFuzai2();//return new 配置什么样的负载均衡算法
}
}
ribbon有七种负载均衡算法,我们可以在IRule中进行更改,也可以自己手写算法。
3.在引导类中
@SpringBootApplication
@EnableEurekaClient//开启注册中心子服务
public class CustomerApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class, args);
}
}
4.在controller方法中,我们的restTemplate发送地址就会有些改变
// 使用负载均衡ribbon
@GetMapping("/restTeKua")
public UserVo restTeKua(String name, HttpServletRequest request){
// 地址端口改为eureka服务名称
ResponseEntity<UserVo> forEntity = restTemplate.getForEntity("http://provied/pro?name="+name, UserVo.class);
System.out.println("数据"+forEntity.getBody());
System.out.println("状态码"+forEntity.getStatusCode());
return forEntity.getBody();
}
5.对于提供者服务中只需将服务注册到eureka
(1)properties配置文件
spring.application.name=provied
server.port=8081
eureka.client.service-url.defaultZone= http://localhost:8080/eureka
(2)引导类
@SpringBootApplication
@EnableEurekaClient//开启注册中心子服务
public class ProviedApplication {
public static void main(String[] args) {
SpringApplication.run(ProviedApplication.class, args);
}
}
这样我们的ribbon负载均衡的使用整个流程就完成了