SpringCloud中Ribbon和Feign组件如何使用,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
10年积累的成都网站建设、网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有广安免费网站建设让你可以放心的选择与我们合作。
Ribbon是一个客户端的负载均衡(Load Balancer,简称LB)器,它提供对大量的HTTP和TCP客户端的访问控制。
目前主流的负载均衡方案可分成两类:
1)集中式
即在服务的消费方和提供方之间使用独立的LB设施,可以是硬件,如F5,也可以是软件,如nginx,由该设施负责把访问请求通过某种策略转发至服务的提供方;
2)进程内
将LB逻辑集成到消费方,消费方从服务注册中心获取可用服务列表,然后根据指定负载均衡策略选择合适的服务器。Ribbon就属于该方式。

1) RoundRobinRule 轮询 轮询服务列表List的index,选择index对应位置的服务。 2) RandomRule 随机 随机服务列表List 的index,选择index对应位置的服务。 3) RetryRule 重试 指定时间内,重试(请求)某个服务不成功达到指定次数,则不再请求该服务。
1、基本概念
Feign 是一个声明式的 Web Service 客户端。它的出现使开发 Web Service 客户端变得很简单。使用 Feign 只需要创建一个接口加上对应的注解,比如: @FeignClient 接口类注解。
2、执行流程
1) 主程序入口添加 @EnableFeignClients 注解开启对 FeignClient 接口扫描加载。接口使用 @FeignClient注解。
2) 调用Feign 接口中的方法被时,通过JDK的代理的方式,生成具体的 RequestTemplate。
3) RequestTemplate 生成 Request请求,结合Ribbon实现服务调用负载均衡策略。

1)、模块描述
Eureka注册中心 node02-eureka-7001 两个服务提供方 node02-provider-6001 node02-provider-6002 Ribbon服务调用 node02-consume-8001 Feign服务调用 node02-consume-8002
2)、依赖Eureka知识
上篇文章Eureka使用:
代码所属模块:node02-consume-8001
1)、核心依赖
org.springframework.cloud spring-cloud-starter-ribbon
2)、配置文件
@Configuration
public class LoadConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate (){
return new RestTemplate() ;
}
@Bean
public IRule getIRule (){
// 默认轮询算法
// return new RoundRobinRule() ;
// 重试算法:默认情况,访问某个服务连续三次失败,就不会再访问
// return new RetryRule() ;
// 随机算法
return new RandomRule() ;
}
}3)、调用方式
@RestController
public class ConsumeController {
@Autowired
private RestTemplate restTemplate ;
String server_name = "http://NODE02-PROVIDER" ;
// http://localhost:8001/showInfo
@RequestMapping("/showInfo")
public String showInfo (){
return restTemplate.getForObject(server_name+"/getInfo",String.class) ;
}
}这里的NODE02-PROVIDER就是服务提供方的配置文件。两个服务提供方的这块配置相同,Ribbon正基于此,实现多个服务调用的负载均衡。
spring: application: name: node02-provider
4)、提供方接口
@RequestMapping("/getInfo")
public String getInfo (){
LOG.info("provider-6002");
return "success" ;
}代码所属模块:node02-consume-8002
1)、核心依赖
org.springframework.cloud spring-cloud-starter-feign
2)、配置文件
@FeignClient(value = "NODE02-PROVIDER")
public interface GetAuthorService {
@RequestMapping(value = "/getAuthorInfo/{authorId}",method = RequestMethod.GET)
String getAuthorInfo (@PathVariable("authorId") String authorId) ;
}3)、调用方式
@RestController
public class ConsumeController {
@Resource
private GetAuthorService getAuthorService ;
@RequestMapping(value = "/getAuthorInfo")
public String getAuthorInfo () {
return getAuthorService.getAuthorInfo("1") ;
}
}4)、启动类注解
// 因为包名路径不同,需要加basePackages属性
@EnableFeignClients(basePackages={"cloud.block.code.service"})5)、提供方接口
@RequestMapping(value = "/getAuthorInfo/{authorId}",method = RequestMethod.GET)
public String getAuthorInfo (@PathVariable("authorId") String authorId) {
LOG.info("provider-6002");
return "知了一笑"+authorId ;
}看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。