SpringCloud(Greenwich版)Fegin声明式服务调用
推荐以下稳定版本号:
Spring Boot: 2.1.9.RELEASE
Spring Cloud: Greenwich.SR3
一、Fegin简介
Fegin 是 Netflix 开发的声明式、模板化的 HTTP 客户端。Feign 可以通过 @FeignClient 注解绑定到接口上来简化 Http 请求访问。Feign 默认集成 Ribbon,具有负载均衡的能力。整合了 Hystrix,具有服务熔断的能力。
二、快速上手Fegin
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-eureka-client'
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-openfeign'
}
server:
port: 9090
spring:
application:
name: api-fegin
eureka:
instance:
hostname: localhost
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://localhost:8761/eureka/
在启动类上加上@EnableFeignClients,注解含义:开启Fegin功能,扫描接口声明他们是Feign客户端。
package org.wesson.springcloud.fegin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class ApiFeginApplication {
public static void main(String[] args) {
SpringApplication.run(ApiFeginApplication.class, args);
}
}
4)定义FeginApi接口
在接口上添加@FeignClient标记,注解含义:注解里的参数值是获取在Eureka注册中心的服务名。
package org.wesson.springcloud.fegin.api;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient("PROVIDER-SERVICE")
public interface FeginApi {
@GetMapping("/client/info")
public String info();
}
注意一下:
5)Controller
package org.wesson.springcloud.fegin.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wesson.springcloud.fegin.api.FeginApi;
@RestController
@RequestMapping("/client")
public class FeginController {
@Autowired
private FeginApi feginApi;
@GetMapping("/info")
public String info() {
return feginApi.info();
}
}
Step1:运行 eureka-server 启动类,端口为8761
Step2:运行 provider-service 启动类2个实例,端口为8081、8082
Step3:运行 api-fegin 启动类,端口为9090
Step4:先访问http://localhost:8761/,结果如下图:
Step5:多次访问
-
hello, service provider port is from:8081
-
hello, service provider port is from:8082
Fegin 中的日志级别:
-
NONE:默认值,不显示任何日志记录;
-
BASIC:仅记录请求方法、URL、响应状态代码和执行时间;
-
HEADERS:除了 BASIC 中定义的信息之外,还有请求和响应的头信息;
-
FULL:除了 HEADERS 中定义的信息之外,还有请求和响应的正文及元数据。
package org.wesson.springcloud.fegin.config;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeginConfig {
@Bean
public Logger.Level feignLoggerLevel() {
//FULL记录所有,根据实际情况选择合适的日志level(此处日志记录器是fegin.Logger中的包)
return Logger.Level.FULL;
}
}
2)在api-fegin项目的application.yaml配置文件需要开启日志的 Fegin 客户端
logging:
level:
org.wesson.springcloud.fegin.api.FeginApi: debug #fegin日志以什么样的级别监控哪个接口(我定义的接口名为FeginApi全名,配置文件日志级别为debug)
3)控制台打印日志后输出的结果如下:
2020-03-16 18:05:19.588 DEBUG 22804 --- [vider-service-2] o.wesson.springcloud.fegin.api.FeginApi : [FeginApi#info] ---> GET http://provider-service/client/info HTTP/1.1
2020-03-16 18:05:19.588 DEBUG 22804 --- [vider-service-2] o.wesson.springcloud.fegin.api.FeginApi : [FeginApi#info] ---> END HTTP (0-byte body)
2020-03-16 18:05:19.642 DEBUG 22804 --- [vider-service-2] o.wesson.springcloud.fegin.api.FeginApi : [FeginApi#info] <--- HTTP/1.1 200 (53ms)
2020-03-16 18:05:19.642 DEBUG 22804 --- [vider-service-2] o.wesson.springcloud.fegin.api.FeginApi : [FeginApi#info] content-length: 41
2020-03-16 18:05:19.642 DEBUG 22804 --- [vider-service-2] o.wesson.springcloud.fegin.api.FeginApi : [FeginApi#info] content-type: text/plain;charset=UTF-8
2020-03-16 18:05:19.642 DEBUG 22804 --- [vider-service-2] o.wesson.springcloud.fegin.api.FeginApi : [FeginApi#info] date: Mon, 16 Mar 2020 10:05:19 GMT
2020-03-16 18:05:19.642 DEBUG 22804 --- [vider-service-2] o.wesson.springcloud.fegin.api.FeginApi : [FeginApi#info]
2020-03-16 18:05:19.643 DEBUG 22804 --- [vider-service-2] o.wesson.springcloud.fegin.api.FeginApi : [FeginApi#info] hello, service provider port is from:8081
2020-03-16 18:05:19.643 DEBUG 22804 --- [vider-service-2] o.wesson.springcloud.fegin.api.FeginApi : [FeginApi#info] <--- END HTTP (41-byte body)
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-hystrix'
feign:
hystrix:
enabled: true #开启Fegin对Hystrix服务降级功能,默认为false
@EnableCircuitBreaker
package org.wesson.springcloud.fegin.api;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.wesson.springcloud.fegin.fallback.FeginFallback;
/**
* 重点属性:使用@FeignClient的fallback属性指定服务降级类(FeginFallback.class)
*/
@FeignClient(name = "provider-service", fallback = FeginFallback.class)
public interface FeginApi {
@GetMapping("/client/info")
public String info();
}
创建fallback包,服务降级FeginFallback类要实现 (implements) FeginApi接口,使用@Component注解进行组件扫描
package org.wesson.springcloud.fegin.fallback;
import org.springframework.stereotype.Component;
import org.wesson.springcloud.fegin.api.FeginApi;
@Component
public class FeginFallback implements FeginApi {
/**
* 实现的方法是远程服务调用的降级方法
*/
@Override
public String info() {
return "Error Warning!!!";
}
}
Step1:运行 eureka-server 启动类,端口为8761
Step2:运行 provider-service 启动类,端口为8081
Step3:运行 api-fegin 启动类,端口为9090
Step4:访问http://localhost:9090/client/info,服务正常返回结果如下:
-
hello, service provider port is from:8081
Step5:停止 provider-service 应用程序
Step6:再次访问http://localhost:9090/client/info,获得如下结果:
-
Error Warning!!!
说明当服务提供者provider-service微服务不可用时,就进入了服务降级的逻辑方法。