做设计有哪些好用的素材网站有哪些,基于阿里云的网站开发,做网站一定要认证吗,网站如何跟域名绑定一、简介
OpenFeign客户端是一个web声明式http远程调用工具#xff0c;直接可以根据服务名称去注册中心拿到指定的服务IP集合#xff0c;提供了接口和注解方式进行调用#xff0c;内嵌集成了Ribbon本地负载均衡器。
二、SpringCloud集成OpenFeign
版本说明#xff1a;
S…一、简介
OpenFeign客户端是一个web声明式http远程调用工具直接可以根据服务名称去注册中心拿到指定的服务IP集合提供了接口和注解方式进行调用内嵌集成了Ribbon本地负载均衡器。
二、SpringCloud集成OpenFeign
版本说明
Spring Cloud VersionHoxton.SR12
Spring Boot Version2.3.12.RELEASE不同版本源码可能会有差异具体参考版本对照
1、引入依赖
!--openfeign--
dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId
/dependency
2、在启动类或者配置类上加EnableFeignClients注解
SpringBootApplication
EnableFeignClients
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
3、声明Feign接口
/*** 用户 远程服务*///1.1、如果是注册到nacos服务则填写nacos服务名称contextId唯一标识
FeignClient(value user,contextId userInfoClient01)
public interface UserFeignApi{/** 获取用户信息 */GetMapping(/user/info)User getInfo(Long id);
}//2、如果调用第三方接口直接通过url调用 (value服务名,url远程调用地址,path统一前缀)
FeignClient(value 自定义服务名,url http://127.0.0.1:8080,path /user)
public interface UserFeignApi{/** 获取用户信息 */GetMapping(/info)User getInfo(Long id);
}
相同服务名的坑在使用了Feign的SpringCloud项目启动时当存在多个FeignClient接口且name值相同就会报错 A bean with that name has already been defined and overriding is disabled。这是因为SpringBoot会为每个FeginClient创建Bean但当name值相同时SpringBoot会认为是有两个相同名字的Bean需要被创建由此抛出异常。给每个FeignClient都设置自己唯一的contextId
4、测试feign接口
RestController
RequestMapping(/order)
public class OrderController {Autowiredprivate UserFeignApi userFeignApi;GetMapping(/getInfo)public User getInfo(Long id) {return userFeignApi.getInfo(id);}5、日志配置OpenFeign feign: client:config: default: loggerLevel: FULL logging:level:com.atguigu.gulimall: debug # 需要打印日志的包package# 这里用default就是全局配置如果是写服务名称user或者唯一标识contextId则是针对某个微服务的配置
# 日志级别 NONE默认不打印 BASIC打印简单信息 HEADERS打印头信息 FULL打印全部信息默认 NONE
6、OpenFeign超时配置
使用配置文件配置是最推荐的也是在项目中使用最多的。 feign:client:config:default: # 默认全局配置也可配置单独FeignClientdefault替换对应contextIdconnectTimeout: 5000 # 连接超时时间 默认值1000毫秒readTimeout: 5000 # 读取超时时间 默认值1000毫秒
7、OpenFeign拦截器
拦截器是OpenFeign可用的一种强大的工具它可以被用来在请求和响应前后进行一些额外的处理
1、通过OpenFeign请求拦截器设置统一请求头
public class MyHeaderInterceptor implements RequestInterceptor {private static String headerName token;Overridepublic void apply(RequestTemplate requestTemplate) {// 在这里添加额外的处理逻辑添加请求头RequestAttributes requestAttributes RequestContextHolder.getRequestAttributes();if (requestAttributes instanceof ServletRequestAttributes) {ServletRequestAttributes attributes (ServletRequestAttributes) requestAttributes;HttpServletRequest request attributes.getRequest();String value request.getHeader(headerName);requestTemplate.header(headerName, value);}}
}
在配置文件中添加拦截器配置
feign:client:config:# 默认配置 如果不单独配置每个服务会走默认配置default:request-interceptors:- com.config.MyHeaderInterceptor