The development environment

See Quarkus getting Started

  1. The installationIntelliJ IDEA.
  2. Configuration and developmentJDK, you can useJDK8, it is recommended thatJDK11. Of course if you tryNativeThat can be installedGraalVM.
  3. The installationMaven3.6.2The above.

​

Build a Quarkus app quickly

seeQuarkus Start Coding We chose the Quarkus features that appealed to us and started playing Reactive.

Once selected, click Generate Your Application to download the project.

Analysis of POM

Introduce Quarkus’ POM

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>${quarkus.platform.group-id}</groupId>
        <artifactId>${quarkus.platform.artifact-id}</artifactId>
        <version>${quarkus.platform.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
Copy the code

Quarkus version updates are frequent, but are managed through Quarkus-university-BOM. After introducing this part, the subsequent version updates will be much more convenient.

Reactive Routes

    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-vertx-web</artifactId>
    </dependency>
Copy the code

Quarkus’s Reactive Web is based on vert. x, with some annotations and encapsulation to make code writing easier.

Reactive Panache

    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-hibernate-reactive-panache</artifactId>
    </dependency>
Copy the code

Panache is a Hibernate based implementation, similar to JPA, and much simpler.

Reactive DB Client

    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-reactive-mysql-client</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-reactive-pg-client</artifactId>
    </dependency>
Copy the code

Introduce the corresponding database Client according to the database type.

OpenAPI

    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-smallrye-openapi</artifactId>
    </dependency>
Copy the code

If you like Swagger, you can introduce Quarkus’ corresponding OpenApi implementation.

Interface 1-hello1, Quarkus

code

(info = @info (title = "Hello API", version = "1.0.0", Contact = @ contact (name = "Hello API Support", url = "http://127.0.0.1:8080/swagger-ui/", Email = "[email protected]"), license = @license (name = "Apache 2.0", Url = "https://www.apache.org/licenses/LICENSE-2.0.html")))
@ApplicationScoped
public class HelloResource extends BaseResource {
    private static final Logger LOGGER = LoggerFactory.getLogger(HelloResource.class);

    @Route(path = "/hello1", methods = HttpMethod.GET)
    @operation (description = "hello1 interface ")
    void hello1(RoutingContext ctx) {
        ctx.response().end("hello1, quarkus");
    }
    
    @Route(path = "/hello2", methods = HttpMethod.GET)
    String hello2(RoutingContext ctx) {
        // non-blocking
        return "hello2, quarkus";
    }

    @Route(path = "/hello3", methods = HttpMethod.GET)
    String hello3(@Param Optional<String> name) {
        return "hello3, " + name.orElse("quarkus");
    }

    @Route(path = "/hello4", methods = HttpMethod.GET)
    Uni<String> hello4(RoutingContext context) {
        return Uni.createFrom().item("hello4, quarkus"); }}Copy the code

annotations

  1. @OpenAPIDefinition 和 @OperationAre allOpenApiAnnotations to generate interface documentation.
  2. @ApplicationScopedgenerateBean.
  3. @Route(path = "/hello1", methods = HttpMethod.GET)Define a Get interface.

​

Start the

Start in Dev mode

./mvnw compile quarkus:dev
Copy the code

packaging

./mvnw package
Copy the code

Packaging uber – jar

./mvnw package -Dquarkus.package.type=uber-jar
Copy the code

GraalVM is required to package Native

./mvnw package -Pnative
Copy the code

Packaging Native does not require GraalVM, but Docker.

./mvnw package -Pnative -Dquarkus.native.container-build=true
Copy the code

call

[root@ecs-1b4c-0002 k6]# curl http://127.0.0.1:8080/hello1
hello1, quarkus
Copy the code

performance

[root@ecs-1b4c-0002 k6]# wrk -c50 -d20s 'http://localhost:8080/hello1' --latency
Running 20s test50 connections @ http://localhost:8080/hello1 2 threads and Thread Stats Avg Stdev Max + / - Stdev Latency 0.92 ms 2.57 ms 46.89ms 92.41% Req/Sec 120.34k 22.68k 185.55k Latency Distribution 50% 125.00us 75% 398.00us 90% 2.28ms 99% 12.56 ms 4792636 requestsin20.03 s, 457.06 MBreadRequests/ SEC: 239328.58 Transfer/ SEC: 22.82MB [root@ecs-1b4c-0002 k6]# wrk -c50 -d20s 'http://localhost:8080/hello2' --latency
Running 20s test50 connections @ http://localhost:8080/hello2 2 threads and Thread Stats Avg Stdev Max + / - Stdev Latency 1.24 ms 3.09 ms 57.90ms 90.09% Req/Sec 124.35K 22.26k 194.19k 74.50% Latency Distribution 50% 103.00us 75% 506.00us 90% 4.28ms 99% 14.60 ms 4949693 requestsin20.02 s, 472.04 MBreadRequests/ SEC: 247208.04 Transfer/ SEC: 23.58MB [root@ecs-1b4c-0002 ~]# wrk -c50 -d20s 'http://localhost:8080/hello3' --latency
Running 20s test50 connections @ http://localhost:8080/hello3 2 threads and Thread Stats Avg Stdev Max + / - Stdev Latency 0.92 ms 2.75 ms 46.71ms 93.14% Req/Sec 115.25k 20.64k 177.04k 70.00% Latency Distribution 50% 130.00us 75% 400.00us 90% 1.63ms 99% 14.12 ms 4589497 requestsin20.03 s, 437.69 MBreadRequests/ SEC: 229162.08 Transfer/ SEC: 21.85MB [root@ecs-1b4c-0002 ~]# wrk -c50 -d20s 'http://localhost:8080/hello4' --latency
Running 20s test50 connections @ http://localhost:8080/hello4 2 threads and Thread Stats Avg Stdev Max + / - Stdev Latency 797.89 us 2.37 ms 53.33ms 93.64% Req/Sec 109.77k 20.36k 171.02k 70.00% Latency Distribution 50% 152.00us 75% 390.00us 90% 1.36ms 99% 11.81 ms 4375750 requestsin20.07 s, 417.30 MBread
Requests/sec: 218072.60
Transfer/sec:     20.80MB
Copy the code

The QPS of a 4C8G server can reach 20W.

Interface 2- Query database

code

Route

    @Inject
    BrandInfoRepository brandInfoRepository;
	

	@Route(path = "/brandInfo", methods = HttpMethod.GET)
    Uni<BrandInfo> brandInfo(@Param Optional<String> brandId) {
        return brandInfoRepository.findById(brandId.orElse(""));
    }
Copy the code

BrandInfoRepository

@ApplicationScoped
public class BrandInfoRepository extends ResourceRepository<BrandInfo> {}Copy the code

ResourceRepository does not directly inherit from PanacheRepositoryBase for subsequent extensions.

<br> * <p>Created Time: 2021/5/25 5:29 PM </p> **@author<a href="mail to: [email protected]" rel="nofollow"
public class ResourceRepository<Entity> implements PanacheRepositoryBase<Entity.String> {}Copy the code

BrandInfo

@Entity(name = "brand_info")
public class BrandInfo extends PanacheEntityBase {

    @Id
    private String brandId;
    private String brandName;
    
    / / get the set...
}
Copy the code

The configuration file

quarkus.datasource.db-kind=mysql
quarkus.datasource.username=dev
quarkus.datasource.password=123456
quarkus.datasource.reactive.url=mysql://xxx.xxx.xxx.xxx:3306/quarkus
Copy the code

performance

The brand_info table in Mysql database contains about 180,000 data, with QPS of 30,000 +

[root@ecs-1b4c-0002 ~]# WRK - d20s c50 - http://127.0.0.1:8080/brandInfo? brandId=1000003
Running 20s test@ http://127.0.0.1:8080/brandInfo? Avg Stdev Max +/ -stdev Latency 1.50ms 1.52ms 40.92ms 1.31 Req/Sec 18.56K 1.19K 31.68K 82.04% 740605 requestsin20.10 s, 92.52 MBread
Requests/sec:  36846.26
Transfer/sec:      4.60MB
Copy the code

gossip

Quarkus’s Reactive coding is relatively simple and has high performance, which makes full use of server resources and reduces waiting.