千家信息网

Spring Cloud怎么配置tomcat和Hystrix

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章主要讲解了"Spring Cloud怎么配置tomcat和Hystrix",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Spring Cloud
千家信息网最后更新 2026年11月03日Spring Cloud怎么配置tomcat和Hystrix

这篇文章主要讲解了"Spring Cloud怎么配置tomcat和Hystrix",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Spring Cloud怎么配置tomcat和Hystrix"吧!

1、替换 tomcat

首先,把 tomcat 换成 undertow,这个性能在 Jmeter 的压测下,undertow 比 tomcat 高一倍 第一步,pom 修改去除tomcat

        org.springframework.boot        spring-boot-starter-web                                                org.springframework.boot                        spring-boot-starter-tomcat                                org.springframework.boot        spring-boot-starter-undertow

第二步,配置

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
2、替换 HTTPClient

第一步,加依赖

        io.github.openfeign        feign-httpclient

第二部,在 application.yml或者 bootstrap.yml 里面配置

# feign配置feign:  hystrix:    # 在feign中开启hystrix功能,默认情况下feign不开启hystrix功能    enabled: true  ## 配置httpclient线程池  httpclient:    enabled: true  okhttp:    enabled: false

第三步,配置 HTTPClient Bean

import java.util.Timer;import java.util.TimerTask;import java.util.concurrent.TimeUnit;import org.apache.http.client.HttpClient;import org.apache.http.client.config.RequestConfig;import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;import org.apache.http.impl.client.HttpClientBuilder;import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class HttpPool {    @Bean    public HttpClient httpClient(){        System.out.println("===== Apache httpclient 初始化连接池开始===" );        // 生成默认请求配置        RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();        // 超时时间        requestConfigBuilder.setSocketTimeout(5 * 1000);        // 连接时间        requestConfigBuilder.setConnectTimeout(5 * 1000);        RequestConfig defaultRequestConfig = requestConfigBuilder.build();        // 连接池配置        // 长连接保持30秒        final PoolingHttpClientConnectionManager pollingConnectionManager = new PoolingHttpClientConnectionManager(30, TimeUnit.MILLISECONDS);        // 总连接数        pollingConnectionManager.setMaxTotal(1000);        // 同路由的并发数        pollingConnectionManager.setDefaultMaxPerRoute(100);        // httpclient 配置        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();        // 保持长连接配置,需要在头添加Keep-Alive        httpClientBuilder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy());        httpClientBuilder.setConnectionManager(pollingConnectionManager);        httpClientBuilder.setDefaultRequestConfig(defaultRequestConfig);        HttpClient client = httpClientBuilder.build();        // 启动定时器,定时回收过期的连接        Timer timer = new Timer();        timer.schedule(new TimerTask() {            @Override            public void run() {                System.out.println("=====closeIdleConnections===");                pollingConnectionManager.closeExpiredConnections();                pollingConnectionManager.closeIdleConnections(5, TimeUnit.SECONDS);            }        }, 10 * 1000, 5 * 1000);        System.out.println("===== Apache httpclient 初始化连接池完毕===");        return client;    }}
3、配置 Hystrix

第一步,依赖

        org.springframework.cloud        spring-cloud-starter-hystrix

第二步,配置

# 配置hystrix的参数hystrix:  threadpool:    # default: 默认参数,作用的所有的hystrix的客户端,如果需要对某个具体的接口,可以写接口+方法名称    default:      coreSize: 500  command:    default:      fallback:        # 是否开启回退方法        enabled: true      execution:        isolation:          thread:            timeoutInMilliseconds: 30000 #缺省为1000

感谢各位的阅读,以上就是"Spring Cloud怎么配置tomcat和Hystrix"的内容了,经过本文的学习后,相信大家对Spring Cloud怎么配置tomcat和Hystrix这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

配置 线程 参数 学习 阻塞 任务 内存 内容 功能 大小 情况 接口 数量 方法 时间 空间 分配 作用 内核 取决于 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网科技公司经营范围 科技互联网技术 互联网科技视频 北京互联网络科技有限公司 郑州华宇互联网科技 老年人脱离互联网科技的作文 玖二柒互联网科技公司待遇 广西互联网科技孵化中心 华夏恒生互联网科技013171 华中科技大学互联网的牛人 美国互联网科技公司 江苏互联网智能科技软件 35岁以上互联网科技行业 易企秀互联网科技是做什么的 福建互联网展示科技馆设计 上海万顺科技互联网有限公司招聘 高科技和互联网的现代国家 山东亿川互联网科技有限责任公司 株洲百竞科技的互联网营销顾问 颐高云石互联网科技 互联网科技图片 湖北九亚互联网络科技有限公司 互联网和高科技公司 深圳市运趣互联网科技有限公司 茂名市互联网科技产业园 贵州叶脉互联网科技有限公司所属行业 智腾互联网科技有限公司 郑州欣桑互联网科技有限公司 中国科技和美国互联网 第三届互联网大会安防黑科技
0