5

微服务设计指导-spring cloud2.0面向万级TPS的调优实战

 2 years ago
source link: https://blog.csdn.net/lifetragedy/article/details/121957473
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

微服务设计指导-spring cloud2.0面向万级TPS的调优实战

专栏收录该内容
6 篇文章 0 订阅

调优涉及到3个方面:

  • service prodiver端即服务提供者;
  • service facade端即服务消费端(按照J2EE的标准称呼叫facade,还有人喜欢叫proxy端,消费端是中国人的说法);
  • mysql端

service provider端

去tomcat改用undertow

在service provider端,不用tomcat而改用undertow,因为undertow是nio的,它特别适合高并发场景,而且undertow可以结合操作系统底层的jni调用使得java容器接近ng的反应能力。因此在provider端把tomcat禁用而改用undertow。undertow跟着spring boot版本走,见以下xml。

我这边spring boot用的是:<spring-boot.version>2.3.1.RELEASE</spring-boot.version>

undertow在application.yml中的配置

我这边是按照4核CPU,8GB内存来配置的。

service facade端配置

去ClosableHttpClient改用OkHttp

其实你只要引入okhttp组件即可,在spring boot.2.2.x以后都可以用okhttp 4.0.0.

然后改动application.yml中feign的客户端如下:

启用feign的compress功能它是会导致CPU升高(因此至少要用4核)

如果有用到hystrix熔断请启用大并发

service facade端不需要用undertow,你可以延用tomcat

hytrix端采用釠饿加载机制

spring cloud, dubbo和最早的ejb一样,都是远程方法调用。

远程方法调用在第一次调用时我们把它称为“寻址”,这个过程是一个network i/o的过程,它会很漫长。

一旦建立上的第一次调用联接后,它的后续访问是很快的,基本都是毫秒级的。因此第一次这种远程方法调用都会超过容忍极限。

在以前远古ejb时代,我们为了让这个“寻址过程”加速,是会在ejb的残根"stub“上对session facade bean把它的context.lookUp("jndi name");放入servlet的init方法中去的。而在我们现代化的spring cloud中也有一种叫饿加载机制以实现相同的原理:

mysql端调优

一般来说service facade(api/proxy)层是不会访问db的,最最多会访问redis、mongo一类。同时我们的nacos也连着mysql。因此对于spring cloud所要涉及到的db操作,mysql中有几个值将决定着spring cloud的读写性能,而且这几个值又对spring cloud的grpc效率影响特别严重。

我仅仅按照mysql位于8C CPU, 16GB内存,硬盘为固态硬盘即SSD来调就可以到达千级TPS(3,800TPS),它们是:

  • max_allowed_packet = 256M
  • innodb_buffer_pool_size=4G

  • innodb_buffer_pool_load_at_startup=1

  • innodb_buffer_pool_dump_at_shutdown=1

  • innodb_lock_wait_timeout = 60

  • innodb_io_capacity_max = 8000 #早其SSD或者是“高速云盘”,这个值设4000

  • innodb_io_capacity = 8000 #早其SSD或者是“高速云盘”,这个值设4000

    innodb_flush_method = O_DIRECT

    innodb_file_format = Barracuda

    join_buffer_size=2097152

    read_buffer_size=1M

    tmp_table_size = 67108864

    max_heap_table_size = 256M

    innodb_buffer_pool_chunk_size = 256M

    innodb_buffer_pool_instances = 8

    innodb_thread_concurrency = 4

    innodb_flush_log_at_trx_commit=2

    #skip-grant-tables

    transaction_isolation=READ-COMMITTED

    #from old-139 it is a default isolation level of mysql, we do not suggest use the default only difference with READ-COMMITTED is select for update but for other things is the same

    #transaction_isolation = REPEATABLE-READ

    #skip-grant-tables

    innodb_log_buffer_size = 16M #致命增加写的效率

    innodb_log_file_size = 128M #致命增加写的效率


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK