SpringCloud Alibaba 2021微服务实战二十五 openfeign最佳实践
2021/5/25 18:16:59
本文主要是介绍SpringCloud Alibaba 2021微服务实战二十五 openfeign最佳实践,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1,依赖pom
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>cloud_alibaba_learn</artifactId> <groupId>com.liu.learn</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud_order</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-loadbalancer --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> </dependencies> </project>
配置文件
server: port: 8090 spring: #profiles: #active: dev application: main: allow-bean-definition-overriding: true name: order-service #服务名 cloud: nacos: discovery: server-addr: 127.0.0.1:8848 feign: sentinel: #为feign整合Sentinel enabled: true okhttp: enabled: true httpclient: enabled: false max-connections: 1000 max-connections-per-route: 100 server: undertow: max-http-post-size: 0 # 设置IO线程数, 它主要执行非阻塞的任务,它们会负责多个连接, 默认设置每个CPU核心一个线程,数量和CPU 内核数目一样即可 io-threads: 4 # 阻塞任务线程池, 当执行类似servlet请求阻塞操作, undertow会从这个线程池中取得线程,它的值设置取决于系统的负载 io-threads*8 worker-threads: 32 # 以下的配置会影响buffer,这些buffer会用于服务器连接的IO操作,有点类似netty的池化内存管理 # 每块buffer的空间大小,越小的空间被利用越充分 buffer-size: 1024 # 每个区分配的buffer数量 , 所以pool的大小是buffer-size * buffers-per-region # buffers-per-region: 1024 # 这个参数不需要写了 # 是否分配的直接内存 direct-buffers: true
feign客户端
import com.liu.consts.SecurityConstants; import com.liu.consts.ServiceNameConstants; import com.liu.entity.SysLog; import com.liu.feign.factory.RemoteLogServiceFallbackFactory; import com.liu.rs.CommonResult; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; //value = ServiceNameConstants.LOG_SERVICE 改为自己服务名 //CommonResult 返回值类 @FeignClient(contextId = "remoteLogService", value = ServiceNameConstants.LOG_SERVICE, fallbackFactory = RemoteLogServiceFallbackFactory.class) public interface RemoteLogService { /** * 保存日志 * @param sysLog 日志实体 * @param from 内部调用标志 * @return succes、false */ @PostMapping("/log") CommonResult saveLog(@RequestBody SysLog sysLog, @RequestHeader(SecurityConstants.FROM) String from); }
import com.liu.feign.RemoteLogService; import com.liu.feign.fallback.RemoteLogServiceFallbackImpl; import org.springframework.cloud.openfeign.FallbackFactory; import org.springframework.stereotype.Component; @Component public class RemoteLogServiceFallbackFactory implements FallbackFactory<RemoteLogService> { @Override public RemoteLogService create(Throwable throwable) { RemoteLogServiceFallbackImpl remoteLogServiceFallback = new RemoteLogServiceFallbackImpl(); remoteLogServiceFallback.setCause(throwable); return remoteLogServiceFallback; } }
import com.liu.entity.SysLog; import com.liu.feign.RemoteLogService; import com.liu.rs.CommonResult; import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @Slf4j @Component public class RemoteLogServiceFallbackImpl implements RemoteLogService { @Setter private Throwable cause; /** * 保存日志 * @param sysLog 日志实体 * @param from 内部调用标志 * @return succes、false */ @Override public CommonResult saveLog(SysLog sysLog, String from) { log.error("feign 插入日志失败,{}", cause.getMessage()); cause.printStackTrace(); log.error("feign 插入日志失败,{}", cause.getLocalizedMessage()); return null; } }
-
fallback和fallbackFactory是有一定的冲突的,你可以理解为fallbackFactory是fallback的一个增强版,
-
因为fallback只是复写方法 ,没办法打印日志,而这个更强一点,fallbackFactory可以拦截异常这一块
这篇关于SpringCloud Alibaba 2021微服务实战二十五 openfeign最佳实践的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南
- 2024-09-30Dnd-Kit学习:新手快速入门指南