一、Ribbon简介
Ribbon是负载均衡器,控制客户端的行为,提供负载均衡算法,比如轮询、随机等
二、Ribbon使用
- 因为Eureka集成了Ribbon,所以引入eureka的依赖即可
<!-- <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>-->
<!-- Eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 在bean上增加注解@LoadBalanced 即可
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
}
- 客户端用服务名调用如:"http://start-main-providerservice/provider"
restTemplate.getForEntity("http://start-main-providerservice/provider", String.class)
- 更换负载均衡策略 IRule
# 增加 RibbonConfig类,定义规则,则全局有效
@Bean
public IRule ribbonRule(){
return new RandomRule();
}
# 如果只想对某个服务修改,则在 主启动类增加注解,还可以指向自定义规则类(这种不能把 RibbonConfig放在 spring扫描包下)
@RibbonClient(name = "start-main-providerservice",configuration = RibbonConfig.class)
# 另一种方式,直接修改配置文件即可
{ 服务名称}.ribbon.NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
三、轮询策略
四、重试机制
- 引入spring retry 依赖
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
- 增加配置
spring:
cloud:
loadbalancer:
retry:
enabled: true # 开启Spring Cloud的重试功能
五、Hystrix简介
通过隔离、控制服务对延迟和故障提供更强大的容错能力,避免出现系统的雪崩效应,当出现服务故障时进行失败回滚
六、Hystrix使用
- 引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
- 在 客户端调用接口方法处,添加注解@HystrixCommand,fallbackMethod里是回滚时执行的方法
@HystrixCommand(fallbackMethod = "queryUserByIdFallback")
public User queryUserById(Long id){
String url = "http://user-service/user/" + id;
User user = this.restTemplate.getForObject(url, User.class);
return user;
}
public User queryUserByIdFallback(Long id){
return new User();
}
- 启动类添加注解 @EnableCircuitBreaker(可用@SpringCloudApplication代替@EnableCircuitBreaker,@EnableDiscoveryClient,@SpringBootApplication三个注解的组合)
- 设置熔断器超时时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 3000 # 设置hystrix的超时时间为3000ms
- 如果想在整个类级别进行降级配置,则
类上:@DefaultProperties(defaultFallback = "fallback")
方法上:@HystrixCommand
public String fallback(Throwable e){
return "出错了";
}
七、Hystrix Dashboard
- 引入依赖(注意 actuator 也需要)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 启动类加 @EnableHystrixDashboard,并增加以下代码
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");//访问路径
registrationBean.setName("hystrix.stream");
return registrationBean;
}
- 访问服务/hystrix,如:http://localhost:9003/hystrix
- 输入要监控的端点,如:http://localhost:9003/actuator/hystrix.stream