跟我一塊兒學vert.x,建立vert.x的第一個程序

vert.x是高性能,高併發,可伸縮,支持多語言的web框架,特別適用於手機客戶端的服務端,3.0的資料已經不少了,由於項目大併發的需求,通過調研使用vert.x這個框架。
html

官方例子java

https://github.com/vert-x3/vertx-examples git


官方例子文件很大,若是你想立馬看到效果,就按個人來吧。github

maven,jdk8.0web


建立maven程序apache

pom.xml併發

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>atest</groupId>
	<artifactId>atest</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>atest Maven Webapp</name>
	<url>http://maven.apache.org</url>


	<properties>
		<vertx.version>3.0.0</vertx.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>


		<dependency>
			<groupId>io.vertx</groupId>
			<artifactId>vertx-core</artifactId>
			<version>${vertx.version}</version>
		</dependency>

		<dependency>
			<groupId>io.vertx</groupId>
			<artifactId>vertx-web</artifactId>
			<version>${vertx.version}</version>
		</dependency>


	</dependencies>
	<build>
		<finalName>atest</finalName>

		<!-- jdk使用的版本 -->
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>


VertxTest.javaapp

package bb;

import java.util.function.Consumer;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;

public class VertxTest extends AbstractVerticle {

	@Override
	public void start() throws Exception {

		Router router = Router.router(vertx);

		router.route();
		router.get("/products").handler(this::print2);
		router.get("/*").handler(this::print1);

		// 傳遞方法引用,監聽端口
		vertx.createHttpServer().requestHandler(router::accept).listen(8080);
	}
	
	public void print1(RoutingContext routingContext){
		routingContext.response().putHeader("content-type", "text/html").end("Hello World");
	}
	public void print2(RoutingContext routingContext){
		routingContext.response().putHeader("content-type", "text/html").end("Hi products");
	}

	public static void main(String[] args) {
                
                //路徑前綴
	        System.setProperty("vertx.cwd", "E:\\work\\atest\\src\\main\\java\\bb");

		Vertx vertx = Vertx.vertx(new VertxOptions().setClustered(false));
		vertx.deployVerticle(VertxTest.class.getName());
	}

}


好了,只須要兩步就建立好了
框架

http://localhost:8080/ maven


另外須要輸出日誌 ,我也是拷貝過來的。

vertx-default-jul-logging.properties

handlers=java.util.logging.ConsoleHandler,java.util.logging.FileHandler
java.util.logging.SimpleFormatter.format=%5$s %6$s\n
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.level=FINEST
java.util.logging.FileHandler.level=INFO
java.util.logging.FileHandler.formatter=io.vertx.core.logging.impl.VertxLoggerFormatter

# Put the log in the system temporary directory
java.util.logging.FileHandler.pattern=%t/vertx.log

.level=INFO
io.vertx.ext.web.level=FINEST
io.vertx.level=INFO
com.hazelcast.level=INFO
io.netty.util.internal.PlatformDependent.level=SEVERE
相關文章
相關標籤/搜索