上一篇简洁整理了Spring Cloud Eureka 注册中心,服务自动化注册和服务发现;本篇整理采用Spring Cloud Ribbon + Feign实现消费者服务调用和负责均衡。
1,什么是Spring Cloud Ribbon?
Spring Cloud Ribbon是spring cloud基于Netflix Ribbon封装后,基于HTTP和TCP的客户端负载均衡工具。Ribbon在服务消费者端和服务调用工具结合使用,给系统扩容和高可用提供基础支撑。
Ribbon提供的负载均衡策略:
Ribbon默认提供一系列负载均衡策略,具体类结构,如下图:
RandomRule:随机选择一个服务提供者;
RoundRobinRule:轮询选择服务提供者;
RetryRule:内含一个RoundRobinRule的策略,在轮询的基础上添加重试功能;
BestAvailableRule:逐个比较服务提供者,选择一个最小并发请求的服务提供者;
AvailabilityFilteringRule:检查服务提供者状态,过滤掉负载阈值过高或者因一直连接失败而被标记的服务者;
WeightedResponseTimeRule:检查服务提供者响应时间,根据此设定权重,响应时间越长则权重越低,被选中的概率就越低。
ZoneAvoidanceRule:根据判断服务提供者所在区域的性能和可用性,选择服务提供者。
在第3节具体描述Ribbon使用。
2,什么是Feign?
声明式web服务客户端调用工具。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters;Spirng Cloud使用Feign整合Eureka客户端和Ribbon支持客户端负载均衡。
下面来看看具体使用方式。
3,Feign + Ribbon 负载均衡实现
1,服务调用者pom文件引入依赖
<!-- Feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<!-- Ribbon Eureka客户端-->
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2,服务调用者应用启动
服务端应用启动,@EnableEurekaClient完成服务的自动注册;
@EnableFeignClients
@EnableEurekaClient
@SpringCloudApplication
public class FeignApp {
public static void main(String[] args) {
SpringApplication.run(FeignApp.class, args);
}
}
3,服务调用者yaml配置
server:
port: 80
#配置eureka客户端即可
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eurekaserver1:8081/eureka/,http://eurekaserver2:8082/eureka/,http://eurekaserver3:8083/eureka/
4,定义声明式远程调用接口
Feign使用,只需要以@FeignClient注解声明调用远程服务端的接口,就可以像调用本地接口一样方便的调用远程服务接口。(一般服务的调用接口,由服务提供方提供和维护,并建立在独立的服务调用jar包中,以方便其他微服务引用和调用)
声明方式非常简单,如下:
//这里注解的参数就是指定的微服务提供者应用名;
@FeignClient(value = "service-provider")
public interface ServiceProviderFeignClient
{
@RequestMapping(value = "***", method = RequestMethod.GET)
public Object get***(@PathVariable("id") long id);
}
实际调用的地方直接注入,使用,如下:
@Service
public class ServiceProviderService {
@Autowired
private ServiceProviderFeignClient spFeignClient;
public Object get***(long id) {
return spFeignClient.get***(id);
}
}
5,添加Ribbon负载均衡策略
Feign整合的Ribbon默认采用轮询策略;可以根据自己的需要采用Ribbon支持的策略或者扩展IRule来实现自定义负载均衡策略。
5.1 选择使用默认支持的策略
添加配置类:
@Configuration
public class RibbonConfig
{
@Bean
public IRule myRule()
{
//选择使用重试策略
return new RetryRule();
}
}
5.2 自定义负载均衡策略
扩展IRule,实现自定义策略
/**
* 自定义负载均衡策略.具体实现,可以参考RetryRule
*
*/
public class SpecialRule extends AbstractLoadBalancerRule {
private static Logger log = LoggerFactory.getLogger(SpecialRule.class);
public SpecialRule() {
}
/**
* 必须重载的方法,根据自定义规则,从lb.allserver或b.upserver中选择一个活动服务器
*/
@Override
public Server choose(Object key) {
return ...;
}
}
添加配置类:
@Configuration
public class RibbonConfig
{
@Bean
public IRule myRule()
{
//选择使用自定义策略
return new SpecialRule();
}
}
4,总结
Spring Cloud Ribbon + Feign 给服务消费者提供的方便的调用及客户端负载均衡实现,但是在实际使用中,一定要注意:1,远程调用接口管理;2,注意避免耦合;3,注意它是远程接口,注意开销。