概念:就是远程调用方法,跟Ribbon类似。可以理解为封装的一套服务接口+注解的方式的远程调用器。
它的宗旨是在编写Java Http客户端接口的时候变得更加容易,其底层整合了Ribbon,所以也支持负载均衡。
之前我们使用Ribbon的时候,利用RestTemplate对Http请求进行封装处理,但是在实际开发中,由于对服务依赖的调用不可能就一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以OpenFeign在此基础之上做了进一步的封装,由它来帮助我们定义和实现依赖服务接口的定义,我们只需创建一个接口并使用注解的方式来配置它,即可完成对微服务提供方的接口绑定,简化Ribbon的操作。
>
>1.8 >
-cloud-alibaba-version>2.2.5.RELEASE -cloud-alibaba-version>
-cloud-openfeign-version>2.2.6.RELEASE -cloud-openfeign-version>
>
>
>org.springframework.cloud >
>spring-cloud-starter-openfeign >
>${spring-cloud-openfeign-version} >
>
<?xml version="1.0" encoding="UTF-8"?>
://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">
>4.0.0 >
>
>com.gek >
>SpringCloudAlibabaMSB >
>0.0.1-SNAPSHOT >
> <!-- lookup parent from repository -->
com.gek
cloudAlibaba-openFegin-8888
0.0.1-SNAPSHOT
cloudAlibaba-openFegin-8888
cloudAlibaba-openFegin-8888
1.8
org.springframework.boot
spring-boot-starter
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
<!-- openfeign-->
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.boot
spring-boot-starter-test
test
com.gek
cloudalibaba-commons
0.0.1-SNAPSHOT
compile
>
>
>
>
>org.springframework.boot >
>spring-boot-maven-plugin >
>
>
>
>
package com.gek.cloudalibabaopenfegin8888;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients //openfeign注解
public class CloudAlibabaOpenFegin8888Application {
public static void main(String[] args) {
SpringApplication.run(CloudAlibabaOpenFegin8888Application.class, args);
}
}
server:
port: 8888
spring:
application:
name: nacos-consumer-openfeign
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
management:
endpoints:
web:
exposure:
include: "*"
package com.gek.cloudalibabaopenfegin8888.service;
import com.gek.cloudalibabacommons.entity.JsonResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Service
/**
* 表示调用服务的名称
*/
@FeignClient("nacos-provider")
public interface OpenFeignService {
/**
* 表示调用服务的方法
* @param id
* @return
*/
@GetMapping("info/{id}")
public JsonResult<String> msbSql(@PathVariable("id") Long id);
}
package com.gek.cloudalibabaopenfegin8888.controller;
import com.gek.cloudalibabacommons.entity.JsonResult;
import com.gek.cloudalibabaopenfegin8888.service.OpenFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OpenFeignController {
@Autowired
private OpenFeignService openFeignService;
@GetMapping("info/{id}")
public JsonResult<String> msbSql(@PathVariable("id") Long id){
return openFeignService.msbSql(id);
}
}
OpenFeign 客户端默认等待1秒钟,但是如果服务端业务超过1秒,则会报错。为了避免这样的情况,我们需要设置feign客户端的超时控制。
由于OpenFeign 底层是ribbon 。所以超时控制由ribbon来控制。在yml文件中配置
首先演示超时效果,我们现在9003/9004上设置一个延迟3秒执行的方法,来模仿长业务线调用。
@GetMapping("/timeOut")
public String timeOut() {
try {
System.out.println("延迟响应");
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
return serverPort;
}
service
/**
* 测试openfeign超时时间控制
* @return
*/
@GetMapping("/timeOut")
public String timeOut();
controller
@GetMapping("/testTimeOut")
public String testTimeOut(){
return openFeignService.timeOut();
}
测试结果:
时间超过1秒,报错
server:
port: 8888
spring:
application:
name: nacos-consumer-openfegin
cloud:
nacos:
discovery:
server-addr: localhost:8848
#设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
#指的是建立连接后从服务器读取到可用资源所用的时间
ReadTimeout: 5000
#指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
ConnectTimeout: 5000
management:
endpoints:
web:
exposure:
include: '*'
测试结果:
可以正常响应
日志级别:
NONE:默认的,不显示任何日志;
BASIC:仅记录请求方法、URL、响应状态码及执行时间;
HEADERS:除了 BASIC 中定义的信息之外,还有请求和响应的头信息;
FULL:除了 HEADERS 中定义的信息之外,还有请求和响应的正文及元数据。
package com.gek.cloudalibabaopenfegin8888;
import feign.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients //openfeign注解
public class CloudAlibabaOpenFegin8888Application {
public static void main(String[] args) {
SpringApplication.run(CloudAlibabaOpenFegin8888Application.class, args);
}
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
server:
port: 8888
spring:
application:
name: nacos-consumer-openfeign
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
# 配置openFeign超时时间
# 设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
ReadTimeout: 5000 #指的是建立连接后从服务器读取到可用资源所用的时间
ConnectTimeout: 5000 #指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
logging:
level:
# openfeign日志以什么级别监控哪个接口
com.gek.cloudalibabaopenfegin8888.service.OpenFeignService: debug
management:
endpoints:
web:
exposure:
include: "*"
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
#配置sentinel下的openfeign支持
feign:
sentinel:
enabled: true
package com.gek.cloudalibabaconsumer8084;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class CloudAlibabaConsumer8084Application {
public static void main(String[] args) {
SpringApplication.run(CloudAlibabaConsumer8084Application.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
这里我们的接口写法和之前保持一致,但是要注意,我们这里要多增加一个FeignClient的属性:
package com.gek.cloudalibabaconsumer8084.service;
import com.gek.cloudalibabacommons.entity.JsonResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Service
@FeignClient(value = "nacos-provider",fallback = FeignServiceImpl.class)
public interface FeignService {
@GetMapping("info/{id}")
public JsonResult<String> msbSql(@PathVariable("id") Long id);
}
FeignServiceImpl
package com.gek.cloudalibabaconsumer8084.service;
import com.gek.cloudalibabacommons.entity.JsonResult;
import org.springframework.stereotype.Component;
@Component
public class FeignServiceImpl implements FeignService {
@Override
public JsonResult<String> msbSql(Long id) {
return new JsonResult<String>(444,"服务降级返回");
}
}
controller
@GetMapping("info/{id}")
public JsonResult<String> msbSql(@PathVariable("id") Long id){
if(id > 3){
throw new RuntimeException("没有该id");
}
return feignService.msbSql(id);
}
测试
此时如果我们访问http://localhost:8084/getInfo/1的地址,是没有问题的,但是如果此时我们人为结束9003/9004服务,这个时候就会触发fallback属性对应的处理类型,完成服务降级。
断开服务后