首页 > 其他分享 >4.open feign

4.open feign

时间:2022-10-07 18:55:52浏览次数:57  
标签:feign com springcloud springframework junfu org import open

一、OpenFeign是什么
OpenFeign是一个声明式的Web服务客户端,让编写Web服务客户端变得非常容易,只需创建一个接口并在接口上添加注解即可

二、OpenFeign能干什么
Feign旨在使编写Java Http客户端变得更容易。
前面在使用Ribbon+RestTemplate时,利用RestTemplate对http请求的封装处理,形成了一套模版化的调用方法。但是在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以,Feign在此基础上做了进一步封装,由他来帮助我们定义和实现依赖服务接口的定义。在Feign的实现下,我们只需创建一个接口并使用注解的方式来配置它(以前是Dao接口上面标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解即可),即可完成对服务提供方的接口绑定,简化了使用Spring cloud.Ribbon时,自动封装服务调用客户端的开发量。

三、Feign集成了Ribbon

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

四、OpenFeign使用步骤

  • 创建工程cloud-consumer-feign-order80

  • 修改pom.xml文件,添加openfeign依赖以及一些必要依赖
1 <dependency>
2     <groupId>org.springframework.cloud</groupId>
3     <artifactId>spring-cloud-starter-openfeign</artifactId>
4 </dependency>
 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>SpringCloudStudy</artifactId>
 7         <groupId>com.junfu.springcloud</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11 
12     <artifactId>cloud-consumer-feign-order80</artifactId>
13 
14     <properties>
15         <maven.compiler.source>8</maven.compiler.source>
16         <maven.compiler.target>8</maven.compiler.target>
17     </properties>
18 
19     <dependencies>
20         <dependency>
21             <groupId>org.springframework.boot</groupId>
22             <artifactId>spring-boot-starter-web</artifactId>
23         </dependency>
24         <dependency>
25             <groupId>org.springframework.boot</groupId>
26             <artifactId>spring-boot-starter-actuator</artifactId>
27         </dependency>
28         <dependency>
29             <groupId>org.springframework.boot</groupId>
30             <artifactId>spring-boot-devtools</artifactId>
31             <scope>runtime</scope>
32             <optional>true</optional>
33         </dependency>
34         <dependency>
35             <groupId>org.projectlombok</groupId>
36             <artifactId>lombok</artifactId>
37             <optional>true</optional>
38         </dependency>
39         <dependency>
40             <groupId>org.springframework.boot</groupId>
41             <artifactId>spring-boot-starter-test</artifactId>
42             <scope>test</scope>
43         </dependency>
44         <dependency>
45             <groupId>com.junfu.springcloud</groupId>
46             <artifactId>cloud-api-commons</artifactId>
47             <version>${project.version}</version>
48         </dependency>
49 
50         <dependency>
51             <groupId>org.springframework.cloud</groupId>
52             <artifactId>spring-cloud-starter-openfeign</artifactId>
53         </dependency>
54     </dependencies>
55 </project>
  • 修改application.yaml文件
1 server:
2   port: 80
3 
4 
5 eureka:
6   client:
7     register-with-eureka: false
8     service-url:
9       defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/ 
  • 创建主启动类:OpenFeignMain80
 1 package com.junfu.springcloud;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.openfeign.EnableFeignClients;
 6 
 7 @SpringBootApplication
 8 @EnableFeignClients
 9 public class OrderFeignMain80 {
10     public static void main(String[] args) {
11         SpringApplication.run(OrderFeignMain80.class,args);
12     }
13 }
  • 创建接口
package com.junfu.springcloud.service;

import com.junfu.springcloud.entities.CommonResult;
import com.junfu.springcloud.entities.Payment;
import feign.Param;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentService {

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);

}
  • 创建controller
 1 package com.junfu.springcloud.controller;
 2 
 3 import com.junfu.springcloud.entities.CommonResult;
 4 import com.junfu.springcloud.entities.Payment;
 5 import com.junfu.springcloud.service.PaymentFeignService;
 6 import lombok.extern.slf4j.Slf4j;
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.web.bind.annotation.GetMapping;
 9 import org.springframework.web.bind.annotation.PathVariable;
10 import org.springframework.web.bind.annotation.RestController;
11 
12 import javax.annotation.Resource;
13 
14 @RestController
15 @Slf4j
16 public class OrderFeignController {
17     @Resource
18     private PaymentFeignService paymentFeignService;
19 
20     @GetMapping(value = "/consumer/payment/get/{id}")
21     public CommonResult getPaymentById(@PathVariable("id") Long id){
22         log.info("feign测试");
23         CommonResult paymentById = paymentFeignService.getPaymentById(id);
24         return paymentById;
25     }
26 }
  • 测试

标签:feign,com,springcloud,springframework,junfu,org,import,open
From: https://www.cnblogs.com/midiyu/p/16760400.html

相关文章

  • dremio openjdk 11 docker 镜像
    dremio官方也说明了,已经支持openjdk11了,但是默认官方的还是openjdk8,为了体验jdk11所以基于官方的搞了一个openjdk11的镜像,很简单dockerfileARGJAVA_IMAGE="openjdk......
  • 实验2:Open vSwitch虚拟交换机实践 实验3:OpenFlow协议分析实践
    实验2:OpenvSwitch虚拟交换机实践一、实验目的能够对OpenvSwitch进行基本操作;能够通过命令行终端使用OVS命令操作OpenvSwitch交换机,管理流表;能够通过Mininet的Pytho......
  • 实验3:OpenFlow协议分析实践
    (三)实验报告3.1请用Markdown排版;3.2基础要求只需要提交导入到/home/用户名/学号/lab3/目录下的拓扑文件,wireshark抓包的结果截图和对应的文字说明;3.2.1搭建所示拓扑,完......
  • 实验3:OpenFlow协议分析实践
    (一)基本要求1.搭建下图所示拓扑,完成相关IP配置,并实现主机与主机之间的IP通信。用抓包软件获取控制器与交换机之间的通信数据。主机 IP地址h1 192.168.0.101/24h2 1......
  • Open Quantum Systems
    \[\mathcal{K}(t)\rho=-i[H,\rho]+\sum_i\gamma_i\left[A_i\rhoA_i^{\dagger}-\frac{1}{2}\left\{A_i^{\dagger}A_i,\rho\right\}\right]\]with\(H(t)\)theHerm......
  • 实验3:OpenFlow协议分析实践
    实验3:OpenFlow协议分析实践一、实验目的能够运用wireshark对OpenFlow协议数据交互过程进行抓包;能够借助包解析工具,分析与解释OpenFlow协议的数据包交互过程与机制......
  • 通过openresty 解决遗留 webservice 接口安全问题
    技术一直在变革,老的技术一般都会成为现在的技术债,加上早期大家一般对于安全不是很重视(尤其是在内网环境的时候),尽管webservice是包含了ws-security安全指南的,但是很多时......
  • nginx ngx_http_addition_module 模块openresty content_by_lua 不能生效的原因
    nginx的ngx_http_addition_module模块也是一个修改content的好东西,对于openresty我们经常使用content_by_lua阶段处理但是经过分析ngx_http_addition_module源码的......
  • OpenHarmony/HarmonyOS的ArkUI的类Web范式开发详解
    一.OpenHarmony/HarmonyOS的ArkUI的类Web范式开发1.1类Web范式~三件套开发基于JS扩展的类****Web开发范****式的方舟开发框架包括应用层(Application)、前端框架层(Framewo......
  • 实验3:OpenFlow协议分析实践
    一、实验目的能够运用wireshark对OpenFlow协议数据交互过程进行抓包;能够借助包解析工具,分析与解释OpenFlow协议的数据包交互过程与机制。二、实验环境Ubuntu20......