用的centos7 最小化安装
步骤
- wget 更换源,提示wget命令不存在
yum -y install wget
运行yum - y update 出现如下
Loaded plugins fastestmirror
Loading mirror speed from cached hostfile
There are no enabled repos.- 安装mysql
# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
# rpm -ivh mysql-community-release-el7-5.noarch.rpm
# ls -1 /etc/yum.repos.d/mysql-community*
# yum install mysql-server
- 设置为vbox为桥接模式
- 安装jdk
从网上下载jdk.rpm
用xshell上传,先ifconfig找到地址(和主机都是192.168.1.xxx),然后连接,输入用户名密码连上
rpm -qa |grep sz 和 rpm -qa |grep rz
如果没安装则yum -install sz yum -install rz,然后输入rz,标识receive
由于装成了32为,会报错,然后卸载jdk
yum -y remove java java1.8.0_121-1.8.0_121-fcs.i586
再安装64位的jdk 之后查看jdk版本,ok -
安装tomcat
从网上下载tomcat,上传到虚拟机,安装,重命名安装后的目录名为tomcat(为了以后方便),开启80端口
firewall-cmd –zone=public –add-port=8080/tcp –permanent
然后主机反问ip:port就可以看见tomcat的页面了 -
拔了网线再插上后,虚拟机中的centos无法联网
配置静态ip
vim /etc/sysconfig/network-scripts/ifcfg-enp0s3
BOOTPROTO="static"
IPADDR=192.168.1.135
NETMASK=255.255.255.0 - 设置虚拟机桥接,就可以用xshell继续连接了
- 决定用spring boot框架
-
先用maven构建一个项目
下载maven,解压,配置环境变量,然后用mvn archetype:generate -DarchetypeCatalog=internal 构建一个项目(maven3),可能会网络异常,可以再次执行
步骤11尝试了很久,始终不成功,换之,使用idea构建。
- 激活idea:教程地址
-
使用idea构建
- 用idea创建一个maven工程,创建好之后,只有如下,和一个pom文件
- 修改maven配置
- 创建java目录结构
-
创建Application.java 文件
package page.tan.testAp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
/**
* Created by tansl on 2017/3/19.
*/
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
} -
创建Example.java文件
package page.tan.testAp;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by tansl on 2017/3/19.
*/
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
String home() {
return "Hello World!";
}
@RequestMapping("/hello/{myName}")
String index(@PathVariable String myName) {
return "Hello "+myName+"!!!";
}
} 运行,这个运行配置是自动生成的
运行成功结果如下- 测试
输入http://localhost:8080
输入http://localhost:8080/hello/SpringBoot
- 用idea创建一个maven工程,创建好之后,只有如下,和一个pom文件
-
部署到centorOs中的tomcat中
- 配置maven运行命令:clean package,并运行得到war,并放入tomcat的webapps中,bin中startup,输入地址,404错误
- 换一个教材尝试
工程目录下新建java文件
package page.tan.testAp;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
/**
* Created by tansl on 2017/3/19.
*/
public class MySpringBootStartApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// 注意这里要指向原先用main方法执行的Application启动类
return builder.sources(Application.class);
}
} -
修改pom文件如下
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>page.tan</groupId>
<artifactId>testAp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging><!--打包成war-->
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.BUILD-SNAPSHOT</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 移除嵌入式tomcat插件 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>8.0.36</version>
<scope>provided</scope>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!-- Add Spring repositories -->
<!-- (you don't need this if you are using a .RELEASE version) -->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project> 运行maven命令 clearn package得到testAp-1.0-SNAPSHOT.war
- 修改war文件名为项目名字(必须),我这里是testAp
- 把war放入tomcat的webapps中,运行bin下的startup
-
windows 下部署成功,可以正常访问、
http://localhost:8080/testAp/hello/sd
- 部署到centos中
在xshell中输入进入tomcat的webapps目录,输入rz命令,把testAp.war推到这个目录中,进入tomcat的bin目录,输入./startup.sh,开启tomcat服务输入地址http://192.168.1.135:8080/testAp/,访问成功