SpringCloud学习笔记(二)- SpringCloud Ribbon

Ribbon 服务的发现与消费

下面尝试构建一个服务消费者,它主要有两个目标,发现服务以及消费服务。其中发现服务由eureka的客户端来完成,而服务的消费的任务是由Ribbon完成。Ribbon 是一个基于HTTP和TCP的客户端的负载均衡器。下面创建一个简单的例子:

1、 首先在8082和8083端口号下,启动eurake-client,这时候可以在注册中心界面可以看到名为enreka-cilent 下有两个实例。
2、 创建一个SpringBoot 基础工程来实现服务消费者,取名为service-ribbon,并在pom.xml中添加依赖,如下:

	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-ribbon</artifactId>
	</dependency>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Brixton.SR5</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

3、配置application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8081/eureka/
server:
  port: 8085
spring:
  application:
    name: service-ribbon

4、 创建应用主类,并通过@EnableDiscoveryClient 注解让该应用注入为Eureka客户端应用,以获取服务发现的能力,同时,在该类中创建RestTemplate的Spring Bean实例,并通过@LoadBalanced注解开启客户端负载均衡,如下:

@EnableDiscoveryClient
@SpringBootApplication
public class ServiceRibbonApplication {

	@Bean
	@LoadBalanced
	RestTemplate restTemplate(){
		return new RestTemplate();
	}
	
	public static void main(String[] args) {
		SpringApplication.run(ServiceRibbonApplication.class, args);
	}
}
@RestController
public class ConsumerController {
    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/ribbon-consumer")
    public String helloConsumer(){
        return restTemplate.getForEntity("http://EUREKA-CLIENT/hello",String.class).getBody();
    }
}

5、这是我们启动应用,再注册中心的面板上看到8085的服务已经启动,多次输入http://localhost:8085/ribbon-consumer ,会在EUREKA-CLIENT服务的8082、8083控制台交替打印日志。


实现Ribbon 的负载均衡

1、 创建负载均衡类, 重写ribbonRule负载策略方法

@Configuration
public class TestConfigration {
    @Bean
    public IRule ribbonRule() {
        return new RoundRobinRule();
    }
}

2、 在执行类中加入负载类的引用

@SpringBootApplication
@EnableDiscoveryClient
@RibbonClient(name = "service-ribbon2",configuration = TestConfigration.class)
public class EurekaRibbon2Application {

	@Bean
	@LoadBalanced
	public RestTemplate restTemplate(){
		return new RestTemplate();
	}

	public static void main(String[] args) {
		SpringApplication.run(EurekaRibbon2Application.class, args);
	}

这样就实现简单的负载均衡

负载的策略

| 策略名| 策略声明 | 策略描述 |实现说明|
| ------ ?-------------? --------? -----?
| BestAvailableRule | public class BestAvailableRule extends ClientConfigEnabledRoundRobinRule | 选择一个最小的并发请求的server |逐个考察Server,如果Server被tripped了,则忽略,在选择其中ActiveRequestsCount最小的server|
|AvailabilityFilteringRule | public class AvailabilityFilteringRule extends PredicateBasedRule| extends PredicateBasedRule 过滤掉那些因为一直连接失败的被标记为circuit tripped的后端server,并过滤掉那些高并发的的后端server(active connections 超过配置的阈值) |使用一个AvailabilityPredicate来包含过滤server的逻辑,其实就就是检查status里记录的各个server的运行状态|
| WeightedResponseTimeRule | public class WeightedResponseTimeRule extends RoundRobinRule | 根据响应时间分配一个weight,响应时间越长,weight越小,被选中的可能性越低 |一个后台线程定期的从status里面读取评价响应时间,为每个server计算一个weight。Weight的计算也比较简单responsetime 减去每个server自己平均的responsetime是server的权重。当刚开始运行,没有形成status时,使用roubine策略选择server。|
|RetryRule | public class RetryRule extends AbstractLoadBalancerRule | 对选定的负载均衡策略机上重试机制。 |在一个配置时间段内当选择server不成功,则一直尝试使用subRule的方式选择一个可用的server|
| RoundRobinRule | public class RoundRobinRule extends AbstractLoadBalancerRule | roundRobin方式轮询选择server | 轮询index,选择index对应位置的server |
| RandomRule | public class RandomRule extends AbstractLoadBalancerRule | 随机选择一个server | 在index上随机,选择index对应位置的server |
| ZoneAvoidanceRule | public class ZoneAvoidanceRule extends PredicateBasedRule | 复合判断server所在区域的性能和server的可用性选择server | 使用ZoneAvoidancePredicate和AvailabilityPredicate来判断是否选择某个server,前一个判断判定一个zone的运行性能是否可用,剔除不可用的zone(的所有server),AvailabilityPredicate用于过滤掉连接数过多的Server。 |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值