这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
sudo docker run \
--name postgres \
-p 5432:5432 \
-e POSTGRES_DB=quarkus_test \
-e POSTGRES_USER=quarkus \
-e POSTGRES_PASSWORD=123456 \
-d \
postgres:15
create table person (
id serial primary key,
name varchar(255),
gender varchar(255),
age int,
external_id int
);
insert into person(name, age, gender, external_id) values('John Smith', 25, 'MALE', 10);
insert into person(name, age, gender, external_id) values('Paul Walker', 65, 'MALE', 20);
insert into person(name, age, gender, external_id) values('Lewis Hamilton', 35, 'MALE', 30);
insert into person(name, age, gender, external_id) values('Veronica Jones', 20, 'FEMALE', 40);
insert into person(name, age, gender, external_id) values('Anne Brown', 60, 'FEMALE', 50);
insert into person(name, age, gender, external_id) values('Felicia Scott', 45, 'FEMALE', 60);
quarkus.datasource.db-kind=postgresql
quarkus.datasource.username=quarkus
quarkus.datasource.password=123456
quarkus.datasource.reactive.url=postgresql://192.168.0.99:5432/quarkus_test
我这里,application.properties的路径是:/home/lighthouse/config/quarkus/application.properties
检查application.properties文件的可读性,执行以下命令设置
sudo chmod a+r application.properties
docker run -idt \
--name quarkus \
-p 8080:8080 \
-v /home/lighthouse/config/quarkus/application.properties:/application/config/application.properties \
bolingcavalry/quarkus-virual-threads-demo:x64-0.0.3
上述命令中,quarkus应用的镜像bolingcavalry/quarkus-virual-threads-demo:x64-0.0.3是我提前准备好的,本篇只管使用即可,至于如何制作此镜像,接下来的文章会有详细说明
用sudo docker logs quarkus命令查看启动日志,如果启动成功,信息如下所示
__ ____ __ _____ ___ __ ____ ______
--/ __ \/ / / / _ | / _ \/ //_/ / / / __/
-/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/
2022-10-16 11:46:41,406 INFO [io.quarkus] (main) quarkus-virual-threads-demo 1.0-SNAPSHOT on JVM (powered by Quarkus 2.13.2.Final) started in 0.804s. Listening on: http://0.0.0.0:8080
2022-10-16 11:46:41,414 INFO [io.quarkus] (main) Profile prod activated.
2022-10-16 11:46:41,414 INFO [io.quarkus] (main) Installed features: [cdi, reactive-pg-client, resteasy-reactive, resteasy-reactive-jackson, smallrye-context-propagation, vertx]
对于虚拟线程的接口http://192.168.0.1:8080/vt/persons/1,可以尝试多次访问,可见每次返回的虚拟线程Id都不一样,而使用线程池的接口http://192.168.0.1:8080/pool/persons/1,多次访问,返回的始终是同一个线程的id
基本功能似乎没有问题,接下来可以压测了,用数据说话
import http from 'k6/http';
import { sleep, check } from 'k6';
export let options = {
vus: 30,
duration: '60s',
};
export default function () {
let r = Math.floor(Math.random() * 6) + 1;
const res = http.get(`http://192.168.0.1:8080/vt/persons/${r}`);
check(res, {
'is status 200': (res) => res.status === 200,
'body size is > 0': (r) => r.body.length > 0,
});
sleep(1);
}
docker run --rm -i loadimpact/k6 run - < k6-vt-docker.js
再次压测,得到结果如下,并发数涨了10倍,QPS也涨了10倍,很完美的线程提升,不过平均等待时间略涨
再来个狠的,并发数一口暴涨到5000试试,如下图,这么高的并发,已经无法保障100%的成功率了,好在95%也不低,另外平均等待时间从39毫秒暴涨到6.26秒,至于QPS当然不会太高,仅比300并发的时候高了百分之五十
import http from 'k6/http';
import { sleep, check } from 'k6';
export let options = {
vus: 30,
duration: '60s',
};
export default function () {
let r = Math.floor(Math.random() * 6) + 1;
const res = http.get(`http://192.168.0.1:8080/pool/persons/${r}`);
check(res, {
'is status 200': (res) => res.status === 200,
'body size is > 0': (r) => r.body.length > 0,
});
sleep(1);
}
30并发的压测结果如下,和使用虚拟线程并无区别
300并发压测结果如下,和使用虚拟线程并无区别
在响应式web服务中,并且关联的数据库操作也是响应式的,相比传统的线程池模型,虚拟线程并未带来明显收益
不甘心啊,接下来就换成SpringBoot应用,模拟咱们日常开发最常见的数据库访问场景,看看相比之下,差距有多大?
sudo docker stop quarkus
spring.datasource.url=jdbc:postgresql://42.193.162.141:5432/quarkus_test
spring.datasource.username=quarkus
spring.datasource.password=123456
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.datasource.hikari.maximum-pool-size=256
我这里,application.properties的路径是:/home/lighthouse/config/springboot/application.properties
检查application.properties文件的可读性,执行以下命令设置
sudo chmod a+r application.properties
docker run -idt \
--name springboot \
-p 8080:8080 \
-v /home/lighthouse/config/springboot/application.properties:/application/BOOT-INF/classes/application.properties \
bolingcavalry/springboot-postgresql-demo:x64-0.0.3
上述命令中,springboot应用的镜像bolingcavalry/springboot-postgresql-demo:x64-0.0.3是我提前准备好的,本篇只管使用即可,至于如何制作此镜像,接下来的文章会有详细说明
看到以下日志,证明应用启动成功
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.4)
2022-10-16 13:01:01.022 INFO 1 --- [ main] .b.s.SpringbootPostgresqlDemoApplication : Starting SpringbootPostgresqlDemoApplication v0.0.1-SNAPSHOT using Java 11.0.13 on 5c25db81639e with PID 1 (/application/BOOT-INF/classes started by root in /application)
2022-10-16 13:01:01.025 INFO 1 --- [ main] .b.s.SpringbootPostgresqlDemoApplication : No active profile set, falling back to 1 default profile: "default"
2022-10-16 13:01:01.795 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-10-16 13:01:01.857 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 47 ms. Found 1 JPA repository interfaces.
2022-10-16 13:01:02.392 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-10-16 13:01:02.405 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-10-16 13:01:02.405 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-10-16 13:01:02.492 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-10-16 13:01:02.492 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1364 ms
2022-10-16 13:01:02.701 INFO 1 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-10-16 13:01:02.741 INFO 1 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.11.Final
2022-10-16 13:01:02.867 INFO 1 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-10-16 13:01:02.942 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-10-16 13:01:03.164 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2022-10-16 13:01:03.179 INFO 1 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2022-10-16 13:01:03.688 INFO 1 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-10-16 13:01:03.695 INFO 1 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
import http from 'k6/http';
import { sleep, check } from 'k6';
export let options = {
vus: 30,
duration: '60s',
};
export default function () {
let r = Math.floor(Math.random() * 6) + 1;
const res = http.get(`http://192.168.0.1:8080/springboot/persons/${r}`);
check(res, {
'is status 200': (res) => res.status === 200,
'body size is > 0': (r) => r.body.length > 0,
});
sleep(1);
}
本篇咱们从零开发一个quarkus应用,支持虚拟线程响应web服务,响应式操作postgresql数据库,并且在quarkus官方还未支持的情况下,率先并将其制作成docker镜像
quarkus是如何支持虚拟线程的呢?今天咱们一起来阅读quarkus源码,学习从框架开发视角去添加新特性,除了开阔眼界,也为为自己的设计能力提升增加有效的参考信息
ThreadLocal,这个多线程场景中重要的特性,在虚拟线程领域兴风作浪,为了应付它,quarkus也是够拼,今天咱们就来聊聊这个话题,在轻松的气氛中结束《支持JDK19虚拟线程的web框架》系列
本篇咱们从零开发一个quarkus应用,支持虚拟线程响应web服务,响应式操作postgresql数据库,并且在quarkus官方还未支持的情况下,率先并将其制作成docker镜像