Skip to content

Commit

Permalink
完成 master分支对 vert.x4的适配,改进了一些包的依赖和写法
Browse files Browse the repository at this point in the history
  • Loading branch information
aruis committed Dec 28, 2020
1 parent fdf3ecc commit d735b2e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 44 deletions.
27 changes: 12 additions & 15 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
plugins {
id 'java'
id 'application'
id 'com.github.johnrengelman.shadow' version '2.0.4'
id 'com.github.johnrengelman.shadow' version '5.2.0'
}
ext {
vertxVersion = '3.6.3'
junitJupiterEngineVersion = '5.2.0'
vertxVersion = '4.0.0'
}

repositories {
Expand All @@ -22,7 +21,6 @@ repositories {
group = 'com.aruistar'
version = '1.0.0-SNAPSHOT'

sourceCompatibility = '1.8'
mainClassName = 'io.vertx.core.Launcher'

def mainVerticleName = 'com.aruistar.vertxstarter.MainVerticle'
Expand All @@ -33,20 +31,22 @@ dependencies {

implementation "io.vertx:vertx-core:$vertxVersion"
implementation "io.vertx:vertx-web:$vertxVersion"
implementation "io.vertx:vertx-pg-client:$vertxVersion"

compile 'io.reactiverse:reactive-pg-client:0.11.2'

compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
compile group: 'org.slf4j', name: 'jul-to-slf4j', version: '1.7.25'
implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
implementation group: 'org.slf4j', name: 'jul-to-slf4j', version: '1.7.30'

testImplementation "io.vertx:vertx-junit5:$vertxVersion"
testRuntime("org.junit.jupiter:junit-jupiter-engine:$junitJupiterEngineVersion")

testRuntime "io.vertx:vertx-web-client:$vertxVersion"
testRuntime "org.codehaus.groovy:groovy-sql:2.5.5"
testRuntime group: 'org.postgresql', name: 'postgresql', version: '42.2.5'
testImplementation "io.vertx:vertx-web-client:$vertxVersion"
testImplementation "org.codehaus.groovy:groovy-sql:3.0.7"
testImplementation group: 'org.postgresql', name: 'postgresql', version: '42.2.5'
}

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

shadowJar {
classifier = 'fat'
Expand All @@ -69,6 +69,3 @@ run {
args = ['run', mainVerticleName, "--redeploy=$watchForChange", "--launcher-class=$mainClassName", "--on-redeploy=$doOnChange"]
}

task wrapper(type: Wrapper) {
gradleVersion = '4.6'
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
47 changes: 28 additions & 19 deletions src/main/java/com/aruistar/vertxstarter/MainVerticle.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.aruistar.vertxstarter;

import io.reactiverse.pgclient.PgClient;
import io.reactiverse.pgclient.PgPool;
import io.reactiverse.pgclient.PgPoolOptions;
import io.reactiverse.pgclient.Tuple;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.pgclient.PgPool;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.sqlclient.Tuple;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.bridge.SLF4JBridgeHandler;
Expand All @@ -21,46 +22,54 @@ public class MainVerticle extends AbstractVerticle {
SLF4JBridgeHandler.install();
}

private static Logger logger = LoggerFactory.getLogger(MainVerticle.class);
private static final Logger logger = LoggerFactory.getLogger(MainVerticle.class);


@Override
public void start(Future<Void> startFuture) throws Exception {
public void start(Promise<Void> startPromise) throws Exception {
logger.info("verticle start");

PgPoolOptions options = new PgPoolOptions()
PgConnectOptions connectOptions = new PgConnectOptions()
.setPort(5432)
.setHost("127.0.0.1")
.setDatabase("studypg")
.setUser("postgres")
.setPassword("secret")
.setPassword("muyuntage");

PoolOptions poolOptions = new PoolOptions()
.setMaxSize(5);

// Create the client pool
PgPool client = PgClient.pool(vertx, options);
PgPool client = PgPool.pool(vertx, connectOptions, poolOptions);

Router router = Router.router(vertx);

router.get("/")
.handler(routingContext -> {
routingContext.response().putHeader("Context-Type", "text/plain")
.end("Hello from Vert.x!");
});

router.post("/score")
.handler(BodyHandler.create())
.handler(routingContext -> {
HttpServerResponse response = routingContext.response();
JsonObject json = routingContext.getBodyAsJson();

client.preparedQuery("insert into edu_score (v_lesson, n_score, v_name) values ($1,$2,$3) returning id;",
Tuple.of(json.getString("v_lesson"), json.getFloat("n_score"), json.getString("v_name")),
ar -> {
String id = ar.result().iterator().next().getString("id");
response.end(id);
});
client.preparedQuery("insert into edu_score (v_lesson, n_score, v_name) values ($1,$2,$3) returning id;")
.execute(Tuple.of(json.getString("v_lesson"), json.getFloat("n_score"), json.getString("v_name")),
ar -> {
String id = ar.result().iterator().next().getString("id");
response.end(id);
});
});

vertx.createHttpServer().requestHandler(router)
.listen(8080, http -> {
if (http.succeeded()) {
startFuture.complete();
startPromise.complete();
System.out.println("HTTP server started on http://localhost:8080");
} else {
startFuture.fail(http.cause());
startPromise.fail(http.cause());
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/table.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
create table edu_score
(
id varchar default uuid_generate_v4() not null
id varchar default gen_random_uuid() not null
constraint edu_score_pk
primary key,
v_lesson varchar,
Expand Down
19 changes: 11 additions & 8 deletions src/test/java/com/aruistar/vertxstarter/TestMainVerticle.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.aruistar.vertxstarter;

import com.sun.tools.javac.util.List;
import groovy.sql.Sql;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClientResponse;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.HttpResponse;
import io.vertx.ext.web.client.WebClient;
Expand All @@ -16,6 +17,7 @@

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

Expand All @@ -34,13 +36,14 @@ void deploy_verticle(Vertx vertx, VertxTestContext testContext) {
@DisplayName("Should start a Web Server on port 8080")
@Timeout(value = 10, timeUnit = TimeUnit.SECONDS)
void start_http_server(Vertx vertx, VertxTestContext testContext) throws Throwable {
vertx.createHttpClient().getNow(8080, "localhost", "/", response -> testContext.verify(() -> {
assertTrue(response.statusCode() == 200);
response.handler(body -> {
assertTrue(body.toString().contains("Hello from Vert.x!"));

vertx.createHttpClient().request(HttpMethod.GET, 8080, "localhost", "/")
.compose(req -> req.send().compose(HttpClientResponse::body))
.onComplete(testContext.succeeding(buffer -> testContext.verify(() -> {
assertTrue(buffer.toString().contains("Hello from Vert.x!"));
testContext.completeNow();
});
}));
})));

}

@Test
Expand All @@ -53,7 +56,7 @@ void postScore(Vertx vertx, VertxTestContext testContext) throws Throwable {
.put("n_score", score)
.put("v_lesson", "语文");

Sql db = Sql.newInstance("jdbc:postgresql://localhost:5432/studypg", "postgres", "", "org.postgresql.Driver");
Sql db = Sql.newInstance("jdbc:postgresql://localhost:5432/studypg", "postgres", "muyuntage", "org.postgresql.Driver");

WebClient client = WebClient.create(vertx);
client.post(8080, "127.0.0.1", "/score")
Expand Down

0 comments on commit d735b2e

Please sign in to comment.