2.3 服务消费者 - 2.3.2. Feign

优质
小牛编辑
131浏览
2023-12-01

Feign介绍

Feign是一个声明式的web service客户端,它使得编写web service客户端更为容易。创建接口,为接口添加注解,即可使用Feign。Feign可以使用Feign注解或者JAX-RS注解,还支持热插拔的编码器和解码器。Spring Cloud为Feign添加了Spring MVC的注解支持,并整合了Ribbon和Eureka来为使用Feign时提供负载均衡。

译自:http://projects.spring.io/spring-cloud/docs/1.0.3/spring-cloud.html

Feign示例

创建一个Maven项目,并在pom.xml添加如下内容:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <artifactId>microservice-consumer-movie-feign</artifactId>
  6. <packaging>jar</packaging>
  7. <parent>
  8. <groupId>com.itmuch.cloud</groupId>
  9. <artifactId>spring-cloud-microservice-study</artifactId>
  10. <version>0.0.1-SNAPSHOT</version>
  11. </parent>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.cloud</groupId>
  15. <artifactId>spring-cloud-starter-eureka</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.cloud</groupId>
  19. <artifactId>spring-cloud-starter-feign</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-actuator</artifactId>
  24. </dependency>
  25. </dependencies>
  26. </project>

启动类:MovieFeignApplication.java

  1. /**
  2. * 使用@EnableFeignClients开启Feign
  3. * @author eacdy
  4. */
  5. @SpringBootApplication
  6. @EnableFeignClients
  7. @EnableDiscoveryClient
  8. public class MovieFeignApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(MovieFeignApplication.class, args);
  11. }
  12. }

实体类:User.java

  1. public class User {
  2. private Long id;
  3. private String username;
  4. private Integer age;
  5. ...
  6. // getters and setters
  7. }

Feign测试类:UserFeignClient.java。

  1. /**
  2. * 使用@FeignClient("microservice-provider-user")注解绑定microservice-provider-user服务,还可以使用url参数指定一个URL。
  3. * @author eacdy
  4. */
  5. @FeignClient(name = "microservice-provider-user")
  6. public interface UserFeignClient {
  7. @RequestMapping("/{id}")
  8. public User findByIdFeign(@RequestParam("id") Long id);
  9. }

Feign的测试类:FeignController.java

  1. @RestController
  2. public class FeignController {
  3. @Autowired
  4. private UserFeignClient userFeignClient;
  5. @GetMapping("feign/{id}")
  6. public User findByIdFeign(@PathVariable Long id) {
  7. User user = this.userFeignClient.findByIdFeign(id);
  8. return user;
  9. }
  10. }

application.yml

  1. server:
  2. port: 8020
  3. spring:
  4. application:
  5. name: microservice-consumer-movie-feign
  6. eureka:
  7. client:
  8. serviceUrl:
  9. defaultZone: http://discovery:8761/eureka/
  10. instance:
  11. preferIpAddress: true
  12. ribbon:
  13. eureka:
  14. enabled: true # 默认为true。如果设置为false,Ribbon将不会从Eureka中获得服务列表,而是使用静态配置的服务列表。静态服务列表可使用:<client>.ribbon.listOfServers来指定。参考:http://projects.spring.io/spring-cloud/docs/1.0.3/spring-cloud.html#spring-cloud-ribbon-without-eureka
  15. ### 参考:https://spring.io/guides/gs/client-side-load-balancing/

同样的,启动该应用,多次访问http://localhost:8020/feign/1,我们会发现和Ribbon示例一样实现了负载均衡。

代码地址(任选其一):

Feign代码地址:

http://git.oschina.net/itmuch/spring-cloud-study/tree/master/microservice-consumer-movie-feign
https://github.com/eacdy/spring-cloud-study/tree/master/microservice-consumer-movie-feign