微服务精通之Feign原理解析

前言

       经过微服务精通之Ribbon原理解析的学习,我们了解到了服务消费者获取服务提供者实例的过程,都是通过RestTemplate来实现的,而且,都是模板化操作。那spring cloud是否有哪个组件可以通过注解或者配置的方式,来简化这个过程?答案是有的,就是Feign。


一、Feign是什么?

       Feign是一个声明式的伪HTTP客户端,它使得HTTP请求变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔注释支持,包括Feign注解和JAX-RS注解、Feign还支持可插拔编码器和解码器、Spring Cloud增加了对Spring MVC注释的支持。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。

二、Feign原理解析

1.总体流程

在这里插入图片描述

2.Hystrix支持

       Feign是自带Hystrix熔断器的,不过在D版本之后,熔断器默认是关闭的,需要通过如下配置进行开启。

feign.hystrix.enabled=true

       Feign启动熔断器后,可以在@FeignClient中指定熔断器类的方式,实现熔断器。每个接口的fallback方法为熔断器类中相同方法名的方法。

3.请求压缩

       Feign支持通过配置的方式,开启请求和返回的GZIP压缩,减少请求的网络消耗。


三、Feign实战

1.Feign服务提供者

       沿用微服务精通之Ribbon原理解析中的service-hi服务。

2.创建Feign消费者

(1)创建名为service-feign的maven工程

(2)引入Eureka-client和Feign依赖

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.hxq</groupId>
		<artifactId>spring-cloud-hxq</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>service-feign</artifactId>
	<name>service-feign</name>
	
	<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>
	</dependencies>
</project>

       (1)Eureka-client依赖:spring-cloud-starter-netflix-eureka-client;
       (2)Feign依赖:spring-cloud-starter-openfeign。

(3)创建启动类

package com.hxq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * 服务消费者
 * 
 * @author Administrator
 *
 */
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {

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

}

       (1)启动类要加上@EnableEurekaClient注解和@EnableDiscoveryClient注解,使程序注册到Eureka。
       (2)启动类要加上@EnableFeignClients注解,是程序支持Feign操作。

(4)创建一个测试接口

package com.hxq.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hxq.service.HelloService;

@RestController
public class HelloController {

    @Resource
    private HelloService helloService;
    
    @RequestMapping("/hi")
    public String hi(@RequestParam(value="name", defaultValue="hxq")String name) {
        return helloService.hiService(name);
    }
}

package com.hxq.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "service-hi", fallback = HelloServiceHystrix.class)
public interface HelloService {

    @RequestMapping("/hi")
    String hiService(@RequestParam("name") String name);
}

(5)创建熔断器Fallback类

package com.hxq.service.hystrix;

import org.springframework.stereotype.Component;

import com.hxq.service.HelloService;

@Component("helloServiceHystrix")
public class HelloServiceHystrix implements HelloService {

    @Override
    public String hiService(String name) {
        return "hi," + name + ",sorry,error!";
    }

}

(6)创建application.yml配置文件

server:
    port: 9700
    
spring:
    application:
        name: service-feign

eureka:
    serviceUrl:
        defaultZone: http://localhost:8761/eureka/

feign:
    hystrix:
        enabled: true
    compression:
        request:
            enabled: true
        response:
            enabled: true

       配置说明:
              server.port:Feign消费者服务端口
              eureka.serviceUrl.defaultZone:Eureka服务器的地址,类型为HashMap,缺省的Key为 defaultZone;缺省的Value为 http://localhost:8761/eureka。如果服务注册中心为高可用集群时,多个注册中心地址以逗号分隔。
              spring.application.name:应用名称,将会显示在Eureka界面的应用名称列。
              feign.hystrix.enabled:Hystrix开关。
              feign.compression.request.enabled:请求GZIP压缩开关。
              feign.compression.response.enabled:返回GZIP压缩开关。

3.服务验证

(1)启动service-hi服务

在这里插入图片描述

(2)启动service-feign服务

在这里插入图片描述

(3)调用测试接口

       在浏览器中输入http://localhost:9700/hi,运行结果如下图,可以看到接口返回service-hi服务的数据。
在这里插入图片描述

四、微服务精通系列文章

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

石头聊技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值