SpringCloud-OpenFeign基础
OpenFeign
OpenFeign简介
- OpenFeign是一个基于HTTP协议的RPC(远程过程调用)组件,用于简化HTTP请求和响应的处理。
- 它通过声明式的方式定义REST API接口,并自动生成实现该接口的客户端代码,从而简化了RESTful服务的调用过程。
- 它和其它框架,比如Dubbo、Mybatis等发送请求的框架相同,底层是动态代理
- 它是Feign的增强版,同时整合了Ribbon和Eureka,使Feign更加灵活
Feign.client
OpenFeign默认使用jdk的HttpUrlConnection,没有链接池,也没有资源管理,性能不是很好
Feign的日志
- Feign的日志级别
- NONE:默认的,不打印任何日志
- BASIC:仅记录请求方法,URL、响应状态码以及执行时间
- HEADERS:在BASIC基础上,记录请求和响应的header信息
- FULL:记录响应和请求的header、body和元数据
- 开发环境一般使用FULL,生产环境一般使用BASIC
配置Feign日志可以通过两种方式配置
Feign的日志打印是基于springboot日志的所以要配置springboot日志
- 引入springboot日志依赖
<!--日志-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
- 配置springboot日志输出级别
logging:
level:
com.shaoby: debug
- 配置类,需要注意的是如果使用配置类的方式,配置类加了@Configuration注解,全局使用;如果不加可以通过@FeignClient注解的configuration属性指定配置类;
public class FeignLogConfig {
@Bean
public Logger.Level logger(){
return Logger.Level.FULL;
}
}
@FeignClient(value = "SMS-cache",configuration = FeignLogConfig.class)
public interface SmsCacheClient {
@GetMapping(value = "/cache/hGetAll/{key}")
Map hGetAll(@PathVariable("key") String key);
}
- 配置文件,或直接使用配置文件配置feign日志
logging:
level:
com.shaoby: debug
feign:
client:
config:
# feignClient的value,可以配置多个
SMS-cache:
loggerLevel: full
Feign支持的配置项
Feign契约
- Feign默认使用springMVC的契约,即springMVC中的注解如@RequestMapping、@PostMapper、@PathVariable等。
- 如果使用Feign的默认契约,就必须使用Feign中的注解调用远程接口,可通过配置文件或者配置类修改契约,默认值feign.Contract.Default
- 不建议修改
编解码
- 编解码是指的:指将请求数据编码成HTTP请求体并将HTTP响应体解码成Java对象的过程。
- Feign中提供了自定义编解码配置,同时也提供了多项编解码的实现,比如Gson、Jaxb、Jackson.默认使用SpringEncoder&&SpringDecoder
- 可通过实现Encoder&&Decoder自定义编解码
- 可通过配置文件指定编解码方式,一般不建议修改
feign:
client:
config:
#调用的服务名称,可配置多个
SMS-cache:
encoder: com.xxx.xxxEncoder
decoder: com.xxx.xxxDecoder
拦截器
- 实现RequestInterceptor接口,自定义拦截器
public class FeignAuthRequestInterceptor implements RequestInterceptor {
private String tookenId;
public FeignAuthRequestInterceptor(String tookenId) {
this.tookenId = tookenId;
}
@Override
public void apply(RequestTemplate requestTemplate) {
requestTemplate.header("Authorization",tookenId);
}
}
- 通过配置类将拦截器配置到Feign中
- 服务提供端加入拦截器
Client设置
Feign中默认使用JDK原生的URLConnection发送HTTP请求,可以自定义实现替换,一般使用feign自带的,比如OkHTTPClient,apche httpclient等,配置方式相同。只要引入依赖,springboot自动装配。
比如使用apche httpclient
- 引入依赖
<dependency>
<groupId>io.github.openfeign</groupId>
<atrifactId>feign-httpclient</atrifactId>
</dependency>
- 配置类
feign:
# 使用apche httpclient
httpclient: true
# 最大连接数
max-connections: 200
# 单路径最大链接数
max-connections-pre-rote: 50
超时时间配置
- 注入配置:
@Configuration
public class FeignConfig {
@Bean
public Request.Options options(){
return new Request.Options(200,400);
}
}
基本使用案例
本案例基于nacos作为注册中心和配置中心,用OpenFeign实现远程过程调用。
服务提供端
- 引入nacos相关依赖,用于将服务注册到nacos中
<!--注册到nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--nacos拉取配置-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
- 配置nacos,配置注册中心地址
spring:
application:
name: SMS-cache
profiles:
active: dev
cloud:
nacos:
discovery:
server-addr: 47.92.xxx.xxx:8848
config:
server-addr: 47.92.xxx.xxx:8848
file-extension: yml
redis:
host: 47.92.xxx.xxx
port: 6379
- 开启nacos配置,启动类标记注解@EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
public class CacheStarterApp {
public static void main(String[] args) {
SpringApplication.run(CacheStarterApp.class,args);
}
}
- 提供restful接口
/**
* @Author Cookie
* @Date 2024/6/22 0:42
*/
@RestController
@Slf4j
public class CacheController {
@Autowired
private RedisClient redisClient;
@PostMapping(value = "/cache/hset/{key}")
void hSet(@PathVariable("key") String key, @RequestBody Map map){
log.info("【缓存模块】hSet方法存储数据成功key={},value{}",key,map);
redisClient.putMap(key,map);
}
@GetMapping(value = "/cache/set/{key}/{value}")
void set(@PathVariable("key") String key, @PathVariable("value") String value){
log.info("【缓存模块】set方法存储数据成功key={},value{}",key,value);
}
}
消费者
- 引入相关依赖,nacos、OpenFeign等
<!--注册到nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--nacos拉取配置-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
- 配置nacos
同被调用端,将服务注册到nacos中
- 标记启动类,使用@EnableFeignClients自动装配OpenFeign
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class TestAppStarter {
public static void main(String[] args) {
SpringApplication.run(TestAppStarter.class,args);
}
}
- 消费者调用提供者接口,通过@FeignClient注解实现。
@FeignClient("SMS-cache")
public interface CacheClient {
@PostMapping(value = "/cache/hset/{key}")
void hSet(@PathVariable("key") String key, @RequestBody Map map);
@GetMapping(value = "/cache/set/{key}/{value}")
void set(@PathVariable("key") String key, @PathVariable("value") String value);
}
@FeignClient中有以下属性:
- value:指定服务的名称,要和注册中心中服务提供者名称一致
- name:指定该类的容器名称,即ioc中的类ID
- url: url一般用于调试,可以手动指定@FeignClient调用的地址
- decode404:当发生http 404错误时,如果该字段位true,会调用decoder进行解码,否则抛出FeignException
- configuration: Feign配置类,可以自定义Feign的Encoder、Decoder、LogLevel、Contract
- fallback: 服务容错处理类
- fallbackFactory: 工厂类,用于生成fallback类示例,通过这个属性我们可以实现每个接口通用的容错逻辑
- path: 定义当前FeignClient的统一前缀,项目中配置了server.context-path,server.servlet-path时使用
- 测试,注入消费者调用提供者接口的接口,由组件自动生产动态代理类
@SpringBootTest
@RunWith(SpringRunner.class)
public class ClientBusinessMapperTest {
@Autowired
private ClientBusinessMapper mapper;
@Autowired
private CacheClient cacheClient;
@Test
public void findById() throws JsonProcessingException {
ClientBusiness clientBusiness = mapper.findById(1L);
ObjectMapper objectMapper = new ObjectMapper();
Map map = objectMapper.readValue(objectMapper.writeValueAsString(clientBusiness), Map.class);
cacheClient.hSet("test"+clientBusiness.getApikey(),map);
System.out.println("success...");
}
}