Feign简介

Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性, 可使用Feign注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认继承了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。

  • Feign采用的是基于接口的注解
  • Feign整合了Ribbon
  • Feign自带Hystrix熔断器

创建服务消费者

新建SpringBoot工程eureka-consumer-feignpom.xml文件配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springcloud.bh</groupId>
    <artifactId>eureka-consumer-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-consumer-feign</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

主要增加了Feign的依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

配置application.yml

通过注解@EnableFeignClients 注解,开启Feign功能

package com.springcloud.bh;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableFeignClients          /*启用Feign*/
@EnableDiscoveryClient      /*开启服务发现*/
@SpringBootApplication
public class EurekaConsumerFeignApplication {

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

Application

设置端口号为:8765

server:
  port: 8765

spring:
  application:
    name: eureka-consumer-feign

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

创建Feign接口

通过@FeignClient(服务名)注解来指定调用哪个服务,代码如下:

package com.springcloud.bh.service;

import com.springcloud.bh.service.hystrix.AdminServiceHystrix;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *  @FeignClient 注解中value为服务提供者的名字,fallback为服务熔断类
 */
@FeignClient(value = "eureka-client",fallback = AdminServiceHystrix.class)
public interface AdminService {

    @GetMapping("hi")
    public String sayHi(@RequestParam("message")String message);  //需使用@RequestParam,否则报405错误
}

创建测试用的Controller

package com.springcloud.bh.controller;

import com.springcloud.bh.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;

    @GetMapping("hi")
    public String sayHi(String message)
    {
        return adminService.sayHi(message);
    }
}

测试访问

打开浏览器,多次访问http://localhost:8765/hi?message=hi,Feign 浏览器交替显示:

  • 你的消息是:hi,Feign 端口号:8762
  • 你的消息是:hi,Feign 端口号:8763

请求成功则表示已成功实现了Feign功能来访问不同端口的实例。

源码地址

Github: https://github.com/yeshang5/springcloud-example

赞 赏