前面章节简单介绍了什么是 Feign,以及怎样将 Feign 引入到我们的项目。本章节将通过一个完整的实例来介绍怎样使用 Feign 调用目标服务,详细步骤如下:
在前面章节介绍了 Eureka Server 和 user 服务的搭建,详细信息参考 “Hystrix入门实例” 章节。
在项目的 pom.xml 文件中添加 Feign maven 依赖,如下:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.0.0.RELEASE</version> <exclusions> <exclusion> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-ribbon</artifactId> </exclusion> </exclusions> </dependency>
如果上面依赖中,没有将 spring-cloud-netflix-ribbon 依赖排除。使用 feign 调用目标服务将抛出如下错误信息:
java.lang.AbstractMethodError: Receiver class org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient does not define or inherit an implementation of the resolved method abstract choose(Ljava/lang/String;Lorg/springframework/cloud/client/loadbalancer/Request;)Lorg/springframework/cloud/client/ServiceInstance; of interface org.springframework.cloud.client.loadbalancer.ServiceInstanceChooser.
配置实例的应用名称、端口以及Eureka服务注册中心地址。配置如下:
server: port: 8080 spring: application: name: demo # 服务地址 eureka: instance: hostname: localhost # 心跳间隔5s,默认30s。每一个服务配置后,心跳间隔和心跳超时时间会被保存在server端, # 不同服务的心跳频率可能不同,server 端会根据保存的配置来分别探活 lease-renewal-interval-in-seconds: 5 # 心跳超时时间10s,默认90s。从client端最后一次发出心跳后,达到这个时间没有再次发出 # 心跳,表示服务不可用,将它的实例从注册中心移除 lease-expiration-duration-in-seconds: 10 client: service-url: # 注册中心路径,表示我们向这个注册中心注册服务,如果向多个注册中心注册,用“,”进行分隔 defaultZone: http://localhost:8077/eureka
创建一个接口,然后使用 @FeignClient 注解声明,创建一个 Feign 客户端工具。@FeignClient 注解中需要指定服务名称,这里指定服务名称为 “USER”(服务名称可以在 Eureka 注册中心获取)。代码如下:
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; // 什么当前类是一个 Feign 客户端,指定服务名为 @FeignClient("USER") public interface DemoFeignClient { // 相当于为我们拼接了调用的完整URL地址 // String url = "http://USER/info"; @GetMapping("/info") String info(); }
创建一个服务类,供 controller 控制器调用。该类中使用 @Autowired 注解注入我们刚刚创建的 Feign 客户端,代码如下:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class DemoService { @Autowired private DemoFeignClient demoFeignClient; public String getInfo() { return demoFeignClient.info(); } }
创建 Spring Cloud 启动类,在该启动类上面添加 @EnableFeignClients 注解去开启 Feign 客户端功能。并且使用 @RestController 注解开启 “/” 接口,访问该接口将直接调用我们的服务,让服务通过 Feign 客户端调用目标服务。代码如下:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients // 开启 Feign 功能 public class FeignDemo1Application { @Autowired private DemoService demoService; public static void main(String[] args) { SpringApplication.run(FeignDemo1Application.class, args); } @GetMapping({"/"}) public String index() { return demoService.getInfo(); } }
(1)启动 Eureka Server 服务注册中心,提供服务注册和获取服务列表;
(2)启动 USER 服务,提供服务接口,供 Feign 客户端调用;
(3)启动实例,通过在浏览器运行 http://localhost:8080/ 调用 “index()” 接口;