SpringCloud for Alibaba 全套文章整理本系列全部文章由75888丶编写,非盗用他人文档,由于是根据教学视频进行整理,可能存在类似,保证全部手打,非复制粘贴他人文档、代码。从文章的编写到代码,全部跟进行了测试,保证可用。
文章末尾提供了githup、gitee的源代码地址,如有疑问或问题,可在文章底部留言,我们共同讨论。如解决您的问题,欢迎留言点赞!最后感谢您的阅读。
SpringCloud、SpringBoot群聊:958923746
第一章:SpringCloud for Alibaba 环境准备、版本统一
第二章:SpringCloud for Alibaba 集成 Ncoas服务注册与发现、雪崩保护、集群配置等
第三章:SpringCloud for Alibaba 集成Ribbon负载均衡器
第四章:SpringCloud for Alibaba 集成LoadBalancer
第五章:SpringCloud for Alibaba 集成OpenFeign解决调用远程地址硬编码等问题
第六章:SpringCloud for Alibaba 集成Nacos配置中心
第七章:SpringCloud for Alibaba 集成Sentinel高可用防护组件
第八章:SpringCloud for Alibaba 集成Seata之分布式事务
第九章:SpringCloud for Alibaba 集成Seata解决分布式事务
第十章:SpringCloud for Alibaba 集成Gateway之网关配置、了解
第十一章:SpringCloud for Alibaba 集成Gateway之路由断言工厂
第十二章:SpringCloud for Alibaba 集成Gateway之过滤器、自定义全局过滤器
第十三章:SpringCloud for Alibaba 集成Gateway整合Sentinel
第十四章:SpringCloud for Alibaba 集成SkyWalking之特性介绍
第十五章:SpringCloud for Alibaba 集成SkyWalking之环境搭建
第十六章:SpringCloud for Alibaba 集成SkyWalking之集群配置与集成logback等日志框架
Feign是声明式、模块化的HTTP客户端。可以做到使用HTTP请求远程服务时就像调用本地方法一样的体验。完全不知是远程方法。
解决在客户端调用服务端使用RestTemplate硬编码远程地址的问题
有什么好处?SpringCloud OpenFeign对Feign进行增强,支持了SpingMVC注解,整合了nacos与Ribbon,使用更加方便
快速使用 POM代码部分org.springframework.boot spring-boot-starter-web com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery org.springframework.cloud spring-cloud-starter-openfeign
@FeignClient(name = "stock-service",path = "stock")
public interface StockFeignService {
@RequestMapping("/reduct")
String reduct();
}
客户端调用服务端代码
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private StockFeignService stockFeignService;
@RequestMapping("/add")
public String add() {
System.out.println("下单成功");
//使用nacos的服务名称来调用远程服务
String reduct = stockFeignService.reduct();
System.out.println(reduct);
return "通过Feign调用微服务" + reduct;
}
}
Feign日志配置
灵活日志打印输入,可以看到调用远程服务的地址,响应时间,请求头、请求参数、返回数据等信息。
第一种日志配置 (全局配置)代码配置
@Configuration
public class FeignConfig {
@Bean
public Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
yml配置
#正常启动项目,默认是info等级,而feign的日志级别是debug,需要修改项目日志级别
#指定项目包的日志级别是debug
logging:
level:
com.wangsh.spirngcloudforalibaba.feign: debug
(局部配置)代码配置
//@Configuration
public class FeignConfig {
@Bean
public Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
@FeignClient(name = "product-nacos-service", path = "product",configuration = FeignConfig.class)
public interface ProductFeignServcice {
@RequestMapping("/get/{id}")
String get(@PathVariable("id") Integer id);
}
第二种日志配置
yml
#正常启动项目,默认是info等级,而feign的日志级别是debug,需要修改项目日志级别
#指定项目包的日志级别是debug
logging:
level:
com.wangsh.spirngcloudforalibaba.feign: debug
#第二种办法设置指定服务打印日志,设置服务日志级别,打印调用服务日志
feign:
client:
config:
product-nacos-service:
loggerLevel: BASIC
stock-service:
loggerLevel: BASIC
契约配置
什么是契约配置?
在调用远程服务的时候,通过openFeign可以使用MVC的注解进行调用。但是如果是早起的SpringCloud1.X版本中,无法支持用MVC的注解调用远程服务。
那么就可以使用契约配置,可以告知开发者有哪些MVC注解没有被替换,在启动的时候会报错。这样既可以保留继续使用openFeign,不需要更换jar。
只有老版本的才会使用Feign原生注解,目前使用openFeign都是整合的MVC注解
@PathVariable 原生注解: @Param @RequestMapping 原生注解: @RequestLine
feign:
client:
config:
stock-service: #微服务名称
loggerLevel: BASIC
contract: feign.Contract.Default #契约配置,将feign注解还原成原生注解
超时时间配置
feign:
client:
config:
stock-service: #微服务名称
loggerLevel: BASIC
#连接超时时间,默认2S。网络调用时间
connecTimeout: 5000
#请求处理超时时间,服务处理时间
readTimeout: 3000
自定义拦截器
第一种:通过代码配置全局生效
@Configuration
public class CustomFeignIntercepter implements RequestInterceptor{
@Override
public void apply(RequestTemplate requestTemplate) {
System.out.println("====================");
requestTemplate.header("test","123456");
}
@Bean
public CustomFeignIntercepter feignIntercepter(){
return new CustomFeignIntercepter();
}
}
第二种:通过代码+yml进行配置拦截器生效
feign:
client:
config:
stock-service:
#通过yam配置拦截器生效
requestInterceptors[0]:
com.wangsh.spirngcloudforalibaba.intercepter.CustomFeignIntercepter
@Configuration
public class CustomFeignIntercepter implements RequestInterceptor{
@Override
public void apply(RequestTemplate requestTemplate) {
System.out.println("====================");
requestTemplate.header("test","123456");
}
// @Bean
// public CustomFeignIntercepter feignIntercepter(){
// return new CustomFeignIntercepter();
// }
}
源码提供(照顾某些git很卡的同学,提供gitee地址)
githup地址:https://github.com/wangsh6379/SpringCloud-for-Alibaba gitee地址:https://gitee.com/75888/spring-cloud-for-alibaba



