提高网站流量,天猫分销平台,做网站的优点,关于学院网站建设的通知Feign 配置优化hystrix配置 优化ribbon 优化Servlet 容器 优化Zuul配置 优化 文章目录 1.Servlet 容器 优化2.Feign 配置优化3.Zuul配置 优化4.hystrix配置 优化5.ribbon 优化 1.Servlet 容器 优化
默认情况下, Spring Boot 使用 Tomcat 来作为内嵌的 Servlet 容器, 可以将 We…Feign 配置优化hystrix配置 优化ribbon 优化Servlet 容器 优化Zuul配置 优化 文章目录 1.Servlet 容器 优化2.Feign 配置优化3.Zuul配置 优化4.hystrix配置 优化5.ribbon 优化 1.Servlet 容器 优化
默认情况下, Spring Boot 使用 Tomcat 来作为内嵌的 Servlet 容器, 可以将 Web 服务器切换到 Undertow 来提高应用性能, Undertow 是红帽公司开发的一款基于 NIO 的高性能 Web 嵌入式。
Zuul使用的内置容器默认是Tomcat, 可以将其换成undertow, 可以显著减少线程的数量。 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactIdexclusionsexclusiongroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-tomcat/artifactId/exclusion/exclusions
/dependency
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-undertow/artifactId
/dependencyserver:undertow:io-threads: 16worker-threads: 256buffer-size: 1024buffers-per-region: 1024direct-buffers: trueserver.undertow.io-threads: 设置IO线程数, 主要执行非阻塞的任务。server.undertow.worker-threads: 阻塞任务线程池 。server.undertow.buffer-size: 类似netty的池化内存管理, buffer的大小, 小的空间被利用更充分。server.undertow.buffers-per-region: 每个区分配的buffer数量。server.undertow.direct-buffers: 是否分配的直接内存(NIO直接分配的堆外内存)。
2.Feign 配置优化
feign 默认不启用hystrix, feign.hystrix.enabledtrue 开启熔断。
feign 启用压缩也是一种有效的性能优化方式。
feign:compression:request:enabled: truemime-types: text/xml,application/xml,application/jsonresponse:enabled: truefeign HTTP请求方式选择。
feign默认使用的是基于JDK提供的URLConnection调用HTTP接口, 无线程池, Apache HttpClient和okhttp都支持配置连接池功能, 也可以使用okhttp请求方式。
HttpClient:
feign:httpclient:enabled: truemax-connections:1000max-connections-per-route: 200 okHttp:
feign:okhttp:enabled: truehttpclient:max-connections: 1000max-connections-per-route: 200 max-connections: 设置整个连接池最大连接数。max-connections-per-route: 设置路由的默认最大连接。
3.Zuul配置 优化
Hystrix有隔离策略: THREAD 以及SEMAPHORE, 默认是 SEMAPHORE 。
Zuul默认是使用信号量隔离, 信号量数量为100, 请求的并发线程超过100就会报错。
zuul:semaphore:max-semaphores: 5000为了方便ThreadLocal的使用, 可以改变隔离策略, 需要调大hystrix的线程池大小。
zuul:ribbonIsolationStrategy: THREAD
hystrix:threadpool:default:coreSize: 100maximumSize: 400allowMaximumSizeToDivergeFromCoreSize: truemaxQueueSize: -1hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize: 是否让maximumSize生效, false为只有coreSize会生效。hystrix.threadpool.default.maxQueueSize: 线程池的队列大小。hystrix.threadpool.default.maximumSize: 最大线程数。zuul.ribbon-isolation-strategy: 线程隔离策略。
4.hystrix配置 优化
需要设置参数hystrix.threadpool.default.coreSize 来指定熔断隔离的线程数, 这个数需要调优。
hystrix:threadpool:default:coreSize: 500command:default:circuitBreaker: requestVolumeThreshold: 1000fallback:enabled: trueexecution:isolation:thread:timeoutInMilliseconds: 100000 hystrix.command.default: 全局作用域, 作用的所有的hystrix的客户端, 如果需要对某个微服务, 可以写serviceId。hystrix.command.default.fallback.enabled: 是否开启回退方法。hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 请求处理的超时时间, 缺省为1000,表示默认的超时时间为1S。hystrix.threadpool.default.coreSize 核心线程池数量。hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests 回退最大线程数。hystrix.command.default.circuitBreaker.requestVolumeThreshold: 熔断器失败的个数
5.ribbon 优化
Ribbon进行客户端负载均衡的Client并不是在服务启动的时候就初始化好的, 而是在调用的时候才会去创建相应的Client, 所有第一次调用的耗时不仅仅包含发送HTTP请求的时间, 还包括了创建RibbonClient的时间。
ribbon:eager-load:enabledtrueclientsservice-1,service-2,service-nribbon.eager-load.enabled: 开启Ribbon的饥饿模式。ribbon.eager-load.clients: 指定需要饥饿加载的服务名。