Spark开发环境搭建

Spark的一些基本情况如下:

Spark:一个java web框架
License:Apache License
服务器:Jettry
jre版本:8
 
准备工作:
    下载并配置eclipse for javaee、maven、jdk1.8
    大概的了解一下以下东西:
            java8里的lambda表达式是什么
            jetty是什么
            jetty嵌入方式开发是什么样的
 
环境搭建:
    1,新建一个web maven工程
            注:若eclipse新建的maven工程并没有包含web相关配置,可以新建一个web工程然后对工程右键configure --> convert to maven project,来转换为maven工程即可。
            检查工程中是否有src/mian/java、src/test/java、src/main/resources这几个source folder,若没有则在windows资源管理器中手动创建,然后回到工程打开“project视图”,找到刚才新建的目录,点击右键”build path->Use as Source Folder“将其作为source folder。
 
    2,将工程编码设置为utf-8,maven也是,如下
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

 

    3,设置maven的使用java8编译
<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

 

4,加入Spark和日志类库(加入spark后会自动加入jettry的类库
<dependencies>
        <dependency>
            <groupId>com.sparkjava</groupId>
            <artifactId>spark-core</artifactId>
            <version>2.5</version>
        </dependency> 
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.21</version>
        </dependency> 
    </dependencies>

 

5,编写“hello world”
下面是官方示例
package me.ooi.testSpark;
import static spark.Spark.*;
public class HelloSpark {
    
    public static void main(String[] args) {
        get("/hello", (request, response) -> "Hello World!");
    }
}

    注:若你在 get("/hello", ......) ,这个地方感觉很奇怪,奇怪这个get是哪里来的,可以先看一下如下写法

package me.ooi.testSpark;
import spark.Spark;
public class HelloSpark {
    
    public static void main(String[] args) {        
        Spark.get("/hello", (request, response) -> "Hello World!");        
    }
}

 

然后看一下import static”的用处,就会明白了
 
6,执行这个main方法就可以访问了
    访问地址:http://localhost:4567/hello
 
7,发布(发布为单个jar文件)
在pom.xml中添加如下配置以用于打成单个jar包(“me.ooi.testSpark.HelloSpark”这个文件是入口)
<plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <descriptorRefs>
                    <!-- This tells Maven to include all dependencies -->
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>me.ooi.testSpark.HelloSpark</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

并将<packaging>war</packaging>改为<packaging>jar</packaging>

 
然后对工程右键“Run As -> maven clean”,若没有问题,再“Run As -> maven install”,若没有报错就ok了。
导出的jar包文件名形如“testSpark-0.0.1-SNAPSHOT-jar-with-dependencies.jar”这样的。
运行:
        在cmd窗口使用“java -jar testSpark-0.0.1-SNAPSHOT-jar-with-dependencies.jar”
停止:
        直接关闭cmd窗口即可
 
 
注:若需要修改日志输出参数,则需要在classpath下面新建一个名为simplelogger.properties(若使用的是slf4j-simple)的文件,下面是slf4j-simple官网上可以设置的参数说明
#org.slf4j.simpleLogger.logFile - The output target which can be the path to a file, or the special values "System.out" and "System.err". Default is "System.err".
#org.slf4j.simpleLogger.defaultLogLevel - Default log level for all instances of SimpleLogger. Must be one of ("trace", "debug", "info", "warn", or "error"). If not specified, defaults to "info".
#org.slf4j.simpleLogger.log.a.b.c - Logging detail level for a SimpleLogger instance named "a.b.c". Right-side value must be one of "trace", "debug", "info", "warn", or "error". When a SimpleLogger named "a.b.c" is initialized, its level is assigned from this property. If unspecified, the level of nearest parent logger will be used, and if none is set, then the value specified by org.slf4j.simpleLogger.defaultLogLevel will be used.
#org.slf4j.simpleLogger.showDateTime - Set to true if you want the current date and time to be included in output messages. Default is false
#org.slf4j.simpleLogger.dateTimeFormat - The date and time format to be used in the output messages. The pattern describing the date and time format is defined by SimpleDateFormat. If the format is not specified or is invalid, the number of milliseconds since start up will be output.
#org.slf4j.simpleLogger.showThreadName -Set to true if you want to output the current thread name. Defaults to true.
#org.slf4j.simpleLogger.showLogName - Set to true if you want the Logger instance name to be included in output messages. Defaults to true.
#org.slf4j.simpleLogger.showShortLogName - Set to true if you want the last component of the name to be included in output messages. Defaults to false.
#org.slf4j.simpleLogger.levelInBrackets - Should the level string be output in brackets? Defaults to false.
#org.slf4j.simpleLogger.warnLevelString - The string value output for the warn level. Defaults to WARN.

 

 
 
 
posted @ 2016-10-25 16:40  行-云  阅读(2925)  评论(0编辑  收藏  举报