OpenFeign简介
Feign是一个声明式的Web Service客户端。它的出现使开发Web Service客户端变得很简单。使用Feign只需要创建一个接口加上对应的注解,比如:FeignClient注解。Feign有可插拔的注解,包括Feign注解和JAX-RS注解。
Feign也支持编码器和解码器,Spring Cloud Open Feign对Feign进行增强支持Spring MVC注解,可以像Spring Web一样使用HttpMessageConverters等。
Feign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用Feign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求。
openFeign的功能
1.可插拔的注解支持,包括Feign注解和JAX-RS注解。
2.支持可插拔的HTTP编码器和解码器(Gson,Jackson,Sax,JAXB,JAX-RS,SOAP)。
3.支持Hystrix和它的Fallback。
4.支持Ribbon的负载均衡。
5.支持HTTP请求和响应的压缩。
6.灵活的配置:基于 name 粒度进行配置
7.支持多种客户端:JDK URLConnection、apache httpclient、okhttp,ribbon)
8.支持日志
9.支持错误重试
10.url支持占位符
11.可以不依赖注册中心独立运行
原文链接:https://blog.csdn.net/qq_43565087/article/details/106207867
创建项目
在Nacos项目下继续创建一个Springboot项目名为nacos-feign,创建时添加OpenFeign的依赖,如图:
nacos-fegin的pom.xml文件如下:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
添加项目配置文件
在resourse目录下,添加application.yml配置
server:
port: 10003
spring:
application:
name: nacos-feign
cloud:
nacos:
discovery:
server-addr: 192.168.230.136:8848
#服务挂掉, 开启熔断处理
feign:
hystrix:
enabled: true
启动类设置
package com.jq;
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
public class NacosFeignApplication {
public static void main(String[] args) {
SpringApplication.run(NacosFeignApplication.class, args);
}
}
创建控制层测试:
package com.jq.controller;
import com.jq.config.RemoteClient;
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;
@RestController
@RequestMapping("feign")
public class FeignClientsController {
@Autowired
private RemoteClient remoteClient;
@GetMapping("/feign")
public String test() {
return remoteClient.helloNacos();
}
}
创建FeignClient接口
package com.jq.config;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name = "server",fallback = RemoteHystrix.class)//指定降级的服务
//创建RemoteClient接口,来定义OpenFeign要调用的远程服务接口。
// 同时通过@FeginClient注解指定被调用方的服务名,通过fallback属性指定RemoteHystrix类,来进行远程调用的熔断和降级处理。
public interface RemoteClient {
//降级的方法
@GetMapping("/hell/helloNacos")
String helloNacos();
}
指定RemoteHystrix类,来进行远程调用的熔断和降级处理。
package com.jq.config;
import org.springframework.stereotype.Component;
@Component
public class RemoteHystrix implements RemoteClient{
public String helloNacos() {
return "请求超时了?????";
}
}
浏览器访问 http://localhost:10003/feign/feign, 可以看到返回结果与RestTemplate结果无异,但对于编码和操作方式都更加优雅。
访问nacos-feign的接口 hhttp://localhost:10003/feign/feign, 可以通过OpenFeign远程调用server的接口,返回结果:
调用者:
提供者:
服务降级:
关闭提供者:
访问测试:下图为成功
总结
OpenFegin整合Ribbon和Hystrix,为微服务中远程调用提供了一种更优雅的调用方式,它支持负载均衡和容错熔断机制。
原文 https://blog.csdn.net/qq_33619378/article/details/95353326?spm=1001.2014.3001.5501