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”
下面是 官方示例
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

    注:若你在 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.
复制代码

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值