当前位置:首页 > 后端开发 > Feign【入门】

Feign【入门】

6个月前 (05-23)47

feign简介:

feign是一种声明式,模板化的HTTP客户端,spring cloud对feign进行了增强,使其支持SpringMvc的相关注解,并整合了ribbon做负载均衡。在spring cloud中使用feign做HTTP远程服务请求,可以做到就像调用本地方法一样,完全感知不到是在调用远程方法,具体特性如下:

  • 可插拔的注解支持,包括feign注解和Jax-rs注解、
  • 支持可插拔的HTTP编码器和解码器、
  • 支持hystrix和它的fallback、
  • 支持ribbon负载均衡、
  • 支持HTTP请求和响应的压缩、

1、创建feign-demo工程

1.1、工程依赖

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
</parent>

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
</dependencyManagement>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Cloud OpenFeign的Starter的依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

1.2、工程启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class SpringCloudFeignApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudFeignApplication.class, args);
    }
}

可以看到启动类上加了 @EnableFeignClients 注解,意思是当该工程在启动的时候,会进行包扫描,扫描该启动类包以下,子包中所有带 @FeignClient 注解的类(包括启动类所在包),并进行处理。

1.3、编写相关代码

HelloFeignService接口:

import cn.springcloud.book.feign.config.HelloFeignServiceConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

//https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/121.6544,25.1552/forecast.json 彩云天气API
@FeignClient(name = "caiyunapp", url = "https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/121.6544,25.1552", configuration = HelloFeignServiceConfig.class)
public interface HelloFeignService {


    @RequestMapping(value = "/forecast.json", method = RequestMethod.GET)
    String searchRepo();

}

如上所示,@FeignClient注解手动指定了URL,最终会根据指定的URL和@RequestMapping对应的方法转换成完整的请求地址。如下:https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/121.6544,25.1552/forecast.json

HelloFeignServiceConfig配置类:

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HelloFeignServiceConfig {

    /**
     * Logger.Level 的具体级别如下:
     * NONE:不记录任何信息
     * BASIC:仅记录请求方法、URL以及响应状态码和执行时间
     * HEADERS:除了记录 BASIC级别的信息外,还会记录请求和响应的头信息
     * FULL:记录所有请求与响应的明细,包括头信息、请求体、元数据
     *
     * @return
     */
    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

}

controller类:

import cn.springcloud.book.feign.service.HelloFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloFeignController {

    @Autowired
    private HelloFeignService helloFeignService;

    // 服务消费者对位提供的服务
    @GetMapping(value = "/search")
    public String searchGithubRepoByStr(@RequestParam("str") String queryStr) {
        return helloFeignService.searchRepo(queryStr);
    }

}

如上所示,controller类中注入了上面编写的接口类,直接调用了相关方法。

2、启动工程

2.1、执行命令:

mvn spring-boot:run

2.2、访问:losthost:8080/search/

Feign【入门】 _ Java侠

 

 说明访问成功!

 

作者:KingJames、
来源链接:https://www.cnblogs.com/idoljames/p/11667938.html

标签: Feign

“Feign【入门】” 的相关文章

Feign的高级配置

Feign的高级配置

Feign的配置 从Spring Cloud Edgware开始,Feign支持使用属性自定义Feign。对于一个指定名称的FeignClient(例如该F...

Feign的详解与使用

Feign详解与使用 文章目录 Feign详解与使用 一、什么是Feig...

spring cloud学习笔记三 Feign与Ribbon负载均衡的区别

一、Feign的介绍   Feign一般比较书面的解释是:Feign是一个声明式的WebService客户端,使用Feign编写的WebService客户端更加简单,他的使用方法是...

OpenFeign和feign使用简介

OpenFeign和feign使用简介

1.OpenFeign简介 Feign是一个声明式的Web Service客户端。它的出现使开发Web Service客户端变得很简...

Feign注解说明

Feign是常用的微服务rpc调用框架,下面对一些注解说明 @Target({ ElementType.TYPE}) @Retent...

SpringBoot 关于Feign的超时时间配置

 feign 时间设置:   contextId: 可以指定为某个接口进行单独的超时设置 @FeignClient(value = "user"...

Feign的基本使用、日志配置和连接池配置详解

Feign的基本使用、日志配置和连接池配置详解 一、概述 Feign主要是微服务项目中远程调用的一种实现方式。 常见的远程调用方式...

feign.exception Connection reset executing

使用feign调用服务时出现Connection reset executing ,项目是注册在eureka中 在项目中,同一个接口中使用了多次feign调用,第一次调...

feign的配置与使用

添加依赖 <dependency> <groupId>org.springframework.cl...

feign 调用失败