vert.x入门工程

入门工程
  • eclipse中创建maven工程
  • 工程目录
src
│  ├─main
│  │  ├─java
│  │  │  └─io
│  │  │      └─vertx
│  │  │          └─starter
│  │  └─resources
│  └─test
│      └─java
│          └─io
│              └─vertx
│                  └─starter
  • 添加依赖
pom.xml文件
<? xml version = "1.0" encoding = "UTF-8" ?>
  < modelVersion > 4.0.0 </ modelVersion >
  < groupId > io.vertx.starter </ groupId >
  < artifactId > vertx -start-project </ artifactId >
  < version > 1.0-SNAPSHOT </ version >
  < properties >
    < vertx.version > 3.5.1 </ vertx.version >
    < vertx.projectVersion > 3.4.0 </ vertx.projectVersion >
    < fabric8-vertx-maven-plugin.projectVersion > 1.0.5 </ fabric8-vertx-maven-plugin.projectVersion >
    < main.verticle > io.vertx.starter.MainVerticle </ main.verticle >
  </ properties >
  < dependencyManagement >
    < dependencies >
      < dependency >
        < groupId > io.vertx </ groupId >
        < artifactId > vertx -dependencies </ artifactId >
        < version > ${vertx.version} </ version >
        < type > pom </ type >
        < scope > import </ scope >
      </ dependency >
      < dependency >
        < groupId > io.vertx </ groupId >
        < artifactId > vertx -dependencies </ artifactId >
        < version > ${vertx.projectVersion} </ version >
        < type > pom </ type >
        < scope > import </ scope >
      </ dependency >
    </ dependencies >
  </ dependencyManagement >
  < dependencies >
    < dependency >
      < groupId > io.vertx </ groupId >
      < artifactId > vertx -core </ artifactId >
    </ dependency >
    < dependency >
      < groupId > junit </ groupId >
      < artifactId > junit </ artifactId >
      < version > 4.12 </ version >
      < scope > test </ scope >
    </ dependency >
    < dependency >
      < groupId > io.vertx </ groupId >
      < artifactId > vertx -unit </ artifactId >
      < scope > test </ scope >
    </ dependency >
  </ dependencies >
  < build >
    < pluginManagement >
      < plugins >
        < plugin >
          < artifactId > maven -compiler- plugin </ artifactId >
          < version > 3.5.1 </ version >
          < configuration >
            < source > 1.8 </ source >
            < target > 1.8 </ target >
          </ configuration >
        </ plugin >
        <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
        < plugin >
             < groupId > org.eclipse.m2e </ groupId >
             < artifactId > lifecycle -mapping </ artifactId >
             < version > 1.0.0 </ version >
             < configuration >
                   < lifecycleMappingMetadata >
                         < pluginExecutions >
                               < pluginExecution >
                                     < pluginExecutionFilter >
                                           < groupId > io.fabric8 </ groupId >
                                           < artifactId >
                                                vertx-maven-plugin
                                           </ artifactId >
                                           < versionRange > [1.0.5,) </ versionRange >
                                           < goals >
                                                 < goal > initialize </ goal >
                                           </ goals >
                                     </ pluginExecutionFilter >
                                     < action >
                                           < ignore ></ ignore >
                                     </ action >
                               </ pluginExecution >
                         </ pluginExecutions >
                   </ lifecycleMappingMetadata >
             </ configuration >
        </ plugin >
      </ plugins >
    </ pluginManagement >
    < plugins >
      < plugin >
        < artifactId > maven -shade- plugin </ artifactId >
        < version > 2.4.3 </ version >
        < executions >
          < execution >
            < phase > package </ phase >
            < goals >
              < goal > shade </ goal >
            </ goals >
            < configuration >
              < transformers >
                < transformer implementation = "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" >
                  < manifestEntries >
                    < Main-Class > io.vertx.core.Launcher </ Main-Class >
                    < Main-Verticle > ${main.verticle} </ Main-Verticle >
                  </ manifestEntries >
                </ transformer >
                < transformer implementation = "org.apache.maven.plugins.shade.resource.AppendingTransformer" >
                  < resource > META-INF/services/io.vertx.core.spi.VerticleFactory </ resource >
                </ transformer >
              </ transformers >
              < artifactSet ></ artifactSet >
              < outputFile > ${project.build.directory}/${project.artifactId}-${project.version}-fat.jar </ outputFile >
            </ configuration >
          </ execution >
        </ executions >
      </ plugin >
      < plugin >
        < groupId > org.codehaus.mojo </ groupId >
        < artifactId > exec - maven - plugin </ artifactId >
        < version > 1.5.0 </ version >
        < configuration >
          < mainClass > io.vertx.core.Launcher </ mainClass >
          < arguments >
            < argument > run </ argument >
            < argument > ${main.verticle} </ argument >
          </ arguments >
        </ configuration >
      </ plugin >
      < plugin >
        < groupId > io.fabric8 </ groupId >
        < artifactId > vertx - maven - plugin </ artifactId >
        < version > ${fabric8- vertx - maven -plugin.projectVersion} </ version >
        < executions >
          < execution >
            < id > vmp - init -package </ id >
            < goals >
              < goal > initialize </ goal >
              < goal > package </ goal >
            </ goals >
          </ execution >
        </ executions >
        < configuration >
          < redeploy > true </ redeploy >
        </ configuration >
      </ plugin >
    </ plugins >
  </ build >
</ project>
  • 创建主程序
package io.vertx.starter;
import io.vertx.core.AbstractVerticle;
public class MainVerticle extends AbstractVerticle {
    @Override
    public void start() {
       vertx .createHttpServer()
            .requestHandler( req -> req .response()
                        .end( "Yes!" ))
                        .listen(8080);
    }
}
  • //注:也可以使用rxjava实现服务器的创建:
    package io.vertx.starter;
    
    import io.vertx.rxjava.core.AbstractVerticle;
    import io.vertx.rxjava.core.http.HttpServer;
    
    public class MyFirstRXVerticle extends AbstractVerticle {
    
        @Override
        public void start() {
        	vertx.createHttpServer()
        		.requestHandler(req -> {
        		req.response()
        				.end("Yes !From "+Thread.currentThread().getName());})
        				.listen(8080);
        	//Using RxJava
        	/*HttpServer server = vertx.createHttpServer();
        	server.requestStream().toObservable()//通过Observable订阅接收数据
        		.subscribe(req ->
    	    		req.response()
    	    			.end("Yes2 !From "+Thread.currentThread().getName())
    	    			);
        	server
        		.rxListen(8080)
        		.subscribe();*/
        }
    
    }


  • 在eclipse中启动
右键目标工程,选择Run As,选择Run Configurations…,选择Arguments,在Program arguments中添加 
run io.vertx.starter.MainVerticle

下图选择Apply,然后选择Run。

然后下图选择第一项

  • 启动成功

  • 访问监听的8080端口,查看响应值

由此说明监听端口接收到HTTP请求,并做出了响应。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值