zoukankan      html  css  js  c++  java
  • 【SpringCloud】OpenFeign服务调用(八)

    OpenFeign介绍

      前面在使用Ribbon+RestTemplate时,利用RestTemplate对http请求的封装处理,形成了一套模版化的调用方法。但是在实际开发中,对于服务依赖的调用可能不止一处,往往一个接口会被多处调用。所有Feign在此基础上做了进一步封装,由他来帮助我们定义和实现依赖服务接口的定义。在Feign的实现下,我们只需创建一个接口并使用注解的方式来配置它(以前是Dao接口上面标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解即可),集可完成对服务提供方的接口绑定,简化了使用Spring Cloud Ribbon时,自动封装服务调用客户端的开发量。

      Feign旨在式编写Java Http客户端变得更容易。

      Feign集成了Ribbon,利用Ribbon维护了服务列表信息,并且通过轮询实现了客户端的负载均衡。而与Ribbon不同的是,通过feign只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用

      git地址:https://github.com/spring-cloud/spring-cloud-openfeign

      Feign与OpenFeign的区别

      1)Feign是Spring Cloud组件中一个轻量级RESTful的HTTP服务客户端,Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。Feign的使用方式是:使用Feign的注解定义接口,调用接口,就可以调用服务注册中心的服务

        Feign的依赖

    1 <dependency>
    2     <groupId>org.springframework.cloud</groupId>
    3     <artifactId>spring-cloud-starter-feign</artifactId>
    4 </dependency>

      2)OpenFeign是Spring Cloud在Feign的基础上支持了SpringMVC的注解,如@RequestMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中

        OpenFeign的依赖

    1 <dependency>
    2     <groupId>org.springframework.cloud</groupId>
    3     <artifactId>spring-cloud-starter-openfeign</artifactId>
    4 </dependency>  

    OpenFeign使用

      项目架构如下:

      1个Eureka注册中心,2个服务提供者,1个服务消费者

      

      1、新建项目,参考【SpringCloud】服务提供者集群与服务发现Discovery(三) 

      2、新建一个服务消费者模块(test-springcloud-order7997)的maven工程

      3、修改pom文件,引入OpenFeign依赖,完整pom文件如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0"
     3          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     <parent>
     6         <artifactId>test-springcloud</artifactId>
     7         <groupId>com.test</groupId>
     8         <version>1.0-SNAPSHOT</version>
     9     </parent>
    10     <modelVersion>4.0.0</modelVersion>
    11 
    12     <artifactId>test-springcloud-order7997</artifactId>
    13 
    14 
    15     <dependencies>
    16 
    17         <!-- openfeign -->
    18         <dependency>
    19             <groupId>org.springframework.cloud</groupId>
    20             <artifactId>spring-cloud-starter-openfeign</artifactId>
    21         </dependency>
    22 
    23         <!-- eureka client -->
    24         <dependency>
    25             <groupId>org.springframework.cloud</groupId>
    26             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    27         </dependency>
    28 
    29         <!-- spring boot -->
    30         <dependency>
    31             <groupId>org.springframework.boot</groupId>
    32             <artifactId>spring-boot-starter-web</artifactId>
    33         </dependency>
    34         <dependency>
    35             <groupId>org.springframework.boot</groupId>
    36             <artifactId>spring-boot-starter-actuator</artifactId>
    37         </dependency>
    38         <dependency>
    39             <groupId>org.springframework.boot</groupId>
    40             <artifactId>spring-boot-devtools</artifactId>
    41             <scope>runtime</scope>
    42             <optional>true</optional>
    43         </dependency>
    44 
    45         <dependency>
    46             <groupId>org.projectlombok</groupId>
    47             <artifactId>lombok</artifactId>
    48             <optional>true</optional>
    49         </dependency>
    50         <dependency>
    51             <groupId>org.springframework.boot</groupId>
    52             <artifactId>spring-boot-starter-test</artifactId>
    53             <scope>test</scope>
    54         </dependency>
    55 
    56     </dependencies>
    57 
    58     <build>
    59         <finalName>test-springcloud-order8000</finalName>
    60     </build>
    61 </project>
    pom.xml

        Feign集成了Ribbon:

        

      4、修改配置文件,application.yml

     1 # 端口
     2 server:
     3   port: 7997
     4 
     5 spring:
     6   application:
     7     name: cloud-order
     8 
     9 eureka:
    10   client:
    11     # 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
    12     service-url:
    13       defaultZone: http://localhost:8761/eureka
    14   instance:
    15     #  instance:
    16     instance-id: ${spring.cloud.client.ip-address}:${server.port}
    17     # 访问路径可以显示IP地址
    18     prefer-ip-address: true

      4、编写SpringBoot启动类,并使用@EnableFeignClients注解,开始Feign客户端

    1 // 开启Feign客户端
    2 @EnableFeignClients
    3 @SpringBootApplication
    4 public class OrderMain7997 {
    5     public static void main(String[] args) {
    6         SpringApplication.run(OrderMain7997.class, args);
    7     }
    8 }

      5、编写接口,使用@FeignClient("CLOUD-PAYMENT-SERVICE")注解,表名此接口做为一个feign客户端,并调用CLOUD-PAYMENT-SERVICE服务。

    1 @Component
    2 // Feign客户端
    3 @FeignClient("CLOUD-PAYMENT-SERVICE")
    4 public interface PaymentFeignService {
    5     // 获取
    6     @GetMapping("/payment/get/{id}")
    7     public CommonResult getPaymentById(@PathVariable("id") Long id)
    8

      6、编写Controller,使用接口调用服务

     1 @RestController
     2 @Slf4j
     3 public class OrderFeignController {
     4 
     5     @Autowired
     6     private PaymentFeignService paymentFeignService;
     7 
     8     @GetMapping("/consumer/payment/get/{id}")
     9     public CommonResult getPaymentById(@PathVariable("id") Long id) {
    10         return paymentFeignService.getPaymentById(id);
    11     }
    12 }

      7、启动项目,测试

        访问地址:http://localhost:7997/consumer/payment/get/1

        结果:

          正常远程调用服务提供者的服务,且达到负载均衡的目的

  • 相关阅读:
    Linux: 安装和启用firefox浏览器的java
    Latex: beamer
    时频分析:窗口傅立叶变换
    Python: 面向对象
    Linux: 安装NVIDIA显卡驱动
    Matlab: 路径的操作
    python--文件读写
    python--函数
    python--数据类型
    网络基础——网络协议
  • 原文地址:https://www.cnblogs.com/h--d/p/12690840.html
Copyright © 2011-2022 走看看