Spring-cloud之声明式服务调用Feign

短命女 2022-01-27 08:35 377阅读 0赞

什么是Feign

  1. Spring cloud实现服务调用的方式有两种分别是ribbonfeignRibbon的调用比较麻烦而且不灵活可以参考博客[https://blog.csdn.net/sinat\_32366329/article/details/90383664][https_blog.csdn.net_sinat_32366329_article_details_90383664] 看下具体的调用过程。
  2. Feignnetflix开发的声明书模板化的HTTP客户端,可以帮助我们更加灵活和便捷的通过HTTPAPI调用服务。Feign是支持所有Spring MVC注解的,同时整合了ribbon做负载均衡。

服务注册中心搭建

  1. 由于之前已经介绍过注册中心/服务提供者和基于ribbon的消费者调用,不懂的可以看下面的博客:[https://blog.csdn.net/sinat\_32366329/article/details/90383664][https_blog.csdn.net_sinat_32366329_article_details_90383664]

服务提供者

  1. 我们在之前的服务提供者中创建一个Controller后添加一个方法如下:
  2. @ResponseBody
  3. @GetMapping(value = "/get")
  4. public String get(@RequestParam(value = "name") String name) {
  5. System.out.println("--------------------provider--------------------");
  6. return "Hello " + name;
  7. }

服务消费者

  1. 这里为了大家更加简便的理解,所以贴全部代码出来,如果不理解的可以看看之前的博文系统的学习比较好。

POM

  1. Pom这里的spring boot版本使用了2.1.5.RELEASE版本,如果有问题可以修改回之前的版本,但是消息POM的其他依赖就会有很大的区别。因为spring cloud2.X版本和1.X版本对包名做了比较大的改动,所以需要小心。
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>2.1.5.RELEASE</version>
  10. <relativePath/> <!-- lookup parent from repository -->
  11. </parent>
  12. <groupId>com.example</groupId>
  13. <artifactId>consumer</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. <name>consumer</name>
  16. <description>Demo project for Spring Boot</description>
  17. <properties>
  18. <java.version>1.8</java.version>
  19. <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-web</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.cloud</groupId>
  28. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.cloud</groupId>
  32. <artifactId>spring-cloud-starter-openfeign</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-test</artifactId>
  37. <scope>test</scope>
  38. </dependency>
  39. </dependencies>
  40. <dependencyManagement>
  41. <dependencies>
  42. <dependency>
  43. <groupId>org.springframework.cloud</groupId>
  44. <artifactId>spring-cloud-dependencies</artifactId>
  45. <version>${spring-cloud.version}</version>
  46. <type>pom</type>
  47. <scope>import</scope>
  48. </dependency>
  49. </dependencies>
  50. </dependencyManagement>
  51. <build>
  52. <plugins>
  53. <plugin>
  54. <groupId>org.springframework.boot</groupId>
  55. <artifactId>spring-boot-maven-plugin</artifactId>
  56. </plugin>
  57. </plugins>
  58. </build>
  59. </project>

Application.yml

  1. server:
  2. port: 8971
  3. spring:
  4. application:
  5. name: consumer
  6. eureka:
  7. client:
  8. service-url:
  9. default-zone: http://localhost:8761/eureka/

服务端接口

  1. Feign是基于接口的方式实现对服务提供者的调用的,所以需要创建和服务提供者参数一致的接口,并且需要给接口配置对应的注解才可以实现。

@FeignClient:注解的name属性是注册中心服务提供者的实例名称,可以通过这个名称找到对应的服务提供者调用对应的方法。

@RequestParam:注解是Spring MVC的注解,前面说过feign支持Spring MVC注解,这里的作用是传参时候需要指定传递的参数,这个注解也支持传递Map对象。如果参数没有注解调用会出错。该注解是针对简单的参数类型,下面简单介绍一下复杂的参数使用什么注解。

@RequestHeader:这个注解是将参数放到请求头

@RequestBody:这个注解可以传递自定义类型的参数例如User.class这些类等。

  1. import org.springframework.cloud.openfeign.FeignClient;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RequestParam;
  4. @FeignClient(name = "PROVIDER-USER")
  5. public interface UserFeignClientInterface {
  6. @GetMapping(value = "/get")
  7. String get(@RequestParam(value = "name") String name);
  8. }

启动类修改

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.openfeign.EnableFeignClients;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.PathVariable;
  9. import org.springframework.web.bind.annotation.ResponseBody;
  10. import org.springframework.web.bind.annotation.RestController;
  11. @Controller
  12. @EnableFeignClients
  13. @EnableDiscoveryClient
  14. @SpringBootApplication
  15. public class ConsumerApplication {
  16. @Autowired
  17. private UserFeignClientInterface userFeignClientInterface;
  18. public static void main(String[] args) {
  19. SpringApplication.run(ConsumerApplication.class, args);
  20. }
  21. @ResponseBody
  22. @GetMapping(value = "/get/{name}")
  23. public String get(@PathVariable String name) {
  24. System.out.println("--------------------consumer--------------------");
  25. return this.userFeignClientInterface.get(name);
  26. }
  27. }

启动

  1. 启动注册中心
  2. 启动服务提供者
  3. 启动服务消费者
  4. 访问服务消费者的get方法,看日志是否有正常访问到服务提供者中的方法

微信公众号

  1. 本人开通了个人公众号,以后所有在CSDN发表的博客都会同步到公众号,可以关注以便第一时间学习到新的技术知识。

20190526023509704.jpg

发表评论

表情:
评论列表 (有 0 条评论,377人围观)

还没有评论,来说两句吧...

相关阅读