这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
本篇内容并非数据库相关的核心知识,而是对一个实用工具的说明介绍,此工具在官方介绍中被称为Zero Config Setup (Dev Services),(零配置的设置,忒莫名其妙)
我这边简单总结为:如果你没有数据库可用,只要你有docker,quarkus应用就能进行数据库相关的开发工作,增删改查啥都行,和有数据库的时候没啥区别
看到这里,经验丰富的您应该会觉得:既然有docker,那么用docker run装一个数据库不就行了吗,和quarkus工具有啥关系?
其实这个Zero Config Setup还算是有那么一点自己的特色,和自己动手在docker中装数据库有一丢丢区别,我做了个对比图如下
可见Zero Config Setup的好处是啥都不用配,有docker就行,劣势是必须要用mvn quarkus:dev启动应用,profile固定是dev
看到这里,您是否会这么觉得:哦,知道了,那我去建一个application-dev.properites文件,里面没有数据库配置,然后执行mvn quarkus:dev启动应用就行了,就这点内容呗,欣宸你别写了,太啰嗦...
确实内容少,但是它有坑啊,所以请您随本文一同实战吧,等到操作成功的那一刻,新技能get带来的舒适感相信您也不会拒绝,然后用起Zero Config Setup直呼666
接下来咱们亲自动手体验这个Zero Config Setup,看看适不适合开发阶段使用
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
[INFO] Compiling 1 source file to /Users/zhaoqin/github/blog_demos/quarkus-tutorials/basic-db/target/test-classes
Listening for transport dt_socket at address: 5005
2022-05-08 10:52:56,714 ERROR [com.git.doc.api.asy.ResultCallbackTemplate] (docker-java-stream-1954350275) Error during callback: com.github.dockerjava.api.exception.InternalServerErrorException: Status 500: {"message":"Head \"https://registry-1.docker.io/v2/testcontainers/ryuk/manifests/0.3.3\": unauthorized: incorrect username or password"}
at org.testcontainers.shaded.com.github.dockerjava.core.DefaultInvocationBuilder.execute(DefaultInvocationBuilder.java:247)
at org.testcontainers.shaded.com.github.dockerjava.core.DefaultInvocationBuilder.lambda$executeAndStream$1(DefaultInvocationBuilder.java:269)
at java.base/java.lang.Thread.run(Thread.java:829)
2022-05-08 10:52:57,019 INFO [io.qua.dep.dev.IsolatedDevModeMain] (main) Attempting to start live reload endpoint to recover from previous Quarkus startup failure
2022-05-08 10:52:57,037 ERROR [io.qua.dep.dev.IsolatedDevModeMain] (main) Failed to start quarkus: java.lang.RuntimeException: io.quarkus.builder.BuildException: Build failure: Build failed due to errors
[error]: Build step io.quarkus.datasource.deployment.devservices.DevServicesDatasourceProcessor#launchDatabases threw an exception: java.lang.RuntimeException: com.github.dockerjava.api.exception.InternalServerErrorException: Status 500: {"message":"Head \"https://registry-1.docker.io/v2/testcontainers/ryuk/manifests/0.3.3\": unauthorized: incorrect username or password"}
at io.quarkus.datasource.deployment.devservices.DevServicesDatasourceProcessor.startDevDb(DevServicesDatasourceProcessor.java:314)
at io.quarkus.datasource.deployment.devservices.DevServicesDatasourceProcessor.launchDatabases(DevServicesDatasourceProcessor.java:121)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at io.quarkus.deployment.ExtensionLoader$2.execute(ExtensionLoader.java:882)
at io.quarkus.builder.BuildContext.run(BuildContext.java:277)
at org.jboss.threads.ContextHandler$1.runWith(ContextHandler.java:18)
at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2449)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1478)
at java.base/java.lang.Thread.run(Thread.java:829)
at org.jboss.threads.JBossThread.run(JBossThread.java:501)
Caused by: com.github.dockerjava.api.exception.InternalServerErrorException: Status 500: {"message":"Head \"https://registry-1.docker.io/v2/testcontainers/ryuk/manifests/0.3.3\": unauthorized: incorrect username or password"}
at org.testcontainers.shaded.com.github.dockerjava.core.DefaultInvocationBuilder.execute(DefaultInvocationBuilder.java:247)
at org.testcontainers.shaded.com.github.dockerjava.core.DefaultInvocationBuilder.lambda$executeAndStream$1(DefaultInvocationBuilder.java:269)
at java.base/java.lang.Thread.run(Thread.java:829)
quarkus.hibernate-orm.sql-load-script=import.sql
[INFO] Nothing to compile - all classes are up to date
Listening for transport dt_socket at address: 5005
2022-05-08 17:51:48,010 INFO [io.qua.dat.dep.dev.DevServicesDatasourceProcessor] (build-12) Dev Services for the default datasource (postgresql) started.
2022-05-08 17:51:48,011 INFO [io.qua.hib.orm.dep.HibernateOrmProcessor] (build-5) Setting quarkus.hibernate-orm.database.generation=drop-and-create to initialize Dev Services managed database
__ ____ __ _____ ___ __ ____ ______
--/ __ \/ / / / _ | / _ \/ //_/ / / / __/
-/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/
2022-05-08 17:51:48,244 INFO [io.agr.pool] (Quarkus Main Thread) Datasource '<default>': Initial size smaller than min. Connections will be created when necessary
Hibernate:
drop table if exists known_fruits cascade
2022-05-08 17:51:48,510 WARN [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) SQL Warning Code: 0, SQLState: 00000
2022-05-08 17:51:48,511 WARN [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) table "known_fruits" does not exist, skipping
Hibernate:
drop sequence if exists known_fruits_id_seq
2022-05-08 17:51:48,512 WARN [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) SQL Warning Code: 0, SQLState: 00000
2022-05-08 17:51:48,512 WARN [org.hib.eng.jdb.spi.SqlExceptionHelper] (JPA Startup Thread: <default>) sequence "known_fruits_id_seq" does not exist, skipping
Hibernate: create sequence known_fruits_id_seq start 10 increment 1
Hibernate:
create table known_fruits (
id int4 not null,
name varchar(40),
primary key (id)
)
Hibernate:
alter table if exists known_fruits
add constraint UK_57g3m8wr3qxoj706a6hsqg6ye unique (name)
Hibernate:
INSERT INTO known_fruits(id, name) VALUES (1, 'Cherry')
Hibernate:
INSERT INTO known_fruits(id, name) VALUES (2, 'Apple')
Hibernate:
INSERT INTO known_fruits(id, name) VALUES (3, 'Banana')
2022-05-08 17:51:48,558 INFO [io.quarkus] (Quarkus Main Thread) basic-db 1.0-SNAPSHOT on JVM (powered by Quarkus 2.7.3.Final) started in 2.706s.
2022-05-08 17:51:48,559 INFO [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
2022-05-08 17:51:48,559 INFO [io.quarkus] (Quarkus Main Thread) Installed features: [agroal, cdi, hibernate-orm, jdbc-postgresql, narayana-jta, smallrye-context-propagation]
--
--
Tests paused
Press [r] to resume testing, [o] Toggle test output, [:] for the terminal, [h] for more options>
ibmcom/db2:11.5.0.0a
mcr.microsoft.com/mssql/server:2017-CU12
quarkus.datasource.devservices.container-properties.TC_MY_CNF=testcontainers/mysql-conf
接下来聊聊一次偶然的发现,既有惊喜也有疑惑,还希望亲爱的读者能够给予指导和建议
回想一下,当您使用mvn quarkus:dev启动应用后,控制台提示如下信息
Press [r] to resume testing, [o] Toggle test output, [:] for the terminal, [h] for more options>
You are now in Quarkus Terminal. Your app is still running. Use `help` or tab completion to explore, `quit` or `q` to return to your application.
quarkus$
quarkus$ postgres print-command
PGPASSWORD=quarkus psql --host=localhost --port=49294 --username=quarkus default
本以为发现了quarkus的惊天秘密,今后开发中随时可以连接此数据库查看数据,结果发现单元测试对数据的任何写操作,都不会改变上图表中的内容,这和使用自己的数据库是完全不同的,上一篇文章中,咱们执行完单元测试后,写操作的结果在数据库中是可以查到的
目前还没有查到上述问题的原因,估计是quarkus自己内部的处理机制吧,例如自动rollback,或者某些程度的可见性隔离等,这都是猜的,亲爱的读者,如果您知道了原因,麻烦您在回复中指点一二,谢谢了
其实这也不是什么问题,不去查那个表就行了,单元测试的读写功能是正常的,也就是说单元测试中,数据发生变化后程序可以读取到变化后的数据,只是我们用工具看不到变化而已(官方文档也没有提及用工具去连接那个表,应该是不推荐这么做)
至此,quarkus的Zero Config Setup体验完成,希望这个小技能可以对您有所帮助,以更简单的操作度过编码和自测的时光
《quarkus数据库篇》系列的开篇,编码实战最基础的数据库增删改查,数据库用的是PostgreSQL,在官方demo基础上进一步精简,极速入门quarkus数据库操作
本篇咱们从零开发一个quarkus应用,支持虚拟线程响应web服务,响应式操作postgresql数据库,并且在quarkus官方还未支持的情况下,率先并将其制作成docker镜像
本篇咱们从零开发一个quarkus应用,支持虚拟线程响应web服务,响应式操作postgresql数据库,并且在quarkus官方还未支持的情况下,率先并将其制作成docker镜像
本次实验学习记录主题为“FIFO_IP核实现算术求和”,主要内容是上位机通过串口向FPGA发送一定规格的数字矩阵,FPGA对矩阵处理,按规定逻辑实现求和运算,将结果返回串口转发至上位机。