OpenFeign远程调用

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

OpenFeign远程调用

莫须有007   2022-09-28 我要评论

什么是OpenFeign

OpenFeign目前是Spring Cloud 二级子项目。平时说的Feign指的是Netflix下的Feign,现在我们学习的是OpenFeign,是Spring提供的。

OpenFeign是一种声明式、模板化的HTTP客户端(仅在Application Client中使用)(称OpenFeign作用:声明式服务调用)。声明式调用是指,就像调用本地方法一样调用远程方法,无需感知操作远程http请求。学习完OpenFeign后可以不使用RestTemplate进行调用。

Spring Cloud的声明式调用, 可以做到使用 HTTP请求远程服务时能就像调用本地方法一样的体验,开发者完全感知不到这是远程方法,更感知不到这是个HTTP请求。Feign的应用,让Spring Cloud微服务调用像Dubbo一样,Application Client直接通过接口方法调用Application Service,而不需要通过常规的RestTemplate构造请求再解析返回数据。它解决了让开发者调用远程接口就跟调用本地方法一样,无需关注与远程的交互细节,更无需关注分布式环境开发。

使用OpenFeign时就好像在写控制器方法,OpenFeign都是写在接口中,在声明的方法上添加SpringMVC注解或声明的参数上添加SpringMVC注解就可以完成调用远程的控制器方法。

OpenFeign用途

openfeign的用途:服务发现,负载均衡,服务调用

openfeign的实现原理:基于@EnableFeignClients 将所有被@FeignClient注解的类 注册到容器中。当这些被@FeignClient注解的类被调用时会创建一个动态代理的对象为我们创建被调用类的实例,然后都会被统一转发给 Feign 框架所定义的一个 InvocationHandler , 由该 Handler 完成后续的 HTTP 转换, 发送, 接收, 翻译HTTP响应的工作。本文主要介绍OpenFeign服务调用的用途,实现微服务间的远程调用。

具体案例

springboot版本为2.3.5

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

pom依赖

<!-- openfeign -->
     <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
        <version>2.2.5.RELEASE</version>
    </dependency>

     <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
     <version>2.2.5.RELEASE</version>
</dependency>

服务请求方

1.在springboot启动类中添加注解@EnableFeignClients

package com.anjiplus.template.gaea.business;

import com.anji.plus.gaea.annotation.enabled.EnabledGaeaConfiguration;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import springfox.documentation.oas.annotations.EnableOpenApi;

// openFeign注解,括号内容为远程调用类包路径
@EnableFeignClients("com.anjiplus.template.gaea.business.modules.model.openFeign")
@EnableOpenApi
public class ReportApplication {
    public static void main( String[] args ) {
        SpringApplication.run(ReportApplication.class);
    }
}

2.添加远程调用接口类,接口类上需要使用@FeignClient注解标注调用服务的服务名和服务地址

package com.anjiplus.template.gaea.business.modules.model.openFeign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

/**
 * @author 莫须有
 * @Date 2022/6/10 15:52
 * @Description openFeign服务调用类
 */
//注解表示服务调用的服务名称和服务地址
@FeignClient(value = "kg-data-manage", url = "http://127.0.0.1:10100/datamanage")
@Component
public interface ModelOpenFeign {

    /**
     * @Description 查询用户下所用的模型信息
     * @author 莫须有
     * @date 2022/6/17
     * @param userId 用户编号
     * @return list
     */
    @GetMapping("/dashBoard/getFactory")// 调用的路径为服务提供方接口请求路径
    String getFactoryList(@RequestParam("userId") String userId);

    @GetMapping("/dashBoard/getPointList")
    String getPointList(@RequestParam("taskId") String factoryId);

    @GetMapping("/dashBoard/getData")
    String getData(@RequestParam("sql") String sql);
}

服务提供方

服务提供方只需像正常的controller层接口一样编写就可以,不需要额外的配置,根据需要在controller层进行接口开发,然后再service层中做具体的实现即可,需要注意的是请求参数和返回参数的类型需要两边一致,这是必须满足的。

package com.xasj.controller.model.openFeign;

import com.xasj.entity.model.vo.DashBoardModelInfo;
import com.xasj.entity.model.vo.DashBoardPointInfo;
import com.xasj.service.model.openFeign.DashBoardOpenFeignService;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 莫须有
 * @Date 2022/6/10 17:52
 * @Description openFeign服务提供类
 */
@RestController
public class DashBoardOpenFeignController {
    @Resource
    private DashBoardOpenFeignService dashBoardOpenFeignService;

    @GetMapping("/dashBoard/getFactory")
    public List<DashBoardModelInfo> getFactoryList(@RequestParam(name = "userId") String userId){
        return dashBoardOpenFeignService.getFactoryList(userId);
    }

    @GetMapping("/dashBoard/getPointList")
    public List<DashBoardPointInfo> getPointList(@RequestParam(name = "taskId")String taskId){
        return dashBoardOpenFeignService.getPointList(taskId);
    }

    @GetMapping("/dashBoard/getData")
    public List getData(@RequestParam("sql") String sql){
        return dashBoardOpenFeignService.getData(sql);
    }
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们