u=3928737256,4216920742&fm=253&fmt=auto&app=138&f=JPEG.webp
本文已参加「新人创作礼」活动,一同敞开创作之路。

前语

       经过《微服务通晓之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服务的数据。
在这里刺进图片描绘

四、微服务通晓系列文章

  • 微服务通晓之Eureka原理解析
  • 微服务通晓之Ribbon原理解析
  • 微服务通晓之Hystrix原理解析
  • 微服务通晓之Feign原理解析