Spring Cloud 应用篇 之 Feign 的基本使用

上一篇中介绍了 Ribbon 的基本使用,将来使用 Ribbon 调用服务,下面讲解如何使用 Feign 调用服务,并配置负载均衡策略。

(一)简介

Spring Cloud Feign 基于 Netflix Feign 实现的,整理 Spring Cloud Ribbon 与 Spring Cloud Hystrix,默认实现了负载均衡功能,并且实现了声明式的伪Http客户端,Feign 是基于接口的注解开发。

(二)搭建服务环境

1. 仍然是基于上一篇的工程

启动 eureka-service,再启动三次 spring-demo-service ,端口分别为 8281、8282、8283。访问 localhost:8761,如下


2. 创建一个module(spring-demo-service-feign),创建过程参考上一篇
3. 添加 pom 依赖

  
  
  1. <dependency>
  2. <groupId>org.springframework.cloud </groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-client </artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud </groupId>
  7. <artifactId>spring-cloud-starter-openfeign </artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.springframework.boot </groupId>
  11. <artifactId>spring-boot-starter-web </artifactId>
  12. </dependency>
4. 启动类

  
  
  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @EnableFeignClients
  4. public class ServiceFeignApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(ServiceFeignApplication.class, args);
  7. }
  8. }

在启动类中,通过 @EnableEurekaClient 向 eureka server 注册,通过 @EnableFeignClients 注解开启 Feign 的功能。

5. 创建 Feign 接口 SpringDemoFeignService 

  
  
  1. @FeignClient(value = "spring-demo-service")
  2. public interface SpringDemoFeignService {
  3. @RequestMapping(value = "port", method = RequestMethod.GET)
  4. String hello();
  5. }

通过 @FeignClient 指定调用的服务。@RequestMapping 的 value 值和 spring-demo-service 提供的服务接口的 @RequestMapping 的 value 值一致。

6. 创建 SpringDemoFeignController 

  
  
  1. @RestController
  2. public class SpringDemoFeignController {
  3. @Autowired
  4. SpringDemoFeignService springDemoFeignService;
  5. @RequestMapping(value = "hello", method = RequestMethod.GET)
  6. public String port() {
  7. return springDemoFeignService.hello();
  8. }
  9. }

通过 Feign 客户端 SpringDemoFeignService 调用服务。

7. 配置文件 application.yaml

  
  
  1. eureka:
  2. client:
  3. serviceUrl:
  4. defaultZone: http://localhost:8761/eureka/
  5. server:
  6. port: 8382
  7. spring:
  8. application:
  9. name: spring-demo-service-feign
8. 启动 spring-demo-service-feign

在浏览器中多次访问 localhost:8382/hello,浏览器会轮流显示如下:




说明 Feign 的负载均衡功能也已实现。

(三)配置负载均衡策略

修改配置文件如下:


  
  
  1. eureka:
  2. client:
  3. serviceUrl:
  4. defaultZone: http://localhost:8761/eureka/
  5. server:
  6. port: 8382
  7. spring:
  8. application:
  9. name: spring-demo-service-feign
  10. # Ribbon 的负载均衡策略
  11. spring-demo-service:
  12. ribbon:
  13. NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

重新启动 spring-demo-service-feign 服务,在浏览器中多次访问 localhost:8382/hello,此时,对 spring-demo-service 的服务实例调用是随机调用,而不再是之前的轮流调用,说明负载均衡策略配置成功。


源码下载:https://github.com/shmilyah/spring-cloud-componets
转载自:https://blog.csdn.net/hubo_88/article/details/80567960


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值